-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5a33dce
commit e653254
Showing
5 changed files
with
172 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
import numpy as np | ||
import pyarrow as pa | ||
import pytest | ||
from ipywidgets import Widget | ||
from traitlets import TraitError | ||
|
||
from lonboard.traits import ColorAccessor, FloatAccessor | ||
|
||
|
||
class ColorAccessorWidget(Widget): | ||
color = ColorAccessor() | ||
|
||
|
||
def test_color_accessor_validation_list_length(): | ||
# tuple or list must have 3 or 4 elements | ||
with pytest.raises(TraitError): | ||
ColorAccessorWidget(color=()) | ||
|
||
with pytest.raises(TraitError): | ||
ColorAccessorWidget(color=(1, 2)) | ||
|
||
with pytest.raises(TraitError): | ||
ColorAccessorWidget(color=(1, 2, 3, 4, 5)) | ||
|
||
ColorAccessorWidget(color=(1, 2, 3)) | ||
ColorAccessorWidget(color=(1, 2, 3, 255)) | ||
|
||
|
||
def test_color_accessor_validation_list_type(): | ||
# tuple or list must have int values | ||
with pytest.raises(TraitError): | ||
ColorAccessorWidget(color=(1.0, 2.0, 4.0)) | ||
|
||
|
||
def test_color_accessor_validation_list_range(): | ||
# tuple or list must have values between 0-255 | ||
with pytest.raises(TraitError): | ||
ColorAccessorWidget(color=(-1, 2, 4)) | ||
|
||
with pytest.raises(TraitError): | ||
ColorAccessorWidget(color=(1, 2, 300)) | ||
|
||
|
||
def test_color_accessor_validation_dim_shape_np_arr(): | ||
# must be two dimensions | ||
with pytest.raises(TraitError): | ||
ColorAccessorWidget(color=np.array([1, 2, 3], dtype=np.uint8).reshape(-1, 3, 1)) | ||
|
||
# Second dim must be 3 or 4 | ||
with pytest.raises(TraitError): | ||
ColorAccessorWidget(color=np.array([1, 2, 3], dtype=np.uint8).reshape(-1, 1)) | ||
|
||
with pytest.raises(TraitError): | ||
ColorAccessorWidget(color=np.array([1, 2, 3, 4], dtype=np.uint8).reshape(-1, 2)) | ||
|
||
with pytest.raises(TraitError): | ||
ColorAccessorWidget( | ||
color=np.array([1, 2, 3, 4, 5], dtype=np.uint8).reshape(-1, 5) | ||
) | ||
|
||
ColorAccessorWidget(color=np.array([1, 2, 3], dtype=np.uint8).reshape(-1, 3)) | ||
ColorAccessorWidget(color=np.array([1, 2, 3, 255], dtype=np.uint8).reshape(-1, 4)) | ||
|
||
|
||
def test_color_accessor_validation_np_dtype(): | ||
# must be np.uint8 | ||
with pytest.raises(TraitError): | ||
ColorAccessorWidget(color=np.array([1, 2, 3]).reshape(-1, 3)) | ||
|
||
ColorAccessorWidget(color=np.array([1, 2, 3], dtype=np.uint8).reshape(-1, 3)) | ||
|
||
|
||
def test_color_accessor_validation_pyarrow_array_type(): | ||
# array type must be FixedSizeList | ||
with pytest.raises(TraitError): | ||
ColorAccessorWidget(color=pa.array(np.array([1, 2, 3], dtype=np.float64))) | ||
|
||
np_arr = np.array([1, 2, 3], dtype=np.uint8) | ||
ColorAccessorWidget(color=pa.FixedSizeListArray.from_arrays(np_arr, 3)) | ||
|
||
np_arr = np.array([1, 2, 3, 255], dtype=np.uint8) | ||
ColorAccessorWidget(color=pa.FixedSizeListArray.from_arrays(np_arr, 4)) | ||
|
||
# array type must have uint8 child | ||
np_arr = np.array([1, 2, 3, 255], dtype=np.uint64) | ||
with pytest.raises(TraitError): | ||
ColorAccessorWidget(color=pa.FixedSizeListArray.from_arrays(np_arr, 4)) | ||
|
||
|
||
class FloatAccessorWidget(Widget): | ||
value = FloatAccessor() | ||
|
||
|
||
def test_float_accessor_validation_type(): | ||
# must be int or float scalar | ||
with pytest.raises(TraitError): | ||
FloatAccessorWidget(value=()) | ||
|
||
with pytest.raises(TraitError): | ||
FloatAccessorWidget(value="2") | ||
|
||
FloatAccessorWidget(value=2) | ||
FloatAccessorWidget(value=2.0) | ||
FloatAccessorWidget(value=np.array([2, 3, 4])) | ||
FloatAccessorWidget(value=np.array([2, 3, 4], dtype=np.float32)) | ||
FloatAccessorWidget(value=np.array([2, 3, 4], dtype=np.float64)) | ||
|
||
# Must be floating-point array type | ||
with pytest.raises(TraitError): | ||
FloatAccessorWidget(value=pa.array(np.array([2, 3, 4]))) | ||
|
||
FloatAccessorWidget(value=pa.array(np.array([2, 3, 4], dtype=np.float32))) | ||
FloatAccessorWidget(value=pa.array(np.array([2, 3, 4], dtype=np.float64))) |