Skip to content

Commit

Permalink
Extracting methods that relied on noseOfYeti inheritence tricks
Browse files Browse the repository at this point in the history
  • Loading branch information
delfick committed Dec 25, 2024
1 parent c42c881 commit 10a4f86
Show file tree
Hide file tree
Showing 11 changed files with 332 additions and 319 deletions.
80 changes: 40 additions & 40 deletions apps/interactor/tests/database/models/test_scene_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -514,46 +514,46 @@ def setter(h, s, b, k, **kwargs):

return setter

def test_it_works(self, V, setter):
zones = [
[0, 0, 0, 3500],
[0, 0, 0, 3500],
[0, 0, 0, 3500],
[100, 1, 0, 3500],
[100, 0.5, 0, 3500],
[100, 0.5, 0, 3500],
[100, 0.5, 1, 3500],
[100, 0.5, 1, 9000],
[100, 0.5, 1, 9000],
]

V.assertCorrect(
zones,
setter(0, 0, 0, 3500, start_index=0, end_index=2, duration=1),
setter(100, 1, 0, 3500, start_index=3, end_index=3, duration=1),
setter(100, 0.5, 0, 3500, start_index=4, end_index=5, duration=1),
setter(100, 0.5, 1, 3500, start_index=6, end_index=6, duration=1),
setter(100, 0.5, 1, 9000, start_index=7, end_index=8, duration=1),
)

def test_it_works2(self, V, setter):
zones = [
[0, 0, 0, 3500],
[100, 1, 0, 3500],
[100, 0.5, 0, 3500],
[100, 0.5, 0, 3500],
[100, 0.5, 1, 3500],
[100, 0.5, 1, 9000],
]

V.assertCorrect(
zones,
setter(0, 0, 0, 3500, start_index=0, end_index=0, duration=1),
setter(100, 1, 0, 3500, start_index=1, end_index=1, duration=1),
setter(100, 0.5, 0, 3500, start_index=2, end_index=3, duration=1),
setter(100, 0.5, 1, 3500, start_index=4, end_index=4, duration=1),
setter(100, 0.5, 1, 9000, start_index=5, end_index=5, duration=1),
)
def test_it_works(self, V, setter):
zones = [
[0, 0, 0, 3500],
[0, 0, 0, 3500],
[0, 0, 0, 3500],
[100, 1, 0, 3500],
[100, 0.5, 0, 3500],
[100, 0.5, 0, 3500],
[100, 0.5, 1, 3500],
[100, 0.5, 1, 9000],
[100, 0.5, 1, 9000],
]

V.assertCorrect(
zones,
setter(0, 0, 0, 3500, start_index=0, end_index=2, duration=1),
setter(100, 1, 0, 3500, start_index=3, end_index=3, duration=1),
setter(100, 0.5, 0, 3500, start_index=4, end_index=5, duration=1),
setter(100, 0.5, 1, 3500, start_index=6, end_index=6, duration=1),
setter(100, 0.5, 1, 9000, start_index=7, end_index=8, duration=1),
)

def test_it_works2(self, V, setter):
zones = [
[0, 0, 0, 3500],
[100, 1, 0, 3500],
[100, 0.5, 0, 3500],
[100, 0.5, 0, 3500],
[100, 0.5, 1, 3500],
[100, 0.5, 1, 9000],
]

V.assertCorrect(
zones,
setter(0, 0, 0, 3500, start_index=0, end_index=0, duration=1),
setter(100, 1, 0, 3500, start_index=1, end_index=1, duration=1),
setter(100, 0.5, 0, 3500, start_index=2, end_index=3, duration=1),
setter(100, 0.5, 1, 3500, start_index=4, end_index=4, duration=1),
setter(100, 0.5, 1, 9000, start_index=5, end_index=5, duration=1),
)

class TestChainMsgs:
def test_it_yields_power_message_if_we_have_one(self, V, overrides):
Expand Down
97 changes: 48 additions & 49 deletions modules/tests/photons_app_tests/tasks/test_task_specifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,89 +15,88 @@
from photons_transport.targets.base import Target


class TestTaskSpecifierSpec:
def meta(config="", extra_prepare=None):
with hp.a_temp_file() as fle:
fle.write(dedent(config).encode())
fle.close()

@pytest.fixture()
def spec(self):
return task_specifier_spec()
original = Collector.extra_prepare_after_activation

def meta(self, config="", extra_prepare=None):
with hp.a_temp_file() as fle:
fle.write(dedent(config).encode())
fle.close()
class Prepare:
def __enter__(s):
if extra_prepare:

original = Collector.extra_prepare_after_activation
def extra(*args, **kwargs):
extra_prepare(*args, **kwargs)
return original(*args, **kwargs)

class Prepare:
def __enter__(s):
if extra_prepare:
s.patch = mock.patch.object(Collector, "extra_prepare_after_activation", extra)
s.patch.start()

