Exemplo n.º 1
0
    def test_build_instance(self):
        # meet required_fields and strict_fields
        # required_fields = [
        #     'id', 'name',
        #     'attributes.strength', 'attributes.armor',
        #     'skills', 'skills.name', 'skills.damage'
        # ]
        # strict_fields = ['id', 'slots', 'skills.damage', 'skills.level']

        kwargs = dict(
            id=ObjectId(),
            name='reorx',
            attributes={
                'strength': 10,
                'armor': 10
            },
            skills=[{'name': 'test', 'damage': 1.1}],
            slots=[]
        )

        ins = self.UserDict.build_instance(**kwargs)
        d = build_dict(self.UserDict.struct, **kwargs)

        assert d['name'] == 'reorx'
        assert d['skills'][0]['name'] == 'test'

        del ins['id']
        del d['id']
        assert hash_dict(ins) == hash_dict(d)

        with assert_raises(TypeError):
            ins = self.UserDict.build_instance(name=1)
Exemplo n.º 2
0
    def test_build_instance(self):
        # meet required_fields and strict_fields
        # required_fields = [
        #     'id', 'name',
        #     'attributes.strength', 'attributes.armor',
        #     'skills', 'skills.name', 'skills.damage'
        # ]
        # strict_fields = ['id', 'slots', 'skills.damage', 'skills.level']

        kwargs = dict(id=ObjectId(),
                      name='reorx',
                      attributes={
                          'strength': 10,
                          'armor': 10
                      },
                      skills=[{
                          'name': 'test',
                          'damage': 1.1
                      }],
                      slots=[])

        ins = self.UserDict.build_instance(**kwargs)
        d = build_dict(self.UserDict.struct, **kwargs)

        assert d['name'] == 'reorx'
        assert d['skills'][0]['name'] == 'test'

        del ins['id']
        del d['id']
        assert hash_dict(ins) == hash_dict(d)

        with assert_raises(TypeError):
            ins = self.UserDict.build_instance(name=1)
Exemplo n.º 3
0
    def test_gen(self):
        assert isinstance(self.UserDict.gen.id(), ObjectId)

        d = self.UserDict.gen.attributes()
        print d
        assert len(d.keys()) == 3 and 'strength' in d

        d = self.UserDict.gen.skills.parents(name='foo')
        assert hash_dict(d) == hash_dict(
            build_dict(self.UserDict.struct['skills'][0]['parents'][0], name='foo'))
Exemplo n.º 4
0
    def test_gen(self):
        assert isinstance(self.UserDict.gen.id(), ObjectId)

        d = self.UserDict.gen.attributes()
        print d
        assert len(d.keys()) == 3 and 'strength' in d

        d = self.UserDict.gen.skills.parents(name='foo')
        assert hash_dict(d) == hash_dict(
            build_dict(self.UserDict.struct['skills'][0]['parents'][0],
                       name='foo'))
Exemplo n.º 5
0
    def test_hash_dict(self):
        d1 = self.d()
        d2 = self.d()
        assert d1 is not d2
        assert hash_dict(d1) == hash_dict(d2)

        # change d2
        d2['id'] = ObjectId()
        assert hash_dict(d1) != hash_dict(d2)

        # acturally a test for validate_dict,
        # to test whether dict is changed or not after validate process
        d3 = self.d()
        hash_before = hash_dict(d3)
        validate_dict(d3, self.s())
        assert hash_dict(d3) == hash_before
Exemplo n.º 6
0
    def test_hash_dict(self):
        d1 = self.d()
        d2 = self.d()
        assert d1 is not d2
        assert hash_dict(d1) == hash_dict(d2)

        # change d2
        d2['id'] = ObjectId()
        assert hash_dict(d1) != hash_dict(d2)

        # acturally a test for validate_dict,
        # to test whether dict is changed or not after validate process
        d3 = self.d()
        hash_before = hash_dict(d3)
        validate_dict(d3, self.s())
        assert hash_dict(d3) == hash_before