def test_get_all_spoilers(self): mock_spoilers = [ Spoiler(('test1', 'attach1', '2019-01-01', 1)), Spoiler(('test2', 'attach2', '2019-01-01', 2)), Spoiler(('test3', 'attach3', '2019-01-01', 3)), ] for spoiler in mock_spoilers: self.insert_spoiler(spoiler) self.assertCountEqual(self.test_db.get_all_spoilers(), mock_spoilers)
def test_get_latest_spoiler_date(self): spoil1 = Spoiler(('test1', 'attach1', '2019-01-01', 1)) spoil2 = Spoiler(('test2', 'attach2', '2019-01-02', 2)) spoil3 = Spoiler(('test3', 'attach3', '2018-01-01', 3)) mock_spoilers = [spoil1, spoil2, spoil3] for s in mock_spoilers: self.insert_spoiler(s) self.assertEqual(self.test_db.get_latest_spoiler_date(), '2019-01-02')
def test_get_spoilers_later_than(self): spoil1 = Spoiler(('test1', 'attach1', '2019-01-01', 1)) spoil2 = Spoiler(('test2', 'attach2', '2019-01-01', 2)) spoil3 = Spoiler(('test3', 'attach3', '2019-01-01', 3)) mock_spoilers = [spoil1, spoil2, spoil3] for spoiler in mock_spoilers: self.insert_spoiler(spoiler) self.assertCountEqual(self.test_db.get_spoilers_later_than(1), [spoil2, spoil3]) self.assertCountEqual(self.test_db.get_spoilers_later_than(2), [spoil3])
def test_is_spoiler_allowed_by_options_duplicates_on(self): options_dict = {msbot.constants.DUPLICATES: True} options = UserOptions(json.dumps(options_dict)) # normal image url test_spoiler = Spoiler(('image.jpg', 123456, '2019-01-01', None)) self.assertTrue( webhook.is_spoiler_allowed_by_options(test_spoiler, options)) # duplicate image url test_spoiler = Spoiler(('image1.jpg', 123456, '2019-01-01', None)) self.assertTrue( webhook.is_spoiler_allowed_by_options(test_spoiler, options))
def test_filter_spoilers_by_user_duplicates_on(self): options_dict = {msbot.constants.DUPLICATES: True} options_json = json.dumps(options_dict) test_user = User((1234, 0, 0, options_json)) spoilers = [ Spoiler(('test.jpg', 1, '2019-01-01', None)), Spoiler(('test1.jpg', 2, '2019-01-01', None)), Spoiler(('test2.jpg', 3, '2019-01-01', None)), Spoiler(('other.jpg', 4, '2019-01-01', None)), ] self.assertCountEqual( spoilers, webhook.filter_spoilers_by_user(spoilers, test_user))
def test_handle_message_send_when_subbed(self, spoil_mock, send_mock): alice = User(('Alice', 5, 5, '{}')) spoiler1 = Spoiler(('spoila', 'attach1', '2019-01-01', None)) spoiler2 = Spoiler(('spoilb', 'attach2', '2019-01-01', None)) spoiler3 = Spoiler(('spoilc', 'attach3', '2019-01-01', None)) self.db_mock.user_exists.return_value = True self.db_mock.get_user_from_id.return_value = User( ('Alice', 5, 5, '{}')) latest_spoiler = 8 self.db_mock.get_latest_spoiler_id.return_value = latest_spoiler self.db_mock.get_spoilers_later_than.return_value = [] sender_psid = 1234 # no new spoilers webhook.handle_message(sender_psid, msbot.constants.SEND_CMD) send_mock.assert_called_once_with( sender_psid, webhook.text_quick_reply_response( msbot.constants.RESP_UPDATE_UPDATED, [webhook.RECENT_BUTTON])) # new spoilers send_mock.reset_mock() self.db_mock.get_spoilers_later_than.return_value = [ spoiler1, spoiler2, spoiler3, ] webhook.handle_message(sender_psid, msbot.constants.SEND_CMD) calls = [ mock.call(alice, spoiler1), mock.call(alice, spoiler2), mock.call(alice, spoiler3), ] spoil_mock.assert_has_calls(calls, any_order=True) self.assertEqual(spoil_mock.call_count, len(calls)) send_mock.assert_called_once_with( sender_psid, webhook.to_text_response(msbot.constants.RESP_UPDATE_COMPLETE)) self.db_mock.update_user.assert_called_once_with( alice.user_id, last_updated=latest_spoiler, last_spoiled=latest_spoiler)
def test_get_all_unnotified_users(self): alice = User(('Alice', 0, 2, '{}')) bob = User(('Bob', 1, 0, '{}')) carol = User(('Carol', 2, 1, '{}')) mock_users = [alice, bob, carol] mock_spoilers = [ Spoiler(('test1', 'attach1', None, None)), Spoiler(('test2', 'attach2', None, None)), ] for user in mock_users: self.insert_user(user) for spoiler in mock_spoilers: self.insert_spoiler(spoiler) self.assertCountEqual(self.test_db.get_all_unnotified_users(), [alice, bob])
def getCardsBySet(setname): """ :type setname: String :rtype: List[Spoiler] """ url = url_base + 'APIv2/cards/by/set' payload = {'key': msbot.settings.API_KEY, 'param': setname} r = requests.get(url, params=payload) cards = json.loads(r.text[1:len(r.text) - 1])['item'] spoiler_list = [] for card in cards: spoiler_list.append(Spoiler(card)) return spoiler_list
def test_handle_message_recent_when_subbed(self, spoil_mock, send_mock): alice = User(('Alice', 8, 8, '{}')) latest_date = '2019-02-02' spoiler1 = Spoiler(('spoil1', 'attach1', latest_date, None)) spoiler2 = Spoiler(('spoil2', 'attach2', latest_date, None)) self.db_mock.user_exists.return_value = True self.db_mock.get_user_from_id.return_value = User( ('Alice', 8, 8, '{}')) latest_spoiler = 8 self.db_mock.get_latest_spoiler_id.return_value = latest_spoiler self.db_mock.get_latest_spoiler_date.return_value = latest_date self.db_mock.get_all_spoilers_on_date.return_value = [ spoiler1, spoiler2, ] sender_psid = 1234 # new spoilers webhook.handle_message(sender_psid, msbot.constants.RECENT_CMD) calls = [ mock.call(alice, spoiler1), mock.call(alice, spoiler2), ] spoil_mock.assert_has_calls(calls, any_order=True) self.assertEqual(spoil_mock.call_count, len(calls)) send_mock.assert_called_once_with( sender_psid, webhook.to_text_response( msbot.constants.RESP_LAST_SPOILER_INFO.format( date_string=latest_date))) self.db_mock.update_user.assert_called_once_with( alice.user_id, last_updated=latest_spoiler, last_spoiled=latest_spoiler)
def test_send_spoiler_to(self, send_mock): test_user = User((1234, 0, 0, '{}')) test_spoiler = Spoiler(('test', 123456, '2019-01-01', None)) test_response = { 'attachment': { 'type': 'image', 'payload': { 'attachment_id': test_spoiler.attach_id } } } webhook.send_spoiler_to(test_user, test_spoiler) send_mock.assert_called_once_with(test_user.user_id, test_response)
def get_all_spoilers_on_date(self, date): sql = "SELECT * FROM spoilers WHERE date_spoiled = '{date}'".format(date=date) self.query(sql) return [ Spoiler(row) for row in self.fetchall() ]
def test_get_spoiler_from_id(self): spoiler = Spoiler(('spoil1', 'attach1', '2019-01-01', 1234)) self.insert_spoiler(spoiler) self.assertEqual(spoiler, self.test_db.get_spoiler_from_id(1234))
def get_spoilers_later_than(self, row_id): sql = 'SELECT * FROM spoilers WHERE id > {row_id}'.format(row_id=row_id) self.query(sql) return [ Spoiler(row) for row in self.fetchall() ]
def get_spoiler_from_id(self, spoiler_id): sql = "SELECT * FROM spoilers WHERE id = '{spoiler_id}'".format(spoiler_id=spoiler_id) self.query(sql) return Spoiler(self.fetchone())
def test_spoiler_exists(self): test_spoiler = Spoiler(('test_spoiler_img', 0, None, 0)) self.assertFalse(self.test_db.spoiler_exists(test_spoiler)) self.insert_spoiler(test_spoiler) self.assertTrue(self.test_db.spoiler_exists(test_spoiler.image_url))
def test_update_user(self, send_update_mock, send_spoiler_mock): spoil1 = Spoiler(('one.jpg', 'attach1', '2019-01-01', None)) spoil2 = Spoiler(('one1.jpg', 'attach2', '2019-01-01', None)) spoil3 = Spoiler(('one2.jpg', 'attach2', '2019-01-01', None)) self.db_mock.get_spoilers_later_than.return_value = [ spoil1, spoil2, ] # poll user duplicates on options_dict = {msbot.constants.DUPLICATES: True} alice = User(('Alice', 0, 0, json.dumps(options_dict))) self.db_mock.get_latest_spoiler_id.return_value = 2 webhook.update_user(alice) send_update_mock.assert_called_once_with( alice.user_id, msbot.constants.RESP_UPDATE.format(num_spoilers=2)) self.db_mock.update_user.assert_called_once_with(alice.user_id, last_updated=2) # poll user duplicates off send_update_mock.reset_mock() self.db_mock.update_user.reset_mock() options_dict = {msbot.constants.DUPLICATES: False} alice = User(('Alice', 0, 0, json.dumps(options_dict))) self.db_mock.get_latest_spoiler_id.return_value = 2 webhook.update_user(alice) send_update_mock.assert_called_once_with( alice.user_id, msbot.constants.RESP_UPDATE.format(num_spoilers=1)) self.db_mock.update_user.assert_called_once_with(alice.user_id, last_updated=2) # poll user duplicates off spoilers all duplicates send_update_mock.reset_mock() self.db_mock.update_user.reset_mock() self.db_mock.get_spoilers_later_than.return_value = [spoil2, spoil3] options_dict = {msbot.constants.DUPLICATES: False} alice = User(('Alice', 0, 0, json.dumps(options_dict))) self.db_mock.get_latest_spoiler_id.return_value = 2 webhook.update_user(alice) send_update_mock.assert_not_called() self.db_mock.update_user.assert_called_once_with(alice.user_id, last_updated=2) # asap user duplicates on self.db_mock.update_user.reset_mock() self.db_mock.get_spoilers_later_than.return_value = [ spoil1, spoil2, ] options_dict = {msbot.constants.DUPLICATES: True} alice = User(('Alice', 0, 0, json.dumps(options_dict))) alice.options.update_mode = msbot.constants.ASAP_MODE_CMD webhook.update_user(alice) calls = [ mock.call(alice, spoil1), mock.call(alice, spoil2), ] send_spoiler_mock.assert_has_calls(calls, any_order=True) self.assertEqual(send_spoiler_mock.call_count, len(calls)) self.db_mock.update_user.assert_called_once_with(alice.user_id, last_spoiled=2, last_updated=2) # asap user duplicates off self.db_mock.update_user.reset_mock() send_spoiler_mock.reset_mock() self.db_mock.get_spoilers_later_than.return_value = [ spoil1, spoil2, ] options_dict = {msbot.constants.DUPLICATES: False} alice = User(('Alice', 0, 0, json.dumps(options_dict))) alice.options.update_mode = msbot.constants.ASAP_MODE_CMD webhook.update_user(alice) calls = [ mock.call(alice, spoil1), ] send_spoiler_mock.assert_has_calls(calls, any_order=True) self.assertEqual(send_spoiler_mock.call_count, len(calls)) self.db_mock.update_user.assert_called_once_with(alice.user_id, last_spoiled=2, last_updated=2) # asap user duplicates off spoilers all duplicates self.db_mock.update_user.reset_mock() send_spoiler_mock.reset_mock() self.db_mock.get_spoilers_later_than.return_value = [ spoil2, spoil3, ] options_dict = {msbot.constants.DUPLICATES: False} alice = User(('Alice', 0, 0, json.dumps(options_dict))) alice.options.update_mode = msbot.constants.ASAP_MODE_CMD webhook.update_user(alice) send_spoiler_mock.assert_not_called() self.db_mock.update_user.assert_called_once_with(alice.user_id, last_spoiled=2, last_updated=2) # unsupported mode send_update_mock.reset_mock() self.db_mock.update_user.reset_mock() options_dict = {msbot.constants.DUPLICATES: True} alice = User(('Alice', 0, 0, json.dumps(options_dict))) self.db_mock.get_latest_spoiler_id.return_value = 2 alice.options.update_mode = 'UNSUPPORTED' webhook.update_user(alice) send_update_mock.assert_called_once_with( alice.user_id, msbot.constants.RESP_UPDATE.format(num_spoilers=2)) self.db_mock.update_user.assert_called_once_with(alice.user_id, last_updated=2)
def get_all_spoilers(self): sql = 'SELECT * FROM spoilers' self.query(sql) return [ Spoiler(row) for row in self.fetchall() ]