Refactored Command to add owner

This commit is contained in:
2025-12-08 21:07:34 +01:00
parent 3aa36a91aa
commit 045f01b48a
19 changed files with 138 additions and 101 deletions

View File

@@ -45,7 +45,7 @@ def test_i_can_mk_button_with_attrs():
def test_i_can_mk_button_with_command(user, rt):
def new_value(value): return value
command = Command('test', 'TestingCommand', new_value, "this is my new value")
command = Command('test', 'TestingCommand', None, new_value, "this is my new value")
@rt('/')
def get(): return mk.button('button', command)

View File

@@ -27,21 +27,21 @@ def reset_command_manager():
class TestCommandDefault:
def test_i_can_create_a_command_with_no_params(self):
command = Command('test', 'Command description', callback)
command = Command('test', 'Command description', None, callback)
assert command.id is not None
assert command.name == 'test'
assert command.description == 'Command description'
assert command.execute() == "Hello World"
def test_command_are_registered(self):
command = Command('test', 'Command description', callback)
command = Command('test', 'Command description', None, callback)
assert CommandsManager.commands.get(str(command.id)) is command
class TestCommandBind:
def test_i_can_bind_a_command_to_an_element(self):
command = Command('test', 'Command description', callback)
command = Command('test', 'Command description', None, callback)
elt = Button()
updated = command.bind_ft(elt)
@@ -50,7 +50,7 @@ class TestCommandBind:
assert matches(updated, expected)
def test_i_can_suppress_swapping_with_target_attr(self):
command = Command('test', 'Command description', callback).htmx(target=None)
command = Command('test', 'Command description', None, callback).htmx(target=None)
elt = Button()
updated = command.bind_ft(elt)
@@ -70,7 +70,7 @@ class TestCommandBind:
make_observable(data)
bind(data, "value", on_data_change)
command = Command('test', 'Command description', another_callback).bind(data)
command = Command('test', 'Command description', None, another_callback).bind(data)
res = command.execute()
@@ -88,14 +88,14 @@ class TestCommandBind:
make_observable(data)
bind(data, "value", on_data_change)
command = Command('test', 'Command description', another_callback).bind(data)
command = Command('test', 'Command description', None, another_callback).bind(data)
res = command.execute()
assert res == ["another 1", "another 2", ("hello", "new value")]
def test_by_default_swap_is_set_to_outer_html(self):
command = Command('test', 'Command description', callback)
command = Command('test', 'Command description', None, callback)
elt = Button()
updated = command.bind_ft(elt)
@@ -113,7 +113,7 @@ class TestCommandBind:
def another_callback():
return return_values
command = Command('test', 'Command description', another_callback)
command = Command('test', 'Command description', None, another_callback)
res = command.execute()
@@ -125,7 +125,7 @@ class TestCommandBind:
class TestCommandExecute:
def test_i_can_create_a_command_with_no_params(self):
command = Command('test', 'Command description', callback)
command = Command('test', 'Command description', None, callback)
assert command.id is not None
assert command.name == 'test'
assert command.description == 'Command description'
@@ -137,7 +137,7 @@ class TestCommandExecute:
def callback_with_param(param):
return f"Hello {param}"
command = Command('test', 'Command description', callback_with_param, "world")
command = Command('test', 'Command description', None, callback_with_param, "world")
assert command.execute() == "Hello world"
def test_i_can_execute_a_command_with_open_parameter(self):
@@ -146,7 +146,7 @@ class TestCommandExecute:
def callback_with_param(name):
return f"Hello {name}"
command = Command('test', 'Command description', callback_with_param)
command = Command('test', 'Command description', None, callback_with_param)
assert command.execute(client_response={"name": "world"}) == "Hello world"
def test_i_can_convert_arg_in_execute(self):
@@ -155,7 +155,7 @@ class TestCommandExecute:
def callback_with_param(number: int):
assert isinstance(number, int)
command = Command('test', 'Command description', callback_with_param)
command = Command('test', 'Command description', None, callback_with_param)
command.execute(client_response={"number": "10"})
def test_swap_oob_is_added_when_multiple_elements_are_returned(self):
@@ -164,7 +164,7 @@ class TestCommandExecute:
def another_callback():
return Div(id="first"), Div(id="second"), "hello", Div(id="third")
command = Command('test', 'Command description', another_callback)
command = Command('test', 'Command description', None, another_callback)
res = command.execute()
assert "hx-swap-oob" not in res[0].attrs
@@ -177,7 +177,7 @@ class TestCommandExecute:
def another_callback():
return Div(id="first"), Div(), "hello", Div()
command = Command('test', 'Command description', another_callback)
command = Command('test', 'Command description', None, another_callback)
res = command.execute()
assert "hx-swap-oob" not in res[0].attrs
@@ -188,9 +188,9 @@ class TestCommandExecute:
class TestLambaCommand:
def test_i_can_create_a_command_from_lambda(self):
command = LambdaCommand(lambda resp: "Hello World")
command = LambdaCommand(None, lambda resp: "Hello World")
assert command.execute() == "Hello World"
def test_by_default_target_is_none(self):
command = LambdaCommand(lambda resp: "Hello World")
command = LambdaCommand(None, lambda resp: "Hello World")
assert command.get_htmx_params()["hx-swap"] == "none"

View File

@@ -34,13 +34,13 @@ def rt(user):
class TestingCommand:
def test_i_can_trigger_a_command(self, user):
command = Command('test', 'TestingCommand', new_value, "this is my new value")
command = Command('test', 'TestingCommand', None, new_value, "this is my new value")
testable = TestableElement(user, mk.button('button', command))
testable.click()
assert user.get_content() == "this is my new value"
def test_error_is_raised_when_command_is_not_found(self, user):
command = Command('test', 'TestingCommand', new_value, "this is my new value")
command = Command('test', 'TestingCommand', None, new_value, "this is my new value")
CommandsManager.reset()
testable = TestableElement(user, mk.button('button', command))
@@ -50,7 +50,7 @@ class TestingCommand:
assert "not found." in str(exc_info.value)
def test_i_can_play_a_complex_scenario(self, user, rt):
command = Command('test', 'TestingCommand', new_value, "this is my new value")
command = Command('test', 'TestingCommand', None, new_value, "this is my new value")
@rt('/')
def get(): return mk.button('button', command)

View File

@@ -463,7 +463,7 @@ class TestPredicates:
div = Div(hx_post="/url")
assert HasHtmx(hx_post="/url").validate(div)
c = Command("c", "testing has_htmx", None)
c = Command("c", "testing has_htmx", None, None)
c.bind_ft(div)
assert HasHtmx(command=c).validate(div)