Пример #1
0
    def test_record_with_record(self):
        """ tests nested records work """

        from records import RecordWithRecord
        from records import Thing

        data1 = {'thing1': {'id': 10}, 'thing2': {'id': 0}}
        data2 = {'thing1': Thing({'id': 10}), 'thing2': Thing({'id': 0})}
        data_with_default = {'thing1': {'id': 10}}

        record1 = RecordWithRecord(data1)
        record2 = RecordWithRecord(data2)
        record3 = RecordWithRecord(data_with_default)

        self.assertEqual(
            record1.serialize(),
            record2.serialize(),
            'nested records should be equal'
        )

        self.assertEqual(
            record1.serialize(),
            record3.serialize(),
            'records should be equal'
        )
Пример #2
0
    def test_thing(self):
        """ tests that you can serialize and deserialize thing record """

        from records import Thing

        data = {'id': 10}
        data_json = json.dumps(data)
        thing = Thing(data)
        thing_from_thing = Thing(thing)
        thing_from_json = Thing(data_json)

        self.assertEqual(
            eval(thing.serialize()),
            data,
            'thing should serialize to {"id": 10}'
        )

        self.assertEqual(
            json.dumps(thing.__dict__),
            thing_from_thing.serialize(),
            'thing should be able to serialize itself'
        )

        self.assertEqual(
            thing.serialize(),
            thing_from_json.serialize(),
            'thing should be able to initialize from json'
        )
Пример #3
0
    def test_map_record(self):
        """ tests that nested things have correct mappings and namespaces """

        from records import RecordWithMap
        from records import Thing

        data1 = {'thingMap': [{'string1': {'id': 10}},
                              {'string2': Thing({'id': 10})}],
                 'intMap': [{'lksdfl': 23}],
                 'thingMap2': [{'string1': {'id': 10}},
                              {'string2': Thing({'id': 10})}],
                 'thingMap3': [{'string1': {'id': 10}},
                              {'string2': Thing({'id': 10})}]}
        data2 = {'thingMap': [{'string1': {'id': 10}},
                              {'string2': Thing({'id': 10})}],
                 'intMap': [{'lksdfl': 'NOT A STRING'}]}

        record1 = RecordWithMap(data1)
        record2 = RecordWithMap(record1)

        with self.assertRaises(TypeError):
            RecordWithMap(data2)

        self.assertEqual(
            record1.serialize(),
            record2.serialize()
        )
Пример #4
0
    def test_record_with_array(self):
        """ tests records with arrays """

        from records import RecordWithArray
        from records import Thing

        data1 = {
            'things': [{
                'id': 10
            }, {
                'id': 50
            }],
            'numbers': [10, 40],
            'things2': []
        }  # NOQA
        data2 = {
            'things': [Thing({'id': 10}), {
                'id': 50
            }],
            'numbers': [10, 40],
            'things2': []
        }  # NOQA
        data3 = {'things': [], 'numbers': [], 'things2': []}
        data4 = {
            'things': [{
                'id': 10
            }, {
                'id': 50
            }],
            'numbers': ['not a long'],
            'things2': []
        }  # NOQA
        data5 = {
            'things': [{
                'id': 'not a long'
            }, {
                'id': 50
            }],
            'numbers': [10, 40],
            'things2': []
        }  # NOQA

        record1 = RecordWithArray(data1)
        record2 = RecordWithArray(data2)
        record3 = RecordWithArray(data3)

        self.assertEqual(
            record1.serialize(), record2.serialize(),
            'array records not able to initialize with nested objects')

        self.assertEqual(record3.serialize(),
                         '{"things": [], "numbers": [], "things2": []}')

        self.assertEqual(record1.dict(), data1)

        with self.assertRaises(TypeError):
            RecordWithArray(data4)

        with self.assertRaises(TypeError):
            RecordWithArray(data5)
Пример #5
0
    def test_record_with_union(self):
        """ tests records with union types """

        from records import RecordWithUnion
        from records import Thing

        data1 = {'optionalString': 'hello', 'intOrThing': Thing({'id': 2}), 'nullOrThingArray': None}
        data2 = {'optionalString': 'hello', 'intOrThing': {'id': 2}}
        data3 = {'optionalString': None, 'intOrThing': 10, 'nullOrThingArray': [{'id': 2}]}
        data4 = {'optionalString': 'hello', 'intOrThing': 'not int or thing'}

        record1 = RecordWithUnion(data1)
        record2 = RecordWithUnion(data2)
        record3 = RecordWithUnion(data3)

        self.assertEqual(
            '{"optionalString": "hello", "intOrThing": {"id": 2}, "nullOrThingArray": null}',
            record1.serialize()
        )

        self.assertEqual(
            record1.serialize(),
            record2.serialize()
        )

        self.assertEqual(
            record3.nullOrThingArray[0].id,
            2
        )

        self.assertEqual(
            record3.optionalString,
            None
        )

        self.assertEqual(
            record3.intOrThing,
            10
        )


        self.assertEqual(
            record3.serialize(),
            '{"optionalString": null, "intOrThing": 10, "nullOrThingArray": [{"id": 2}]}'
        )

        with self.assertRaises(TypeError):
            RecordWithUnion(data4)
Пример #6
0
    def test_nested_map_record(self):
        """ tests that nested maps will work """

        from records import RecordWithNestedMap
        from records import Thing

        data1 = {
            'thingMap': [{
                'string1': {
                    'id': 10
                }
            }, {
                'string2': Thing({'id': 10})
            }],
            'nestedThingMap': [
                {
                    'foo': [{
                        'string1': {
                            'id': 10
                        }
                    }, {
                        'string2': Thing({'id': 10})
                    }]
                },
                {
                    'bar': [{
                        'string1': {
                            'id': 10
                        }
                    }, {
                        'string2': Thing({'id': 10})
                    }]
                },
            ],
            'nestedIntMap': [
                {
                    'foo': [{
                        'string1': 10
                    }, {
                        'string2': 11
                    }]
                },
                {
                    'bar': [{
                        'string1': 10
                    }, {
                        'string2': 11
                    }]
                },
            ],
            'mappedThingArray': [{
                'foo': [{
                    'id': 10
                }, Thing({'id': 20})]
            }, {
                'bar': [{
                    'id': 10
                }, Thing({'id': 20})]
            }]
        }
        data2 = {
            'thingMap': [{
                'string1': {
                    'id': 10
                }
            }, {
                'string2': Thing({'id': 10})
            }],
            'nestedThingMap': [
                {
                    'foo': [{
                        'string1': {
                            'id': 10
                        }
                    }, {
                        'string2': Thing({'id': 10})
                    }]
                },
                {
                    'bar': [{
                        'string1': {
                            'id': 10
                        }
                    }, {
                        'string2': Thing({'id': 10})
                    }]
                },
            ],
            'nestedIntMap': [
                {
                    'foo': [{
                        'string1': 'NOT AN INT'
                    }, {
                        'string2': 11
                    }]
                },
                {
                    'bar': [{
                        'string1': 10
                    }, {
                        'string2': 11
                    }]
                },
            ],
            'mappedThingArray': [{
                'foo': [{
                    'id': 10
                }, Thing({'id': 20})]
            }, {
                'bar': [{
                    'id': 10
                }, Thing({'id': 20})]
            }]
        }
        record1 = RecordWithNestedMap(data1)
        record2 = RecordWithNestedMap(record1)

        with self.assertRaises(TypeError):
            RecordWithNestedMap(data2)

        self.assertEqual(record1.serialize(), record2.serialize())