Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
enekomartinmartinez committed Jul 17, 2024
1 parent 3e2ecf5 commit cc95fae
Show file tree
Hide file tree
Showing 8 changed files with 446 additions and 19 deletions.
17 changes: 5 additions & 12 deletions pysd/py_backend/external.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
from .lookups import Lookups


_SPREADSHEET_EXTS = {'.xls', '.xlsx', '.xlsm', '.xlsb', '.odf', '.ods', '.odt'}


class Excels():
"""
Class to save the read Excel files and thus avoid double reading
Expand All @@ -36,20 +39,16 @@ def read(cls, file_name, tab):
# get the function to read the data based on its extension
read_kwargs = {}
ext = file_name.suffix.lower()
if ext in ['.xls', '.xlsx', '.xlsm', 'xlsb', 'odf', 'ods', 'odt']:
if ext in _SPREADSHEET_EXTS:
read_func = pd.read_excel
read_kwargs['sheet_name'] = tab
elif ext == '.csv':
# TODO test
read_func = pd.read_csv
if tab and not tab[0].isalnum():
# TODO test
read_kwargs['sep'] = tab
else:
# TODO test
read_func = pd.read_table
if tab and not tab[0].isalnum():
# TODO test
read_kwargs['sep'] = tab
# read the data
excel = np.array([
Expand Down Expand Up @@ -177,7 +176,6 @@ def _get_data_from_file_opyxl(self, cellname):
try:
excel = Excels.read_opyxl(self.file)
except InvalidFileException:
# TODO test
raise ValueError(
self.py_name + "\n"
f"Cannot read the file '{self.file}'...\n"
Expand Down Expand Up @@ -1084,20 +1082,16 @@ def get_subscripts_cell(self, row_first, col_first, lastcell):

# get the function to read the data based on its extension
ext = self.file.suffix.lower()
if ext in ['.xls', '.xlsx', '.xlsm', 'xlsb', 'odf', 'ods', 'odt']:
if ext in _SPREADSHEET_EXTS:
read_func = pd.read_excel
read_kwargs['sheet_name'] = self.tab
elif ext == '.csv':
# TODO test
read_func = pd.read_csv
if self.tab and not self.tab[0].isalnum():
# TODO test
read_kwargs['sep'] = self.tab
else:
# TODO test
read_func = pd.read_table
if self.tab and not self.tab[0].isalnum():
# TODO test
read_kwargs['sep'] = self.tab

# read the data
Expand All @@ -1120,7 +1114,6 @@ def get_subscripts_name(self, cellname):
try:
excel = load_workbook(self.file, read_only=True, data_only=True)
except InvalidFileException:
# TODO test
raise ValueError(
self.py_name + "\n"
f"Cannot read the file '{self.file}'...\n"
Expand Down
1 change: 0 additions & 1 deletion pysd/py_backend/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,6 @@ def initialize_external_data(self, externals=None):
except ValueError: # pragma: no cover
raise ModuleNotFoundError("No module named 'netCDF4'")


for ext in self._external_elements:
if ext.py_name in ds.data_vars.keys():
# Initialize external from nc file
Expand Down
6 changes: 5 additions & 1 deletion tests/data/input.csv
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
1,2,3,4,5,6,7,8,9,10,
,1,2,3,4,5,6,7,8,9,10,,
A,0,0,1,1,-1,-1,0,0,,,
B,0,1,1,-1,-1,0,0,0,,,
C,1,1,-1,-1,0,0,0,0,,,
,,,,,,,,,,,,
5 changes: 5 additions & 0 deletions tests/data/input.tab
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
1 2 3 4 5 6 7 8 9 10
A 0 0 1 1 -1 -1 0 0
B 0 1 1 -1 -1 0 0 0
C 1 1 -1 -1 0 0 0 0

5 changes: 5 additions & 0 deletions tests/data/input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
=1=2=3=4=5=6=7=8=9=10==
A=0=0=1=1=-1=-1=0=0===
B=0=1=1=-1=-1=0=0=0===
C=1=1=-1=-1=0=0=0=0===
============
5 changes: 5 additions & 0 deletions tests/data/input2.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
;1;2;3;4;5;6;7;8;9;10;;
A;0;0;1;1;-1;-1;0;0;;;
B;0;1;1;-1;-1;0;0;0;;;
C;1;1;-1;-1;0;0;0;0;;;
;;;;;;;;;;;;
23 changes: 18 additions & 5 deletions tests/pytest_builders/pytest_python.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,19 +190,32 @@ def test_referencebuilder_subscripts_nowarning(self, component,

class TestSubscriptManager:
@pytest.mark.parametrize(
"arguments,raise_type,error_message",
"asrs,raise_type,error_message",
[
( # invalid definition
[[AbstractSubscriptRange("my subs", 5, [])], Path("here")],
[AbstractSubscriptRange("my subs", 5, [])],
ValueError,
"Invalid definition of subscript 'my subs':\n\t5"
),
( # empty range exts
[AbstractSubscriptRange("my subs", dict(
file="data/input.csv",
tab="",
firstcell="A5",
lastcell="5",
prefix="sr"
), [])],
ValueError,
"Subscript range 'my subs' empty:\n\t"
"{'file': 'data/input.csv', 'tab': '', "
"'firstcell': 'A5', 'lastcell': '5', 'prefix': 'sr'}"
),
],
ids=["invalid definition"]
ids=["invalid definition", "empty range exts"]
)
def test_invalid_subscripts(self, arguments, raise_type, error_message):
def test_invalid_subscripts(self, asrs, raise_type, error_message, _root):
with pytest.raises(raise_type, match=error_message):
SubscriptManager(*arguments)
SubscriptManager(asrs, _root)


class TestNamespaceManager:
Expand Down
Loading

0 comments on commit cc95fae

Please sign in to comment.