def test_cooldown_updates(setup): """Test sending after allowed and not allowed cooldown.""" user_set = {(1, 1), (1, 2), (2, 3)} result = sorted(database.get_and_update_messages_to_send(user_set), key=lambda x: (x)) db = assert_db_consistent(result) timestamp_insert_no_cd = db[0][3] timestamp_insert_big_cd = db[1][3] timestamp_insert_small_cd = db[2][3] assert len(result) == 3 for row in result: assert row in [{'1': '1'}, {'1': '2'}, {'2': '3'}] time.sleep(1) result = sorted(database.get_and_update_messages_to_send(user_set), key=lambda x: (x)) db = assert_db_consistent([{'1': '1'}, {'1': '2'}, {'2': '3'}]) timestamp_update_no_cd = db[0][3] timestamp_update_big_cd = db[1][3] timestamp_update_small_cd = db[2][3] assert timestamp_insert_no_cd < timestamp_update_no_cd assert timestamp_insert_big_cd == timestamp_update_big_cd assert timestamp_insert_small_cd < timestamp_update_small_cd assert len(result) == 2 for row in result: assert row in [{'1': '1'}, {'2': '3'}]
def test_empty_table_one_user_cooldown(setup): """Send a message with cooldown for the first time.""" user_set = {(1, 2)} result = sorted(database.get_and_update_messages_to_send(user_set), key=lambda x: (x)) assert result == [{'1': '2'}] assert_db_consistent(result)
def test_duplicate_pairs(setup): """Test if duplicates and handled.""" user_set = {(1, 1), (1, 1)} result = sorted(database.get_and_update_messages_to_send(user_set), key=lambda x: (x)) assert_db_consistent(result) assert result == [{'1': '1'}]
def test_empty_table_two_users(setup): """Send messages for 2 users for the first time.""" user_set = {(2, 1), (3, 2)} result = sorted(database.get_and_update_messages_to_send(user_set), key=lambda x: (x)) assert len(result) == 2 for row in result: assert row in [{'2': '1'}, {'3': '2'}] assert_db_consistent(result)
def filter_messages(self, messages): """Filters out messages that shouldn't be send according to cooldown""" if len(messages) > 0: pairs = {(message['login_id'], message['message_id']) for message in messages} pairs_to_send = database.get_and_update_messages_to_send(pairs) if pairs_to_send is not None and len(pairs_to_send) > 0: # converts [{user_id: message_id}, ...] to [(user_id, message_id), ...] pairs_to_send_tuple = set([(pair.iteritems().next()) for pair in pairs_to_send]) return [message for message in messages if (str(message['login_id']), str(message['message_id'])) in pairs_to_send_tuple] else: return [] return messages
def filter_messages(self, messages): """Filters out messages that shouldn't be send according to cooldown""" if len(messages) > 0: pairs = {(message['login_id'], message['message_id']) for message in messages} pairs_to_send = database.get_and_update_messages_to_send(pairs) if pairs_to_send is not None and len(pairs_to_send) > 0: # converts [{user_id: message_id}, ...] to [(user_id, message_id), ...] pairs_to_send_tuple = set([(pair.iteritems().next()) for pair in pairs_to_send]) return [ message for message in messages if (str(message['login_id']), str(message['message_id'])) in pairs_to_send_tuple ] else: return [] return messages