def test_should_send_message_weekdays(self): chat = ChatConfig(days="weekdays", hour=datetime.utcnow().hour, username="******", chat_id="pipipi") if datetime.utcnow().weekday() < 5: self.assertTrue(chat.should_send_message()) else: self.assertFalse(chat.should_send_message())
def all_configs(self): stmt = "SELECT chat_id, username, hour, days FROM config" cur = self.conn.execute(stmt) chats = [] for row in cur: chats.append(ChatConfig(row[0], row[1], row[2], row[3])) return chats
def get_config(self, chat_id): try: chat_id = str(chat_id) stmt = "SELECT username, hour, days FROM config WHERE chat_id=?" cur = self.conn.execute(stmt, (chat_id, )) for x in cur: return ChatConfig(chat_id, x[0], x[1], x[2]) except Exception as e: print(e) return None
def set_config(self, chat_id, username=None, hour=None, days=None): chat_id = str(chat_id) query = "SELECT username, hour, days FROM config WHERE chat_id=?" cur = self.conn.execute(query, (chat_id, )) row = None for x in cur: row = ChatConfig(chat_id, username=x[0], hour=x[1], days=x[2]) if row is not None: # Chat already on the database row = row.update( username, hour, days) # Will update the fields that are not of NoneType stmt = "UPDATE config SET username=?, hour=?, days=? WHERE chat_id=?" self.conn.execute(stmt, ( row.username, row.hour, row.days, row.chat_id, )) self.conn.commit() else: # Chat not on the database yet row = ChatConfig(chat_id, username, hour, days) stmt = "INSERT INTO config (chat_id, username, hour, days) VALUES (?, ?, ?, ?)" self.conn.execute(stmt, ( row.chat_id, row.username, row.hour, row.days, )) self.conn.commit() return row
def test_should_send_message_daily(self): chat = ChatConfig(days="daily", hour=datetime.utcnow().hour, username="******", chat_id="abc123") self.assertTrue(chat.should_send_message())
def test_should_not_send_message_chat_id(self): chat = ChatConfig(days="daily", hour=datetime.utcnow().hour, username="******", chat_id="") self.assertFalse(chat.should_send_message())
def test_create_default_chat_config(self): chat = ChatConfig() self.assertEqual(chat.hour, 0) self.assertEqual(chat.days, "weekdays") self.assertEqual(chat.chat_id, "")
def test_chat_is_valid(self): chat = ChatConfig(username="******", chat_id="123abc") self.assertTrue(chat.valid())
def test_should_not_send_message_no_username(self): chat = ChatConfig(days="daily", hour=(datetime.utcnow().hour + 2) % 24, username="", chat_id="abc123") self.assertFalse(chat.should_send_message())
def test_update_multiple_fields(self): chat = ChatConfig() chat.update(days="weekdays", username="******", hour=14) self.assertEqual(chat.days, "weekdays") self.assertEqual(chat.username, "devmob") self.assertEqual(chat.hour, 14)
def test_empty_chat_is_not_valid(self): chat = ChatConfig() self.assertFalse(chat.valid())
def test_update_chained_fields(self): chat = ChatConfig() chat.update(days="daily") chat.update(hour=12) self.assertEqual(chat.days, "daily") self.assertEqual(chat.hour, 12)
def test_update_default_to_invalid_days(self): chat = ChatConfig() chat.update(days="today") self.assertEqual(chat.days, "weekdays")
def test_update_days(self): chat = ChatConfig() chat.update(days="weekdays") self.assertEqual(chat.days, "weekdays")
def test_update_hour(self): chat = ChatConfig() chat.update(hour=9) self.assertEqual(chat.hour, 9)
def test_update_username(self): chat = ChatConfig() chat.update(username="******") self.assertEqual(chat.username, "devmob")