def extra(*args, **kwargs):
extra_prepare(*args, **kwargs)
return original(*args, **kwargs)
def __exit__(s, exc_typ, exc, tb):
if hasattr(s, "patch"):
s.patch.stop()

s.patch = mock.patch.object(
Collector, "extra_prepare_after_activation", extra
)
s.patch.start()
with alt_pytest_asyncio.Loop(new_loop=False), Prepare(), open(fle.name) as realfile:
args_dict = {"photons_app": {"config": realfile}}

def __exit__(s, exc_typ, exc, tb):
if hasattr(s, "patch"):
s.patch.stop()
app = App()
logging_handler = mock.Mock(name="logging_handler")
collector = app.setup_collector(args_dict, logging_handler, None)
return Meta({"collector": collector}, [])

with alt_pytest_asyncio.Loop(new_loop=False), Prepare(), open(fle.name) as realfile:
args_dict = {"photons_app": {"config": realfile}}

app = App()
logging_handler = mock.Mock(name="logging_handler")
collector = app.setup_collector(args_dict, logging_handler, None)
return Meta({"collector": collector}, [])
class TestTaskSpecifierSpec:

@pytest.fixture()
def spec(self):
return task_specifier_spec()

def test_it_can_have_default(self, spec):
for val in ("", None, sb.NotSpecified):
result = spec.normalise(self.meta(), val)
result = spec.normalise(meta(), val)
assert result == (sb.NotSpecified, "list_tasks")

def test_it_can_treat_task_as_just_a_task(self, spec):
result = spec.normalise(self.meta(), "things")
result = spec.normalise(meta(), "things")
assert result == (sb.NotSpecified, "things")

def test_it_can_split_by_last(self, spec):
result = spec.normalise(self.meta(), "one:two:three")
result = spec.normalise(meta(), "one:two:three")
assert result == ("one:two", "three")

result = spec.normalise(self.meta(), "one:three")
result = spec.normalise(meta(), "one:three")
assert result == ("one", "three")

class TestTakingInAnIterable:

def test_it_can_take_a_two_item(self, spec):
result = spec.normalise(self.meta(), ("one", "three"))
result = spec.normalise(meta(), ("one", "three"))
assert result == ("one", "three")

result = spec.normalise(self.meta(), ["one", "three"])
result = spec.normalise(meta(), ["one", "three"])
assert result == ("one", "three")

def test_it_complains_about_non_two_item(self, spec):
invalids = [(), ("adsf",), ("adsf", "afd", "df"), (1,), (1, 2, 3)]
for invalid in invalids:
with assertRaises(BadSpecValue, "Expected tuple to be of a particular length"):
spec.normalise(self.meta(), invalid)
spec.normalise(meta(), invalid)

def test_it_allows_not_specified_for_target(self, spec):
result = spec.normalise(self.meta(), (sb.NotSpecified, "amaze"))
result = spec.normalise(meta(), (sb.NotSpecified, "amaze"))
assert result == (sb.NotSpecified, "amaze")

def test_it_complains_about_a_two_item_tuple_with_bad_types(self, spec):
invalids = [(None, None), ("asdf", sb.NotSpecified), (1, 2), ([], []), ({}, {})]
for invalid in invalids:
with assertRaises(BadSpecValue, "Value failed some specifications"):
spec.normalise(self.meta(), invalid)
spec.normalise(meta(), invalid)

class TestOverridingATarget:
def test_it_complains_if_the_target_being_overridden_doesnt_exist(self, spec):
with assertRaises(TargetNotFound, name="wat", available=[]):
spec.normalise(self.meta(), "wat():meh")
spec.normalise(meta(), "wat():meh")

config = """
targets:
Expand All @@ -107,7 +106,7 @@ def test_it_complains_if_the_target_being_overridden_doesnt_exist(self, spec):
"""

with assertRaises(TargetNotFound, name="wat", available=[]):
spec.normalise(self.meta(config), "wat():meh")
spec.normalise(meta(config), "wat():meh")

config = """
photons_app:
Expand All @@ -117,13 +116,13 @@ def test_it_complains_if_the_target_being_overridden_doesnt_exist(self, spec):
"""

with assertRaises(TargetNotFound, name="wat"):
spec.normalise(self.meta(config), "wat():meh")
spec.normalise(meta(config), "wat():meh")

def test_it_complains_if_we_dont_cant_parse_tokens(self, spec):
token_errors = ["wat(", "wat({)", "("]
for specifier in token_errors:
with assertRaises(BadSpecValue, "Failed to parse specifier"):
spec.normalise(self.meta(), f"{specifier}:meh")
spec.normalise(meta(), f"{specifier}:meh")

