class TestCommonFollow(unittest.TestCase): def setUp(self): self.client = Redis(decode_responses=True) self.client.flushdb() self.peter = Relationship(self.client, "peter") self.jack = Relationship(self.client, "jack") self.common_following = CommonFollowing(self.client) def follow_users(self): self.peter.follow("tom") self.peter.follow("mary") self.peter.follow("david") self.jack.follow("tom") self.jack.follow("mary") self.jack.follow("john") def test_calculate(self): self.assertEqual(self.common_following.calculate("peter", "jack"), set()) self.follow_users() self.assertEqual(self.common_following.calculate("peter", "jack"), {"tom", "mary"}) def test_calculate_and_store(self): self.assertEqual(self.client.smembers("peter-and-jack-common-follow"), set()) self.follow_users() self.assertEqual( self.common_following.calculate_and_store( "peter", "jack", "peter-and-jack-common-follow"), 2) self.assertEqual(self.client.smembers("peter-and-jack-common-follow"), {"tom", "mary"})
from redis import Redis from relationship import Relationship from recommend_follow import RecommendFollow client = Redis() # user peter = Relationship(client, "peter") # targets jack = Relationship(client, "jack") tom = Relationship(client, "tom") mary = Relationship(client, "mary") david = Relationship(client, "david") sam = Relationship(client, "sam") # user follow targets peter.follow("jack") peter.follow("tom") peter.follow("mary") peter.follow("david") peter.follow("sam") for i in range(10): jack.follow("J{0}".format(i)) tom.follow("T{0}".format(i)) mary.follow("M{0}".format(i)) david.follow("D{0}".format(i)) sam.follow("S{0}".format(i))
class TestRelationship(unittest.TestCase): def setUp(self): self.client = Redis(decode_responses=True) self.client.flushdb() self.peter = Relationship(self.client, "peter") self.jack = Relationship(self.client, "jack") def test_follow(self): self.assertEqual(self.peter.get_all_following(), set()) self.assertEqual(self.jack.get_all_follower(), set()) self.peter.follow("jack") self.assertNotEqual(self.peter.get_all_following(), set()) self.assertNotEqual(self.jack.get_all_follower(), set()) def test_unfollow(self): self.peter.follow("jack") self.peter.unfollow("jack") self.assertEqual(self.peter.get_all_following(), set()) self.assertEqual(self.jack.get_all_follower(), set()) def test_is_following(self): self.assertFalse(self.peter.is_following("jack")) self.peter.follow("jack") self.assertTrue(self.peter.is_following("jack")) def test_get_all_following(self): self.assertEqual(self.peter.get_all_following(), set()) self.peter.follow("jack") self.assertEqual(self.peter.get_all_following(), {"jack"}) def test_get_all_follower(self): self.assertEqual(self.jack.get_all_follower(), set()) self.peter.follow("jack") self.assertEqual(self.jack.get_all_follower(), {"peter"}) def test_count_following(self): self.assertEqual(self.peter.count_following(), 0) self.peter.follow("jack") self.assertEqual(self.peter.count_following(), 1) def test_count_follower(self): self.assertEqual(self.jack.count_follower(), 0) self.peter.follow("jack") self.assertEqual(self.jack.count_follower(), 1)
class TestRecommendFollow(unittest.TestCase): def setUp(self): self.client = Redis(decode_responses=True) self.client.flushdb() # user self.peter = Relationship(self.client, "peter") # targets self.jack = Relationship(self.client, "jack") self.tom = Relationship(self.client, "tom") self.mary = Relationship(self.client, "mary") self.david = Relationship(self.client, "david") self.sam = Relationship(self.client, "sam") # user follow targets self.peter.follow("jack") self.peter.follow("tom") self.peter.follow("mary") self.peter.follow("david") self.peter.follow("sam") # create ten following user for each target self.jack_following_set = set() self.tom_following_set = set() self.mary_following_set = set() self.david_following_set = set() self.sam_following_set = set() for i in range(10): self.jack_following_set.add("J{0}".format(i)) self.tom_following_set.add("T{0}".format(i)) self.mary_following_set.add("M{0}".format(i)) self.david_following_set.add("D{0}".format(i)) self.sam_following_set.add("S{0}".format(i)) for target in list(self.jack_following_set): self.jack.follow(target) for target in list(self.tom_following_set): self.tom.follow(target) for target in list(self.mary_following_set): self.mary.follow(target) for target in list(self.david_following_set): self.david.follow(target) for sam in list(self.sam_following_set): self.sam.follow(target) # self.peter_recommend_follow = RecommendFollow(self.client, "peter") def test_calculate(self): self.assertEqual(self.peter_recommend_follow.fetch_result(10), []) self.peter_recommend_follow.calculate(2) self.assertNotEqual(self.peter_recommend_follow.fetch_result(10), []) def test_fetch_result(self): self.assertEqual(self.peter_recommend_follow.fetch_result(10), []) self.peter_recommend_follow.calculate(3) result = set(self.peter_recommend_follow.fetch_result(10)) all_possible_recommend_targets = self.jack_following_set | self.tom_following_set | self.mary_following_set | self.david_following_set | self.sam_following_set self.assertNotEqual(result & all_possible_recommend_targets, set()) def test_delete_result(self): self.peter_recommend_follow.calculate(3) self.peter_recommend_follow.delete_result() self.assertEqual(self.peter_recommend_follow.fetch_result(10), [])