Exemple #1
0
def load():
    ds.flushdb()

    c1 = create_city('Reixte', True)
    c2 = create_city('Damtoo', True)
    c3 = create_city('Toynbe', False)
    create_connection(c1, c2)
    create_connection(c1, c3)

    f1 = create_fighter('Alice', 20, 107.44, 1400000002)
    f2 = create_fighter('Bob', 23, 102.923, 1400000001)

    g1 = create_gang('Ghetto Warriors', f1)
    add_member(g1, f1)
    add_member(g1, f2)
    add_gang_city(g1, c1)
    add_gang_city(g1, c3)
    g2 = create_gang('Midnight Club', Fighter.by_id(0))

    # add weapons to fighter; weapons are owned (owned = True on the
    # SortedSetField), so we attach weapon writer to fighter weapons writer
    weapon_writer = ModelWriter(Weapon)
    fighter_weapons_writer = SortedSetFieldWriter(Fighter.weapons, weapon_writer)
    w1 = Weapon(description = 'second', power = 50.5)
    w2 = Weapon(description = 'third', power = 34.2)
    w3 = Weapon(description = 'first', power = 50.7)
    for w in w1, w2, w3:
        fighter_weapons_writer.append(f1.weapons, w)

    # skills
    skill_writer = ModelWriter(Skill)
    sk1 = Skill(category = 1, name = 'Strength', description = 'Strength...')
    sk2 = Skill(category = 3, name = 'Karate', description = 'Karate...')
    map(skill_writer.create, [sk1, sk2])

    fighter_skill_list_writer = ModelWriter(FighterSkillList)
    f1skills = FighterSkillList()
    f2skills = FighterSkillList()
    fighter_skill_list_writer.create(f1skills, f1)
    fighter_skill_list_writer.create(f2skills, f2)

    # can do this if and only if owned = True in the ListField
    skill_instance_writer = ModelWriter(SkillInstance)
    fighter_skills_writer = ListFieldWriter(FighterSkillList.skills, element_writer = skill_instance_writer)
    ski1 = SkillInstance(skill = sk1.handle(), value = 21)
    ski2 = SkillInstance(skill = sk2, value = 15)
    fighter_skills_writer.append(f1skills.skills, ski1)
    fighter_skills_writer.append(f1skills.skills, ski2)

    ski1 = SkillInstance(skill = sk1, value = 27)
    ski2 = SkillInstance(skill = sk2, value = 91)
    fighter_skills_writer.append(f2skills.skills, ski1)
    fighter_skills_writer.append(f2skills.skills, ski2)
Exemple #2
0
connection.flushdb()

# define Color class
class Color(Model):
    name = Attribute(unique = True)
    rgb = IntegerField()

# define Person class
class Person(Model):
    name = Attribute(unique = True)
    fave_colors = ListField(Color, indexed = True)

# create writer objects
color_writer = ModelWriter(Color)
person_writer = ModelWriter(Person)
fave_colors_writer = ListFieldWriter(Person.fave_colors)

# create some colors
c1 = Color(name = 'red', rgb = 0xff0000)
c2 = Color(name = 'green', rgb = 0x00ff00)
c3 = Color(name = 'blue', rgb = 0x0000ff)

# save the colors in Redis
map(color_writer.create, [c1, c2, c3])

# create a person in Redis
person = Person(name = 'Cristina')
person_writer.create(person)

