Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Tests on Windows #203

Merged
merged 11 commits into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .github/workflows/python.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ on: [push, pull_request]
jobs:
build:

runs-on: ubuntu-latest
runs-on: ${{ matrix.os }}
strategy:
matrix:
python-version: ["3.9", "3.10", "3.11"]
os: [ubuntu-latest, windows-latest]

steps:
- uses: actions/checkout@v3
Expand Down
19 changes: 14 additions & 5 deletions src/otoole/input.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ def convert(self, input_filepath: str, output_filepath: str, **kwargs: Dict):

class Strategy(ABC):
"""

Arguments
---------
user_config : dict, default=None
Expand All @@ -139,10 +138,20 @@ def _add_dtypes(self, config: Dict):
dtypes = {}
for column in details["indices"] + ["VALUE"]:
if column == "VALUE":
dtypes["VALUE"] = details["dtype"]
dtypes["VALUE"] = (
details["dtype"] if details["dtype"] != "int" else "int64"
)
else:
dtypes[column] = config[column]["dtype"]
dtypes[column] = (
config[column]["dtype"]
if config[column]["dtype"] != "int"
else "int64"
)
details["index_dtypes"] = dtypes
elif details["type"] == "set":
details["dtype"] = (
details["dtype"] if details["dtype"] != "int" else "int64"
)
return config

@property
Expand Down Expand Up @@ -491,8 +500,8 @@ def _check_index_dtypes(
except ValueError: # ValueError: invalid literal for int() with base 10:
df = df.dropna(axis=0, how="all").reset_index()
for index, dtype in config["index_dtypes"].items():
if dtype == "int":
df[index] = df[index].astype(float).astype(int)
if dtype == "int64":
df[index] = df[index].astype(float).astype("int64")
else:
df[index] = df[index].astype(dtype)
df = df.set_index(config["indices"])
Expand Down
2 changes: 1 addition & 1 deletion src/otoole/results/result_package.py
Original file line number Diff line number Diff line change
Expand Up @@ -858,7 +858,7 @@ def discount_factor(
if regions and years:
discount_rate["YEAR"] = [years]
discount_factor = discount_rate.explode("YEAR").reset_index(level="REGION")
discount_factor["YEAR"] = discount_factor["YEAR"].astype(int)
discount_factor["YEAR"] = discount_factor["YEAR"].astype("int64")
discount_factor["NUM"] = discount_factor["YEAR"] - discount_factor["YEAR"].min()
discount_factor["RATE"] = discount_factor["VALUE"] + 1
discount_factor["VALUE"] = (
Expand Down
4 changes: 2 additions & 2 deletions src/otoole/results/results.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ def read_model(self, file_path: Union[str, TextIO]) -> pd.DataFrame:
df["INDEX"] = df["INDEX"].map(lambda x: x.split("]")[0])
df = (
df[["ID", "NUM", "NAME", "INDEX"]]
.astype({"ID": str, "NUM": int, "NAME": str, "INDEX": str})
.astype({"ID": str, "NUM": "int64", "NAME": str, "INDEX": str})
.reset_index(drop=True)
)

Expand Down Expand Up @@ -425,7 +425,7 @@ def read_solution(
data = (
data[["ID", "NUM", "STATUS", "PRIM", "DUAL"]]
.astype(
{"ID": str, "NUM": int, "STATUS": str, "PRIM": float, "DUAL": float}
{"ID": str, "NUM": "int64", "STATUS": str, "PRIM": float, "DUAL": float}
)
.reset_index(drop=True)
)
Expand Down
14 changes: 12 additions & 2 deletions src/otoole/write_strategies.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,12 @@ def _write_parameter(
df = self._form_parameter(df, default)
handle.write("param default {} : {} :=\n".format(default, parameter_name))
df.to_csv(
path_or_buf=handle, sep=" ", header=False, index=True, float_format="%g"
path_or_buf=handle,
sep=" ",
header=False,
index=True,
float_format="%g",
lineterminator="\n",
)
handle.write(";\n")

Expand All @@ -171,7 +176,12 @@ def _write_set(self, df: pd.DataFrame, set_name, handle: TextIO):
"""
handle.write("set {} :=\n".format(set_name))
df.to_csv(
path_or_buf=handle, sep=" ", header=False, index=False, float_format="%g"
path_or_buf=handle,
sep=" ",
header=False,
index=False,
float_format="%g",
lineterminator="\n",
)
handle.write(";\n")

