Skip to content

Commit

Permalink
Move Jump's default condition to class level field, add unit test
Browse files Browse the repository at this point in the history
No functional changes intended. I was hunting a transient bug in
Jump which turned out to be a "feature" when executing jumps under
PyCharm's debugger.

Re spine-tools/Spine-Toolbox#2925
  • Loading branch information
soininen committed Sep 2, 2024
1 parent 4fa66a3 commit ce1e5cd
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
10 changes: 6 additions & 4 deletions spine_engine/project_item/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -589,20 +589,22 @@ def from_dict(cls, connection_dict):
class Jump(ConnectionBase):
"""Represents a conditional jump between two project items."""

_DEFAULT_CONDITION = {"type": "python-script", "script": "exit(1)", "specification": ""}

def __init__(
self, source_name, source_position, destination_name, destination_position, condition={}, cmd_line_args=()
self, source_name, source_position, destination_name, destination_position, condition=None, cmd_line_args=()
):
"""
Args:
source_name (str): source project item's name
source_position (str): source anchor's position
destination_name (str): destination project item's name
destination_position (str): destination anchor's position
condition (dict): jump condition
condition (dict, optional): jump condition
cmd_line_args (Iterable of str): command line arguments
"""
super().__init__(source_name, source_position, destination_name, destination_position)
default_condition = {"type": "python-script", "script": "exit(1)", "specification": ""}
self.condition = condition if condition else default_condition
self.condition = condition if condition is not None else self._DEFAULT_CONDITION
self._resources_from_source = set()
self._resources_from_destination = set()
self.cmd_line_args = list(cmd_line_args)
Expand Down
28 changes: 27 additions & 1 deletion tests/project_item/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@
######################################################################################################################
""" Uni tests for the ``connection`` module. """
import os.path
import pathlib
from tempfile import TemporaryDirectory
import unittest
from unittest.mock import Mock
from spine_engine.project_item.connection import Connection, FilterSettings, Jump
from spine_engine.project_item.project_item_resource import database_resource
from spine_engine.project_item.project_item_resource import LabelArg, database_resource, file_resource
from spinedb_api import DatabaseMapping, import_alternatives, import_entity_classes, import_scenarios
from spinedb_api.filters.scenario_filter import SCENARIO_FILTER_TYPE

Expand Down Expand Up @@ -169,6 +170,31 @@ def test_counter_passed_to_condition(self):
jump.make_logger(Mock())
self.assertTrue(jump.is_condition_true(23))

def test_command_line_args_with_whitespace_are_not_broken_into_tokens(self):
# Curiously, this test fails when run under PyCharm's debugger.
with TemporaryDirectory() as temp_dir:
path = pathlib.Path(temp_dir) / "path with spaces" / "file name.txt"
condition = {
"type": "python-script",
"script": "\n".join(
(
"from pathlib import Path",
"import sys",
"if len(sys.argv) != 3:",
" exit(1)",
f"expected_path = Path(r'{str(path)}').resolve()",
"if Path(sys.argv[1]).resolve() != expected_path:",
" exit(1)",
"exit(0 if int(sys.argv[2]) == 23 else 1)",
)
),
}
jump = Jump("source", "bottom", "destination", "top", condition, [LabelArg("arg_label")])
resource = file_resource("provider: unit test", str(path), "arg_label")
jump.receive_resources_from_source([resource])
jump.make_logger(Mock())
self.assertTrue(jump.is_condition_true(23))

def test_dictionary(self):
jump = Jump("source", "bottom", "destination", "top", {"type": "python-script", "script": "exit(23)"})
jump_dict = jump.to_dict()
Expand Down

0 comments on commit ce1e5cd

Please sign in to comment.