def test_encode_vector(self): """ Test that when encoding a vector, its array representation is returned. """ v = Vector({'a': 1}, {'b': 2}) self.assertEqual(v.to_array(), Exportable.encode(v))
def test_encode_vector_list_in_dict(self): """ Test that when encoding a dictionary with a list of vectors in it, the list's items are encoded as well. """ v = [Vector({'a': 1}, {'b': 2}), Vector({'c': 3}, {'d': 4})] data = {'a': v, 'e': 5} self.assertEqual( { 'a': [{ 'class': "<class 'vsm.vector.Vector'>", 'attributes': { 'b': 2 }, 'dimensions': { 'a': 1 } }, { 'class': "<class 'vsm.vector.Vector'>", 'attributes': { 'd': 4 }, 'dimensions': { 'c': 3 } }], 'e': 5, }, Exportable.encode(data))
def test_decode_vector_list(self): """ Test that when decoding a list of vectors, the list's items are decoded as well. """ vectors = [Vector({'a': 1}, {'b': 2}), Vector({'c': 3}, {'d': 4})] encoded = Exportable.encode(vectors) decoded = Exportable.decode(encoded) self.assertTrue( all(vector.dimensions == v.dimensions for vector, v in zip(vectors, decoded))) self.assertTrue( all(vector.attributes == v.attributes for vector, v in zip(vectors, decoded)))
def test_decode_vector_list_in_dict(self): """ Test that when decoding a dictionary with a list of vectors in it, the list's items are decoded as well. """ v = [Vector({'a': 1}, {'b': 2}), Vector({'c': 3}, {'d': 4})] data = {'a': v, 'e': 5} encoded = Exportable.encode(data) decoded = Exportable.decode(encoded) self.assertTrue( all(vector.dimensions == v.dimensions for vector, v in zip(v, decoded['a']))) self.assertTrue( all(vector.attributes == v.attributes for vector, v in zip(v, decoded['a']))) self.assertEqual(5, decoded['e'])
def test_decode_vector(self): """ Test that when decoding a vector, its array representation is returned. """ v = Vector({'a': 1}, {'b': 2}) encoded = Exportable.encode(v) decoded = Exportable.decode(encoded) self.assertEqual(v.dimensions, decoded.dimensions) self.assertEqual(v.attributes, decoded.attributes)
def test_decode_vector(self): """ Test that when decoding a vector, it is converted into a dictionary. """ v = Vector({'a': 1}, {'b': 2}) data = Exportable.encode({'vector': v}) decoded = Exportable.decode(data) self.assertTrue({v}, decoded.keys()) self.assertEqual(v.__dict__, decoded['vector'].__dict__)
def test_encode_vector(self): """ Test that when encoding a vector, it is converted into a dictionary. """ v = Vector({'a': 1}, {'b': 2}) data = {'vector': v} encoding = Exportable.encode(data) json.loads(json.dumps(encoding)) self.assertEqual("<class 'vsm.vector.Vector'>", encoding['vector']['class']) self.assertEqual({'a': 1}, encoding['vector']['dimensions']) self.assertEqual({'b': 2}, encoding['vector']['attributes'])
def test_encode_vector_list(self): """ Test that when encoding a list of vectors, the list's items are encoded as well. """ v = [Vector({'a': 1}, {'b': 2}), Vector({'c': 3}, {'d': 4})] self.assertEqual([{ 'class': "<class 'vsm.vector.Vector'>", 'attributes': { 'b': 2 }, 'dimensions': { 'a': 1 } }, { 'class': "<class 'vsm.vector.Vector'>", 'attributes': { 'd': 4 }, 'dimensions': { 'c': 3 } }], Exportable.encode(v))