def find_all(self) -> list: result = [] alarm_result_converter = AlarmResultConverter() connection = ConnectorFactory().build_connector().get_connection() cursor = connection.cursor() for row in cursor.execute(FIND_ALARMS): result.append( alarm_result_converter.convert_cursor_result_to_alarm(row)) connection.close() return result
def delete(self, entity): connection = ConnectorFactory().build_connector().get_connection() cursor = connection.cursor() cursor.execute(DELETE_ALARM, (entity.id, )) connection.commit() connection.close()
def setup_alarm(): ConnectorFactory().build_connector().clear_db() now = datetime.now() yesterday = now - timedelta(1) repo = AlarmRepository() alarm = Alarm(name="Alarm 1", hour=now.hour, minute=now.minute, last_activated=yesterday) repo.insert(alarm)
def update(self, entity) -> Alarm: if type(entity) is not Alarm: raise ValueError("'entity' must be of type 'Alarm'") if entity.id <= 0: return self.insert(entity) else: connection = ConnectorFactory().build_connector().get_connection() cursor = connection.cursor() cursor.execute(UPDATE_ALARM, (entity.name, entity.hour, entity.minute, entity.last_activated, entity.id)) connection.commit() connection.close() return self.find_by_id(entity.id)
def insert(self, entity) -> Alarm: if type(entity) is not Alarm: raise ValueError("'entity' must be of type 'Alarm'") connection = ConnectorFactory().build_connector().get_connection() cursor = connection.cursor() cursor.execute( INSERT_ALARM, (entity.name, entity.hour, entity.minute, entity.last_activated)) entity.id = cursor.lastrowid connection.commit() connection.close() return entity
def find_by_id(self, entity_id) -> Alarm: connection = ConnectorFactory().build_connector().get_connection() cursor = connection.cursor() result = None for row in cursor.execute(FIND_ALARM, (entity_id, )): if result is not None: raise RuntimeError( "Entities with the same id are present in the sqlite table." ) result = row connection.commit() connection.close() if result is not None: return AlarmResultConverter().convert_cursor_result_to_alarm( result) else: return None
def test_get_connector(): connector_factory = ConnectorFactory() connector = connector_factory.build_connector() assert connector is not None
def clear_db(): ConnectorFactory().build_connector().clear_db()