def test01_loop(self): player1 = 1 player2 = 2 player3 = 88 game1 = 111 game2 = 122 game3 = 133 now = datetime.now() yesterday = now - timedelta(days=1) two_days_ago = now - timedelta(days=2) three_days_ago = now - timedelta(days=3) sec = timedelta(seconds=1) c = self.db.cursor() # Prepopulate the tables with fixture data. # ------------------------------------------------------------ # 1. auth_user auth_user_fixtures = [ (player1, '*****@*****.**', 'John Johnson'), (player2, '*****@*****.**', 'Bill Billson'), (player3, '*****@*****.**', None) ] for player in auth_user_fixtures: c.execute('INSERT INTO auth_user (id, username, first_name) VALUES (?, ?, ?)', player) # 2. games games_fixtures = [ (game1, player1, 'Sentence 1', 'invitation', three_days_ago), (game2, player1, 'Sentence 2', 'complete', three_days_ago), (game3, player2, 'Sentence 3', 'invitation', now) ] for game in games_fixtures: c.execute('INSERT INTO games (id, owner_id, sentence, state, created) VALUES (?, ?, ?, ?, ?)', game) # 3. player2game player2game_fixtures =[ (player1, game1), (player1, game2), (player2, game1), (player2, game2), (player2, game3), (player3, game2) ] for player_id, game_id in player2game_fixtures: c.execute('insert into player2game (player_id, game_id) values (?, ?)', [player_id, game_id]) # 4. event_logs event_logs_fixtures = [ # Game 1 [game1, two_days_ago, event_log.GAME_CREATED, player1, ''], [game1, two_days_ago, event_log.OWNER_CHOSE_CARD, player1, 22], [game1, two_days_ago, event_log.OWNER_WROTE_STORY, player1, 'Sentence 1'], [game1, yesterday, event_log.PLAYER_JOINED, player2, ''], [game1, yesterday, event_log.PLAYER_VOTED, player2, 33], # Game 2 [game2, two_days_ago, event_log.GAME_CREATED, player1, ''], [game2, two_days_ago, event_log.OWNER_CHOSE_CARD, player1, 34], [game2, two_days_ago, event_log.OWNER_WROTE_STORY, player1, 'Sentence 2'], [game2, two_days_ago, event_log.PLAYER_INVITED, player1, player3], [game2, two_days_ago, event_log.PLAYER_JOINED, player3, ''], [game2, two_days_ago, event_log.PLAYER_PICKED_CARD, player3, 23], [game2, two_days_ago, event_log.PLAYER_JOINED, player2, ''], [game2, two_days_ago, event_log.PLAYER_PICKED_CARD, player2, 24], [game2, two_days_ago, event_log.GAME_MOVED_TO_VOTING, player1, ''], [game2, two_days_ago, event_log.PLAYER_VOTED, player2, 44], [game2, two_days_ago, event_log.PLAYER_VOTED, player3, 45], [game2, two_days_ago, event_log.GAME_COMPLETED, player1, ''], # Game 3 [game3, now, event_log.GAME_CREATED, player2, ''], [game3, now, event_log.OWNER_CHOSE_CARD, player2, 34], [game3, now, event_log.OWNER_WROTE_STORY, player2, 'Sentence 3'] ] for event in event_logs_fixtures: c.execute('INSERT INTO event_logs (game_id, timestamp, event_type, player_id, data) VALUES (?, ?, ?, ?, ?)', event) # ------------------------------------------------------------ self.db.commit() c.close() # Mock out send.send_mail to collect arguments it's been called with. calls = [] def mock_send_mail(email, name, context): calls.append([email, name, context]) send.send_mail = mock_send_mail count = loop.loop(self.database, self.database) # Should send out three emails (for each of the three players in the db). self.assertEquals(count, 3) # Let's see what send_mail has been called with. self.assertEquals(len(calls), 3) # For player1: self.assertEquals(calls[0][0], '*****@*****.**') self.assertEquals(calls[0][1], 'John Johnson') # No completed games: self.assertEquals(len(calls[0][2]['completed_games']), 0) # One available game (game3 by player2): self.assertEquals(len(calls[0][2]['available_games']), 1) game = calls[0][2]['available_games'][0] self.assertEquals(game['game_id'], game3) self.assertEquals(game['owner_name'], 'Bill Billson') self.assertEquals(game['sentence'], 'Sentence 3') # Player1 was last active two_days ago. # Since then (yesterday), two events happened on one of his games (game1). self.assertEquals(len(calls[0][2]['game_activities']), 1) activity = calls[0][2]['game_activities'][0] self.assertEquals(activity['game_id'], game1) self.assertEquals(activity['state'], 'invitation') self.assertEquals(activity['owner_name'], 'You') self.assertEquals(activity['sentence'], 'Sentence 1') self.assertEquals(len(activity['events']), 2) self.assertEquals(activity['events'][0], 'Bill Billson joined the game') self.assertEquals(activity['events'][1], 'Bill Billson voted') # For player2: self.assertEquals(calls[1][0], '*****@*****.**') self.assertEquals(calls[1][1], 'Bill Billson') # Player2 has last been active 'now', not much has happened since 'now', obviously. # No completed games: self.assertEquals(len(calls[1][2]['completed_games']), 0) # No available games: self.assertEquals(len(calls[1][2]['available_games']), 0) # No game acitvities: self.assertEquals(len(calls[1][2]['game_activities']), 0) # For player3: self.assertEquals(calls[2][0], '*****@*****.**') self.assertEquals(calls[2][1], 'bigjoe99') # No completed games: self.assertEquals(len(calls[2][2]['completed_games']), 0) # One available game (game3 by player2): self.assertEquals(len(calls[2][2]['available_games']), 1) self.assertEquals(game['game_id'], game3) self.assertEquals(game['owner_name'], 'Bill Billson') self.assertEquals(game['sentence'], 'Sentence 3') # No game acitvities - player3 has less been active two days ago, but he has only # participated in game2 which hasn't seen any activity since then: self.assertEquals(len(calls[2][2]['game_activities']), 0)
def test01_loop(self): player1 = 1 player2 = 2 player3 = 88 player4 = 122 game1 = 111 game2 = 122 game3 = 133 # Mock out should_send_email to always return True, # we'll test that function separately. original_should_send_email = loop.should_send_email def always_true(last_active, game_activities_24h): return True loop.should_send_email = always_true now = datetime.now() yesterday = now - timedelta(days=1) two_days_ago = now - timedelta(days=2) three_days_ago = now - timedelta(days=3) sec = timedelta(seconds=1) c = self.db.cursor() # Prepopulate the tables with fixture data. # ------------------------------------------------------------ # 1. auth_user auth_user_fixtures = [ (player1, '*****@*****.**', 'John Johnson', False), (player2, '*****@*****.**', 'Bill Billson', False), (player3, '*****@*****.**', None, False), (player4, '*****@*****.**', None, True) ] for player in auth_user_fixtures: c.execute( 'INSERT INTO auth_user (id, username, first_name) VALUES (?, ?, ?)', player[:3]) c.execute( 'INSERT INTO cardstories_userprofile (user_id, activity_notifications_disabled) VALUES (?, ?)', (player[0], player[3])) # 2. games games_fixtures = [ (game1, player1, 'Sentence 1', 'invitation', three_days_ago), (game2, player1, 'Sentence 2', 'complete', three_days_ago), (game3, player2, 'Sentence 3', 'invitation', now) ] for game in games_fixtures: c.execute( 'INSERT INTO games (id, owner_id, sentence, state, created) VALUES (?, ?, ?, ?, ?)', game) # 3. player2game player2game_fixtures = [(player1, game1), (player1, game2), (player2, game1), (player2, game2), (player2, game3), (player3, game2)] for player_id, game_id in player2game_fixtures: c.execute( 'insert into player2game (player_id, game_id) values (?, ?)', [player_id, game_id]) # 4. event_logs event_logs_fixtures = [ # Game 1 [game1, two_days_ago, event_log.GAME_CREATED, player1, ''], [game1, two_days_ago, event_log.OWNER_CHOSE_CARD, player1, 22], [ game1, two_days_ago, event_log.OWNER_WROTE_STORY, player1, 'Sentence 1' ], [game1, yesterday, event_log.PLAYER_JOINED, player2, ''], [game1, yesterday, event_log.PLAYER_VOTED, player2, 33], # Game 2 [game2, two_days_ago, event_log.GAME_CREATED, player1, ''], [game2, two_days_ago, event_log.OWNER_CHOSE_CARD, player1, 34], [ game2, two_days_ago, event_log.OWNER_WROTE_STORY, player1, 'Sentence 2' ], [game2, two_days_ago, event_log.PLAYER_INVITED, player1, player3], [game2, two_days_ago, event_log.PLAYER_JOINED, player3, ''], [game2, two_days_ago, event_log.PLAYER_PICKED_CARD, player3, 23], [game2, two_days_ago, event_log.PLAYER_JOINED, player2, ''], [game2, two_days_ago, event_log.PLAYER_PICKED_CARD, player2, 24], [game2, two_days_ago, event_log.PLAYER_JOINED, player4, ''], [game2, two_days_ago, event_log.GAME_MOVED_TO_VOTING, player1, ''], [game2, two_days_ago, event_log.PLAYER_VOTED, player2, 44], [game2, two_days_ago, event_log.PLAYER_VOTED, player3, 45], [game2, two_days_ago, event_log.GAME_COMPLETED, player1, ''], # Game 3 [game3, now, event_log.GAME_CREATED, player2, ''], [game3, now, event_log.OWNER_CHOSE_CARD, player2, 34], [game3, now, event_log.OWNER_WROTE_STORY, player2, 'Sentence 3'] ] for event in event_logs_fixtures: c.execute( 'INSERT INTO event_logs (game_id, timestamp, event_type, player_id, data) VALUES (?, ?, ?, ?, ?)', event) # ------------------------------------------------------------ self.db.commit() c.close() # Mock out send.send_mail to collect arguments it's been called with. calls = [] def mock_send_mail(email, name, context): calls.append([email, name, context]) send.send_mail = mock_send_mail count = loop.loop(self.database, self.database) # Should send out two emails (for player1 and player 3). # The email shouldn't be sent to player2 because he was last active 'now', # and nothing has happened since now (a blank email such as that shouldn't be sent). # The email shouldn't be sent to player4 because he has unsubscribed from the emails. self.assertEquals(count, 2) # Let's see what send_mail has been called with. self.assertEquals(len(calls), 2) # For player1: self.assertEquals(calls[0][1], '*****@*****.**') # No completed games: self.assertEquals(len(calls[0][2]['completed_games']), 0) # One available game (game3 by player2): self.assertEquals(len(calls[0][2]['available_games']), 1) game = calls[0][2]['available_games'][0] self.assertEquals(game['game_id'], game3) self.assertEquals(game['owner_name'], 'Bill Billson') self.assertEquals(game['sentence'], 'Sentence 3') # Player1 was last active two_days ago. # Since then (yesterday), two events happened on one of his games (game1). self.assertEquals(len(calls[0][2]['game_activities']), 1) activity = calls[0][2]['game_activities'][0] self.assertEquals(activity['game_id'], game1) self.assertEquals(activity['state'], 'invitation') self.assertEquals(activity['owner_name'], 'You') self.assertEquals(activity['sentence'], 'Sentence 1') self.assertEquals(len(activity['events']), 2) self.assertEquals(activity['events'][0], 'Bill Billson joined the game') self.assertEquals(activity['events'][1], 'Bill Billson voted') # For player3: self.assertEquals(calls[1][1], '*****@*****.**') # No completed games: self.assertEquals(len(calls[1][2]['completed_games']), 0) # One available game (game3 by player2): self.assertEquals(len(calls[1][2]['available_games']), 1) self.assertEquals(game['game_id'], game3) self.assertEquals(game['owner_name'], 'Bill Billson') self.assertEquals(game['sentence'], 'Sentence 3') # No game activities - player3 has less been active two days ago, but he has only # participated in game2 which hasn't seen any activity since then: self.assertEquals(len(calls[1][2]['game_activities']), 0) loop.should_send_email = original_should_send_email
def test01_loop(self): player1 = 1 player2 = 2 player3 = 88 player4 = 122 game1 = 111 game2 = 122 game3 = 133 # Mock out should_send_email to always return True, # we'll test that function separately. original_should_send_email = loop.should_send_email def always_true(last_active, game_activities_24h): return True loop.should_send_email = always_true now = datetime.now() yesterday = now - timedelta(days=1) two_days_ago = now - timedelta(days=2) three_days_ago = now - timedelta(days=3) sec = timedelta(seconds=1) c = self.db.cursor() # Prepopulate the tables with fixture data. # ------------------------------------------------------------ # 1. auth_user auth_user_fixtures = [ (player1, '*****@*****.**', 'John Johnson', False), (player2, '*****@*****.**', 'Bill Billson', False), (player3, '*****@*****.**', None, False), (player4, '*****@*****.**', None, True) ] for player in auth_user_fixtures: c.execute('INSERT INTO auth_user (id, username, first_name) VALUES (?, ?, ?)', player[:3]) c.execute('INSERT INTO cardstories_userprofile (user_id, activity_notifications_disabled) VALUES (?, ?)', (player[0], player[3])) # 2. games games_fixtures = [ (game1, player1, 'Sentence 1', 'invitation', three_days_ago), (game2, player1, 'Sentence 2', 'complete', three_days_ago), (game3, player2, 'Sentence 3', 'invitation', now) ] for game in games_fixtures: c.execute('INSERT INTO games (id, owner_id, sentence, state, created) VALUES (?, ?, ?, ?, ?)', game) # 3. player2game player2game_fixtures =[ (player1, game1), (player1, game2), (player2, game1), (player2, game2), (player2, game3), (player3, game2) ] for player_id, game_id in player2game_fixtures: c.execute('insert into player2game (player_id, game_id) values (?, ?)', [player_id, game_id]) # 4. event_logs event_logs_fixtures = [ # Game 1 [game1, two_days_ago, event_log.GAME_CREATED, player1, ''], [game1, two_days_ago, event_log.OWNER_CHOSE_CARD, player1, 22], [game1, two_days_ago, event_log.OWNER_WROTE_STORY, player1, 'Sentence 1'], [game1, yesterday, event_log.PLAYER_JOINED, player2, ''], [game1, yesterday, event_log.PLAYER_VOTED, player2, 33], # Game 2 [game2, two_days_ago, event_log.GAME_CREATED, player1, ''], [game2, two_days_ago, event_log.OWNER_CHOSE_CARD, player1, 34], [game2, two_days_ago, event_log.OWNER_WROTE_STORY, player1, 'Sentence 2'], [game2, two_days_ago, event_log.PLAYER_INVITED, player1, player3], [game2, two_days_ago, event_log.PLAYER_JOINED, player3, ''], [game2, two_days_ago, event_log.PLAYER_PICKED_CARD, player3, 23], [game2, two_days_ago, event_log.PLAYER_JOINED, player2, ''], [game2, two_days_ago, event_log.PLAYER_PICKED_CARD, player2, 24], [game2, two_days_ago, event_log.PLAYER_JOINED, player4, ''], [game2, two_days_ago, event_log.GAME_MOVED_TO_VOTING, player1, ''], [game2, two_days_ago, event_log.PLAYER_VOTED, player2, 44], [game2, two_days_ago, event_log.PLAYER_VOTED, player3, 45], [game2, two_days_ago, event_log.GAME_COMPLETED, player1, ''], # Game 3 [game3, now, event_log.GAME_CREATED, player2, ''], [game3, now, event_log.OWNER_CHOSE_CARD, player2, 34], [game3, now, event_log.OWNER_WROTE_STORY, player2, 'Sentence 3'] ] for event in event_logs_fixtures: c.execute('INSERT INTO event_logs (game_id, timestamp, event_type, player_id, data) VALUES (?, ?, ?, ?, ?)', event) # ------------------------------------------------------------ self.db.commit() c.close() # Mock out send.send_mail to collect arguments it's been called with. calls = [] def mock_send_mail(email, name, context): calls.append([email, name, context]) send.send_mail = mock_send_mail count = loop.loop(self.database, self.database) # Should send out two emails (for player1 and player 3). # The email shouldn't be sent to player2 because he was last active 'now', # and nothing has happened since now (a blank email such as that shouldn't be sent). # The email shouldn't be sent to player4 because he has unsubscribed from the emails. self.assertEquals(count, 2) # Let's see what send_mail has been called with. self.assertEquals(len(calls), 2) # For player1: self.assertEquals(calls[0][1], '*****@*****.**') # No completed games: self.assertEquals(len(calls[0][2]['completed_games']), 0) # One available game (game3 by player2): self.assertEquals(len(calls[0][2]['available_games']), 1) game = calls[0][2]['available_games'][0] self.assertEquals(game['game_id'], game3) self.assertEquals(game['owner_name'], 'Bill Billson') self.assertEquals(game['sentence'], 'Sentence 3') # Player1 was last active two_days ago. # Since then (yesterday), two events happened on one of his games (game1). self.assertEquals(len(calls[0][2]['game_activities']), 1) activity = calls[0][2]['game_activities'][0] self.assertEquals(activity['game_id'], game1) self.assertEquals(activity['state'], 'invitation') self.assertEquals(activity['owner_name'], 'You') self.assertEquals(activity['sentence'], 'Sentence 1') self.assertEquals(len(activity['events']), 2) self.assertEquals(activity['events'][0], 'Bill Billson joined the game') self.assertEquals(activity['events'][1], 'Bill Billson voted') # For player3: self.assertEquals(calls[1][1], '*****@*****.**') # No completed games: self.assertEquals(len(calls[1][2]['completed_games']), 0) # One available game (game3 by player2): self.assertEquals(len(calls[1][2]['available_games']), 1) self.assertEquals(game['game_id'], game3) self.assertEquals(game['owner_name'], 'Bill Billson') self.assertEquals(game['sentence'], 'Sentence 3') # No game activities - player3 has less been active two days ago, but he has only # participated in game2 which hasn't seen any activity since then: self.assertEquals(len(calls[1][2]['game_activities']), 0) loop.should_send_email = original_should_send_email