Skip to content

Commit

Permalink
Updated tests to pass
Browse files Browse the repository at this point in the history
  • Loading branch information
lewis-chambers committed Jun 19, 2024
1 parent 3dcf31a commit 7456005
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 16 deletions.
6 changes: 1 addition & 5 deletions src/iotswarm/scripts/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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__)

Expand Down
5 changes: 1 addition & 4 deletions src/tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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': <Logger iotswarm.scripts.cli (INFO)>}\n"
)
assert result.output == "{'logger': <Logger iotswarm.scripts.cli (INFO)>}\n"


@parameterized.expand(["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"])
Expand Down
23 changes: 18 additions & 5 deletions src/tests/test_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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"
Expand All @@ -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)
Expand All @@ -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"]

Expand All @@ -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"

Expand All @@ -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"))

Expand All @@ -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."""

Expand All @@ -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."""

Expand Down
12 changes: 10 additions & 2 deletions src/tests/test_devices.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand All @@ -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."""

Expand Down

0 comments on commit 7456005

Please sign in to comment.