Browse Source

Replace self.raise() with self.fail() + self.contain() in tests

tags/nilmtools-2.0.0
Jim Paris 3 years ago
parent
commit
b1f2a3c5d5
1 changed files with 16 additions and 26 deletions
  1. +16
    -26
      tests/test.py

+ 16
- 26
tests/test.py View File

@@ -88,6 +88,9 @@ class CommandTester():
sys.exit(0) sys.exit(0)
except SystemExit as e: except SystemExit as e:
exitcode = e.code exitcode = e.code
except Exception as e:
traceback.print_exc()
exitcode = 1


# Capture raw binary output, and also try to decode a Unicode # Capture raw binary output, and also try to decode a Unicode
# string copy. # string copy.
@@ -106,19 +109,7 @@ class CommandTester():
self.dump() self.dump()
eq_(self.exitcode, 0) eq_(self.exitcode, 0)


def raises(self, exception, arg_string, infile=None):
try:
self.run(arg_string, infile)
except exception as e:
self.captured = f"Got correct exception: {e!r}\n"
self.captured_binary = b""
return
self.dump()
raise AssertionError(f"Didn't raise exception {exception}")

def fail(self, arg_string, infile=None,
exitcode=None, require_error=True,
exception=None):
def fail(self, arg_string, infile=None, exitcode=None):
self.run(arg_string, infile) self.run(arg_string, infile)
if exitcode is not None and self.exitcode != exitcode: if exitcode is not None and self.exitcode != exitcode:
# Wrong exit code # Wrong exit code
@@ -128,15 +119,12 @@ class CommandTester():
# Success, when we wanted failure # Success, when we wanted failure
self.dump() self.dump()
ne_(self.exitcode, 0) ne_(self.exitcode, 0)
# Make sure the output contains the word "error" at the
# beginning of a line, if requested
if require_error and not re.search("^(?:test_runner: )error",
self.captured, re.MULTILINE):
raise AssertionError("command failed, but output doesn't "
"contain the string 'error'")


def contain(self, checkstring):
in_(checkstring, self.captured)
def contain(self, checkstring, contain=True):
if contain:
in_(checkstring, self.captured)
else:
nin_(checkstring, self.captured)


def match(self, checkstring): def match(self, checkstring):
eq_(checkstring, self.captured) eq_(checkstring, self.captured)
@@ -187,7 +175,7 @@ class TestAllCommands(CommandTester):
data = nilmdb.utils.timestamper.TimestamperRate(fn, start, 120) data = nilmdb.utils.timestamper.TimestamperRate(fn, start, 120)
client.stream_insert("/newton/prep", data); client.stream_insert("/newton/prep", data);


def test_copy(self):
def test_01_copy(self):
self.main = nilmtools.copy_one.main self.main = nilmtools.copy_one.main


client = nilmdb.client.Client(url=self.url) client = nilmdb.client.Client(url=self.url)
@@ -196,16 +184,18 @@ class TestAllCommands(CommandTester):


# basic arguments # basic arguments
self.fail(f"") self.fail(f"")
self.raises(ArgumentError, f"no-such-src no-such-dest")
self.raises(ArgumentError, f"-u {self.url} no-such-src no-such-dest")
self.fail(f"no-such-src no-such-dest")
self.contain("source path no-such-src not found")
self.fail(f"-u {self.url} no-such-src no-such-dest")


# nonexistent dest # nonexistent dest
self.fail(f"/newton/prep /newton/prep-copy", require_error=False)
self.fail(f"/newton/prep /newton/prep-copy")
self.contain("Destination /newton/prep-copy doesn't exist") self.contain("Destination /newton/prep-copy doesn't exist")


# wrong type # wrong type
client.stream_create("/newton/prep-copy-wrongtype", "uint16_6") client.stream_create("/newton/prep-copy-wrongtype", "uint16_6")
self.raises(ValueError, f"/newton/prep /newton/prep-copy-wrongtype")
self.fail(f"/newton/prep /newton/prep-copy-wrongtype")
self.contain("wrong number of fields")


# copy with metadata, and compare # copy with metadata, and compare
client.stream_create("/newton/prep-copy", "float32_8") client.stream_create("/newton/prep-copy", "float32_8")


Loading…
Cancel
Save