Commit 0f25f17e authored by Samuel GAIST's avatar Samuel GAIST
Browse files

[test][test_execution] Code cleanup

parent 1d4ab56a
......@@ -39,11 +39,7 @@
import os
import glob
import logging
logger = logging.getLogger(__name__)
import numpy
import unittest
import nose.tools
from ..experiment import Experiment
from ..execution import LocalExecutor
......@@ -54,11 +50,10 @@ from ..hash import toPath
from ..data import CachedDataSource
from . import prefix, tmp_prefix
from .utils import cleanup
from .utils import slow
import nose.tools
logger = logging.getLogger(__name__)
# ----------------------------------------------------------
......@@ -74,11 +69,11 @@ class BaseExecutionMixIn(object):
indexfiles = glob.glob(finalpath + "*.index")
indexchksums = glob.glob(finalpath + "*.index.checksum")
assert datafiles
nose.tools.assert_true(datafiles)
nose.tools.eq_(len(datafiles), len(indexfiles))
for k in datafiles + indexfiles:
checksum_file = k + ".checksum"
assert checksum_file in datachksums + indexchksums
nose.tools.assert_true(checksum_file in datachksums + indexchksums)
stored_checksum = None
with open(checksum_file, "rt") as f:
stored_checksum = f.read().strip()
......@@ -89,13 +84,16 @@ class BaseExecutionMixIn(object):
"""Loads the result of an experiment, in a single go"""
f = CachedDataSource()
assert f.setup(
os.path.join(executor.cache, executor.data["result"]["path"] + ".data"),
executor.prefix,
nose.tools.assert_true(
f.setup(
os.path.join(executor.cache, executor.data["result"]["path"] + ".data"),
executor.prefix,
)
)
data, start, end = f[0]
nose.tools.eq_(start, 0)
assert end >= start
nose.tools.assert_true(end >= start)
f.close()
return data
......@@ -116,7 +114,9 @@ class BaseExecutionMixIn(object):
prefix, label, dataformat_cache, database_cache, algorithm_cache
)
assert experiment.valid, "\n * %s" % "\n * ".join(experiment.errors)
nose.tools.assert_true(
experiment.valid, "\n * %s" % "\n * ".join(experiment.errors)
)
for block_name, infos in experiment.datasets.items():
view = infos["database"].view(infos["protocol"], infos["set"])
......@@ -141,17 +141,19 @@ class BaseExecutionMixIn(object):
database_cache,
algorithm_cache,
)
assert executor.valid, "\n * %s" % "\n * ".join(executor.errors)
nose.tools.assert_true(
executor.valid, "\n * %s" % "\n * ".join(executor.errors)
)
with executor:
result = executor.process(timeout_in_minutes=3)
assert result
assert "status" in result
assert "stdout" in result
assert "stderr" in result
assert "timed_out" in result
assert "system_error" in result
assert "user_error" in result
nose.tools.assert_true(result)
nose.tools.assert_true("status" in result)
nose.tools.assert_true("stdout" in result)
nose.tools.assert_true("stderr" in result)
nose.tools.assert_true("timed_out" in result)
nose.tools.assert_true("system_error" in result)
nose.tools.assert_true("user_error" in result)
if result["status"] != 0:
logger.warn("status: %i", result["status"])
logger.warn("(eventual) system errors: %s", result["system_error"])
......@@ -162,10 +164,10 @@ class BaseExecutionMixIn(object):
if result["system_error"]:
logger.warn("system errors: %s", result["system_error"])
return result
assert result["status"] == 0
nose.tools.eq_(result["status"], 0)
if "statistics" in result:
assert isinstance(result["statistics"], dict)
nose.tools.assert_true(isinstance(result["statistics"], dict))
if executor.analysis:
self.check_output(tmp_prefix, executor.data["result"]["path"])
......@@ -175,177 +177,179 @@ class BaseExecutionMixIn(object):
self.check_output(tmp_prefix, details["path"])
# compares all results
assert results
nose.tools.assert_true(results)
for k, result in enumerate(results):
expected = result.__class__()
expected.from_dict(expected_result[k], casting="unsafe") # defaults=False
assert result.isclose(expected), "%r is not close enough to %r" % (
result.as_dict(),
expected.as_dict(),
nose.tools.assert_true(
result.isclose(expected),
"%r is not close enough to %r" % (result.as_dict(), expected.as_dict()),
)
@slow
def test_integers_addition_1(self):
assert (
nose.tools.assert_is_none(
self.execute(
"user/user/integers_addition/1/integers_addition",
[{"sum": 495, "nb": 9}],
)
is None
)
@slow
def test_integers_addition_2(self):
assert (
nose.tools.assert_is_none(
self.execute(
"user/user/integers_addition/2/integers_addition",
[{"sum": 4995, "nb": 9}],
)
is None
)
@slow
def test_single_1_single(self):
assert self.execute("user/user/single/1/single", [{"out_data": 42}]) is None
nose.tools.assert_is_none(
self.execute("user/user/single/1/single", [{"out_data": 42}])
)
@slow
def test_single_1_add(self):
assert self.execute("user/user/single/1/single_add", [{"out_data": 43}]) is None
nose.tools.assert_is_none(
self.execute("user/user/single/1/single_add", [{"out_data": 43}])
)
@slow
def test_single_1_add2(self):
assert (
self.execute("user/user/single/1/single_add2", [{"out_data": 44}]) is None
nose.tools.assert_is_none(
self.execute("user/user/single/1/single_add2", [{"out_data": 44}])
)
@slow
def test_single_1_error(self):
result = self.execute("user/user/single/1/single_error", [None])
assert result
nose.tools.assert_true(result)
nose.tools.eq_(result["status"], 1)
assert result["user_error"]
assert "NameError" in result["user_error"]
nose.tools.assert_true(result["user_error"])
nose.tools.assert_true("NameError" in result["user_error"])
nose.tools.eq_(result["system_error"], "")
@slow
def test_single_1_crash(self):
result = self.execute("user/user/single/1/single_crash", [None])
assert result
nose.tools.assert_true(result)
nose.tools.eq_(result["status"], 1)
assert result["user_error"]
assert "NameError" in result["user_error"]
nose.tools.assert_true(result["user_error"])
nose.tools.assert_true("NameError" in result["user_error"])
nose.tools.eq_(result["system_error"], "")
@slow
def test_single_1_db_crash(self):
result = self.execute("user/user/single/1/single_db_crash", [None])
assert result
assert result["status"] != 0
assert result["user_error"]
assert "a = b" in result["user_error"]
nose.tools.assert_true(result)
nose.tools.assert_not_equal(result["status"], 0)
nose.tools.assert_true(result["user_error"])
nose.tools.assert_true("a = b" in result["user_error"])
nose.tools.eq_(result["system_error"], "")
@slow
def test_single_1_large(self):
assert (
self.execute("user/user/single/1/single_large", [{"out_data": 2.0}]) is None
nose.tools.assert_is_none(
self.execute("user/user/single/1/single_large", [{"out_data": 2.0}])
)
@slow
def test_double_1(self):
assert self.execute("user/user/double/1/double", [{"out_data": 42}]) is None
nose.tools.assert_is_none(
self.execute("user/user/double/1/double", [{"out_data": 42}])
)
@slow
def test_triangle_1(self):
assert self.execute("user/user/triangle/1/triangle", [{"out_data": 42}]) is None
nose.tools.assert_is_none(
self.execute("user/user/triangle/1/triangle", [{"out_data": 42}])
)
@slow
def test_too_many_nexts(self):
result = self.execute("user/user/triangle/1/too_many_nexts", [None])
assert result
assert result["status"] != 0
assert result["user_error"]
assert "no more data" in result["user_error"]
nose.tools.assert_true(result)
nose.tools.assert_not_equal(result["status"], 0)
nose.tools.assert_true(result["user_error"])
nose.tools.assert_true("no more data" in result["user_error"])
@slow
def test_double_triangle_1(self):
assert (
nose.tools.assert_is_none(
self.execute(
"user/user/double_triangle/1/double_triangle", [{"out_data": 42}]
)
is None
)
@slow
def test_inputs_mix_1(self):
assert (
self.execute("user/user/inputs_mix/1/test", [{"sum": 495, "nb": 9}]) is None
nose.tools.assert_is_none(
self.execute("user/user/inputs_mix/1/test", [{"sum": 495, "nb": 9}])
)
@slow
def test_inputs_mix_2(self):
assert (
self.execute("user/user/inputs_mix/2/test", [{"sum": 495, "nb": 9}]) is None
nose.tools.assert_is_none(
self.execute("user/user/inputs_mix/2/test", [{"sum": 495, "nb": 9}])
)
@slow
def test_inputs_mix_3(self):
assert (
self.execute("user/user/inputs_mix/3/test", [{"sum": 945, "nb": 9}]) is None
nose.tools.assert_is_none(
self.execute("user/user/inputs_mix/3/test", [{"sum": 945, "nb": 9}])
)
@slow
def test_inputs_mix_3b(self):
assert (
nose.tools.assert_is_none(
self.execute("user/user/inputs_mix/3/test2", [{"sum": 954, "nb": 9}])
is None
)
@slow
def test_inputs_mix_4(self):
assert (
self.execute("user/user/inputs_mix/4/test", [{"sum": 990, "nb": 9}]) is None
nose.tools.assert_is_none(
self.execute("user/user/inputs_mix/4/test", [{"sum": 990, "nb": 9}])
)
@slow
def test_inputs_mix_4b(self):
assert (
nose.tools.assert_is_none(
self.execute("user/user/inputs_mix/4/test2", [{"sum": 1008, "nb": 9}])
is None
)
@slow
def test_integers_labelled_1(self):
assert (
nose.tools.assert_is_none(
self.execute(
"user/user/integers_labelled/1/test",
[{"nb_data_units": 3, "indices": "0 - 4\n5 - 9\n10 - 14\n"}],
)
is None
)
@slow
def test_preprocessing_1(self):
assert (
nose.tools.assert_is_none(
self.execute(
"user/user/preprocessing/1/different_frequencies",
[{"sum": 363, "nb": 8}],
)
is None
)
@slow
def test_single_1_prepare_success(self):
assert (
nose.tools.assert_is_none(
self.execute("user/user/single/1/prepare_success", [{"out_data": 42}])
is None
)
@slow
def test_loop_1(self):
assert self.execute("user/user/loop/1/loop", [{"sum": 504, "nb": 9}]) is None
nose.tools.assert_is_none(
self.execute("user/user/loop/1/loop", [{"sum": 504, "nb": 9}])
)
# For benchmark purposes
# @slow
......@@ -389,16 +393,14 @@ class TestLocalExecution(BaseExecutionMixIn):
@slow
def test_single_1_prepare_error(self):
with nose.tools.assert_raises(RuntimeError) as context:
result = self.execute("user/user/single/1/prepare_error", [None])
assert "Algorithm prepare failed" in context.exception
self.execute("user/user/single/1/prepare_error", [None])
nose.tools.assert_true("Algorithm prepare failed" in context.exception)
@slow
def test_single_1_setup_error(self):
with nose.tools.assert_raises(RuntimeError) as context:
result = self.execute("user/user/single/1/setup_error", [None])
assert "Algorithm setup failed" in context.exception
self.execute("user/user/single/1/setup_error", [None])
nose.tools.assert_true("Algorithm setup failed" in context.exception)
# ----------------------------------------------------------
......@@ -427,12 +429,16 @@ class TestSubprocessExecution(BaseExecutionMixIn):
def test_single_1_prepare_error(self):
result = self.execute("user/user/single/1/prepare_error", [None])
assert result["status"] == 1
assert result["user_error"] == "'Could not prepare algorithm (returned False)'"
nose.tools.eq_(result["status"], 1)
nose.tools.eq_(
result["user_error"], "'Could not prepare algorithm (returned False)'"
)
@slow
def test_single_1_setup_error(self):
result = self.execute("user/user/single/1/setup_error", [None])
assert result["status"] == 1
assert result["user_error"] == "'Could not setup algorithm (returned False)'"
nose.tools.eq_(result["status"], 1)
nose.tools.eq_(
result["user_error"], "'Could not setup algorithm (returned False)'"
)
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment