Esempio n. 1
0
 def test_sorted_set(self):
     writer = SortedSetWriter(str)
     hzset = SortedSetHandle('myzset', str)
     writer.append(hzset, 'spam', 3.25)
     writer.append(hzset, 'eggs', 3.24)
     self.assertEqual(SortedSet(hzset), ('eggs', 'spam'))
     self.assertEqual(SortedSet(hzset, lte = 3.24), ('eggs',))
     self.assertEqual(SortedSet.zrange(hzset, 0, 0), ('eggs',))
     self.assertEqual(SortedSet.zfind(hzset, gt = 3.24), ('spam',))
Esempio n. 2
0
 def test_model_sorted_set(self):
     writer = SortedSetWriter(Fighter)
     hzset = SortedSetHandle('myzset', Fighter)
     f1, f2, f3 = map(Fighter.by_id, [21, 33, 47])
     writer.append(hzset, f1, 3.25)
     writer.append(hzset, f2, 3.24)
     writer.append(hzset, f3, 4)
     self.assertEqual(SortedSet.zrange(hzset), (f2, f1, f3))
     self.assertEqual(SortedSet.zrevrange(hzset, 0, 0), (f3,))
     self.assertEqual(SortedSet(hzset, gt = 3.24), (f1, f3))
Esempio n. 3
0
    def test_read(self):
        handle = Gang.by_id(1)
        gang = Gang(handle)
        self.assertEqual(gang.name, 'Ghetto Warriors')

        members = Set(gang.members)
        self.assertEqual(members, set([Fighter.by_id(1), Fighter.by_id(2)]))

        hfighter1 = Fighter.by_id(1)
        hfighter2 = Fighter.by_id(2)
        fighter1 = Fighter(hfighter1)
        fighter2 = Fighter(hfighter2)
        self.assertEqual(fighter1.name, 'Alice')
        self.assertEqual(fighter2.name, 'Bob')

        dtime = datetime.utcfromtimestamp(1400000001)
        self.assertEqual(fighter2.joined, dtime)

        handle1 = Fighter.by_id(1)
        handle2 = Fighter.by_id(2)
        handle3 = Fighter.by_id(1)
        self.assertEqual(handle1, handle3)
        self.assertFalse(handle1 != handle3)
        self.assertFalse(handle1 == handle2)
        self.assertTrue(handle1 != handle2)

        city = City(fighter1.city)
        self.assertEqual(city.name, 'Reixte')
        self.assertEqual(city.coast, True)
        conns = List(city.connections)
        self.assertEqual(len(conns), 2)
        city2 = City(conns[1])
        self.assertEqual(city2.name, 'Toynbe')
        self.assertEqual(city2.coast, False)

        handle = Gang.by_id(999)
        self.assertRaises(NotFoundError, Gang, handle)

        handle = Fighter.find(name = 'Bob')
        self.assertEqual(handle, fighter2.handle())

        handle = Fighter.find(name = 'NoPlayer')
        self.assertFalse(handle)
        self.assertRaises(NotFoundError, Fighter, handle)

        city_fighters = Fighter.multifind(city = City.by_id(1))
        self.assertEqual(city_fighters, set([hfighter1, hfighter2]))

        handle = Gang.find(leader = hfighter1)
        self.assertEqual(handle, Gang.by_id(1))

        self.assertTrue(not Gang.find(members__contains = Fighter.by_id(3)))
        handle = Gang.find(members__contains = Fighter(hfighter2))
        self.assertEqual(handle, Gang.by_id(1))

        city3 = City(City.by_id(3))
        city_gangs = Gang.multifind(cities__contains = city3)
        self.assertEqual(city_gangs, set([Gang.by_id(1)]))

        handle = FighterSkillList.by_owner(fighter1)
        fsl = FighterSkillList(handle)
        fsl_skills = List(fsl.skills)
        skills = map(SkillInstance, fsl_skills)
        self.assertEqual(skills[0].skill, Skill.by_id(1))
        self.assertEqual(skills[0].value, 21)
        self.assertEqual(skills[1].skill, Skill.by_id(2))
        self.assertEqual(skills[1].value, 15)

        handle = FighterSkillList.by_owner(hfighter2)
        fsl = FighterSkillList(handle)
        fsl_skills = List(fsl.skills)
        skills = map(SkillInstance, fsl_skills)
        self.assertEqual(skills[0].skill, Skill.by_id(1))
        self.assertEqual(skills[0].value, 27)
        self.assertEqual(skills[1].skill, Skill.by_id(2))
        self.assertEqual(skills[1].value, 91)

        sorted_by_weight_1 = Fighter.zrange('weight')
        self.assertEqual(sorted_by_weight_1, [hfighter2, hfighter1])

        sorted_by_weight_2 = Fighter.zrange('weight', 0, 0)
        self.assertEqual(sorted_by_weight_2, [hfighter2])

        sorted_by_weight_3 = Fighter.zrange('weight', 1, -1)
        self.assertEqual(sorted_by_weight_3, [hfighter1])

        sorted_by_weight_4 = Fighter.zrevrange('weight')
        self.assertEqual(sorted_by_weight_4, [hfighter1, hfighter2])

        sorted_by_weight_5 = Fighter.zrevrange('weight', 0, 0)
        self.assertEqual(sorted_by_weight_5, [hfighter1])

        le24a = Fighter.zfind(age__lte = 24)
        le24b = Fighter.zrangebyscore('age', '-inf', 24)
        self.assertEqual(le24a, [hfighter1, hfighter2])
        self.assertEqual(le24a, le24b)

        le23a = Fighter.zfind(age__lte = 23)
        le23b = Fighter.zrangebyscore('age', '-inf', 23)
        self.assertEqual(le23a, [hfighter1, hfighter2])
        self.assertEqual(le23a, le23b)

        lt23a = Fighter.zfind(age__lt = 23)
        lt23b = Fighter.zrangebyscore('age', '-inf', '(23')
        self.assertEqual(lt23a, [hfighter1])
        self.assertEqual(lt23a, lt23b)

        ge24a = Fighter.zfind(age__gte = 24)
        ge24b = Fighter.zrangebyscore('age', 24, '+inf')
        self.assertEqual(ge24a, [])
        self.assertEqual(ge24a, ge24b)

        ge23a = Fighter.zfind(age__gte = 23)
        ge23b = Fighter.zrangebyscore('age', 23, '+inf')
        self.assertEqual(ge23a, [hfighter2])
        self.assertEqual(ge23a, ge23b)

        gt23a = Fighter.zfind(age__gt = 20)
        gt23b = Fighter.zrangebyscore('age', '(20', '+inf')
        self.assertEqual(gt23a, [hfighter2])
        self.assertEqual(gt23a, gt23b)

        age_in_a = Fighter.zfind(age__in = (20, 23))
        age_in_b = Fighter.zrangebyscore('age', 20, 23)
        age_in_c = Fighter.zrangebyscore('age', 20, 23, 0, 2)
        self.assertEqual(age_in_a, [hfighter1, hfighter2])
        self.assertEqual(age_in_a, age_in_b)
        self.assertEqual(age_in_a, age_in_c)

        age_eq_a = Fighter.zfind(age = 20)
        age_eq_b = Fighter.zrangebyscore('age', 20, 20)
        self.assertEqual(age_eq_a, [hfighter1])
        self.assertEqual(age_eq_a, age_eq_b)

        joined_before_2014 = Fighter.zfind(joined__lt = datetime(2014, 1, 1))
        self.assertEqual(joined_before_2014, [])
        joined_before_2020 = Fighter.zfind(joined__lt = datetime(2020, 1, 1))
        self.assertEqual(joined_before_2020, [hfighter2, hfighter1])
        joined_in_201x = Fighter.zfind(joined__in = (datetime(2010, 1, 1), datetime(2020, 1, 1)))
        self.assertEqual(joined_in_201x, [hfighter2, hfighter1])

        rev_age_in_a = Fighter.zrevrangebyscore('age', 23, 20)
        rev_age_in_b = Fighter.zrevrangebyscore('age', 23, 20, 0, 2)
        self.assertEqual(rev_age_in_a, [hfighter2, hfighter1])
        self.assertEqual(rev_age_in_a, rev_age_in_b)

        self.assertEqual(Fighter.zcount('age', 20, 23), 2)
        self.assertEqual(Fighter.zrank('weight', fighter1), 1)
        self.assertEqual(Fighter.zrank('weight', hfighter2), 0)
        self.assertEqual(Fighter.zrevrank('weight', hfighter1), 0)
        self.assertEqual(Fighter.zrevrank('weight', hfighter2), 1)

        hweapon1 = Weapon.by_id(1)
        hweapon2 = Weapon.by_id(2)
        hweapon3 = Weapon.by_id(3)

        sorted_weapons = SortedSet.zrange(fighter1.weapons)
        self.assertEqual(sorted_weapons, (hweapon2, hweapon1, hweapon3))

        top_weapons = SortedSet.zrevrange(fighter1.weapons, 0, 1)
        self.assertEqual(top_weapons, (hweapon3, hweapon1))

        powerful_weapons1 = SortedSet.zfind(fighter1.weapons, gt = 50)
        powerful_weapons2 = SortedSet(fighter1.weapons, gt = 50)
        self.assertEqual(powerful_weapons1, (hweapon1, hweapon3))
        self.assertEqual(powerful_weapons1, powerful_weapons2)

        gangs_by_hqcity = Gang.getlist(hqcity = city3)
        self.assertEqual(gangs_by_hqcity, [Gang.by_id(1), Gang.by_id(2)])

        first_gang_by_hqcity = Gang.getlist(0, 0, hqcity = city3)
        self.assertEqual(first_gang_by_hqcity, [Gang.by_id(1)])