diff --git a/tests/test_bit.py b/tests/test_bit.py index 5a71642..c78ebc0 100644 --- a/tests/test_bit.py +++ b/tests/test_bit.py @@ -61,3 +61,33 @@ def test_repr(self): def test_equality(self): assert Bit([True, False, True]) == Bit([True, False, True]) assert Bit([True, False, True]) != Bit([True, False, False]) + + def test_dimensions(self): + assert len(Bit([True, False, True]).to_list()) == 3 + + def test_to_db(self): + bit = Bit([True, False, True]) + assert Bit._to_db(bit) == '101' + + def test_to_db_invalid(self): + with pytest.raises(ValueError, match='expected bit'): + Bit._to_db([True, False, True]) + + def test_to_db_binary(self): + bit = Bit([True, False, True]) + result = Bit._to_db_binary(bit) + assert result == bit.to_binary() + + def test_to_db_binary_invalid(self): + with pytest.raises(ValueError, match='expected bit'): + Bit._to_db_binary([True, False, True]) + + def test_from_binary(self): + bit = Bit([True, False, True]) + data = bit.to_binary() + result = Bit.from_binary(data) + assert result.to_list() == [True, False, True] + + def test_from_binary_invalid(self): + with pytest.raises(ValueError, match='expected bytes'): + Bit.from_binary([1, 2, 3]) diff --git a/tests/test_half_vector.py b/tests/test_half_vector.py index 78b4977..0b28820 100644 --- a/tests/test_half_vector.py +++ b/tests/test_half_vector.py @@ -57,3 +57,61 @@ def test_from_binary(self): assert vec.to_list() == [1.5, 2, 3] assert np.array_equal(vec.to_numpy(), [1.5, 2, 3]) assert vec.to_binary() == data + + def test_to_text(self): + vec = HalfVector([1.5, 2, 3]) + assert vec.to_text() == '[1.5,2.0,3.0]' + + def test_to_db(self): + vec = HalfVector([1, 2, 3]) + assert HalfVector._to_db(vec) == '[1.0,2.0,3.0]' + + def test_to_db_list(self): + assert HalfVector._to_db([1, 2, 3]) == '[1.0,2.0,3.0]' + + def test_to_db_none(self): + assert HalfVector._to_db(None) is None + + def test_to_db_dim(self): + vec = HalfVector([1, 2, 3]) + assert HalfVector._to_db(vec, 3) == '[1.0,2.0,3.0]' + + def test_to_db_dim_invalid(self): + vec = HalfVector([1, 2, 3]) + with pytest.raises(ValueError, match='expected 2 dimensions, not 3'): + HalfVector._to_db(vec, 2) + + def test_to_db_binary(self): + vec = HalfVector([1, 2, 3]) + result = HalfVector._to_db_binary(vec) + assert result == vec.to_binary() + + def test_to_db_binary_list(self): + result = HalfVector._to_db_binary([1, 2, 3]) + assert result == HalfVector([1, 2, 3]).to_binary() + + def test_to_db_binary_none(self): + assert HalfVector._to_db_binary(None) is None + + def test_from_db_text(self): + result = HalfVector._from_db('[1.5,2,3]') + assert result.to_list() == [1.5, 2, 3] + + def test_from_db_none(self): + assert HalfVector._from_db(None) is None + + def test_from_db_halfvector(self): + vec = HalfVector([1, 2, 3]) + assert HalfVector._from_db(vec) is vec + + def test_from_db_binary(self): + data = pack('>HH3e', 3, 0, 1.5, 2, 3) + result = HalfVector._from_db_binary(data) + assert result.to_list() == [1.5, 2, 3] + + def test_from_db_binary_none(self): + assert HalfVector._from_db_binary(None) is None + + def test_from_db_binary_halfvector(self): + vec = HalfVector([1, 2, 3]) + assert HalfVector._from_db_binary(vec) is vec diff --git a/tests/test_sparse_vector.py b/tests/test_sparse_vector.py index d580f32..1025ee3 100644 --- a/tests/test_sparse_vector.py +++ b/tests/test_sparse_vector.py @@ -110,3 +110,57 @@ def test_from_binary(self): assert vec.to_list() == [1.5, 0, 2, 0, 3, 0] assert np.array_equal(vec.to_numpy(), [1.5, 0, 2, 0, 3, 0]) assert vec.to_binary() == data + + def test_to_db(self): + vec = SparseVector([1, 0, 2, 0, 3, 0]) + assert SparseVector._to_db(vec) == '{1:1.0,3:2.0,5:3.0}/6' + + def test_to_db_list(self): + assert SparseVector._to_db([1, 0, 2, 0, 3, 0]) == '{1:1.0,3:2.0,5:3.0}/6' + + def test_to_db_none(self): + assert SparseVector._to_db(None) is None + + def test_to_db_dim(self): + vec = SparseVector([1, 0, 2, 0, 3, 0]) + assert SparseVector._to_db(vec, 6) == '{1:1.0,3:2.0,5:3.0}/6' + + def test_to_db_dim_invalid(self): + vec = SparseVector([1, 0, 2, 0, 3, 0]) + with pytest.raises(ValueError, match='expected 5 dimensions, not 6'): + SparseVector._to_db(vec, 5) + + def test_to_db_binary(self): + vec = SparseVector([1, 0, 2, 0, 3, 0]) + result = SparseVector._to_db_binary(vec) + assert result == vec.to_binary() + + def test_to_db_binary_list(self): + result = SparseVector._to_db_binary([1, 0, 2, 0, 3, 0]) + assert result == SparseVector([1, 0, 2, 0, 3, 0]).to_binary() + + def test_to_db_binary_none(self): + assert SparseVector._to_db_binary(None) is None + + def test_from_db_text(self): + result = SparseVector._from_db('{1:1.5,3:2,5:3}/6') + assert result.to_list() == [1.5, 0, 2, 0, 3, 0] + + def test_from_db_none(self): + assert SparseVector._from_db(None) is None + + def test_from_db_sparsevector(self): + vec = SparseVector([1, 0, 2, 0, 3, 0]) + assert SparseVector._from_db(vec) is vec + + def test_from_db_binary(self): + data = pack('>iii3i3f', 6, 3, 0, 0, 2, 4, 1.5, 2, 3) + result = SparseVector._from_db_binary(data) + assert result.to_list() == [1.5, 0, 2, 0, 3, 0] + + def test_from_db_binary_none(self): + assert SparseVector._from_db_binary(None) is None + + def test_from_db_binary_sparsevector(self): + vec = SparseVector([1, 0, 2, 0, 3, 0]) + assert SparseVector._from_db_binary(vec) is vec diff --git a/tests/test_vector.py b/tests/test_vector.py index e5a16fe..8612365 100644 --- a/tests/test_vector.py +++ b/tests/test_vector.py @@ -57,3 +57,61 @@ def test_from_binary(self): assert vec.to_list() == [1.5, 2, 3] assert np.array_equal(vec.to_numpy(), [1.5, 2, 3]) assert vec.to_binary() == data + + def test_to_text(self): + vec = Vector([1.5, 2, 3]) + assert vec.to_text() == '[1.5,2.0,3.0]' + + def test_to_db(self): + vec = Vector([1, 2, 3]) + assert Vector._to_db(vec) == '[1.0,2.0,3.0]' + + def test_to_db_list(self): + assert Vector._to_db([1, 2, 3]) == '[1.0,2.0,3.0]' + + def test_to_db_none(self): + assert Vector._to_db(None) is None + + def test_to_db_dim(self): + vec = Vector([1, 2, 3]) + assert Vector._to_db(vec, 3) == '[1.0,2.0,3.0]' + + def test_to_db_dim_invalid(self): + vec = Vector([1, 2, 3]) + with pytest.raises(ValueError, match='expected 2 dimensions, not 3'): + Vector._to_db(vec, 2) + + def test_to_db_binary(self): + vec = Vector([1, 2, 3]) + result = Vector._to_db_binary(vec) + assert result == vec.to_binary() + + def test_to_db_binary_list(self): + result = Vector._to_db_binary([1, 2, 3]) + assert result == Vector([1, 2, 3]).to_binary() + + def test_to_db_binary_none(self): + assert Vector._to_db_binary(None) is None + + def test_from_db_text(self): + result = Vector._from_db('[1.5,2,3]') + assert np.array_equal(result, np.array([1.5, 2, 3], dtype=np.float32)) + + def test_from_db_none(self): + assert Vector._from_db(None) is None + + def test_from_db_ndarray(self): + arr = np.array([1, 2, 3]) + assert Vector._from_db(arr) is arr + + def test_from_db_binary(self): + data = pack('>HH3f', 3, 0, 1.5, 2, 3) + result = Vector._from_db_binary(data) + assert np.array_equal(result, np.array([1.5, 2, 3], dtype=np.float32)) + + def test_from_db_binary_none(self): + assert Vector._from_db_binary(None) is None + + def test_from_db_binary_ndarray(self): + arr = np.array([1, 2, 3]) + assert Vector._from_db_binary(arr) is arr