Skip to content

Commit 05fa659

Browse files
committed
Refactored import and tests
1 parent 8b8b4e3 commit 05fa659

File tree

5 files changed

+68
-42
lines changed

5 files changed

+68
-42
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from .module import get_module_config_file, get_module_compose_file, get_module_linked_resource_file

src/appslab_modules/core/module.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import os
2+
3+
config_file_name = "module_config.yaml"
4+
compose_config_file_name = "module_compose.yaml"
5+
6+
def get_module_config_file(cls) -> str|None:
7+
"""Gets the full path of the module_config.yaml file."""
8+
return get_module_linked_resource_file(cls, config_file_name)
9+
10+
def get_module_compose_file(cls) -> str|None:
11+
"""Gets the full path of the module_compose.yaml file, if present."""
12+
return get_module_linked_resource_file(cls, compose_config_file_name)
13+
14+
def get_module_linked_resource_file(cls, resource_file_name) -> str|None:
15+
"""Gets the full path to a config file in the directory containing a class."""
16+
try:
17+
module = cls.__module__
18+
if module == '__main__':
19+
directory_path = os.path.dirname(os.path.abspath(__file__))
20+
else:
21+
module_obj = __import__(module, fromlist=['__file__'])
22+
file_path = os.path.abspath(module_obj.__file__)
23+
directory_path = os.path.dirname(file_path)
24+
25+
requested_path = os.path.join(directory_path, resource_file_name)
26+
if os.path.exists(requested_path):
27+
return requested_path
28+
else:
29+
return None
30+
except AttributeError:
31+
# Handle built-in classes or other cases where __file__ is not available
32+
return None
33+
except ModuleNotFoundError:
34+
return None

src/appslab_modules/db_storage/database_storage.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
from influxdb_client import InfluxDBClient, Point, WritePrecision
22
from influxdb_client.client.write_api import SYNCHRONOUS
3+
from appslab_modules.core.module import get_module_compose_file
34
import yaml
45
import time
6+
import logging
7+
8+
logger = logging.getLogger(__name__)
59

610
class _InfluxPersistence:
711
def __init__(self, host:str = "db_storage", port:int = 8086):
@@ -17,9 +21,14 @@ def __init__(self, host:str = "db_storage", port:int = 8086):
1721
self._connect()
1822

1923
def load_default_infra(self):
20-
with open("module_compose.yaml", "r") as file:
21-
config = yaml.safe_load(file)
22-
return config
24+
pathfile = get_module_compose_file(self.__class__)
25+
if pathfile:
26+
with open(pathfile) as f:
27+
compose_content = yaml.safe_load(f)
28+
logger.info(f"Loading compose file: {compose_content}")
29+
return compose_content
30+
else:
31+
logger.error("Error: Could not find module_compose.yaml")
2332

2433
def _connect(self):
2534
try:

tests/XXtest_database_storage.py

Lines changed: 0 additions & 37 deletions
This file was deleted.

tests/test_database_storage.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import pytest
22
from appslab_modules.db_storage import DatabasePersistence, DatabaseRetrieval # Assuming this is in database_storage.py
3+
import time
34

45
@pytest.fixture
56
def open_influx_database():
@@ -14,10 +15,28 @@ def open_influx_database():
1415

1516
def test_write_and_read_string(open_influx_database):
1617
"""Test writing and reading a string sample."""
17-
18+
1819
db, dbread = open_influx_database
1920

2021
db.write_sample("test_measurement_str", "test_string")
22+
time.sleep(1)
2123
val = dbread.read_last_sample("test_measurement_str")
2224
assert val[2] == "test_string"
23-
assert val[0] == "test_measurement_str"
25+
assert val[0] == "test_measurement_str"
26+
27+
def test_write_and_read_integer(open_influx_database):
28+
"""Test writing and reading an integer sample."""
29+
30+
db, dbread = open_influx_database
31+
32+
db.write_sample("test_measurement", 1111)
33+
db.write_sample("test_measurement", 1234)
34+
time.sleep(1)
35+
val = dbread.read_last_sample("test_measurement")
36+
assert val[2] == 1234
37+
38+
def test_read_nonexistent_measurement(open_influx_database):
39+
"""Test reading a non-existent measurement."""
40+
db, dbread = open_influx_database
41+
val = dbread.read_last_sample("nonexistent_measurement")
42+
assert val == None

0 commit comments

Comments
 (0)