From 7456005d4a29268864de2ba8020ad03cfa52a2a2 Mon Sep 17 00:00:00 2001 From: Lewis Chambers Date: Wed, 19 Jun 2024 16:29:49 +0100 Subject: [PATCH] Updated tests to pass --- src/iotswarm/scripts/cli.py | 6 +----- src/tests/test_cli.py | 5 +---- src/tests/test_db.py | 23 ++++++++++++++++++----- src/tests/test_devices.py | 12 ++++++++++-- 4 files changed, 30 insertions(+), 16 deletions(-) diff --git a/src/iotswarm/scripts/cli.py b/src/iotswarm/scripts/cli.py index eac5d5f..912a7be 100644 --- a/src/iotswarm/scripts/cli.py +++ b/src/iotswarm/scripts/cli.py @@ -22,6 +22,7 @@ "--log-config", type=click.Path(exists=True), help="Path to a logging config file. Uses default if not given.", + default=Path(Path(__file__).parents[1], "__assets__", "loggers.ini"), ) @click.option( "--log-level", @@ -35,11 +36,6 @@ def main(ctx: click.Context, log_config: Path, log_level: str): """Core group of the cli.""" ctx.ensure_object(dict) - if log_config: - click.echo("Using supplied logger.") - else: - log_config = Path(Path(__file__).parents[1], "__assets__", "loggers.ini") - logging.config.fileConfig(fname=log_config) logger = logging.getLogger(__name__) diff --git a/src/tests/test_cli.py b/src/tests/test_cli.py index e94147b..c771148 100644 --- a/src/tests/test_cli.py +++ b/src/tests/test_cli.py @@ -40,10 +40,7 @@ def test_main_log_config(): ) result = RUNNER.invoke(cli.main, ["--log-config", "logger.ini", "test"]) assert not result.exception - assert ( - result.output - == "Using supplied logger.\n{'logger': }\n" - ) + assert result.output == "{'logger': }\n" @parameterized.expand(["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"]) diff --git a/src/tests/test_db.py b/src/tests/test_db.py index 960e271..34e8675 100644 --- a/src/tests/test_db.py +++ b/src/tests/test_db.py @@ -212,6 +212,7 @@ async def test__repr__(self): CSV_PATH = Path(Path(__file__).parents[1], "iotswarm", "__assets__", "data") CSV_DATA_FILES = [Path(x) for x in glob(str(Path(CSV_PATH, "*.csv")))] +sqlite_db_exist = pytest.mark.skipif(not Path(CSV_PATH, "cosmos.db").exists(), reason="Local cosmos.db does not exist.") data_files_exist = pytest.mark.skipif( not CSV_PATH.exists() or len(CSV_DATA_FILES) == 0, @@ -380,17 +381,21 @@ async def test_flow_with_swarm_attached(self): class TestSqliteDB(unittest.TestCase): + @sqlite_db_exist def setUp(self): self.db_path = Path(Path(__file__).parents[1], "iotswarm", "__assets__", "data", "cosmos.db") self.table = CosmosTable.LEVEL_1_SOILMET_30MIN - self.database = db.LoopingSQLite3(self.db_path) + + if self.db_path.exists(): + self.database = db.LoopingSQLite3(self.db_path) self.maxDiff = None - + @sqlite_db_exist def test_instantiation(self): self.assertIsInstance(self.database, db.LoopingSQLite3) self.assertIsInstance(self.database.connection, sqlite3.Connection) + @sqlite_db_exist def test_latest_data(self): site_id = "MORLY" @@ -399,6 +404,7 @@ def test_latest_data(self): self.assertIsInstance(data, dict) + @sqlite_db_exist def test_site_id_query(self): sites = self.database.query_site_ids(self.table) @@ -408,8 +414,9 @@ def test_site_id_query(self): self.assertIsInstance(sites, list) for site in sites: - self.assertIsInstance(site, str) + self.assertIsInstance(site, str) + @sqlite_db_exist def test_multiple_sites_added_to_cache(self): sites = ["ALIC1", "MORLY", "HOLLN","EUSTN"] @@ -419,7 +426,8 @@ def test_multiple_sites_added_to_cache(self): self.assertEqual(site, data[i]["SITE_ID"]) self.assertIn(site, self.database.cache) self.assertEqual(self.database.cache[site], 0) - + + @sqlite_db_exist def test_cache_incremented_on_each_request(self): site = "MORLY" @@ -434,6 +442,7 @@ def test_cache_incremented_on_each_request(self): last_data = data + @sqlite_db_exist def test_cache_counter_restarts_at_end(self): database = db.LoopingSQLite3(Path(Path(__file__).parent, "data", "database.db")) @@ -453,12 +462,15 @@ def test_cache_counter_restarts_at_end(self): class TestLoopingSQLite3DBEndToEnd(unittest.IsolatedAsyncioTestCase): """Tests the LoopingCsvDB class.""" + @sqlite_db_exist def setUp(self): self.db_path = Path(Path(__file__).parents[1], "iotswarm", "__assets__", "data", "cosmos.db") - self.database = db.LoopingSQLite3(self.db_path) + if self.db_path.exists(): + self.database = db.LoopingSQLite3(self.db_path) self.maxDiff = None self.table = CosmosTable.LEVEL_1_PRECIP_1MIN + @sqlite_db_exist async def test_flow_with_device_attached(self): """Tests that data is looped through with a device making requests.""" @@ -468,6 +480,7 @@ async def test_flow_with_device_attached(self): self.assertDictEqual(self.database.cache, {"ALIC1": 4}) + @sqlite_db_exist async def test_flow_with_swarm_attached(self): """Tests that the database is looped through correctly with multiple sites in a swarm.""" diff --git a/src/tests/test_devices.py b/src/tests/test_devices.py index 0ea097d..ec5f881 100644 --- a/src/tests/test_devices.py +++ b/src/tests/test_devices.py @@ -23,6 +23,10 @@ reason="Config file `config.cfg` not found in root directory.", ) +DATA_DIR = Path(Path(__file__).parents[1], "iotswarm", "__assets__", "data") +sqlite_db_exist = pytest.mark.skipif(not Path(DATA_DIR, "cosmos.db").exists(), reason="Local cosmos.db does not exist.") + + class TestBaseClass(unittest.IsolatedAsyncioTestCase): def setUp(self): @@ -327,19 +331,22 @@ async def test__get_payload(self): self.assertIsInstance(payload, dict) class TestBaseDevicesSQLite3Used(unittest.IsolatedAsyncioTestCase): + def setUp(self): db_path = Path(Path(__file__).parents[1], "iotswarm","__assets__", "data", "cosmos.db") - self.db = LoopingSQLite3(db_path) + if db_path.exists(): + self.db = LoopingSQLite3(db_path) self.table = CosmosTable.LEVEL_1_SOILMET_30MIN @parameterized.expand([-1, -423.78, CosmosQuery.ORACLE_LATEST_DATA, "Four", MockDB(), {"a": 1}]) + @sqlite_db_exist def test_table_value_check(self, table): - with self.assertRaises(TypeError): BaseDevice( "test_id", self.db, MockMessageConnection(), table=table ) + @sqlite_db_exist def test_error_if_table_not_given(self): with self.assertRaises(ValueError): @@ -350,6 +357,7 @@ def test_error_if_table_not_given(self): self.assertEqual(inst.table, self.table) + @sqlite_db_exist async def test__get_payload(self): """Tests that Cosmos payload retrieved."""