示例#1
0
 def test_new_key_failure(self):
     data = [{
         'id': 1,
         'title': 'One',
         'baz': [{
             'id': 2
         }, {
             'id': 1,
             'bar': {
                 'id': 1
             }
         }]
     }]
     norm = Normalize()
     norm.define_primary('foo')
     norm.add_one_to_many_key('asdf', 'qwer', 'foo', 'bar')
     try:
         norm.parse(data)
         self.assertTrue(False)
     except ValueError:
         self.assertTrue(True)
示例#2
0
 def test_swap_failure(self):
     data = [{
         'id': 1,
         'title': 'One',
         'baz': [{
             'id': 2
         }, {
             'id': 1,
             'bar': {
                 'id': 1
             }
         }]
     }]
     norm = Normalize()
     norm.define_primary('foo')
     norm.swap_primary('blah')
     try:
         norm.parse(data)
         self.assertTrue(False)
     except ValueError:
         self.assertTrue(True)
示例#3
0
 def test_remove_flds(self):
     norm = Normalize()
     norm.define_primary('foo')
     norm.remove_flds('foo', 'title')
     self.assertEqual(norm.parse([{
         'id': 1,
         'title': 'One'
     }]), {
         'entities': {
             'foo': {
                 1: {
                     'id': 1
                 }
             }
         },
         'results': [1]
     })
示例#4
0
文件: example.py 项目: farmobile/norm
    #norm.set_entity_order(('addresses', 'users'))

    # you can swap the primary entity in the data set with a nested entity by setting
    # a nested entity name to swap to with this method. If the requested entity to
    # switch is not defined, a ValueError is raised.
    #norm.swap_primary('addresses')

    # You can create a new one to many key between entities by defining the key
    # relationship you want to create. The first argument is the name for the new key
    # field, the second argument is the reference key used to collect the matching rows
    # (the "one" in one to many), the thrid argument is the entity to add the key to,
    # and the forth argument is the entity to pull the keys from.
    #norm.add_one_to_many_key('user_ids', 'address', 'addresses', 'users')

    # normalize and return the data
    pprint.pprint(norm.parse(data))

    # result:

    #{'entities': {'addresses': {1: {'city': 'somewhereville',
    #               'id': 1,
    #               'state': 'Kansas',
    #               'street': '100 somewhere lane'},
    #           2: {'city': 'somewhereville',
    #               'id': 2,
    #               'state': 'Kansas',
    #               'street': '101 somewhere lane'},
    #           6: {'city': 'somewhereville',
    #               'id': 6,
    #               'state': 'Kansas',
    #               'street': '106 somewhere lane'}},
