Beispiel #1
0
    def test_encode_primitive_recursive_dict(self):
        """
        Test that when encoding a dictionary with primitive values stored recursively, the same dictionary is returned.
        """

        data = {'a': 1, 'b': {'c': 1}}
        self.assertEqual(data, Exportable.encode({'a': 1, 'b': {'c': 1}}))
Beispiel #2
0
    def test_encode_primitive_list(self):
        """
        Test that when encoding a list of primitives, the list's items are unchanged.
        """

        data = [1, 2]
        self.assertEqual(data, Exportable.encode(data))
Beispiel #3
0
    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))
Beispiel #4
0
    def test_encode_primitive_dict(self):
        """
        Test that when encoding a dictionary with primitive values, the same dictionary is returned.
        """

        data = {'a': 1, 'b': [1, 2]}
        self.assertEqual(data, Exportable.encode({'a': 1, 'b': [1, 2]}))
Beispiel #5
0
    def test_decode_dict_in_list(self):
        """
        Test that when decoding a list with a dictionary in it, the dictionary is decoded as well.
        """

        data = [{'a': [1, 2], 'b': 3}, 5]
        self.assertEqual(data, Exportable.decode(Exportable.encode(data)))
Beispiel #6
0
    def test_decode_primitive_list_in_dict(self):
        """
        Test that when decoding a dictionary with a list of primitives in it, the list's items are decoded as well.
        """

        data = {'a': [1, 2], 'b': 3}
        self.assertEqual(data, Exportable.decode(Exportable.encode(data)))
Beispiel #7
0
    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))
Beispiel #8
0
    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)
Beispiel #9
0
    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__)
Beispiel #10
0
    def test_encode_primitive_copy(self):
        """
        Test that when encoding a dictionary of primitives, the encoding is a copy.
        """

        data = {'a': 1, 'b': {'c': 1}}
        encoding = Exportable.encode({'a': 1, 'b': {'c': 1}})
        self.assertEqual(data, encoding)
        data['b']['c'] = 2
        self.assertEqual(2, data['b']['c'])
        self.assertEqual(1, encoding['b']['c'])
Beispiel #11
0
    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'])
Beispiel #12
0
    def test_decode_nested(self):
        """
        Test that when decoding an exportable object that has an exportable object, the highest one is decoded.
        """

        tfidf = TFIDF(idf={'a': 1}, documents=10)
        data = Exportable.encode({'tfidf': tfidf})
        decoded = Exportable.decode(data)
        self.assertTrue({tfidf}, decoded.keys())
        self.assertEqual(tfidf.local_scheme.__dict__,
                         decoded['tfidf'].local_scheme.__dict__)
        self.assertEqual(tfidf.global_scheme.__dict__,
                         decoded['tfidf'].global_scheme.__dict__)
Beispiel #13
0
    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)))
Beispiel #14
0
    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'])
Beispiel #15
0
    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))
Beispiel #16
0
    def test_encode_empty_dict(self):
        """
        Test that when encoding an empty dictionary, another empty dictionary is returned.
        """

        self.assertEqual({}, Exportable.encode({}))