def remove_session(exc): if exc and not is_fake_error(exc): app.logger.exception("Rolling back database") Session.rollback() else: Session.commit() Session.remove()
def test_entry_count(self): e = event.Event.for_name("Drink glass of water") e.track() e.track() e.track() Session.commit() self.assertEqual(e.entry_count(), 3)
def test_list_attributes(self): e = event.Event.for_name("Drink glass of water") e.track(attrs=dict(size="16", location="office")) e.track(attrs=dict(hello="world")) e.track(attrs=dict(hello="goodbye", location="office")) event.Event.for_name("Fire ze missile", create=True).track(attrs=dict(le_tired="true")) Session.commit() self.assertEqual(e.attributes(), ["hello", "location", "size"])
def test_latest_entry(self): e = event.Event.for_name("Drink glass of water") e.track(when=earlier(seconds=3)) e.track(when=earlier(seconds=2)) f = e.track(when=earlier(seconds=1)) Session.commit() self.assertEqual(e.latest_entry().id, f.id)
def test_exports_csv(self): e = event.Event.for_name("Drink glass of water") o = e.track(when=earlier(seconds=-1), attrs=dict(size="16", location="office")) e.track(attrs=dict(hello="world", when="now")) e.track(attrs=dict(hello="goodbye", location="office")) Session.commit() csv_file = list(csv.reader(e.export_csv().splitlines())) self.assertEqual(csv_file[0], ["When", "hello", "location", "size", "when"]) self.assertEqual(csv_file[1], [str(o.when), "", "office", "16", ""]) self.assertEqual(len(csv_file), 4)
def test_events_persist(self): e = event.Event.for_name("Drink glass of water") o = e.track(attrs=dict(size="16", location="office")) when = o.when attrs = dict(o.attrs) # Reload from db Session.commit() Session.remove() e = event.Event.for_name("Drink glass of water") o1 = e.entries()[0] self.assertDatetimesEqual(when, o1.when) self.assertEqual(attrs, o1.attrs)