def setUp(self): """Create test client, add sample data.""" User.query.delete() Message.query.delete() self.client = app.test_client() self.testuser = User.signup(username="******", email="*****@*****.**", password="******", image_url=None) self.testuser2 = User.signup(username="******", email="*****@*****.**", password="******", image_url=None) self.testuser4 = User.signup(username="******", email="*****@*****.**", password="******", image_url=None) self.testuser.id = 1 self.testuser2.id = 2 self.testuser4.id = 4 db.session.commit() self.f = FollowersFollowee(follower_id=1, followee_id=2) self.f2 = FollowersFollowee(follower_id=2, followee_id=1) self.m = Message(id=1, text="test message", user_id=2) self.r = Reaction(user_id=1, message_id=1, reaction_type="sad") db.session.add_all([self.f, self.f2, self.m, self.r]) db.session.commit()
def test_follows(self): u = User( id=1, email="*****@*****.**", username="******", password="******" ) f = User( id=2, email="*****@*****.**", username="******", password="******" ) ff = FollowersFollowee( follower_id=1, followee_id=2 ) db.session.add(u) db.session.add(f) db.session.commit() db.session.add(ff) db.session.commit() self.assertEqual(u.is_followed_by(f), True) self.assertEqual(f.is_followed_by(u), False) self.assertEqual(u.is_following(f), False) self.assertEqual(f.is_following(u), True)
def test_is_followed_by(self): """Does this function show user?""" u1 = User( id=1, email="*****@*****.**", username="******", password="******", ) u2 = User( id=2, email="*****@*****.**", username="******", password="******", ) follow = FollowersFollowee(followee_id=1, follower_id=2) db.session.add(u1) db.session.add(u2) db.session.commit() db.session.add(follow) db.session.commit() #user should have the following message self.assertEqual(u2.is_followed_by(u1), True) self.assertEqual(u2.followers.first(), u1)
def test_is_following(self): u1 = User( id=1, email="*****@*****.**", username="******", password="******", ) u2 = User( id=2, email="*****@*****.**", username="******", password="******", ) follow = FollowersFollowee(followee_id=1, follower_id=2) db.session.add(u1) db.session.add(u2) db.session.commit() db.session.add(follow) db.session.commit() self.assertEqual(u1.is_following(u2), True) self.assertEqual(u1.following.first(), u2)
def setUp(self): """Create test client, add sample data.""" self.user1 = User(username="******", email="*****@*****.**", image_url="abc", header_image_url="abc", bio="i am awesome", location="nonesuch", password="******") self.user2 = User(username="******", email="*****@*****.**", header_image_url="", bio="i am great", location="nowhere", password="******") self.new_user = User.signup(username="******", email="*****@*****.**", password="******", image_url="") # self.follows2 = FollowersFollowee(self.user2.id, self.user1.id) db.session.add_all([self.user1, self.user2]) db.session.commit() # import pdb; pdb.set_trace() self.follows1 = FollowersFollowee(followee_id=self.user1.id, follower_id=self.user2.id) db.session.add(self.follows1) db.session.commit() self.client = app.test_client()
def test_is_followed_by_false(self): """Does the is_followed_by work for the user model""" u = User.query.get(99999) u2 = User.query.get(88888) #setting up u99999 to follow u288888: ff = FollowersFollowee(followee_id=99999, follower_id=88888) db.session.add(ff) db.session.commit() self.assertEqual(u2.is_followed_by(u), True)
def test_is_followed_by(self): """Does is_followed_by work?""" # successfully detect when user1 IS NOT followed by user2 self.assertFalse(self._user1.is_followed_by(self._user2)) # successfully detect when user1 IS followed by user2 stalker = FollowersFollowee(followee_id=2000, follower_id=1000) db.session.add(stalker) db.session.commit() self.assertTrue(self._user1.is_followed_by(self._user2))
def test_follow_relationship(self): """Does follow relationship to user work? Create 3 users for chain of u1 followed by u2, u2 followed by u3""" # HOW TO FIX AUTOINCREMENT ID's? u1 = User.signup(email="*****@*****.**", username="******", password="******", image_url=None) u2 = User.signup(email="*****@*****.**", username="******", password="******", image_url=None) u3 = User.signup(email="*****@*****.**", username="******", password="******", image_url=None) db.session.commit() f1 = FollowersFollowee(followee_id=u1.id, follower_id=u2.id) f2 = FollowersFollowee(followee_id=u2.id, follower_id=u3.id) db.session.add_all([f1, f2]) db.session.commit() # USERS GO THROUGH FOLLOWS TABLE AND BACK TO USER TABLE # FOLLOWING = u1 is FOLLOWED by u2 # FOLLOWERS = u3 is FOLLOWING u2 self.assertEqual(u1.followers[0].id, u2.id) self.assertEqual(u3.following[0].id, u2.id) # # Test is_followed_by, is_following methods self.assertEqual(u1.is_followed_by(u2), True) self.assertEqual(u2.is_followed_by(u1), False) self.assertEqual(u3.is_following(u2), True) self.assertEqual(u2.is_following(u3), False)
def setUp(self): """Create test client, add sample data.""" User.query.delete() Message.query.delete() FollowersFollowee.query.delete() Like.query.delete() self.client = app.test_client() self.testuser1 = User.signup(username="******", email="*****@*****.**", password="******", image_url=None) self.testuser2 = User.signup(username="******", email="*****@*****.**", password="******", image_url=None) self.testuser3 = User.signup(username="******", email="*****@*****.**", password="******", image_url=None) db.session.commit() # testuser id not available/created until id session is committed f1 = FollowersFollowee(followee_id=self.testuser1.id, follower_id=self.testuser2.id) f2 = FollowersFollowee(followee_id=self.testuser2.id, follower_id=self.testuser3.id) db.session.add_all([f1, f2]) db.session.commit()
def test_following_mapping(self): """Do our mappings/relationships work""" user1 = User(email="*****@*****.**", username="******", password="******") user2 = User(email="*****@*****.**", username="******", password="******") db.session.add_all([user1, user2]) db.session.commit() # user2 is following user1 db.session.add( FollowersFollowee(followee_id=user2.id, follower_id=user1.id)) db.session.commit() self.assertIn(user2, user1.followers, "User 2 is part of user1's follower list")
def test_is_followed_by(self): user1 = User(email="*****@*****.**", username="******", password="******") user2 = User(email="*****@*****.**", username="******", password="******") db.session.add_all([user1, user2]) db.session.commit() # user2 is following user1 db.session.add( FollowersFollowee(followee_id=user2.id, follower_id=user1.id)) db.session.commit() self.assertTrue(user1.is_followed_by(user2)) self.assertFalse(user2.is_followed_by(user1))
def test_is_followed_by(self): """ Does is_followed_by successfully detect when user1 is followed by user2 and when user2 is not followed by user1? """ u2 = User(id=2000, email="*****@*****.**", username="******", password="******") db.session.add(u2) db.session.commit() f = FollowersFollowee(followee_id=2000, follower_id=1000) db.session.add(f) db.session.commit() self.assertTrue(self.u1.is_followed_by(u2)) self.assertFalse(u2.is_followed_by(self.u1))
def test_show_following(self): """Can we show user following?""" # Since we need to change the session to mimic logging in, # we need to use the changing-session trick: with self.client as c: with c.session_transaction() as sess: sess[CURR_USER_KEY] = self.testuser1.id # Now, that session setting is saved, so we can have # the rest of ours test # import pdb; pdb.set_trace() testuser2 = User.signup( username="******", email="*****@*****.**", password="******", image_url=None, ) db.session.commit() followers = FollowersFollowee( followee_id=testuser2.id, follower_id=self.testuser1.id, ) db.session.add(followers) db.session.commit() resp = c.get(f"/users/{testuser2.id}/following") self.assertEqual(testuser2.following.one().username, 'testuser1') self.assertEqual(resp.status_code, 200) self.assertIn(b'rose-blue-flower-rose-blooms-67636', resp.data)
def setUp(self): """Create test client, add sample data.""" # Create all tables db.create_all() # Add 6 test Users for i in range(6): u = User.signup(email=f"test{i}@test.com", username=f"testuser{i}", password="******", image_url="/static/images/default-pic.png") db.session.add(u) db.session.commit() # Add 6 message for i in range(6): m = Message(text=f"I love numer {i}", user_id=i + 1) db.session.add(m) db.session.commit() # Add 5 likes for i in range(5): like = Like(user_id=i + 1, message_id=i + 2) db.session.add(like) # Add 5 follower/followee relationship for i in range(5): ff = FollowersFollowee(followee_id=i + 1, follower_id=i + 2) db.session.add(ff) db.session.commit() self.testuser = User.query.get(1) self.client = app.test_client()
def test_user_model(self): """Does basic model work?""" u = User.signup(username="******", email="*****@*****.**", password="******", image_url="/static/images/default-pic.png") u.id = 1 u2 = User.signup(username="******", email="*****@*****.**", password="******", image_url="/static/images/default-pic.png") u2.id = 2 db.session.commit() m = Message(id=1, text="test message", user_id=1) r = Reaction(user_id=1, message_id=1, reaction_type="sad") f = FollowersFollowee(follower_id=1, followee_id=2) f2 = FollowersFollowee(follower_id=2, followee_id=1) db.session.add_all([m, r, f, f2]) db.session.commit() # User should have 1 message and 1 follower and 1 following self.assertEqual(u.messages.count(), 1) self.assertEqual(u.followers.count(), 1) self.assertEqual(u.following.count(), 1) db.session.delete(f) db.session.commit() # User should have 0 follower and 1 following self.assertEqual(u.followers.count(), 0) self.assertEqual(u.following.count(), 1) db.session.delete(f2) db.session.commit() # User should have 0 follower and 0 following self.assertEqual(u.followers.count(), 0) self.assertEqual(u.following.count(), 0) # testing attributes self.assertNotEqual(u.password, "HASHED_PASSWORD") self.assertEqual(u.email, "*****@*****.**") self.assertEqual(u.image_url, "/static/images/default-pic.png") self.assertEqual(u.username, "testuser") self.assertEqual(u.bio, None) self.assertEqual(u.header_image_url, "/static/images/warbler-hero.jpg") self.assertEqual(u.location, None) # test get_reactions self.assertEqual(len(u.get_reactions("sad")), 1) self.assertFalse(u.get_reactions("angry")) self.assertFalse(u.get_reactions("smile")) self.assertFalse(u.get_reactions("laugh")) # test get my messages self.assertEqual(len(u.get_my_messages()), 1) # test auth u = User.authenticate(u.username, "HASHED_PASSWORD") self.assertTrue(u) u = User.authenticate(u.username, "asdfasdf") self.assertFalse(u)