|
| 1 | +import numpy as np |
| 2 | +import pytest |
| 3 | + |
| 4 | +from stringdtype import StringDType, StringScalar |
| 5 | + |
| 6 | + |
| 7 | +@pytest.fixture |
| 8 | +def string_list(): |
| 9 | + return ['abc', 'def', 'ghi'] |
| 10 | + |
| 11 | + |
| 12 | +def test_scalar_creation(): |
| 13 | + assert str(StringScalar('abc', StringDType())) == 'abc' |
| 14 | + |
| 15 | + |
| 16 | +def test_dtype_creation(): |
| 17 | + assert str(StringDType()) == 'StringDType' |
| 18 | + |
| 19 | + |
| 20 | +@pytest.mark.parametrize( |
| 21 | + 'data', [ |
| 22 | + ['abc', 'def', 'ghi'], |
| 23 | + ["🤣", "📵", "😰"], |
| 24 | + ["🚜", "🙃", "😾"], |
| 25 | + ["😹", "🚠", "🚌"], |
| 26 | + ] |
| 27 | +) |
| 28 | +def test_array_creation_utf8(data): |
| 29 | + arr = np.array(data, dtype=StringDType()) |
| 30 | + assert repr(arr) == f'array({str(data)}, dtype=StringDType)' |
| 31 | + |
| 32 | + |
| 33 | +def test_array_creation_scalars(string_list): |
| 34 | + dtype = StringDType() |
| 35 | + arr = np.array( |
| 36 | + [ |
| 37 | + StringScalar('abc', dtype=dtype), |
| 38 | + StringScalar('def', dtype=dtype), |
| 39 | + StringScalar('ghi', dtype=dtype), |
| 40 | + ] |
| 41 | + ) |
| 42 | + assert repr(arr) == repr(np.array(string_list, dtype=StringDType())) |
| 43 | + |
| 44 | + |
| 45 | +@pytest.mark.parametrize( |
| 46 | + 'data', [ |
| 47 | + [1, 2, 3], |
| 48 | + [None, None, None], |
| 49 | + [b'abc', b'def', b'ghi'], |
| 50 | + [object, object, object], |
| 51 | + ] |
| 52 | +) |
| 53 | +def test_bad_scalars(data): |
| 54 | + with pytest.raises(TypeError): |
| 55 | + np.array(data, dtype=StringDType()) |
| 56 | + |
| 57 | + |
| 58 | +@pytest.mark.xfail(reason='Not yet implemented') |
| 59 | +def test_cast_to_stringdtype(string_list): |
| 60 | + arr = np.array(string_list, dtype='<U3').astype(StringDType()) |
| 61 | + expected = np.array(string_list, dtype=StringDType()) |
| 62 | + np.testing.assert_array_equal(arr, expected) |
| 63 | + |
| 64 | + |
| 65 | +@pytest.mark.xfail(reason='Not yet implemented') |
| 66 | +def test_cast_to_unicode_safe(string_list): |
| 67 | + arr = np.array(string_list, dtype=StringDType()) |
| 68 | + |
| 69 | + np.testing.assert_array_equal( |
| 70 | + arr.astype('<U3', casting='safe'), |
| 71 | + np.array(string_list, dtype='<U3') |
| 72 | + ) |
| 73 | + |
| 74 | + # Safe casting should preserve data |
| 75 | + with pytest.raises(TypeError): |
| 76 | + arr.astype('<U2', casting='safe') |
| 77 | + |
| 78 | + |
| 79 | +@pytest.mark.xfail(reason='Not yet implemented') |
| 80 | +def test_cast_to_unicode_unsafe(string_list): |
| 81 | + arr = np.array(string_list, dtype=StringDType()) |
| 82 | + |
| 83 | + np.testing.assert_array_equal( |
| 84 | + arr.astype('<U3', casting='unsafe'), |
| 85 | + np.array(string_list, dtype='<U3') |
| 86 | + ) |
| 87 | + |
| 88 | + # Unsafe casting: each element is truncated |
| 89 | + np.testing.assert_array_equal( |
| 90 | + arr.astype('<U2', casting='unsafe'), |
| 91 | + np.array(string_list, dtype='<U2') |
| 92 | + ) |
| 93 | + |
| 94 | + |
| 95 | +def test_insert_scalar(string_list): |
| 96 | + dtype = StringDType() |
| 97 | + arr = np.array(string_list, dtype=dtype) |
| 98 | + arr[1] = StringScalar('what', dtype=dtype) |
| 99 | + assert repr(arr) == repr(np.array(['abc', 'what', 'ghi'], dtype=dtype)) |
0 commit comments