Skip to content

Commit

Permalink
fix NamedTemporaryFile tests for Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
trevorb1 committed Oct 23, 2023
1 parent 6066f34 commit ad87fd2
Show file tree
Hide file tree
Showing 4 changed files with 171 additions and 102 deletions.
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)
60 changes: 36 additions & 24 deletions tests/test_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,21 +59,32 @@ class TestWrite:
def test_write_datafile(self):
"""Test writing data to a file"""
data = {"REGION": pd.DataFrame({"VALUE": ["BB"]})}
temp = NamedTemporaryFile()
assert write(
os.path.join("tests", "fixtures", "config.yaml"),
"datafile",
temp.name,
data,
)
temp = NamedTemporaryFile(delete=False, mode="w")
try:
assert write(
os.path.join("tests", "fixtures", "config.yaml"),
"datafile",
temp.name,
data,
)
finally:
temp.close()
os.unlink(temp.name)

def test_write_excel(self):
"""Test writing data to an Excel file"""
data = {"REGION": pd.DataFrame({"VALUE": ["BB"]})}
temp = NamedTemporaryFile(suffix=".xlsx")
assert write(
os.path.join("tests", "fixtures", "config.yaml"), "excel", temp.name, data
)
temp = NamedTemporaryFile(suffix=".xlsx", delete=False, mode="w")
try:
assert write(
os.path.join("tests", "fixtures", "config.yaml"),
"excel",
temp.name,
data,
)
finally:
temp.close()
os.unlink(temp.name)

def test_write_csv(self):
"""Test writing data to a CSV file"""
Expand All @@ -92,21 +103,22 @@ class TestConvert:

def test_convert_excel_to_datafile(self):
"""Test converting from Excel to datafile"""

user_config = os.path.join("tests", "fixtures", "config.yaml")
tmpfile = NamedTemporaryFile()
from_path = os.path.join("tests", "fixtures", "combined_inputs.xlsx")

convert(user_config, "excel", "datafile", from_path, tmpfile.name)

tmpfile.seek(0)
actual = tmpfile.readlines()
tmpfile.close()

assert actual[-1] == b"end;\n"
assert actual[0] == b"# Model file written by *otoole*\n"
assert actual[2] == b"09_ROK d_bld_2_coal_products 2017 20.8921\n"
assert actual[8996] == b"param default 1 : DepreciationMethod :=\n"
tmpfile = NamedTemporaryFile(delete=False, mode="w+b")

try:
convert(user_config, "excel", "datafile", from_path, tmpfile.name)
tmpfile.seek(0)
actual = tmpfile.readlines()

assert actual[-1] == b"end;\n"
assert actual[0] == b"# Model file written by *otoole*\n"
assert actual[2] == b"09_ROK d_bld_2_coal_products 2017 20.8921\n"
assert actual[8996] == b"param default 1 : DepreciationMethod :=\n"
finally:
tmpfile.close()
os.unlink(tmpfile.name)

def test_convert_excel_to_csv(self):
"""Test converting from Excel to CSV"""
Expand Down
23 changes: 13 additions & 10 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
from tempfile import NamedTemporaryFile

import pandas as pd
Expand Down Expand Up @@ -77,16 +78,18 @@ def test_create_name_mappings_reversed(self, user_config):
def test_excel_name_length_error(user_config_simple, request):
user_config = request.getfixturevalue(user_config_simple)
write_excel = WriteExcel(user_config=user_config)
temp_excel = NamedTemporaryFile(suffix=".xlsx")
handle = pd.ExcelWriter(temp_excel.name)

with pytest.raises(OtooleExcelNameLengthError):
write_excel._write_parameter(
df=pd.DataFrame(),
parameter_name="ParameterNameLongerThanThirtyOneChars",
handle=pd.ExcelWriter(handle),
default=0,
)
temp_excel = NamedTemporaryFile(suffix=".xlsx", delete=False, mode="r")
try:
with pytest.raises(OtooleExcelNameLengthError):
write_excel._write_parameter(
df=pd.DataFrame(),
parameter_name="ParameterNameLongerThanThirtyOneChars",
handle=pd.ExcelWriter(temp_excel.name),
default=0,
)
finally:
temp_excel.close()
os.unlink(temp_excel.name)


class TestYamlUniqueKeyReader:
Expand Down
20 changes: 13 additions & 7 deletions tests/test_write_strategies.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import io
import os
from tempfile import NamedTemporaryFile

import pandas as pd
Expand Down Expand Up @@ -114,15 +115,20 @@ def test_form_no_pivot(self, user_config):

def test_write_out_empty_dataframe(self, user_config):

temp_excel = NamedTemporaryFile(suffix=".xlsx")
handle = pd.ExcelWriter(temp_excel.name)
convert = WriteExcel(user_config)
temp_excel = NamedTemporaryFile(suffix=".xlsx", delete=False, mode="w")
try:
handle = pd.ExcelWriter(temp_excel.name)
convert = WriteExcel(user_config)

df = pd.DataFrame(
data=None, columns=["REGION", "TECHNOLOGY", "YEAR", "VALUE"]
).set_index(["REGION", "TECHNOLOGY", "YEAR"])
df = pd.DataFrame(
data=None, columns=["REGION", "TECHNOLOGY", "YEAR", "VALUE"]
).set_index(["REGION", "TECHNOLOGY", "YEAR"])

convert._write_parameter(df, "AvailabilityFactor", handle, default=0)
convert._write_parameter(df, "AvailabilityFactor", handle, default=0)
finally:
handle.close()
temp_excel.close()
os.unlink(temp_excel.name)


class TestWriteDatafile:
Expand Down

0 comments on commit ad87fd2

Please sign in to comment.