def test_constructor__requirements(): node = Node(requirements=id) assert_equal(node.requirements, frozenset([id])) node = Node(requirements=[id]) assert_equal(node.requirements, frozenset([id])) node = Node(requirements=[id, str]) assert_equal(node.requirements, frozenset([id, str]))
def test_is_done__subnode_output_changes(temp_folder): temp_file = os.path.join(temp_folder, "file.txt") subnode = Node(output_files = temp_file) my_node = Node(subnodes = subnode) assert not my_node.is_done set_file_contents(temp_file, "foo") assert my_node.is_done
def test_is_outdated__updates(): my_node = Node(input_files = "tests/data/timestamp_a_older", output_files = "tests/data/timestamp_a_younger") assert not my_node.is_outdated my_node = Node(input_files = "tests/data/timestamp_a_younger", output_files = "tests/data/timestamp_a_older") assert my_node.is_outdated
def test_metanode__nodes(): subnodes = [Node(), Node()] dependencies = [Node(), Node()] node = MetaNode(subnodes = iter(subnodes), dependencies = iter(dependencies)) assert_equal(node.subnodes, frozenset(subnodes)) assert_equal(node.dependencies, frozenset(dependencies))
def test_run__exception__create_temp_dir(): cfg_mock = flexmock(temp_root=_DUMMY_TEMP_ROOT) node_mock = flexmock(Node()) node_mock.should_receive('_create_temp_dir').with_args(cfg_mock) \ .and_raise(OSError()).ordered.once assert_raises(NodeUnhandledException, node_mock.run, cfg_mock) # pylint: disable=E1103
def test_function(): node_mock = flexmock(Node()) node_mock.should_receive('_create_temp_dir').with_args(cfg_mock) \ .and_return(_DUMMY_TEMP).ordered.once node_mock.should_receive(key).and_raise(exception).ordered.once node_mock.should_receive('_remove_temp_dir').never assert_raises(expectation, node_mock.run, cfg_mock) # pylint: disable=E1103
def test_run__order(): cfg_mock = flexmock(temp_root = "/tmp") node_mock = flexmock(Node()) node_mock.should_receive("_setup").with_args(cfg_mock, "/tmp/xTMPx").ordered.once node_mock.should_receive("_run").with_args(cfg_mock, "/tmp/xTMPx").ordered.once node_mock.should_receive("_teardown").with_args(cfg_mock, "/tmp/xTMPx").ordered.once with MonkeypatchCreateTempDir(): node_mock.run(cfg_mock) # pylint: disable=E1103
def test_is_done__output_changes(temp_folder): temp_file_1 = os.path.join(temp_folder, "file_1.txt") temp_file_2 = os.path.join(temp_folder, "file_2.txt") my_node = Node(output_files = (temp_file_1, temp_file_2)) assert not my_node.is_done set_file_contents(temp_file_1, "foo") assert not my_node.is_done set_file_contents(temp_file_2, "bar") assert my_node.is_done
def test_run__temp_dirs(): def assert_dir(_, path): assert_equal(path, "/tmp/xTMPx") cfg_mock = flexmock(temp_root = "/tmp") node_mock = flexmock(Node(), _setup = assert_dir, _run = assert_dir, _teardown = assert_dir) with MonkeypatchCreateTempDir(): node_mock.run(cfg_mock) # pylint: disable=E1103
def _do_test_run__error_log__node_error(temp_folder, exception): temp = os.path.join(temp_folder, "xTMPx") cfg_mock = flexmock(temp_root=temp_folder) node_mock = flexmock(Node()) node_mock.should_receive("_create_temp_dir").with_args(cfg_mock) \ .and_return(temp).ordered.once node_mock.should_receive("_run").and_raise(exception).ordered.once os.mkdir(temp) assert_raises(NodeError, node_mock.run, cfg_mock) # pylint: disable=E1103 log_file = os.path.join(temp_folder, "xTMPx", "pipe.errors") assert os.path.exists(log_file) assert_in("Errors =", get_file_contents(log_file))
def test_run__order(): cfg_mock = flexmock(temp_root=_DUMMY_TEMP_ROOT) node_mock = flexmock(Node()) node_mock.should_receive("_create_temp_dir").with_args( cfg_mock).and_return(_DUMMY_TEMP).ordered.once node_mock.should_receive("_setup").with_args(cfg_mock, _DUMMY_TEMP).ordered.once node_mock.should_receive("_run").with_args(cfg_mock, _DUMMY_TEMP).ordered.once node_mock.should_receive("_teardown").with_args(cfg_mock, _DUMMY_TEMP).ordered.once node_mock.should_receive("_remove_temp_dir").with_args( _DUMMY_TEMP).ordered.once node_mock.run(cfg_mock) # pylint: disable=E1103
def _do_test_run__error_log__node_error(temp_folder, exception): cfg_mock = flexmock(temp_root = temp_folder) node_mock = flexmock(Node()) node_mock.should_receive("_run").and_raise(exception).once try: os.mkdir(os.path.join(temp_folder, "xTMPx")) with MonkeypatchCreateTempDir(root = temp_folder, subfolder = "xTMPx"): # pylint: disable=E1103 node_mock.run(cfg_mock) # pragma: no coverage except NodeError: log_file = os.path.join(temp_folder, "xTMPx", "pipe.errors") assert os.path.exists(log_file) assert_in("Errors =", get_file_contents(log_file)) return assert False # pragma: no coverage
def __get_node__(self, func, max_threads=1): """Gets the Node containing the passed function. If no Node exists, one is created and a map record is made :func: The passed function :max_threads: The maximum number of threads for a new node. If a node already exists for the function, this parameter is ignored. :return: A node encapsulating the function """ if func in self.func_node_map: return self.func_node_map[func] else: node = Node(func, self, max_threads=max_threads) self.func_node_map[func] = node return node
def test__teardown__output_files_missing(): node = Node(input_files=_EMPTY_FILE, output_files=_OUT_FILES) assert_raises(NodeError, node._teardown, None, None)
def _do_test__setup__input_files_exist(kwargs): assert_raises(NodeError, Node(**kwargs)._setup, None, None)
def test__teardown__output_files(): Node(input_files=_EMPTY_FILE, output_files=_IN_FILES)._teardown(None, None)
def test_is_done__no_output(): assert Node().is_done
def _do_test__setup__input_files_exist(kwargs): Node(**kwargs)._setup(None, None)
def test_is_outdated__input_but_no_output(): assert not Node(input_files = _IN_FILES).is_outdated
def test_is_outdated__no_output(): assert not Node().is_outdated
def _do_test_constructor__iterable(key): sub_nodes = [Node(), Node()] my_node = Node(**{key: iter(sub_nodes)}) assert_equal(getattr(my_node, key), frozenset(sub_nodes))
def test_is_outdated__output_but_no_input(): assert not Node(output_files = _OUT_FILES).is_outdated
def _do_test_constructor__none_nodes(key): my_node = Node(**{key: None}) assert_equal(getattr(my_node, key), frozenset())
def _do_test_constructor__single_node(key): sub_node = Node() my_node = Node(**{key: sub_node}) assert_equal(getattr(my_node, key), frozenset([sub_node]))
def test_function(): node_mock = flexmock(Node()) node_mock.should_receive(key).and_raise(exception).once with MonkeypatchCreateTempDir(): node_mock.run(cfg_mock) # pylint: disable=E1103
def _do_test_constructor__single_value(key, value): defaults = {"input_files": _EMPTY_FILE} defaults[key] = value node = Node(**defaults) expected = safe_coerce_to_frozenset(value) assert_equal(getattr(node, key), expected)
def test__teardown__output_files(): Node(input_files=_EMPTY_FILE, output_files=_IN_FILES)._teardown(None, None) def test__teardown__output_files_missing(): node = Node(input_files=_EMPTY_FILE, output_files=_OUT_FILES) assert_raises(NodeError, node._teardown, None, None) ################################################################################ ################################################################################ ## CommandNode: Constructor _SIMPLE_DEPS = Node() _SIMPLE_SUBS = Node() _SIMPLE_CMD_MOCK = flexmock(input_files=_IN_FILES, output_files=_OUT_FILES, executables=_EXEC_FILES, auxiliary_files=_AUX_FILES, requirements=_REQUIREMENTS) _SIMPLE_CMD_NODE = CommandNode(command=_SIMPLE_CMD_MOCK, subnodes=_SIMPLE_SUBS, dependencies=_SIMPLE_DEPS) def test_commandnode_constructor__input_files(): assert_equal(_SIMPLE_CMD_NODE.input_files, _IN_FILES)
def _do_test_constructor__single_value(key, value): node = Node(**{key : value}) expected = safe_coerce_to_frozenset(value) assert_equal(getattr(node, key), expected)
def test__teardown__output_files(): Node(output_files = _IN_FILES)._teardown(None, None)