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_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_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")