def test_csv(self) -> None: appInfo = AppInfo() dbFilename = os.path.join(appInfo.path, ".temp/situationboard.sqlite") inFilename = os.path.join(appInfo.path, "docs/dummy.csv") outFilename = os.path.join(appInfo.path, ".temp/dummy.csv") csvFilename = os.path.join(appInfo.path, ".temp/corner.csv") ###### TEST 1 (round trip) ###### # create a new (empty) database d = Database(dbFilename, reset = True) assert(d.getEventCount(textOnly = False) == 0) # import data from CSV i = CSVImporter(d) result = i.importEvents(inFilename) assert(result == 0) assert(d.getEventCount(textOnly = False) > 0) # export data to CSV e = CSVExporter(d) result = e.exportEvents(outFilename) assert(result == 0) # compare CSV files assert(filecmp.cmp(inFilename, outFilename, shallow=False)) # close database d.close() ###### TEST 2 (corner cases) ###### # create a new (empty) database d = Database(dbFilename, reset = True) assert(d.getEventCount(textOnly = False) == 0) # add new event with pathologic content ts = datetime.datetime.now().strftime(AlarmEvent.TIMESTAMP_FORMAT) content = "Test\n;\\Test\nTest\"äöüß\"\"äöüß'äöüß''äöüß\näöüß" aeAdded = AlarmEvent() aeAdded.timestamp = ts aeAdded.event = content aeAdded.eventDetails = content aeAdded.location = content aeAdded.locationDetails = content aeAdded.comment = content aeAdded.alarmTimestamp = ts aeAdded.locationLatitude = 1.12 aeAdded.locationLongitude = -13.2 eventID = d.addEvent(aeAdded) # export data to CSV e = CSVExporter(d) result = e.exportEvents(csvFilename) assert(result == 0) # close database d.close() # create a new (empty) database d = Database(dbFilename, reset = True) # import data from CSV i = CSVImporter(d) result = i.importEvents(csvFilename) assert(result == 0) assert(d.getEventCount(textOnly = False) > 0) # load event aeLoaded = d.getEvent(eventID) assert(aeLoaded is not None) # compare event data assert(aeAdded.comment == aeLoaded.comment) # close database d.close()
def test_database(self) -> None: appInfo = AppInfo() databaseFilename = os.path.join(appInfo.path, ".temp/situationboard.sqlite") maxLastEvents = 10 # check that creating a database works and yields an empty database db = Database(databaseFilename, reset=True) assert (db.getEventCount(textOnly=False) == 0) assert (db.getEventCount(textOnly=True) == 0) assert (db.getEvent(1302) is None) assert (len(db.getEvents(textOnly=False)) == 0) assert (len(db.getEvents(textOnly=True)) == 0) assert (len(db.getLastEvents(maxLastEvents, textOnly=False)) == 0) assert (len(db.getLastEvents(maxLastEvents, textOnly=True)) == 0) # check for valid stats assert (db.getEventStats(DatabaseTimespan.TOTAL, textOnly=False) == 0) assert (db.getEventStats(DatabaseTimespan.YEAR, textOnly=False) == 0) assert (db.getEventStats(DatabaseTimespan.MONTH, textOnly=False) == 0) assert (db.getEventStats(DatabaseTimespan.TODAY, textOnly=False) == 0) assert (db.getEventStats(DatabaseTimespan.TOTAL, textOnly=True) == 0) assert (db.getEventStats(DatabaseTimespan.YEAR, textOnly=True) == 0) assert (db.getEventStats(DatabaseTimespan.MONTH, textOnly=True) == 0) assert (db.getEventStats(DatabaseTimespan.TODAY, textOnly=True) == 0) # check that inserting a binary event succeeds newEvent1 = self.__createBinaryEvent() newEventTemp1 = copy.deepcopy(newEvent1) assert (db.updateEvent(newEventTemp1) != 0) eventID1 = db.addEvent(newEventTemp1) assert (eventID1 != AlarmEvent.NO_ID) assert (newEventTemp1.eventID == eventID1) assert (db.getEventCount(textOnly=False) == 1) assert (db.getEventCount(textOnly=True) == 0) assert (len(db.getEvents(textOnly=False)) == 1) assert (len(db.getEvents(textOnly=True)) == 0) assert (len(db.getLastEvents(maxLastEvents, textOnly=False)) == 1) assert (len(db.getLastEvents(maxLastEvents, textOnly=True)) == 0) assert (db.getEvent(eventID1) is not None) # check for valid stats assert (db.getEventStats(DatabaseTimespan.TOTAL, textOnly=False) == 1) assert (db.getEventStats(DatabaseTimespan.YEAR, textOnly=False) == 1) assert (db.getEventStats(DatabaseTimespan.MONTH, textOnly=False) == 1) assert (db.getEventStats(DatabaseTimespan.TODAY, textOnly=False) == 1) assert (db.getEventStats(DatabaseTimespan.TOTAL, textOnly=True) == 0) assert (db.getEventStats(DatabaseTimespan.YEAR, textOnly=True) == 0) assert (db.getEventStats(DatabaseTimespan.MONTH, textOnly=True) == 0) assert (db.getEventStats(DatabaseTimespan.TODAY, textOnly=True) == 0) # check that inserting a text event succeeds newEvent2 = self.__createTextEvent() newEvent2Temp = copy.deepcopy(newEvent2) eventID2 = db.addEvent(newEvent2Temp) assert (eventID2 != AlarmEvent.NO_ID) assert (newEvent2Temp.eventID == eventID2) assert (db.getEventCount(textOnly=False) == 2) assert (db.getEventCount(textOnly=True) == 1) assert (len(db.getEvents(textOnly=False)) == 2) assert (len(db.getEvents(textOnly=True)) == 1) assert (len(db.getLastEvents(maxLastEvents, textOnly=False)) == 2) assert (len(db.getLastEvents(maxLastEvents, textOnly=True)) == 1) assert (db.getEvent(eventID2) is not None) # check for valid stats assert (db.getEventStats(DatabaseTimespan.TOTAL, textOnly=False) == 2) assert (db.getEventStats(DatabaseTimespan.YEAR, textOnly=False) == 2) assert (db.getEventStats(DatabaseTimespan.MONTH, textOnly=False) == 2) assert (db.getEventStats(DatabaseTimespan.TODAY, textOnly=False) == 2) assert (db.getEventStats(DatabaseTimespan.TOTAL, textOnly=True) == 1) assert (db.getEventStats(DatabaseTimespan.YEAR, textOnly=True) == 1) assert (db.getEventStats(DatabaseTimespan.MONTH, textOnly=True) == 1) assert (db.getEventStats(DatabaseTimespan.TODAY, textOnly=True) == 1) db.commitAndClose() # check that reloading a database succeeds and yields existing data dbr = Database(databaseFilename, reset=False) assert (dbr.getEventCount(textOnly=False) == 2) assert (dbr.getEventCount(textOnly=True) == 1) assert (len(dbr.getEvents(textOnly=False)) == 2) assert (len(dbr.getEvents(textOnly=True)) == 1) assert (len(dbr.getLastEvents(maxLastEvents, textOnly=False)) == 2) assert (len(dbr.getLastEvents(maxLastEvents, textOnly=True)) == 1) # check that retrieving an event succeeds after reload succeeds and yields the correct data event1 = dbr.getEvent(eventID1) assert (event1 is not None) assert (isinstance(event1, AlarmEvent)) assert (event1.eventID == eventID1) self.__compareEvents(event1, newEvent1) # check that retrieving an event succeeds after reload succeeds and yields the correct data event2 = dbr.getEvent(eventID2) assert (event2 is not None) assert (isinstance(event2, AlarmEvent)) assert (event2.eventID == eventID2) self.__compareEvents(event2, newEvent2) # check that updating an event succeeds newEvent2Updated = copy.deepcopy(newEvent2Temp) self.__updateTextEvent(newEvent2Updated) newEvent2UpdatedTemp = copy.deepcopy(newEvent2Updated) assert (dbr.updateEvent(newEvent2UpdatedTemp) == 0) event2Updated = dbr.getEvent(eventID2) assert (event2Updated is not None) assert (isinstance(event2Updated, AlarmEvent)) assert (event2Updated.eventID == eventID2) self.__compareEvents(event2Updated, newEvent2Updated) # check that deleting an event by ID succeeds assert (dbr.removeEventID(eventID1) == 0) assert (dbr.getEventCount(textOnly=False) == 1) assert (dbr.getEventCount(textOnly=True) == 1) assert (dbr.getEvent(eventID1) is None) assert (len(dbr.getEvents(textOnly=False)) == 1) assert (len(dbr.getEvents(textOnly=True)) == 1) assert (len(dbr.getLastEvents(maxLastEvents, textOnly=False)) == 1) assert (len(dbr.getLastEvents(maxLastEvents, textOnly=True)) == 1) # check that deleting an event succeeds assert (dbr.removeEvent(newEvent2Updated) == 0) assert (dbr.getEventCount(textOnly=False) == 0) assert (dbr.getEventCount(textOnly=True) == 0) assert (dbr.getEvent(eventID2) is None) assert (len(dbr.getEvents(textOnly=False)) == 0) assert (len(dbr.getEvents(textOnly=True)) == 0) assert (len(dbr.getLastEvents(maxLastEvents, textOnly=False)) == 0) assert (len(dbr.getLastEvents(maxLastEvents, textOnly=True)) == 0) # check that deleting non-existent events fails assert (dbr.removeEvent(event1) != 0) assert (dbr.removeEventID(eventID2) != 0) dbr.commitAndClose()