import pytest

from gaphor import UML
from gaphor.ui.diagrampage import tooliter
from gaphor.UML.toolbox import uml_toolbox_actions


@pytest.fixture
def action_factory():
    return next(
        t for t in tooliter(uml_toolbox_actions) if t.id == "toolbox-action"
    ).item_factory


def test_create_action_should_create_an_activity(diagram, action_factory):
    action = action_factory(diagram)

    assert action.subject.activity
    assert action.subject.owner is action.subject.activity


def test_create_action_should_add_to_existing_activity(
    diagram, action_factory, element_factory
):
    activity = element_factory.create(UML.Activity)
    action = action_factory(diagram)

    assert action.subject.activity is activity


def test_create_action_should_add_to_existing_activity_in_package(
    diagram, action_factory, element_factory
):
    package = element_factory.create(UML.Package)
    diagram.package = package
    activity = element_factory.create(UML.Activity)
    activity.package = package
    action = action_factory(diagram)

    assert action.subject.activity is activity

import pytest

from gaphor import UML
from gaphor.ui.diagrampage import tooliter
from gaphor.UML.toolbox import uml_toolbox_actions

activity_node_names = [
    "action",
    "initial-node",
    "activity-final-node",
    "flow-final-node",
    "decision-node",
    "fork-node",
    "object-node",
    "send-signal-action",
    "accept-event-action",
]


@pytest.fixture
def item_factory(request):
    return next(
        t for t in tooliter(uml_toolbox_actions) if t.id == f"toolbox-{request.param}"
    ).item_factory


@pytest.mark.parametrize("item_factory", activity_node_names, indirect=True)
def test_create_action_should_create_an_activity(diagram, item_factory):
    action = item_factory(diagram)

    assert action.subject.activity
    assert action.subject.owner is action.subject.activity


@pytest.mark.parametrize("item_factory", activity_node_names, indirect=True)
def test_create_action_should_add_to_existing_activity(
    diagram, item_factory, element_factory
):
    activity = element_factory.create(UML.Activity)
    action = item_factory(diagram)

    assert action.subject.activity is activity


@pytest.mark.parametrize("item_factory", activity_node_names, indirect=True)
def test_create_action_should_add_to_existing_activity_in_package(
    diagram, item_factory, element_factory
):
    package = element_factory.create(UML.Package)
    diagram.package = package
    activity = element_factory.create(UML.Activity)
    activity.package = package
    action = item_factory(diagram)

    assert action.subject.activity is activity

Test all activity nodes for namespacing
Test all activity nodes for namespacing

  import pytest
  
  from gaphor import UML
  from gaphor.ui.diagrampage import tooliter
  from gaphor.UML.toolbox import uml_toolbox_actions
  
+ activity_node_names = [
+     "action",
+     "initial-node",
+     "activity-final-node",
+     "flow-final-node",
+     "decision-node",
+     "fork-node",
+     "object-node",
+     "send-signal-action",
+     "accept-event-action",
+ ]
+ 
  
  @pytest.fixture
- def action_factory():
+ def item_factory(request):
      return next(
-         t for t in tooliter(uml_toolbox_actions) if t.id == "toolbox-action"
+         t for t in tooliter(uml_toolbox_actions) if t.id == f"toolbox-{request.param}"
      ).item_factory
  
  
+ @pytest.mark.parametrize("item_factory", activity_node_names, indirect=True)
- def test_create_action_should_create_an_activity(diagram, action_factory):
+ def test_create_action_should_create_an_activity(diagram, item_factory):
-     action = action_factory(diagram)
+     action = item_factory(diagram)
  
      assert action.subject.activity
      assert action.subject.owner is action.subject.activity
  
  
+ @pytest.mark.parametrize("item_factory", activity_node_names, indirect=True)
  def test_create_action_should_add_to_existing_activity(
-     diagram, action_factory, element_factory
+     diagram, item_factory, element_factory
  ):
      activity = element_factory.create(UML.Activity)
-     action = action_factory(diagram)
+     action = item_factory(diagram)
  
      assert action.subject.activity is activity
  
  
+ @pytest.mark.parametrize("item_factory", activity_node_names, indirect=True)
  def test_create_action_should_add_to_existing_activity_in_package(
-     diagram, action_factory, element_factory
+     diagram, item_factory, element_factory
  ):
      package = element_factory.create(UML.Package)
      diagram.package = package
      activity = element_factory.create(UML.Activity)
      activity.package = package
-     action = action_factory(diagram)
+     action = item_factory(diagram)
  
      assert action.subject.activity is activity
  
Test all activity nodes for namespacing
## Code Before:
import pytest

from gaphor import UML
from gaphor.ui.diagrampage import tooliter
from gaphor.UML.toolbox import uml_toolbox_actions


@pytest.fixture
def action_factory():
    return next(
        t for t in tooliter(uml_toolbox_actions) if t.id == "toolbox-action"
    ).item_factory


def test_create_action_should_create_an_activity(diagram, action_factory):
    action = action_factory(diagram)

    assert action.subject.activity
    assert action.subject.owner is action.subject.activity


def test_create_action_should_add_to_existing_activity(
    diagram, action_factory, element_factory
):
    activity = element_factory.create(UML.Activity)
    action = action_factory(diagram)

    assert action.subject.activity is activity


def test_create_action_should_add_to_existing_activity_in_package(
    diagram, action_factory, element_factory
):
    package = element_factory.create(UML.Package)
    diagram.package = package
    activity = element_factory.create(UML.Activity)
    activity.package = package
    action = action_factory(diagram)

    assert action.subject.activity is activity

## Instruction:
Test all activity nodes for namespacing
## Code After:
import pytest

from gaphor import UML
from gaphor.ui.diagrampage import tooliter
from gaphor.UML.toolbox import uml_toolbox_actions

activity_node_names = [
    "action",
    "initial-node",
    "activity-final-node",
    "flow-final-node",
    "decision-node",
    "fork-node",
    "object-node",
    "send-signal-action",
    "accept-event-action",
]


@pytest.fixture
def item_factory(request):
    return next(
        t for t in tooliter(uml_toolbox_actions) if t.id == f"toolbox-{request.param}"
    ).item_factory


@pytest.mark.parametrize("item_factory", activity_node_names, indirect=True)
def test_create_action_should_create_an_activity(diagram, item_factory):
    action = item_factory(diagram)

    assert action.subject.activity
    assert action.subject.owner is action.subject.activity


@pytest.mark.parametrize("item_factory", activity_node_names, indirect=True)
def test_create_action_should_add_to_existing_activity(
    diagram, item_factory, element_factory
):
    activity = element_factory.create(UML.Activity)
    action = item_factory(diagram)

    assert action.subject.activity is activity


@pytest.mark.parametrize("item_factory", activity_node_names, indirect=True)
def test_create_action_should_add_to_existing_activity_in_package(
    diagram, item_factory, element_factory
):
    package = element_factory.create(UML.Package)
    diagram.package = package
    activity = element_factory.create(UML.Activity)
    activity.package = package
    action = item_factory(diagram)

    assert action.subject.activity is activity
