Exemplo n.º 1
0
    def test_delete(self):
        example_data.load()

        # delete object updates indexes
        fighter_writer = ModelWriter(Fighter)
        self.assertEqual(ds.hgetall('u:Fighter:name'), {'Alice': '1', 'Bob': '2'})
        self.assertEqual(ds.smembers('i:Fighter:city:1'), set(['1', '2']))
        self.assertEqual(ds.zrange('z:Fighter:weight', 0, -1), ['2', '1'])
        fighter1 = Fighter(Fighter.by_id(1))
        fighter_writer.delete(fighter1)
        self.assertRaises(NotFoundError, Fighter, Fighter.by_id(1))
        self.assertTrue(fighter1.oid is None)
        self.assertEqual(ds.hgetall('u:Fighter:name'), {'Bob': '2'})
        self.assertEqual(ds.smembers('i:Fighter:city:1'), set(['2']))
        self.assertEqual(ds.zrange('z:Fighter:weight', 0, -1), ['2'])

        # delete container item updates indexes
        gang_members_writer = SetFieldWriter(Gang.members)
        self.assertEqual(ds.smembers('Gang:1:members'), set(['1', '2']))
        self.assertEqual(ds.hgetall('u:Gang:members'), {'1': '1', '2': '1'})
        gang1 = Gang(Gang.by_id(1))
        gang_members_writer.remove(gang1.members, Fighter.by_id(1))
        self.assertEqual(ds.smembers('Gang:1:members'), set(['2']))
        self.assertEqual(ds.hgetall('u:Gang:members'), {'2': '1'})

        gang_cities_writer = SetFieldWriter(Gang.cities)
        self.assertEqual(ds.smembers('Gang:1:cities'), set(['1', '3']))
        self.assertEqual(ds.smembers('i:Gang:cities:1'), set(['1']))
        self.assertEqual(ds.smembers('i:Gang:cities:3'), set(['1']))
        gang1 = Gang(Gang.by_id(1))
        gang_cities_writer.remove(gang1.cities, City.by_id(1))
        self.assertEqual(ds.smembers('Gang:1:cities'), set(['3']))
        self.assertFalse(ds.exists('i:Gang:cities:1'))
        self.assertEqual(ds.smembers('i:Gang:cities:3'), set(['1']))

        # delete object updates lists of listed attributes
        self.assertEqual(ds.lrange('l:Gang:hqcity:3', 0, -1), ['1', '2'])
        gang_writer = ModelWriter(Gang)
        gang_writer.delete(gang1)
        self.assertEqual(ds.lrange('l:Gang:hqcity:3', 0, -1), ['2'])

        # autodelete owned item
        fighter_skill_list_writer = ModelWriter(FighterSkillList)
        skill_instance_writer = ModelWriter(SkillInstance)
        fighter_skills_writer = ListFieldWriter(FighterSkillList.skills, element_writer = skill_instance_writer)

        self.assertEqual(ds.lrange('FighterSkillList:1:skills', 0, -1), ['1', '2'])
        self.assertEqual(ds.lrange('FighterSkillList:2:skills', 0, -1), ['3', '4'])
        for i in range(1, 5):
            self.assertTrue(ds.exists('SkillInstance:{0}'.format(i)))

        handle = FighterSkillList.by_owner(Fighter.by_id(2))
        fsl = FighterSkillList(handle)
        ski = SkillInstance(SkillInstance.by_id(3))
        fighter_skills_writer.remove(fsl.skills, ski)

        self.assertTrue(ski.oid is None)
        self.assertEqual(ds.lrange('FighterSkillList:2:skills', 0, -1), ['4'])
        self.assertFalse(ds.exists('SkillInstance:3'))
        self.assertTrue(ds.exists('SkillInstance:4'))

        # don't allow removing not owned object
        ski = SkillInstance(SkillInstance.by_id(1))
        self.assertRaises(NotFoundError, fighter_skills_writer.remove, fsl.skills, ski)
        self.assertTrue(ds.exists('SkillInstance:1'))
Exemplo n.º 2
0
 def setUp(self):
     example_data.load()
