def test_post_invalid_timestamps(self): ship_id = str(uuid1()) self.addCleanup(DBManager.delete_full_energy_entry, ship_id, getLogger()) data = { "spaceship_id": ship_id, "units": "kWh", "data": [ { "datetime": "2018-08-34T04:00:00Z", "value": randint(0, 500) }, { "datetime": "2018-09-24T99:99:00Z", "value": randint(0, 500) }, ] } resp = requests.post(url=HOST_URL, json=data) expected = 'invalid values in data object' self.assertIn(expected, resp.text) self.assertEqual(resp.status_code, HTTPStatus.BAD_REQUEST) DBManager.delete_full_energy_entry(ship_id, getLogger())
def test_post_response_empty_data(self): ship_id = str(uuid1()) self.addCleanup(DBManager.delete_full_energy_entry, ship_id, getLogger()) data = {"spaceship_id": ship_id, "units": "kWh", "data": []} resp = requests.post(url=HOST_URL, json=data) expected = 'empty data object' self.assertIn(expected, resp.text) self.assertEqual(resp.status_code, HTTPStatus.BAD_REQUEST) DBManager.delete_full_energy_entry(ship_id, getLogger())
def test_db_query_outofrange(self): ship_id = str(uuid1()) # create dataframe date_rng = pd.date_range(start='01/01/2018', end='02/01/2018', freq='15T') df = pd.DataFrame(date_rng, columns=['datetime']) df['value'] = np.random.uniform(0, 1000, size=(len(date_rng))) df['datetime'] = pd.to_datetime(df['datetime']) df = df.set_index('datetime') # save new timeseries DBManager.save_energy_entry(ship_id, df) # query start = '2019-01-01T00:00:00Z' end = '2022-01-05T00:00:00Z' got = DBManager.get_energy_entry(ship_id, start, end, MOCK_LOG) self.assertTrue(not got) # remove ship entry DBManager.delete_full_energy_entry(ship_id, MOCK_LOG)
def test_post_response_missing_units_label(self): ship_id = str(uuid1()) self.addCleanup(DBManager.delete_full_energy_entry, ship_id, getLogger()) data = { "spaceship_id": ship_id, "data": [{ "datetime": "2018-08-24T00:00:00Z", "value": randint(0, 500) }, { "datetime": "2018-08-24T01:00:00Z", "value": randint(0, 500) }, { "datetime": "2018-08-24T02:00:00Z", "value": randint(0, 500) }] } resp = requests.post(url=HOST_URL, json=data) expected = 'missing units in input fields' self.assertEqual(resp.text, expected) self.assertEqual(resp.status_code, HTTPStatus.BAD_REQUEST) DBManager.delete_full_energy_entry(ship_id, getLogger())
def test_db_save_delete_basic(self): empty_db = True ship_id = str(uuid1()) try: all_data = DBManager.dump_energy_ts(MOCK_LOG) db = all_data['energy'] empty_db = False except KeyError: pass # create dataframe date_rng = pd.date_range(start='01/01/2018', end='01/04/2018', freq='15T') df = pd.DataFrame(date_rng, columns=['datetime']) df['value'] = np.random.uniform(0, 1000, size=(len(date_rng))) df['datetime'] = pd.to_datetime(df['datetime']) df = df.set_index('datetime') if not empty_db: for row in df.values: self.assertFalse([ship_id, row[0]] in db.values) # save new timeseries DBManager.save_energy_entry(ship_id, df) all_data = DBManager.dump_energy_ts(getLogger()) db = all_data['energy'] for row in df.values: self.assertTrue([ship_id, row[0]] in db.values) # remove ship entry DBManager.delete_full_energy_entry(ship_id, MOCK_LOG) try: all_data = DBManager.dump_energy_ts(getLogger()) db = all_data['energy'] except KeyError: return for row in df.values: self.assertFalse([ship_id, row[0]] in db.values)
def test_post_missing_data_timestamp(self): ship_id = str(uuid1()) self.addCleanup(DBManager.delete_full_energy_entry, ship_id, getLogger()) data = { "spaceship_id": ship_id, "units": "kWh", "data": [ { "value": randint(0, 500) }, { "datetime": "2018-09-24T01:45:00Z", "value": randint(0, 500) }, ] } resp = requests.post(url=HOST_URL, json=data) expected = 'Data saved successfully for ship' self.assertIn(expected, resp.text) self.assertEqual(resp.status_code, HTTPStatus.OK) DBManager.delete_full_energy_entry(ship_id, getLogger())
def tearDownClass(cls): for data in cls.to_post: DBManager.delete_full_energy_entry(data['spaceship_id'], getLogger())