def GET(self): web.header('Content-Type', 'application/json') try: i = web.input(eventTypeId=None, entityIds=None, end=None) applicationId = int(i.applicationId) start = long(i.start) end = i.end if end is not None: end = long(end) eventTypeId = i.eventTypeId if eventTypeId is not None: eventTypeId = long(eventTypeId) entityIds = i.entityIds if entityIds is not None: entityIds = json.loads(entityIds) eventList = Event.load_from_db(_db, applicationId=applicationId, start=start, end=end, eventTypeId=eventTypeId, entityIds=entityIds) eventJson = [evt.to_dict() for evt in eventList] j = {'status' : 'ok', 'events' : eventJson} return json.dumps(j) except Exception as e: err_json = {'status' : 'error', 'message' : str(e)} raise web.badrequest(json.dumps(err_json))
def test_from_json(self): evt = Event.from_json(json.dumps(self.json)) self.assertEqual(evt.applicationId, self.json['applicationId']) self.assertEqual(evt.eventTypeId, self.json['eventTypeId']) self.assertEqual(evt.headline, self.json['headline']) self.assertEqual(evt.body, self.json['body']) self.assertEqual(evt.eventTime, self.json['eventTime']) self.assertEqual(len(evt.relatedEntities), 4)
def test_entity_query_1(self): with nostderr(): events = Event.load_from_db( self.db, applicationId=1, start=5, entityIds={'1' : [14, 15]} ) self.assertEqual(3, len(events))
def test_cutoff_query_1(self): with nostderr(): events = Event.load_from_db( self.db, applicationId=1, start=15, end=35 ) self.assertEqual(4, len(events))
def POST(self): web.header('Content-Type', 'application/json') try: j = web.data() evt = Event.from_json(j) evt.save(_db) ok_json = { 'status' : 'ok', 'eventId' : evt.eventId } return json.dumps(ok_json) except Exception as e: err_json = {'status' : 'error', 'message' : str(e)} raise web.badrequest(json.dumps(err_json))
def test_query_on_empty_db(self): self.clear() with nostderr(): events = Event.load_from_db(self.db, applicationId=1, start=0) self.assertEqual(len(events), 0)
def save_test_data(self): """save test data to db""" with nostderr(): for d in self.test_data: evt = Event.from_json(d) evt.save(self.db)
def test_app_query(self): with nostderr(): events = Event.load_from_db(self.db, applicationId=1, start=0) self.assertEqual(8, len(events))
def test_raise_error_on_already_saved(self): evt = Event.from_json(self.test_data[0]) with nostderr(): evt.save(self.db) self.assertRaises(EventError, evt.save, self.db)
def test_is_saved_after(self): evt = Event.from_json(self.test_data[0]) with nostderr(): evt.save(self.db) self.assertTrue(evt.is_saved())
def test_is_saved_before(self): evt = Event.from_json(self.test_data[0]) self.assertFalse(evt.is_saved())