Skip to content

Commit

Permalink
Tests rule loading
Browse files Browse the repository at this point in the history
- Checks builtin rules
- Checks loading from YAML file
  • Loading branch information
MattHag committed May 6, 2024
1 parent a317f66 commit efd8f61
Showing 1 changed file with 22 additions and 7 deletions.
29 changes: 22 additions & 7 deletions tests/logitech_receiver/test_diversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,19 @@
from logitech_receiver.diversion import rule_storage


def test_load_builtin_rules(rule_config):
expected_rules = [
[diversion.Key, diversion.KeyPress],
[diversion.Key, diversion.KeyPress],
]

with mock.patch("os.path.isfile", return_value=False):
loaded_rules = rule_storage.load_config()

assert len(loaded_rules.components) == 1 # predefined and user configured rules
assert_expected_rules(loaded_rules, expected_rules)


@pytest.fixture
def rule_config():
rule_content = """
Expand Down Expand Up @@ -36,20 +49,22 @@ def rule_config():
return textwrap.dedent(rule_content)


def test_load_rule_config(rule_config):
def test_load_rule_from_yaml_file(rule_config):
expected_rules = [
[
diversion.MouseGesture,
diversion.KeyPress,
],
[diversion.MouseGesture, diversion.KeyPress],
[diversion.MouseGesture, diversion.KeyPress],
[diversion.Test, diversion.KeyPress],
]

with mock.patch("builtins.open", new=mock_open(read_data=rule_config)):
loaded_rules = rule_storage.load_config()
with mock.patch("os.path.isfile", return_value=True):
with mock.patch("builtins.open", new=mock_open(read_data=rule_config)):
loaded_rules = rule_storage.load_config()

assert len(loaded_rules.components) == 2 # predefined and user configured rules
assert_expected_rules(loaded_rules, expected_rules)


def assert_expected_rules(loaded_rules, expected_rules):
user_configured_rules = loaded_rules.components[0]
assert isinstance(user_configured_rules, diversion.Rule)

Expand Down

0 comments on commit efd8f61

Please sign in to comment.