def setUp(self): parms = settings.BITIA_TEST_ENHYDRIS_INSTALLATION self.cookies = enhydris_api.login(parms['base_url'], parms['user'], parms['password']) # Create two time series j = { 'gentity': parms['station_id'], 'variable': parms['variable_id'], 'unit_of_measurement': parms['unit_of_measurement_id'], 'time_zone': parms['time_zone_id'], } self.ts1_id = enhydris_api.post_model( parms['base_url'], self.cookies, 'Timeseries', j) self.ts2_id = enhydris_api.post_model( parms['base_url'], self.cookies, 'Timeseries', j) assert self.ts1_id != self.ts2_id # Add some data (all but the last record) to the database ts = Timeseries(self.ts1_id) ts.read(StringIO(self.timeseries1_top)) enhydris_api.post_tsdata(parms['base_url'], self.cookies, ts) ts = Timeseries(self.ts2_id) ts.read(StringIO(self.timeseries2_top)) enhydris_api.post_tsdata(parms['base_url'], self.cookies, ts) # Temporary directory for cache files self.tempdir = tempfile.mkdtemp()
def setUp(self): self.parms = json.loads(os.getenv('PTHELMA_TEST_ENHYDRIS_API')) self.cookies = enhydris_api.login(self.parms['base_url'], self.parms['user'], self.parms['password']) # Create two time series j = { 'gentity': self.parms['station_id'], 'variable': self.parms['variable_id'], 'unit_of_measurement': self.parms['unit_of_measurement_id'], 'time_zone': self.parms['time_zone_id'], 'time_step': 3, 'timestamp_offset_minutes': 0, 'timestamp_offset_months': 0, 'remarks': 'Très importante', } self.ts1_id = enhydris_api.post_model( self.parms['base_url'], self.cookies, 'Timeseries', j) self.ts2_id = enhydris_api.post_model( self.parms['base_url'], self.cookies, 'Timeseries', j) assert self.ts1_id != self.ts2_id # Add some data (all but the last record) to the database ts = Timeseries(self.ts1_id) ts.read(StringIO(self.timeseries1_top)) enhydris_api.post_tsdata(self.parms['base_url'], self.cookies, ts) ts = Timeseries(self.ts2_id) ts.read(StringIO(self.timeseries2_top)) enhydris_api.post_tsdata(self.parms['base_url'], self.cookies, ts) # Temporary directory for cache files self.tempdir = tempfile.mkdtemp() self.savedcwd = os.getcwd() os.chdir(self.tempdir)
def test_get_ts_end_date(self): v = json.loads(os.getenv('PTHELMA_TEST_ENHYDRIS_API')) cookies = enhydris_api.login(v['base_url'], v['user'], v['password']) # Create a time series in the database j = { 'gentity': v['station_id'], 'variable': v['variable_id'], 'unit_of_measurement': v['unit_of_measurement_id'], 'time_zone': v['time_zone_id'], } ts_id = enhydris_api.post_model(v['base_url'], cookies, 'Timeseries', j) # Get its last date while it has no data date = enhydris_api.get_ts_end_date(v['base_url'], cookies, ts_id) self.assertEqual(date.isoformat(), '0001-01-01T00:00:00') # Now upload some data ts = Timeseries(ts_id) ts.read(StringIO(self.test_timeseries)) enhydris_api.post_tsdata(v['base_url'], cookies, ts) # Get its last date date = enhydris_api.get_ts_end_date(v['base_url'], cookies, ts_id) self.assertEqual(date.isoformat(), '2014-01-05T08:00:00') # Get the last date of a nonexistent time series self.assertRaises(requests.HTTPError, enhydris_api.get_ts_end_date, v['base_url'], cookies, ts_id + 1)
def test_update_timeseries_cache(self): parms = settings.BITIA_TEST_ENHYDRIS_INSTALLATION bitia_timeseries_groups = { 'one': [{'base_url': parms['base_url'], 'id': self.ts1_id, 'user': parms['user'], 'password': parms['password'], }, {'base_url': parms['base_url'], 'id': self.ts2_id, 'user': parms['user'], 'password': parms['password'], }, ], } with self.settings(BITIA_TIMESERIES_CACHE_DIR=self.tempdir, BITIA_TIMESERIES_GROUPS=bitia_timeseries_groups): # Cache the two timeseries update_timeseries_cache() # Check that the cached stuff is what it should be file1, file2 = [os.path.join(self.tempdir, '{}.hts'.format(x)) for x in (self.ts1_id, self.ts2_id)] with open(file1) as f: self.assertEqual(f.read().replace('\r', ''), self.timeseries1_top) with open(file2) as f: self.assertEqual(f.read().replace('\r', ''), self.timeseries2_top) # Append a record to the database for each timeseries ts = Timeseries(self.ts1_id) ts.read(StringIO(self.timeseries1_bottom)) enhydris_api.post_tsdata(parms['base_url'], self.cookies, ts) ts = Timeseries(self.ts2_id) ts.read(StringIO(self.timeseries2_bottom)) enhydris_api.post_tsdata(parms['base_url'], self.cookies, ts) # Update the cache update_timeseries_cache() # Check that the cached stuff is what it should be file1, file2 = [os.path.join(self.tempdir, '{}.hts'.format(x)) for x in (self.ts1_id, self.ts2_id)] with open(file1) as f: self.assertEqual(f.read().replace('\r', ''), self.test_timeseries1) with open(file2) as f: self.assertEqual(f.read().replace('\r', ''), self.test_timeseries2)
def test_post_tsdata(self): v = json.loads(os.getenv('PTHELMA_TEST_ENHYDRIS_API')) cookies = enhydris_api.login(v['base_url'], v['user'], v['password']) # Create a time series in the database j = { 'gentity': v['station_id'], 'variable': v['variable_id'], 'unit_of_measurement': v['unit_of_measurement_id'], 'time_zone': v['time_zone_id'], } ts_id = enhydris_api.post_model(v['base_url'], cookies, 'Timeseries', j) # Now upload some data ts = Timeseries(ts_id) ts.read(StringIO(self.test_timeseries_top)) enhydris_api.post_tsdata(v['base_url'], cookies, ts) # Read and check the time series url = enhydris_api.urljoin(v['base_url'], 'timeseries/d/{}/download/'.format(ts.id)) r = requests.get(url, cookies=cookies) r.raise_for_status() self.assertEqual(get_after_blank_line(r.text), self.test_timeseries_top) # Upload more data ts = Timeseries(ts_id) ts.read(StringIO(self.test_timeseries_bottom)) enhydris_api.post_tsdata(v['base_url'], cookies, ts) # Read and check the time series url = enhydris_api.urljoin(v['base_url'], 'timeseries/d/{}/download/'.format(ts.id)) r = requests.get(url, cookies=cookies) r.raise_for_status() self.assertEqual(get_after_blank_line(r.text), self.test_timeseries)
def _append_records_to_database(self, ts_to_append): fp = StringIO() ts_to_append.write(fp) r = enhydris_api.post_tsdata(self.base_url, self.cookies, ts_to_append) self.logger.info( 'Successfully appended {0} records'.format(r))
def test_update(self): self.parms = json.loads(os.getenv('PTHELMA_TEST_ENHYDRIS_API')) timeseries_group = [{'base_url': self.parms['base_url'], 'id': self.ts1_id, 'user': self.parms['user'], 'password': self.parms['password'], 'file': 'file1', }, {'base_url': self.parms['base_url'], 'id': self.ts2_id, 'user': self.parms['user'], 'password': self.parms['password'], 'file': 'file2', }, ] # Cache the two timeseries cache = TimeseriesCache(timeseries_group) cache.update() # Check that the cached stuff is what it should be with open('file1') as f: ts1_before = Timeseries() ts1_before.read_file(f) self.assertEqual(ts1_before.time_step.length_minutes, 1440) self.assertEqual(ts1_before.time_step.length_months, 0) c = StringIO() ts1_before.write(c) self.assertEqual(c.getvalue().replace('\r', ''), self.timeseries1_top) with open('file2') as f: ts2_before = Timeseries() ts2_before.read_file(f) self.assertEqual(ts2_before.time_step.length_minutes, 1440) self.assertEqual(ts2_before.time_step.length_months, 0) c = StringIO() ts2_before.write(c) self.assertEqual(c.getvalue().replace('\r', ''), self.timeseries2_top) # Append a record to the database for each timeseries ts = Timeseries(self.ts1_id) ts.read(StringIO(self.timeseries1_bottom)) enhydris_api.post_tsdata(self.parms['base_url'], self.cookies, ts) ts = Timeseries(self.ts2_id) ts.read(StringIO(self.timeseries2_bottom)) enhydris_api.post_tsdata(self.parms['base_url'], self.cookies, ts) # Update the cache cache.update() # Check that the cached stuff is what it should be with open('file1') as f: ts1_after = Timeseries() ts1_after.read_file(f) self.assertEqual(ts1_after.time_step.length_minutes, 1440) self.assertEqual(ts1_after.time_step.length_months, 0) c = StringIO() ts1_after.write(c) self.assertEqual(c.getvalue().replace('\r', ''), self.test_timeseries1) with open('file2') as f: ts2_after = Timeseries() ts2_after.read_file(f) self.assertEqual(ts2_after.time_step.length_minutes, 1440) self.assertEqual(ts2_after.time_step.length_months, 0) c = StringIO() ts2_after.write(c) self.assertEqual(c.getvalue().replace('\r', ''), self.test_timeseries2) # Check that the time series comments are the same before and after self.assertEqual(ts1_before.comment, ts1_after.comment) self.assertEqual(ts2_before.comment, ts2_after.comment)