def test_execute(self): application = cli.App(self.config_file) # Check that the two files don't exist yet self.assertFalse(os.path.exists(os.path.join(self.tempdir, "file1"))) self.assertFalse(os.path.exists(os.path.join(self.tempdir, "file2"))) # Execute the application application.run() # Check that it has created two files self.assertTrue(os.path.exists(os.path.join(self.tempdir, "file1"))) self.assertTrue(os.path.exists(os.path.join(self.tempdir, "file2"))) # Check that the files are what they should be with open("file1", newline="\n") as f: ts1_before = HTimeseries(f) self.assertEqual(ts1_before.time_step, "D") c = StringIO() ts1_before.write(c) self.assertEqual(c.getvalue().replace("\r", ""), self.timeseries1_top) with open("file2", newline="\n") as f: ts2_before = HTimeseries(f) self.assertEqual(ts2_before.time_step, "D") c = StringIO() ts2_before.write(c) self.assertEqual(c.getvalue().replace("\r", ""), self.timeseries2_top) # Append a record to the database for each timeseries self.api_client.post_tsdata( self.station1_id, self.timeseries1_id, HTimeseries(StringIO(self.timeseries1_bottom)), ) self.api_client.post_tsdata( self.station2_id, self.timeseries2_id, HTimeseries(StringIO(self.timeseries2_bottom)), ) # Execute the application again application.run() # Check that the files are what they should be with open("file1", newline="\n") as f: ts1_after = HTimeseries(f) self.assertEqual(ts1_after.time_step, "D") c = StringIO() ts1_after.write(c) self.assertEqual(c.getvalue().replace("\r", ""), self.test_timeseries1) with open("file2", newline="\n") as f: ts2_after = HTimeseries(f) self.assertEqual(ts2_after.time_step, "D") 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)
def test_nonexistent_log_level_raises_error(self): with open(self.configfilename, "w") as f: f.write( textwrap.dedent( """\ [General] loglevel = HELLO """ ) ) msg = "loglevel must be one of ERROR, WARNING, INFO, DEBUG" with self.assertRaisesRegex(click.ClickException, msg): cli.App(self.configfilename).run()
def test_correct_configuration_executes(self, m): with open(self.configfilename, "w") as f: f.write( """\ [General] loglevel = WARNING [Temperature] base_url = https://openmeteo.org/ station_id = 585 timeseries_id = 5850 file = /tmp/temperature.hts """ ) cli.App(self.configfilename).run() m.assert_called_once_with()
def test_missing_timeseries_id_parameter_raises_error(self): with open(self.configfilename, "w") as f: f.write( textwrap.dedent( """\ [General] loglevel = WARNING [Temperature] base_url = https://openmeteo.org/ station_id = 585 file = /tmp/temperature.hts """ ) ) msg = "No option 'timeseries_id'" with self.assertRaisesRegex(click.ClickException, msg): cli.App(self.configfilename).run()
def test_wrong_timeseries_id_parameter_raises_error(self): with open(self.configfilename, "w") as f: f.write( textwrap.dedent( """\ [General] loglevel = WARNING [Temperature] base_url = https://openmeteo.org/ station_id = 585 timeseries_id = hello file = /tmp/temperature.hts """ ) ) with self.assertRaisesRegex(click.ClickException, "not a valid integer"): cli.App(self.configfilename).run()
def test_creates_log_file(self, *args): logfilename = os.path.join(self.tempdir, "enhydris_cache.log") with open(self.configfilename, "w") as f: f.write( textwrap.dedent( """\ [General] logfile = {} loglevel = WARNING [Temperature] base_url = https://openmeteo.org/ station_id = 585 timeseries_id = 5850 file = /tmp/temperature.hts """.format( logfilename ) ) ) cli.App(self.configfilename).run() self.assertTrue(os.path.exists(logfilename))