Skip to content

Commit

Permalink
Increase export_eds() test coverage (#474)
Browse files Browse the repository at this point in the history
* Test export_eds() to stdout
* Use StringIO as a context manager iso. addCleanup
* Expand the test to cover most paths
* Expand test for unknown doctypes
* Use subTest for better debugging
  • Loading branch information
erlend-aasland authored Jul 2, 2024
1 parent 028a57f commit e3af0eb
Showing 1 changed file with 73 additions and 30 deletions.
103 changes: 73 additions & 30 deletions test/test_eds.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,37 +183,80 @@ def test_comments(self):
|-------------|
""".strip())

def test_export_eds(self):
def test_export_eds_to_file(self):
import tempfile
from pathlib import Path
with tempfile.TemporaryDirectory() as tempdir:
for doctype in {"eds", "dcf"}:

# Test export_od with file object
tempfile = str(Path(tempdir, "test." + doctype))
with open(tempfile, "w+") as tempeds:
print(f"exporting {doctype} to {tempeds.name}")
canopen.export_od(self.od, tempeds, doc_type=doctype)
self.verify_od(tempfile, doctype)

# Test export_od handling opening and closing the file
tempfile = str(Path(tempdir, "test2." + doctype))
canopen.export_od(self.od, tempfile, doc_type=doctype)
self.verify_od(tempfile, doctype)

# Test for unknown doctype
with self.assertRaisesRegex(ValueError, "'unknown'"):
tempfile = str(Path(tempdir, "test.unknown"))
canopen.export_od(self.od, tempfile, doc_type="unknown")

# Omit doctype
tempfile = str(Path(tempdir, "test.eds"))
canopen.export_od(self.od, tempfile)
self.verify_od(tempfile, "eds")


def verify_od(self, tempfile, doctype):
exported_od = canopen.import_od(tempfile)
for suffix in "eds", "dcf":
for implicit in True, False:
with tempfile.NamedTemporaryFile() as fn:
dest = f"{fn.name}.{suffix}"
doctype = None if implicit else suffix
with self.subTest(dest=dest, doctype=doctype):
canopen.export_od(self.od, dest, doctype)
self.verify_od(dest, doctype)

def test_export_eds_to_file_unknown_extension(self):
import io
import tempfile
for suffix in ".txt", "":
with tempfile.NamedTemporaryFile() as fn:
dest = f"{fn.name}{suffix}"
with self.subTest(dest=dest, doctype=None):
canopen.export_od(self.od, dest)

# The import_od() API has some shortcomings compared to the
# export_od() API, namely that it does not take a doctype
# parameter. This means it has to be able to deduce the
# doctype from its 'source' parameter. In this case, this
# is not possible, since we're using an unknown extension,
# so we have to do a couple of tricks in order to make this
# work.
with open(dest, "r") as source:
data = source.read()
with io.StringIO() as buf:
buf.write(data)
buf.seek(io.SEEK_SET)
buf.name = "mock.eds"
self.verify_od(buf, "eds")

def test_export_eds_unknown_doctype(self):
import io
filelike_object = io.StringIO()
self.addCleanup(filelike_object.close)
for dest in "filename", None, filelike_object:
with self.subTest(dest=dest):
with self.assertRaisesRegex(ValueError, "'unknown'"):
canopen.export_od(self.od, dest, doc_type="unknown")

def test_export_eds_to_filelike_object(self):
import io
for doctype in "eds", "dcf":
with io.StringIO() as dest:
with self.subTest(dest=dest, doctype=doctype):
canopen.export_od(self.od, dest, doctype)

# The import_od() API assumes the file-like object has a
# well-behaved 'name' member.
dest.name = f"mock.{doctype}"
dest.seek(io.SEEK_SET)
self.verify_od(dest, doctype)

def test_export_eds_to_stdout(self):
import contextlib
import io
with contextlib.redirect_stdout(io.StringIO()) as f:
ret = canopen.export_od(self.od, None, "eds")
self.assertIsNone(ret)

dump = f.getvalue()
with io.StringIO(dump) as buf:
# The import_od() API assumes the TextIO object has a well-behaved
# 'name' member.
buf.name = "mock.eds"
self.verify_od(buf, "eds")


def verify_od(self, source, doctype):
exported_od = canopen.import_od(source)

for index in exported_od:
self.assertIn(exported_od[index].name, self.od)
Expand Down

0 comments on commit e3af0eb

Please sign in to comment.