def test_it_complains_if_we_have_invalid_syntax(self, spec):
config = """
Expand All @@ -136,7 +135,7 @@ def test_it_complains_if_we_have_invalid_syntax(self, spec):
syntax_errors = ["lan('a'=_)", "lan(=a)", "lan(10='a')"]
for specifier in syntax_errors:
with assertRaises(BadSpecValue, "Target options must be valid dictionary syntax"):
spec.normalise(self.meta(config), f"{specifier}:meh")
spec.normalise(meta(config), f"{specifier}:meh")

def test_it_complains_about_keyword_arguments_that_arent_literals(self, spec):
config = """
Expand All @@ -153,7 +152,7 @@ def test_it_complains_about_keyword_arguments_that_arent_literals(self, spec):
]
for specifier in syntax_errors:
with assertRaises(BadSpecValue, "target options can only be python literals.+"):
spec.normalise(self.meta(config), f"{specifier}:meh")
spec.normalise(meta(config), f"{specifier}:meh")

def test_it_can_override_properties_on_a_target(self, spec):
config = """
Expand All @@ -163,13 +162,13 @@ def test_it_can_override_properties_on_a_target(self, spec):
- transport
"""

meta = self.meta(config)
m = meta(config)

target_register = meta.everything["collector"].configuration["target_register"]
target_register = m.everything["collector"].configuration["target_register"]
original = list(target_register.registered)
assert "lan" in original, original

result = spec.normalise(meta, "lan(default_broadcast='9.8.7.6'):amazing_task")
result = spec.normalise(m, "lan(default_broadcast='9.8.7.6'):amazing_task")
assert len(target_register.registered) == len(original) + 1

assert result == (mock.ANY, "amazing_task")
Expand Down Expand Up @@ -212,13 +211,13 @@ def extra_prepare(collector, configuration, args_dict):
default_broadcast: 1.1.1.255
"""

meta = self.meta(config, extra_prepare=extra_prepare)
target_register = meta.everything["collector"].configuration["target_register"]
m = meta(config, extra_prepare=extra_prepare)
target_register = m.everything["collector"].configuration["target_register"]

original = list(target_register.registered)
assert "con" in original, original

result = spec.normalise(meta, "con(network='{targets.lan2}'):amazing_task")
result = spec.normalise(m, "con(network='{targets.lan2}'):amazing_task")
assert len(target_register.registered) == len(original) + 1

assert result == (mock.ANY, "amazing_task")
Expand Down
47 changes: 22 additions & 25 deletions modules/tests/photons_app_tests/test_collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,26 @@
)


@contextmanager
def mocks(collector, configuration, args_dict, photons_app, register):
__main__ = mock.Mock(name="__main__")
find_photons_app_options = mock.Mock(name="find_photons_app_options", return_value=photons_app)
determine_mainline_module = mock.Mock(name="determine_mainline_module", return_value=__main__)
setup_addon_register = mock.Mock(name="setup_addon_register", return_value=register)

with mock.patch.multiple(
collector,
find_photons_app_options=find_photons_app_options,
determine_mainline_module=determine_mainline_module,
setup_addon_register=setup_addon_register,
):
yield __main__

find_photons_app_options.assert_called_once_with(configuration, args_dict)
determine_mainline_module.assert_called_once_with()
setup_addon_register.assert_called_once_with(photons_app, __main__)


class TestCollector:

@pytest.fixture()
Expand Down Expand Up @@ -111,29 +131,6 @@ def test_it_can_be_cloned(self):

class TestExtraPrepare:

@contextmanager
def mocks(self, collector, configuration, args_dict, photons_app, register):
__main__ = mock.Mock(name="__main__")
find_photons_app_options = mock.Mock(
name="find_photons_app_options", return_value=photons_app
)
determine_mainline_module = mock.Mock(
name="determine_mainline_module", return_value=__main__
)
setup_addon_register = mock.Mock(name="setup_addon_register", return_value=register)

with mock.patch.multiple(
collector,
find_photons_app_options=find_photons_app_options,
determine_mainline_module=determine_mainline_module,
setup_addon_register=setup_addon_register,
):
yield __main__

find_photons_app_options.assert_called_once_with(configuration, args_dict)
determine_mainline_module.assert_called_once_with()
setup_addon_register.assert_called_once_with(photons_app, __main__)

def test_it_puts_things_into_the_configuration_and_sets_up_the_addon_register(self):
extra = str(uuid.uuid1())
photons_app = {"extra": extra}
Expand All @@ -142,7 +139,7 @@ def test_it_puts_things_into_the_configuration_and_sets_up_the_addon_register(se
register = mock.Mock(name="register")
args_dict = mock.Mock(name="args_dict")

with self.mocks(collector, configuration, args_dict, photons_app, register):
with mocks(collector, configuration, args_dict, photons_app, register):
collector.extra_prepare(configuration, args_dict)

class AFuture:
Expand Down Expand Up @@ -794,7 +791,7 @@ def test_it_registers_converters_for_serveral_things(self):

assert type(photons_app) is PhotonsApp

assert list(targets.keys()) is ["one"]
assert list(targets.keys()) == ["one"]
assert type(targets["one"]) is Target
assert targets["one"].as_dict() == {
"type": "special",
Expand Down
Loading

0 comments on commit 10a4f86

Please sign in to comment.