# set fave colors of this person
fave_colors_writer.append(person.fave_colors, Color.find(name = 'blue'))
Exemple #3
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'))
Exemple #4
0
    def test_write(self):
        # basic model
        city_writer = ModelWriter(City)
        c1 = City(name = 'Reixte', coast = True)
        c2 = City(name = 'Damtoo', coast = True)
        c3 = City(name = 'Toynbe', coast = False)
        map(city_writer.create, [c1, c2, c3])
        self.assertEqual((c1.oid, c2.oid, c3.oid), ('1', '2', '3'))
        self.assertEqual(City(City.by_id(1)).oid, '1')
        self.assertEqual(ds.hgetall('City:1'), {'name': 'Reixte', 'coast': '1'})
        self.assertEqual(ds.hgetall('City:2'), {'name': 'Damtoo', 'coast': '1'})
        self.assertEqual(ds.hgetall('City:3'), {'name': 'Toynbe', 'coast': '0'})

        # list field referencing model
        city_connections_writer = ListFieldWriter(City.connections)
        city_connections_writer.append(c1.connections, c2)
        city_connections_writer.append(c2.connections, c1)
        city_connections_writer.append(c1.connections, c3)
        city_connections_writer.append(c3.connections, c1)
        self.assertEqual(ds.lrange('City:1:connections', 0, -1), ['2', '3'])
        self.assertEqual(ds.lrange('City:2:connections', 0, -1), ['1'])
        self.assertEqual(ds.lrange('City:3:connections', 0, -1), ['1'])

        # unique indexed field
        fighter_writer = ModelWriter(Fighter)
        dtime1 = datetime.utcfromtimestamp(1400000002)
        dtime2 = datetime.utcfromtimestamp(1400000001)
        f1 = Fighter(name = 'Alice', age = 29, weight = 73.2, joined = dtime1, city = City.by_id(1))
        f2 = Fighter(name = 'Bob', age = 23, weight = 98, joined = dtime2, city = City.by_id(1))
        map(fighter_writer.create, [f1, f2])
        self.assertEqual(ds.hgetall('Fighter:1'), {'name': 'Alice', 'age': '29', 'weight': '73.2', 'joined': '1400000002', 'city': '1'})
        self.assertEqual(ds.hgetall('Fighter:2'), {'name': 'Bob', 'age': '23', 'weight': '98', 'joined': '1400000001', 'city': '1'})
        self.assertEqual(ds.hgetall('u:Fighter:name'), {'Alice': '1', 'Bob': '2'})

        # indexed reference field
        self.assertEqual(ds.smembers('i:Fighter:city:1'), set(['1', '2']))

        # zindexed fields
        self.assertEqual(ds.zrange('z:Fighter:age', 0, -1), ['2', '1'])
        self.assertEqual(ds.zrange('z:Fighter:weight', 0, -1), ['1', '2'])
        self.assertEqual(ds.zrange('z:Fighter:joined', 0, -1), ['2', '1'])

        # missing argument
        self.assertRaises(BadArgsError, Fighter, name = 'MissingWeight', age = 30, city = 1)

        # unique attribute
        f3 = Fighter(name = 'Bob', age = 30, weight = 80, joined = dtime1, city = 1)
        self.assertRaises(UniqueError, fighter_writer.create, f3)

        # basic model
        gang_writer = ModelWriter(Gang)
        g1 = Gang(name = 'Ghetto Warriors', leader = f1, hqcity = c3)
        g2 = Gang(name = 'Midnight Club', leader = f2, hqcity = c1)
        map(gang_writer.create, [g1, g2])

        # unique indexed reference field
        self.assertEqual(ds.hgetall('u:Gang:leader'), {'1': '1', '2': '2' })

        # unique indexed set field
        gang_members_writer = SetFieldWriter(Gang.members)
        gang_members_writer.append(g1.members, f1)
        gang_members_writer.append(g1.members, f2)
        self.assertEqual(ds.smembers('Gang:1:members'), set(['1', '2']))
        self.assertEqual(ds.hgetall('u:Gang:members'), {'1': '1', '2': '1'})

        self.assertRaises(UniqueError, gang_members_writer.append, g2.members, f1)
        self.assertRaises(UniqueError, gang_members_writer.append, g1.members, f1)

        # non-unique indexed set field
        gang_cities_writer = SetFieldWriter(Gang.cities)
        gang_cities_writer.append(g1.cities, c1)
        gang_cities_writer.append(g1.cities, c3)
        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']))

        # listed reference field
        self.assertEqual(ds.lrange('l:Gang:hqcity:1', 0, -1), ['2'])
        self.assertEqual(ds.lrange('l:Gang:hqcity:3', 0, -1), ['1'])

        # basic model
        skill_writer = ModelWriter(Skill)
        sk1 = Skill(category = 1, name = 'Strength', description = 'Strength...')
        sk2 = Skill(category = 3, name = 'Karate', description = 'Karate...')
        map(skill_writer.create, [sk1, sk2])

        # owned model
        fighter_skill_list_writer = ModelWriter(FighterSkillList)
        f1skills = FighterSkillList()
        f2skills = FighterSkillList()
        fighter_skill_list_writer.create(f1skills, f1)
        fighter_skill_list_writer.create(f2skills, f2)
        self.assertEqual(f1skills.oid, '1')
        self.assertEqual(f2skills.oid, '2')
        if __debug__:
            f3skills = FighterSkillList()
            self.assertRaises(AssertionError, fighter_skill_list_writer.create, f3skills)
            f3 = Fighter(name = 'Unsaved', age = 0, weight = 0, joined = None, city = 1)
            self.assertRaises(AssertionError, fighter_skill_list_writer.create, f3skills, f3)

        # owned model list field
        skill_instance_writer = ModelWriter(SkillInstance)
        fighter_skills_writer = ListFieldWriter(FighterSkillList.skills, element_writer = skill_instance_writer)
        ski1 = SkillInstance(skill = sk1.handle(), value = 21)
        ski2 = SkillInstance(skill = sk2, value = 15)
        fighter_skills_writer.append(f1skills.skills, ski1)
        fighter_skills_writer.append(f1skills.skills, ski2)
        self.assertEqual(ds.lrange('FighterSkillList:1:skills', 0, -1), ['1', '2'])
        self.assertEqual(ds.hgetall('SkillInstance:1'), {'skill': '1', 'value': '21'})
        self.assertEqual(ds.hgetall('SkillInstance:2'), {'skill': '2', 'value': '15'})

        ski1 = SkillInstance(skill = sk1, value = 27)
        ski2 = SkillInstance(skill = sk2, value = 91)
        fighter_skills_writer.append(f2skills.skills, ski1)
        fighter_skills_writer.append(f2skills.skills, ski2)
        self.assertEqual(ds.lrange('FighterSkillList:2:skills', 0, -1), ['3', '4'])
        self.assertEqual(ds.hgetall('SkillInstance:3'), {'skill': '1', 'value': '27'})
        self.assertEqual(ds.hgetall('SkillInstance:4'), {'skill': '2', 'value': '91'})

        # owned model sorted set field
        weapon_writer = ModelWriter(Weapon)
        fighter_weapons_writer = SortedSetFieldWriter(Fighter.weapons, weapon_writer)
        w1 = Weapon(description = 'second', power = 50.5)
        w2 = Weapon(description = 'third', power = 34.2)
        w3 = Weapon(description = 'first', power = 50.7)
        fighter_weapons_writer.append(f1.weapons, w1)
        fighter_weapons_writer.append(f1.weapons, w2)
        fighter_weapons_writer.append(f1.weapons, w3)
        self.assertEqual(ds.zrange('Fighter:1:weapons', 0, -1), ['2', '1', '3'])
        self.assertEqual(ds.hgetall('Weapon:1'), {'description': 'second', 'power': '50.5'})
        self.assertEqual(ds.hgetall('Weapon:2'), {'description': 'third', 'power': '34.2'})
        self.assertEqual(ds.hgetall('Weapon:3'), {'description': 'first', 'power': '50.7'})