Exemplo n.º 3
0
    def test_update(self):
        example_data.load()
        fighter_writer = ModelWriter(Fighter)

        # update unique attribute
        fighter = Fighter(Fighter.by_id(2))
        fighter.name = 'Bobby'
        fighter.age = 41
        fighter_writer.update_all(fighter)
        self.assertEqual(ds.hgetall('u:Fighter:name'), {'Alice': '1', 'Bobby': '2'})
        fighter2 = Fighter(Fighter.by_id(2))
        self.assertEqual(fighter2.name, 'Bobby')
        self.assertEqual(fighter2.age, 41)
        fighter_writer.update(fighter2, name = 'Robert', weight = 99.9)
        self.assertEqual(ds.hgetall('u:Fighter:name'), {'Alice': '1', 'Robert': '2'})
        self.assertEqual(fighter2.name, 'Robert')
        self.assertEqual(fighter2.weight, 99.9)
        fighter3 = Fighter(Fighter.by_id(2))
        self.assertEqual(fighter3.name, 'Robert')
        self.assertEqual(fighter3.weight, 99.9)

        # update indexed attribute
        self.assertEqual(ds.smembers('i:Fighter:city:1'), set(['1', '2']))
        self.assertEqual(ds.smembers('i:Fighter:city:2'), set())
        fighter1 = Fighter(Fighter.by_id(1))
        fighter2 = Fighter(Fighter.by_id(2))
        fighter2.city = City.by_id(2)
        fighter_writer.update_all(fighter2)
        self.assertEqual(ds.smembers('i:Fighter:city:1'), set(['1']))
        self.assertEqual(ds.smembers('i:Fighter:city:2'), set(['2']))
        fighter_writer.update(fighter1, city = City.by_id(2))
        self.assertEqual(ds.smembers('i:Fighter:city:1'), set())
        self.assertEqual(ds.smembers('i:Fighter:city:2'), set(['1', '2']))
        city1 = City(City.by_id(1))
        fighter_writer.update(fighter1, city = city1)
        self.assertEqual(ds.smembers('i:Fighter:city:1'), set(['1']))
        self.assertEqual(ds.smembers('i:Fighter:city:2'), set(['2']))

        # update zindexed attribute
        self.assertEqual(ds.zrange('z:Fighter:weight', 0, -1), ['2', '1'])
        fighter_writer.update(fighter1, weight = 99.91)
        self.assertEqual(ds.zrange('z:Fighter:weight', 0, -1), ['2', '1'])
        fighter_writer.update(fighter1, weight = 99.89)
        self.assertEqual(ds.zrange('z:Fighter:weight', 0, -1), ['1', '2'])

        # update listed attribute
        self.assertEqual(ds.lrange('l:Gang:hqcity:1', 0, -1), [])
        self.assertEqual(ds.lrange('l:Gang:hqcity:3', 0, -1), ['1', '2'])
        gang2 = Gang(Gang.by_id(2))
        gang_writer = ModelWriter(Gang)
        gang_writer.update(gang2, hqcity = city1)
        self.assertEqual(ds.lrange('l:Gang:hqcity:1', 0, -1), ['2'])
        self.assertEqual(ds.lrange('l:Gang:hqcity:3', 0, -1), ['1'])

        # update object and sorted set atomically
        self.assertEqual(ds.zrange('Fighter:1:weapons', 0, -1), ['2', '1', '3'])
        self.assertEqual(ds.hgetall('Weapon:2'), {'description': 'third', 'power': '34.2'})
        weapon_writer = ModelWriter(Weapon)
        fighter_weapons_writer = SortedSetFieldWriter(Fighter.weapons, weapon_writer)
        w2 = Weapon(Weapon.by_id(2))
        fighter_weapons_writer.update(fighter1.weapons, w2,
                                      power = 70, description = 'improved')
        self.assertEqual(ds.zrange('Fighter:1:weapons', 0, -1), ['1', '3', '2'])
        self.assertEqual(ds.hgetall('Weapon:2'), {'description': 'improved', 'power': '70'})
        self.assertEqual(w2.power, 70)
        self.assertEqual(w2.description, 'improved')
        w2.power -= 60
        w2.description = 'degraded'
        fighter_weapons_writer.update_all(fighter1.weapons, w2)
        self.assertEqual(ds.zrange('Fighter:1:weapons', 0, -1), ['2', '1', '3'])
        self.assertEqual(ds.hgetall('Weapon:2'), {'description': 'degraded', 'power': '10'})