22# Licensed under the Universal Permissive License v 1.0 as shown at
33# https://oss.oracle.com/licenses/upl.
44
5- from coherence .ai import BitVector , ByteVector , FloatVector
5+ from coherence .ai import BitVector , ByteVector , FloatVector , DocumentChunk
66from coherence .serialization import JSONSerializer , SerializerRegistry
77
88s = SerializerRegistry .serializer (JSONSerializer .SER_FORMAT )
@@ -12,27 +12,63 @@ def test_BitVector_serialization() -> None:
1212 coh_bv = BitVector (hex_string = "AABBCC" )
1313 ser = s .serialize (coh_bv )
1414 assert ser == b'\x15 {"@class": "ai.BitVector", "bits": "0xAABBCC"}'
15+ o = s .deserialize (ser )
16+ assert isinstance (o , BitVector )
1517
1618 coh_bv = BitVector (hex_string = "0xAABBCC" )
1719 ser = s .serialize (coh_bv )
1820 assert ser == b'\x15 {"@class": "ai.BitVector", "bits": "0xAABBCC"}'
21+ o = s .deserialize (ser )
22+ assert isinstance (o , BitVector )
1923
2024 coh_bv = BitVector (hex_string = None , byte_array = bytes ([1 , 2 , 10 ]))
2125 ser = s .serialize (coh_bv )
2226 assert ser == b'\x15 {"@class": "ai.BitVector", "bits": "0x01020a"}'
27+ o = s .deserialize (ser )
28+ assert isinstance (o , BitVector )
2329
2430 coh_bv = BitVector (hex_string = None , int_array = [1234 , 1235 ])
2531 ser = s .serialize (coh_bv )
2632 assert ser == b'\x15 {"@class": "ai.BitVector", "bits": "0x4d24d3"}'
33+ o = s .deserialize (ser )
34+ assert isinstance (o , BitVector )
2735
2836
2937def test_ByteVector_serialization () -> None :
3038 coh_int8v = ByteVector (bytes ([1 , 2 , 3 , 4 ]))
3139 ser = s .serialize (coh_int8v )
3240 assert ser == b'\x15 {"@class": "ai.Int8Vector", "array": "AQIDBA=="}'
41+ o = s .deserialize (ser )
42+ assert isinstance (o , ByteVector )
3343
3444
3545def test_FloatVector_serialization () -> None :
3646 coh_fv = FloatVector ([1.0 , 2.0 , 3.0 ])
3747 ser = s .serialize (coh_fv )
3848 assert ser == b'\x15 {"@class": "ai.Float32Vector", "array": [1.0, 2.0, 3.0]}'
49+ o = s .deserialize (ser )
50+ assert isinstance (o , FloatVector )
51+
52+
53+ def test_DocumentChunk_serialization () -> None :
54+ dc = DocumentChunk ("test" )
55+ ser = s .serialize (dc )
56+ assert ser == b'\x15 {"@class": "ai.DocumentChunk", "dataVersion": 0, "metadata": {"@ordered": true, "entries": []}, "text": "test"}'
57+ o = s .deserialize (ser )
58+ assert isinstance (o , DocumentChunk )
59+
60+ d = {"one" :"one-value" , "two" : "two-value" }
61+ dc = DocumentChunk ("test" ,d )
62+ ser = s .serialize (dc )
63+ assert ser == b'\x15 {"@class": "ai.DocumentChunk", "dataVersion": 0, "metadata": {"entries": [{"key": "one", "value": "one-value"}, {"key": "two", "value": "two-value"}]}, "text": "test"}'
64+ o = s .deserialize (ser )
65+ assert isinstance (o , DocumentChunk )
66+
67+ coh_fv = FloatVector ([1.0 , 2.0 , 3.0 ])
68+ d = {"one" :"one-value" , "two" : "two-value" }
69+ dc = DocumentChunk ("test" ,d , coh_fv )
70+ ser = s .serialize (dc )
71+ assert ser == b'\x15 {"@class": "ai.DocumentChunk", "dataVersion": 0, "metadata": {"entries": [{"key": "one", "value": "one-value"}, {"key": "two", "value": "two-value"}]}, "vector": {"@class": "ai.Float32Vector", "array": [1.0, 2.0, 3.0]}, "text": "test"}'
72+ o = s .deserialize (ser )
73+ assert isinstance (o , DocumentChunk )
74+
0 commit comments