示例#5
0
    def test_parse(self):
        norm = Normalize()
        norm.define_primary('test', 'ID')
        try:
            norm.parse([{'id': 1, 'title': 'Some Article'}])
            self.assertTrue(False)
        except ValueError:
            self.assertTrue(True)

        norm = Normalize()
        norm.define_primary('test')
        self.assertEqual(
            norm.parse([{
                'id': 1,
                'title': 'Some Article'
            }]), {
                'entities': {
                    'test': {
                        1: {
                            'id': 1,
                            'title': 'Some Article'
                        }
                    }
                },
                'results': [1]
            })
        norm = Normalize()
        norm.define_primary('foo')
        norm.define_nested_entity('bar', 'baz')
        norm.define_nested_entity('asdf', 'qwer')
        self.assertEqual(norm.parse([]), None)
        self.assertEqual(
            norm.parse([{
                'id': 1,
                'title': 'One',
                'baz': {
                    'id': 1
                }
            }, {
                'id': 2,
                'title': 'Two',
                'baz': {
                    'id': 2
                }
            }]), {
                'entities': {
                    'foo': {
                        1: {
                            'baz': [1],
                            'id': 1,
                            'title': 'One'
                        },
                        2: {
                            'baz': [2],
                            'id': 2,
                            'title': 'Two'
                        }
                    },
                    'asdf': {},
                    'bar': {
                        1: {
                            'id': 1
                        },
                        2: {
                            'id': 2
                        }
                    }
                },
                'results': [1, 2]
            })

        data = [{
            'id': 1,
            'title': 'One',
            'baz': [{
                'id': 2
            }, {
                'id': 1,
                'bar': {
                    'id': 1
                }
            }]
        }]
        norm = Normalize()
        norm.define_primary('foo')
        norm.define_nested_entity('test', 'baz')
        self.assertEqual(
            norm.parse(data), {
                'entities': {
                    'test': {
                        1: {
                            'bar': {
                                'id': 1
                            },
                            'id': 1
                        },
                        2: {
                            'id': 2
                        }
                    },
                    'foo': {
                        1: {
                            'baz': [2, 1],
                            'id': 1,
                            'title': 'One'
                        }
                    }
                },
                'results': [1]
            })

        data = [{
            'id': 1,
            'title': 'One',
            'baz': [{
                'id': 2
            }, {
                'id': 1,
                'bar': {
                    'id': 1
                }
            }]
        }]
        norm = Normalize()
        norm.define_primary('foo')
        norm.define_nested_entity('test', 'baz')
        norm.swap_primary('test')
        self.assertEqual(
            norm.parse(data), {
                'entities': {
                    'test': {
                        1: {
                            'bar': {
                                'id': 1
                            },
                            'id': 1
                        },
                        2: {
                            'id': 2
                        }
                    },
                    'foo': {
                        1: {
                            'baz': [2, 1],
                            'id': 1,
                            'title': 'One'
                        }
                    }
                },
                'results': [1, 2]
            })

        data = [{
            'id': 1,
            'title': 'One',
            'baz': [{
                'id': 2
            }, {
                'id': 1,
                'bar': {
                    'id': 1
                }
            }]
        }]
        norm = Normalize()
        norm.define_primary('foo')
        norm.define_nested_entity('test', 'baz')
        norm.swap_primary('test')
        norm.add_one_to_many_key('foo_ids', 'id', 'test', 'foo')
        self.assertEqual(
            norm.parse(data), {
                'entities': {
                    'test': {
                        1: {
                            'foo_ids': [1],
                            'bar': {
                                'id': 1
                            },
                            'id': 1
                        },
                        2: {
                            'foo_ids': [],
                            'id': 2
                        }
                    },
                    'foo': {
                        1: {
                            'baz': [2, 1],
                            'id': 1,
                            'title': 'One'
                        }
                    }
                },
                'results': [1, 2]
            })

        data = [{
            'id': 1,
            'title': 'One',
            'baz': [{
                'id': 1,
                'bar': {
                    'id': 1
                }
            }]
        }]
        norm = Normalize()
        norm.define_primary('foo')
        norm.define_nested_entity('test', 'baz')
        self.assertEqual(
            norm.parse(data), {
                'entities': {
                    'test': {
                        1: {
                            'bar': {
                                'id': 1
                            },
                            'id': 1
                        }
                    },
                    'foo': {
                        1: {
                            'baz': [1],
                            'id': 1,
                            'title': 'One'
                        }
                    }
                },
                'results': [1]
            })
示例#6
0
    #norm.set_entity_order(('addresses', 'users'))

    # you can swap the primary entity in the data set with a nested entity by setting
    # a nested entity name to swap to with this method. If the requested entity to
    # switch is not defined, a ValueError is raised.
    #norm.swap_primary('addresses')

    # You can create a new one to many key between entities by defining the key
    # relationship you want to create. The first argument is the name for the new key
    # field, the second argument is the reference key used to collect the matching rows
    # (the "one" in one to many), the thrid argument is the entity to add the key to,
    # and the forth argument is the entity to pull the keys from.
    #norm.add_one_to_many_key('user_ids', 'address', 'addresses', 'users')

    # normalize and return the data
    pprint.pprint(norm.parse(data))

    # result:

    #{'entities': {'addresses': {1: {'city': 'somewhereville',
    #               'id': 1,
    #               'state': 'Kansas',
    #               'street': '100 somewhere lane'},
    #           2: {'city': 'somewhereville',
    #               'id': 2,
    #               'state': 'Kansas',
    #               'street': '101 somewhere lane'},
    #           6: {'city': 'somewhereville',
    #               'id': 6,
    #               'state': 'Kansas',
    #               'street': '106 somewhere lane'}},