Expand Down
170 changes: 109 additions & 61 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,48 +41,69 @@ def test_version(self):
result = run(["otoole", "--version"], capture_output=True)
assert result.stdout.strip().decode() == str(__version__)

def test_help(self):
commands = ["otoole", "-v", "convert", "--help"]
expected = "usage: otoole convert [-h]"
actual = run(commands, capture_output=True)
assert expected in str(actual.stdout)
assert actual.returncode == 0, print(actual.stdout)

temp = mkdtemp()
temp_excel = NamedTemporaryFile(suffix=".xlsx")
temp_datafile = NamedTemporaryFile(suffix=".dat")
simplicity = os.path.join("tests", "fixtures", "simplicity.txt")
config_path = os.path.join("tests", "fixtures", "config.yaml")

test_data = [
(["otoole", "-v", "convert", "--help"], "usage: otoole convert [-h]"),
(
"excel",
[
"otoole",
"-v",
"convert",
"datafile",
"excel",
simplicity,
temp_excel.name,
"convert_to_file_path", # replaced with NamedTemporaryFile
config_path,
],
"",
),
(
"datafile",
[
"otoole",
"-v",
"convert",
"datafile",
"datafile",
simplicity,
temp_datafile.name,
"convert_to_file_path", # replaced with NamedTemporaryFile
config_path,
],
"",
),
]

@mark.parametrize("commands,expected", test_data, ids=["help", "excel", "datafile"])
def test_convert_commands(self, commands, expected):
actual = run(commands, capture_output=True)
assert expected in str(actual.stdout)
print(" ".join(commands))
assert actual.returncode == 0, print(actual.stdout)
@mark.parametrize(
"convert_to,commands,expected", test_data, ids=["excel", "datafile"]
)
def test_convert_commands(self, convert_to, commands, expected):
if convert_to == "datafile":
temp = NamedTemporaryFile(suffix=".dat", delete=False, mode="w")
elif convert_to == "excel":
temp = NamedTemporaryFile(suffix=".xlsx", delete=False, mode="w")
else:
raise NotImplementedError
try:
commands_adjusted = [
x if x != "convert_to_file_path" else temp.name for x in commands
]
actual = run(commands_adjusted, capture_output=True)
assert expected in str(actual.stdout)
print(" ".join(commands_adjusted))
assert actual.returncode == 0, print(actual.stdout)
finally:
temp.close()
os.unlink(temp.name)

