Skip to content

Invoke is a leading creative engine that uses image and video generation models to empower professionals, artists, and enthusiasts to generate and create visual media using the latest AI-driven technologies. The solution offers an industry-leading application interface, and serves as the foundation for multiple commercial products.

test(execution): consolidate overlapping coverage

JPPhoto8d8c26cb0cd5
diff --git a/tests/app/services/shared/import_test_utils.py b/tests/app/services/shared/import_test_utils.py
new file mode 100644
index 0000000000..09df0b8770
--- /dev/null
+++ b/tests/app/services/shared/import_test_utils.py
@@ -0,0 +1,28 @@
+import subprocess
+import sys
+import textwrap
+
+
+def assert_module_imports_without_graph(module_name: str) -> None:
+ """Import a shared execution-engine leaf while rejecting graph-facade imports."""
+
+ script = textwrap.dedent(
+ """
+ import builtins
+ import importlib
+ import sys
+
+ real_import = builtins.__import__
+
+ def blocked_import(name, globals=None, locals=None, fromlist=(), level=0):
+ if name == "invokeai.app.services.shared.graph" or name.startswith("invokeai.app.services.shared.graph."):
+ raise ModuleNotFoundError("graph import blocked")
+ return real_import(name, globals, locals, fromlist, level)
+
+ builtins.__import__ = blocked_import
+ importlib.import_module(MODULE_NAME)
+ assert "invokeai.app.services.shared.graph" not in sys.modules
+ """
+ ).replace("MODULE_NAME", repr(module_name))
+ result = subprocess.run([sys.executable, "-c", script], capture_output=True, text=True, timeout=300, check=False)
+ assert result.returncode == 0, result.stderr
diff --git a/tests/app/services/shared/test_graph_if_activation.py b/tests/app/services/shared/test_graph_if_activation.py
index a496eb6592..f2cfc39dee 100644
--- a/tests/app/services/shared/test_graph_if_activation.py
+++ b/tests/app/services/shared/test_graph_if_activation.py
@@ -1,8 +1,5 @@
from __future__ import annotations
-import subprocess
-import sys
-import textwrap
from types import SimpleNamespace
from typing import Any
@@ -12,6 +9,7 @@ from invokeai.app.invocations.logic import IfInvocation
from invokeai.app.services.shared import graph as graph_module
from invokeai.app.services.shared.execution_engine import ActivationDependency
from invokeai.app.services.shared.graph_if_activation import _IfActivationController
+from tests.app.services.shared.import_test_utils import assert_module_imports_without_graph
def _edge(source: str, destination: str, field: str) -> Any:
@@ -77,30 +75,7 @@ class _State:
def test_standalone_import_does_not_import_graph() -> None:
- script = """
- import builtins
- import sys
-
- real_import = builtins.__import__
-
- def blocked_import(name, globals=None, locals=None, fromlist=(), level=0):
- if name == "invokeai.app.services.shared.graph" or name.startswith("invokeai.app.services.shared.graph."):
- raise ModuleNotFoundError("graph import blocked")
- return real_import(name, globals, locals, fromlist, level)
-
- builtins.__import__ = blocked_import
- import invokeai.app.services.shared.graph_if_activation
-
- assert "invokeai.app.services.shared.graph" not in sys.modules
- """
- result = subprocess.run(
- [sys.executable, "-c", textwrap.dedent(script)],
- capture_output=True,
- text=True,
- timeout=300,
- check=False,
- )
- assert result.returncode == 0, result.stderr
+ assert_module_imports_without_graph("invokeai.app.services.shared.graph_if_activation")
def test_graph_reexports_controller_class() -> None:
diff --git a/tests/app/services/shared/test_graph_if_dependencies.py b/tests/app/services/shared/test_graph_if_dependencies.py
index 36973a7e6c..bc56ab801b 100644
--- a/tests/app/services/shared/test_graph_if_dependencies.py
+++ b/tests/app/services/shared/test_graph_if_dependencies.py
@@ -1,8 +1,5 @@
from __future__ import annotations
-import subprocess
-import sys
-import textwrap
from typing import Any
import pytest
@@ -15,6 +12,7 @@ from invokeai.app.services.shared import graph as graph_module
from invokeai.app.services.shared import graph_if_dependencies
from invokeai.app.services.shared.execution_engine import ActivationDependency
from invokeai.app.services.shared.graph import Edge, EdgeConnection, Graph, GraphExecutionState
+from tests.app.services.shared.import_test_utils import assert_module_imports_without_graph
def _edge(source: str, source_field: str, destination: str, destination_field: str) -> Edge:
@@ -230,30 +228,7 @@ def _indirectly_connected_if_graph() -> Graph:
def test_leaf_import_does_not_import_graph() -> None:
- script = """
- import builtins
- import sys
-
- real_import = builtins.__import__
-
- def blocked_import(name, globals=None, locals=None, fromlist=(), level=0):
- if name == "invokeai.app.services.shared.graph" or name.startswith("invokeai.app.services.shared.graph."):
- raise ModuleNotFoundError("graph import blocked")
- return real_import(name, globals, locals, fromlist, level)
-
- builtins.__import__ = blocked_import
- import invokeai.app.services.shared.graph_if_dependencies
-
- assert "invokeai.app.services.shared.graph" not in sys.modules
- """
- result = subprocess.run(
- [sys.executable, "-c", textwrap.dedent(script)],
- capture_output=True,
- text=True,
- timeout=300,
- check=False,
- )
- assert result.returncode == 0, result.stderr
+ assert_module_imports_without_graph("invokeai.app.services.shared.graph_if_dependencies")
def test_graph_methods_delegate_to_leaf_functions(monkeypatch: pytest.MonkeyPatch) -> None:
diff --git a/tests/app/services/shared/test_graph_if_runtime.py b/tests/app/services/shared/test_graph_if_runtime.py
index f998c71204..5664df7c69 100644
--- a/tests/app/services/shared/test_graph_if_runtime.py
+++ b/tests/app/services/shared/test_graph_if_runtime.py
@@ -1,13 +1,10 @@
-import subprocess
-import sys
-import textwrap
-
import pytest
from invokeai.app.invocations.logic import IfInvocation
from invokeai.app.services.shared.execution_engine.scheduler import ActivationDependency
from invokeai.app.services.shared.graph import Graph, GraphExecutionState
from invokeai.app.services.shared.graph_runtime_records import _ApplyTransaction
+from tests.app.services.shared.import_test_utils import assert_module_imports_without_graph
@pytest.fixture
@@ -20,25 +17,7 @@ def state() -> GraphExecutionState:
def test_runtime_import_without_graph() -> None:
- script = textwrap.dedent(
- """
- import builtins
- import sys
-
- real_import = builtins.__import__
-
- def blocked_import(name, globals=None, locals=None, fromlist=(), level=0):
- if name == "invokeai.app.services.shared.graph":
- raise AssertionError("runtime imported graph")
- return real_import(name, globals, locals, fromlist, level)
-
- builtins.__import__ = blocked_import
- import invokeai.app.services.shared.graph_if_runtime
- assert "invokeai.app.services.shared.graph" not in sys.modules
- """
- )
- result = subprocess.run([sys.executable, "-c", script], capture_output=True, text=True, timeout=300)
- assert result.returncode == 0, result.stderr
+ assert_module_imports_without_graph("invokeai.app.services.shared.graph_if_runtime")
@pytest.mark.parametrize(
diff --git a/tests/test_graph_execution_state.py b/tests/test_graph_execution_state.py
index 6ce05943d0..788f6f9519 100644
--- a/tests/test_graph_execution_state.py
+++ b/tests/test_graph_execution_state.py
@@ -4924,7 +4924,7 @@ def test_if_graph_current_behavior_executes_both_simple_branches():
assert g.results[prepared_selected_output_id].prompt == "true branch"
-def test_if_graph_optimized_behavior_executes_only_selected_simple_branch():
+def test_if_graph_optimized_behavior_executes_only_selected_simple_branch_and_records_history():
graph = Graph()
graph.add_node(BooleanInvocation(id="condition", value=True))
graph.add_node(PromptTestInvocation(id="true_value", prompt="true branch"))
@@ -4943,23 +4943,6 @@ def test_if_graph_optimized_behavior_executes_only_selected_simple_branch():
assert set(executed_source_ids) == {"condition", "true_value", "if", "selected_output"}
assert "false_value" not in executed_source_ids
-
-def test_if_graph_optimized_behavior_does_not_record_unselected_branch_in_execution_history():
- graph = Graph()
- graph.add_node(BooleanInvocation(id="condition", value=True))
- graph.add_node(PromptTestInvocation(id="true_value", prompt="true branch"))
- graph.add_node(PromptTestInvocation(id="false_value", prompt="false branch"))
- graph.add_node(IfInvocation(id="if"))
- graph.add_node(PromptTestInvocation(id="selected_output"))
-
- graph.add_edge(create_edge("condition", "value", "if", "condition"))
- graph.add_edge(create_edge("true_value", "prompt", "if", "true_input"))
- graph.add_edge(create_edge("false_value", "prompt", "if", "false_input"))
- graph.add_edge(create_edge("if", "value", "selected_output", "prompt"))
-
- g = GraphExecutionState(graph=graph)
- execute_all_nodes(g)
-
assert set(g.executed_history) == {"condition", "true_value", "if", "selected_output"}
assert "false_value" not in g.executed_history
@@ -5372,43 +5355,36 @@ def test_if_graph_optimized_behavior_keeps_shared_live_consumers_per_iteration()
assert g.is_complete()
-def test_if_graph_optimized_behavior_handles_selected_true_branch_with_shared_false_input_ancestor():
- graph = Graph()
- graph.add_node(BooleanInvocation(id="condition", value=True))
- graph.add_node(AnyTypeTestInvocation(id="shared_item", value="shared"))
- graph.add_node(AnyTypeTestInvocation(id="true_item", value="true"))
- graph.add_node(CollectInvocation(id="shared_collect"))
- graph.add_node(CollectInvocation(id="true_collect"))
- graph.add_node(IfInvocation(id="if"))
- graph.add_node(AnyTypeTestInvocation(id="selected_output"))
-
- graph.add_edge(create_edge("condition", "value", "if", "condition"))
- graph.add_edge(create_edge("shared_item", "value", "shared_collect", "item"))
- graph.add_edge(create_edge("shared_collect", "collection", "true_collect", "collection"))
- graph.add_edge(create_edge("true_item", "value", "true_collect", "item"))
- graph.add_edge(create_edge("shared_collect", "collection", "if", "false_input"))
- graph.add_edge(create_edge("true_collect", "collection", "if", "true_input"))
- graph.add_edge(create_edge("if", "value", "selected_output", "value"))
-
- g = GraphExecutionState(graph=graph)
- executed_source_ids = execute_all_nodes(g)
-
- prepared_selected_output_id = next(iter(g.source_prepared_mapping["selected_output"]))
- assert g.results[prepared_selected_output_id].value == ["shared", "true"]
- assert set(executed_source_ids) == {
- "condition",
- "shared_item",
- "true_item",
- "shared_collect",
- "true_collect",
- "if",
- "selected_output",
- }
-
-
-def test_if_graph_optimized_behavior_handles_selected_false_branch_with_shared_true_input_ancestor():
+ ("condition", "expected_value", "expected_source_ids"),
+ [
+ pytest.param(
+ True,
+ ["shared", "true"],
+ {
+ "condition",
+ "shared_item",
+ "true_item",
+ "shared_collect",
+ "true_collect",
+ "if",
+ "selected_output",
+ },
+ id="true-branch",
+ ),
+ pytest.param(
+ False,
+ ["shared"],
+ {"condition", "shared_item", "shared_collect", "if", "selected_output"},
+ id="false-branch",
+ ),
+ ],
+)
+def test_if_graph_optimized_behavior_handles_selected_branch_with_shared_ancestor(
+ condition: bool, expected_value: list[str], expected_source_ids: set[str]
+):
graph = Graph()
- graph.add_node(BooleanInvocation(id="condition", value=False))
+ graph.add_node(BooleanInvocation(id="condition", value=condition))
graph.add_node(AnyTypeTestInvocation(id="shared_item", value="shared"))
graph.add_node(AnyTypeTestInvocation(id="true_item", value="true"))
graph.add_node(CollectInvocation(id="shared_collect"))
@@ -5428,16 +5404,8 @@ def test_if_graph_optimized_behavior_handles_selected_false_branch_with_shared_t
executed_source_ids = execute_all_nodes(g)
prepared_selected_output_id = next(iter(g.source_prepared_mapping["selected_output"]))
- assert g.results[prepared_selected_output_id].value == ["shared"]
- assert set(executed_source_ids) == {
- "condition",
- "shared_item",
- "shared_collect",
- "if",
- "selected_output",
- }
- assert "true_item" not in executed_source_ids
- assert "true_collect" not in executed_source_ids
+ assert g.results[prepared_selected_output_id].value == expected_value
+ assert set(executed_source_ids) == expected_source_ids
def test_prepare_if_inputs_raises_when_selected_branch_source_has_no_result():