from unittest.mock import Mock

from gi.repository import Gtk

from gaphor.diagram.diagramtools.txtool import TxData, on_begin, transactional_tool
from gaphor.transaction import TransactionBegin


def xtest_start_tx_on_begin(view, event_manager):
    event_manager.handle = Mock()
    tx_data = TxData(event_manager)
    tool = transactional_tool(Gtk.GestureDrag.new(view), event_manager)

    on_begin(tool, None, tx_data)

    assert tx_data.tx
    assert event_manager.handle.called
    assert isinstance(event_manager.handle.call_args.args[0], TransactionBegin)

from gi.repository import Gtk

from gaphor.diagram.diagramtools.txtool import (
    TxData,
    on_begin,
    on_end,
    transactional_tool,
)
from gaphor.transaction import TransactionBegin


class MockEventManager:
    def __init__(self):
        self.events = []

    def handle(self, event):
        self.events.append(event)


def test_start_tx_on_begin(view):
    event_manager = MockEventManager()
    tx_data = TxData(event_manager)
    tool = transactional_tool(Gtk.GestureDrag.new(view), event_manager)

    on_begin(tool, None, tx_data)
    assert tx_data.tx

    on_end(tool, None, tx_data)

    assert event_manager.events
    assert isinstance(event_manager.events[0], TransactionBegin)

Fix tests for tx tool
Fix tests for tx tool

- from unittest.mock import Mock
- 
  from gi.repository import Gtk
  
- from gaphor.diagram.diagramtools.txtool import TxData, on_begin, transactional_tool
+ from gaphor.diagram.diagramtools.txtool import (
+     TxData,
+     on_begin,
+     on_end,
+     transactional_tool,
+ )
  from gaphor.transaction import TransactionBegin
  
  
+ class MockEventManager:
+     def __init__(self):
+         self.events = []
+ 
+     def handle(self, event):
+         self.events.append(event)
+ 
+ 
- def xtest_start_tx_on_begin(view, event_manager):
+ def test_start_tx_on_begin(view):
-     event_manager.handle = Mock()
+     event_manager = MockEventManager()
      tx_data = TxData(event_manager)
      tool = transactional_tool(Gtk.GestureDrag.new(view), event_manager)
  
      on_begin(tool, None, tx_data)
+     assert tx_data.tx
  
+     on_end(tool, None, tx_data)
-     assert tx_data.tx
-     assert event_manager.handle.called
-     assert isinstance(event_manager.handle.call_args.args[0], TransactionBegin)
  
+     assert event_manager.events
+     assert isinstance(event_manager.events[0], TransactionBegin)
+ 
Fix tests for tx tool
## Code Before:
from unittest.mock import Mock

from gi.repository import Gtk

from gaphor.diagram.diagramtools.txtool import TxData, on_begin, transactional_tool
from gaphor.transaction import TransactionBegin


def xtest_start_tx_on_begin(view, event_manager):
    event_manager.handle = Mock()
    tx_data = TxData(event_manager)
    tool = transactional_tool(Gtk.GestureDrag.new(view), event_manager)

    on_begin(tool, None, tx_data)

    assert tx_data.tx
    assert event_manager.handle.called
    assert isinstance(event_manager.handle.call_args.args[0], TransactionBegin)

## Instruction:
Fix tests for tx tool
## Code After:
from gi.repository import Gtk

from gaphor.diagram.diagramtools.txtool import (
    TxData,
    on_begin,
    on_end,
    transactional_tool,
)
from gaphor.transaction import TransactionBegin


class MockEventManager:
    def __init__(self):
        self.events = []

    def handle(self, event):
        self.events.append(event)


def test_start_tx_on_begin(view):
    event_manager = MockEventManager()
    tx_data = TxData(event_manager)
    tool = transactional_tool(Gtk.GestureDrag.new(view), event_manager)

    on_begin(tool, None, tx_data)
    assert tx_data.tx

    on_end(tool, None, tx_data)

    assert event_manager.events
    assert isinstance(event_manager.events[0], TransactionBegin)