test_errors = [
(
Expand All @@ -98,87 +119,114 @@ def test_convert_error(self, commands, expected):

def test_convert_datafile_datafile_no_user_config(self):
simplicity = os.path.join("tests", "fixtures", "simplicity.txt")
temp_datafile = NamedTemporaryFile(suffix=".dat")
commands = [
"otoole",
"convert",
"datafile",
"datafile",
simplicity,
temp_datafile.name,
]
actual = run(commands, capture_output=True)
assert actual.returncode == 2
temp_datafile = NamedTemporaryFile(suffix=".dat", delete=False, mode="w")
try:
commands = [
"otoole",
"convert",
"datafile",
"datafile",
simplicity,
temp_datafile.name,
]
actual = run(commands, capture_output=True)
assert actual.returncode == 2
finally:
temp_datafile.close()
os.unlink(temp_datafile.name)

def test_convert_datafile_datafile_with_user_config(self):
simplicity = os.path.join("tests", "fixtures", "simplicity.txt")
user_config = os.path.join("tests", "fixtures", "config.yaml")
temp_datafile = NamedTemporaryFile(suffix=".dat")
commands = [
"otoole",
"-vvv",
"convert",
"datafile",
"datafile",
simplicity,
temp_datafile.name,
user_config,
]
actual = run(commands, capture_output=True)
assert actual.returncode == 0
temp_datafile = NamedTemporaryFile(suffix=".dat", delete=False, mode="w")
try:
commands = [
"otoole",
"-vvv",
"convert",
"datafile",
"datafile",
simplicity,
temp_datafile.name,
user_config,
]
actual = run(commands, capture_output=True)
assert actual.returncode == 0
finally:
temp_datafile.close()
os.unlink(temp_datafile.name)

def test_convert_datafile_datafile_with_default_flag(self):
simplicity = os.path.join("tests", "fixtures", "simplicity.txt")
user_config = os.path.join("tests", "fixtures", "config.yaml")
temp_datafile = NamedTemporaryFile(suffix=".dat")
commands = [
"otoole",
"-vvv",
"convert",
"datafile",
"datafile",
simplicity,
temp_datafile.name,
user_config,
"--write_defaults",
]
actual = run(commands, capture_output=True)
assert actual.returncode == 0
temp_datafile = NamedTemporaryFile(suffix=".dat", delete=False, mode="w")
try:
commands = [
"otoole",
"-vvv",
"convert",
"datafile",
"datafile",
simplicity,
temp_datafile.name,
user_config,
"--write_defaults",
]
actual = run(commands, capture_output=True)
assert actual.returncode == 0
finally:
temp_datafile.close()
os.unlink(temp_datafile.name)


class TestSetup:

temp = mkdtemp()
temp_config = NamedTemporaryFile(suffix=".yaml")

test_data = [
(
[
"otoole",
"-v",
"setup",
"config",
NamedTemporaryFile(suffix=".yaml").name,
NamedTemporaryFile(
suffix=".yaml"
).name, # representes a new config file
],
"",
),
(["otoole", "-v", "setup", "config", temp_config.name, "--overwrite"], ""),
(["otoole", "-v", "setup", "config", "temp_file", "--overwrite"], ""),
]

@mark.parametrize(
"commands,expected", test_data, ids=["setup", "setup_with_overwrite"]
)
def test_setup_commands(self, commands, expected):
actual = run(commands, capture_output=True)
assert expected in str(actual.stdout)
print(" ".join(commands))
assert actual.returncode == 0, print(actual.stdout)
temp_yaml = NamedTemporaryFile(suffix=".yaml", delete=False, mode="w+b")
try:
commands_adjusted = [
x if x != "temp_file" else temp_yaml.name for x in commands
]
actual = run(commands_adjusted, capture_output=True)
assert expected in str(actual.stdout)
print(" ".join(commands_adjusted))
assert actual.returncode == 0, print(actual.stdout)
finally:
temp_yaml.close()
os.unlink(temp_yaml.name)

test_errors = [
(["otoole", "-v", "setup", "config", temp_config.name], "OtooleSetupError"),
(["otoole", "-v", "setup", "config", "temp_file"], "OtooleSetupError"),
]

@mark.parametrize("commands,expected", test_errors, ids=["setup_fails"])
def test_setup_error(self, commands, expected):
actual = run(commands, capture_output=True)
assert expected in str(actual.stderr)
temp_yaml = NamedTemporaryFile(suffix=".yaml", delete=False, mode="w")
try:
commands_adjusted = [
x if x != "temp_file" else temp_yaml.name for x in commands
]
actual = run(commands_adjusted, capture_output=True)
assert expected in str(actual.stderr)
finally:
temp_yaml.close()
os.unlink(temp_yaml.name)
Loading