def test_get_followed_list(self): global _app if _app is None: tested_app = create_app(debug=True) _app = tested_app else: tested_app = _app restart_db_tables(db, tested_app) with tested_app.test_client() as client: with client.session_transaction() as session: # push in the users_table 3 users user_a = User() user_a.email = '*****@*****.**' user_a.set_password('test') user_b = User() user_b.email = '*****@*****.**' user_b.set_password('test') user_c = User() user_c.email = '*****@*****.**' user_c.set_password('test') user_d = User() user_d.email = '*****@*****.**' user_d.set_password('test') db.session.add(user_a) db.session.add(user_b) db.session.add(user_c) db.session.add(user_d) db.session.commit() # Get users ID user_a_id = User.query.filter_by( email=user_a.email).first().get_id() user_b_id = User.query.filter_by( email=user_b.email).first().get_id() user_c_id = User.query.filter_by( email=user_c.email).first().get_id() user_d_id = User.query.filter_by( email=user_d.email).first().get_id() with client.session_transaction() as session: follow_ab = _create_follow(user_a_id, user_b_id) follow_ac = _create_follow(user_a_id, user_c_id) follow_ad = _create_follow(user_a_id, user_d_id) follow_bc = _create_follow(user_b_id, user_c_id) db.session.add(follow_ab) db.session.add(follow_ac) db.session.add(follow_ad) db.session.add(follow_bc) db.session.commit() res = get_followed_list(user_a_id) correct = [user_b_id, user_c_id, user_d_id] self.assertEqual(res, correct)
def test_unfollow_post_user(self): global _app if _app is None: tested_app = create_app(debug=True) _app = tested_app else: tested_app = _app restart_db_tables(db, tested_app) with tested_app.test_client() as client: with client.session_transaction() as session: # push in the users_table 3 users user_a = User() user_a.email = '*****@*****.**' user_a.set_password('test') user_b = User() user_b.email = '*****@*****.**' user_b.set_password('test') user_c = User() user_c.email = '*****@*****.**' user_c.set_password('test') db.session.add(user_a) db.session.add(user_b) db.session.add(user_c) db.session.commit() # Get users ID user_a_id = User.query.filter_by( email=user_a.email).first().get_id() user_b_id = User.query.filter_by( email=user_b.email).first().get_id() user_c_id = User.query.filter_by( email=user_c.email).first().get_id() with client.session_transaction() as session: follow_ab = _create_follow(user_a_id, user_b_id) follow_ac = _create_follow(user_a_id, user_c_id) follow_bc = _create_follow(user_b_id, user_c_id) db.session.add(follow_ab) db.session.add(follow_ac) db.session.add(follow_bc) db.session.commit() # login as user_1 login(client, user_a.email, 'test') # call /unfollow/user_1 # assert False data = '/wall/' + str(user_a_id) reply = client.post('/unfollow/' + str(user_a_id)) self.assertFalse(_is_follower(user_a_id, user_a_id)) self.assertIn(data, str(reply.data)) # call /unfollow/user_2 # assert OK data = '/wall/' + str(user_b_id) self.assertTrue(_is_follower(user_a_id, user_b_id)) reply = client.post('/unfollow/' + str(user_b_id)) self.assertFalse(_is_follower(user_a_id, user_b_id)) self.assertIn(data, str(reply.data)) # call unfollow/user_2 # assert EXC data = '/wall/' + str(user_b_id) reply = client.post('/unfollow/' + str(user_b_id)) self.assertFalse(_is_follower(user_a_id, user_b_id)) self.assertIn(data, str(reply.data)) # call unfollow/user_not_exist # assert EXC user_not_exist_id = 999999999999 data = '/wall/' + str(user_not_exist_id) reply = client.post('/unfollow/' + str(user_not_exist_id)) self.assertFalse(_is_follower(user_a_id, user_not_exist_id)) self.assertIn("/stories", str(reply.data)) logout(client)
def test_maker_message(self): global _app if _app is None: tested_app = create_app(debug=True) _app = tested_app else: tested_app = _app restart_db_tables(db, tested_app) with tested_app.test_client() as client: with client.session_transaction() as session: # push in the users_table 3 users user_a = User() user_a.firstname = 'user_a' user_a.email = '*****@*****.**' user_a.set_password('test') user_b = User() user_b.firstname = 'user_b' user_b.lastname = 'surname_b' user_b.email = '*****@*****.**' user_b.set_password('test') user_c = User() user_c.firstname = 'user_c' user_c.lastname = 'surname_c' user_c.email = '*****@*****.**' user_c.set_password('test') user_d = User() user_d.firstname = 'user_d' user_d.lastname = 'surname_d' user_d.email = '*****@*****.**' user_d.set_password('test') db.session.add(user_a) db.session.add(user_b) db.session.add(user_c) db.session.add(user_d) db.session.commit() # Get users ID user_a_id = User.query.filter_by( email=user_a.email).first().get_id() user_b_id = User.query.filter_by( email=user_b.email).first().get_id() user_c_id = User.query.filter_by( email=user_c.email).first().get_id() user_d_id = User.query.filter_by( email=user_d.email).first().get_id() with client.session_transaction() as session: follow_ab = _create_follow(user_a_id, user_b_id) follow_ac = _create_follow(user_a_id, user_c_id) follow_ad = _create_follow(user_a_id, user_d_id) follow_bc = _create_follow(user_b_id, user_c_id) db.session.add(follow_ab) db.session.add(follow_ac) db.session.add(follow_ad) db.session.add(follow_bc) story_b_1 = make_story(user_b_id, "story_b_1") story_b_2 = make_story(user_b_id, "story_b_2") story_b_3 = make_story(user_b_id, "story_b_3", 66, 9) story_b_4 = make_story(user_b_id, "story_b_4", 5, 1) story_c_1 = make_story(user_c_id, "story_c_1") story_c_2 = make_story(user_c_id, "story_c_2", 1000, 162) story_c_3 = make_story(user_c_id, "story_c_3") db.session.add(story_b_1) db.session.add(story_b_2) db.session.add(story_b_3) db.session.add(story_b_4) db.session.add(story_c_1) db.session.add(story_c_2) db.session.add(story_c_3) db.session.commit() res = maker_message(user_a) print(res) correct = "Hello user_a,\n\nhere you can find what's new on the wall of Sweaggers' SocialDice!\n\n - user_b surname_b posts 4 new stories.\n - user_c surname_c posts 3 new stories.\n\nSee you on SocialDice,\nSweaggers Team" self.assertEqual(res, correct) res = maker_message(user_d) correct = "Hello user_d,\n\nYou have no news for today, take a look and add new writers on Sweaggers' SocialDice!" self.assertEqual(res, correct)
def test_get_all_stories_by_writer(self): global _app if _app is None: tested_app = create_app(debug=True) _app = tested_app else: tested_app = _app restart_db_tables(db, tested_app) with tested_app.test_client() as client: with client.session_transaction() as session: # push in the users_table 3 users user_a = User() user_a.email = '*****@*****.**' user_a.set_password('test') user_b = User() user_b.email = '*****@*****.**' user_b.set_password('test') user_c = User() user_c.email = '*****@*****.**' user_c.set_password('test') user_d = User() user_d.email = '*****@*****.**' user_d.set_password('test') db.session.add(user_a) db.session.add(user_b) db.session.add(user_c) db.session.add(user_d) db.session.commit() # Get users ID user_a_id = User.query.filter_by( email=user_a.email).first().get_id() user_b_id = User.query.filter_by( email=user_b.email).first().get_id() user_c_id = User.query.filter_by( email=user_c.email).first().get_id() user_d_id = User.query.filter_by( email=user_d.email).first().get_id() with client.session_transaction() as session: follow_ab = _create_follow(user_a_id, user_b_id) follow_ac = _create_follow(user_a_id, user_c_id) follow_ad = _create_follow(user_a_id, user_d_id) follow_bc = _create_follow(user_b_id, user_c_id) db.session.add(follow_ab) db.session.add(follow_ac) db.session.add(follow_ad) db.session.add(follow_bc) story_b_1 = make_story(user_b_id, "story_b_1") story_b_2 = make_story(user_b_id, "story_b_2") story_b_3 = make_story(user_b_id, "story_b_3", 66, 9) story_b_4 = make_story(user_b_id, "story_b_4", 5, 1) story_c_1 = make_story(user_c_id, "story_c_1") story_c_2 = make_story(user_c_id, "story_c_2", 1000, 162) story_c_3 = make_story(user_c_id, "story_c_3") db.session.add(story_b_1) db.session.add(story_b_2) db.session.add(story_b_3) db.session.add(story_b_4) db.session.add(story_c_1) db.session.add(story_c_2) db.session.add(story_c_3) db.session.commit() res = get_all_stories_by_writer(user_b_id) self.assertEqual(len(res), 4) res = get_all_stories_by_writer(user_c_id) self.assertEqual(len(res), 3) res = get_all_stories_by_writer(user_d_id) self.assertEqual(len(res), 0)