-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow omitted rule constraints to match any value (#101)
The definition of umodbus.server.route gives default values of None to the arguments, suggesting they can be omitted. However, if you actually provide None, it will fail later when evaluating DataRule.match. This change makes it possible to omit any rule constraint to match on any value.
- Loading branch information
Showing
4 changed files
with
47 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import pytest | ||
|
||
from umodbus.route import DataRule | ||
|
||
|
||
endpoint = lambda slave_id, function_code, address: 0 | ||
|
||
|
||
def test_basic_route(): | ||
rule = DataRule(endpoint, slave_ids=[1], function_codes=[1], addresses=[1]) | ||
assert rule.match(slave_id=1, function_code=1, address=1) | ||
assert not rule.match(slave_id=0, function_code=1, address=1) | ||
assert not rule.match(slave_id=1, function_code=0, address=1) | ||
assert not rule.match(slave_id=1, function_code=1, address=0) | ||
|
||
|
||
def test_other_iterables(): | ||
# Other iterable types should work, not just lists | ||
rule = DataRule(endpoint, | ||
slave_ids=set([1]), function_codes=[1], addresses=[1]) | ||
assert rule.match(slave_id=1, function_code=1, address=1) | ||
|
||
|
||
def test_wildcard_slave_id(): | ||
rule = DataRule(endpoint, slave_ids=None, function_codes=[1], addresses=[1]) | ||
assert rule.match(slave_id=1, function_code=1, address=1) | ||
|
||
|
||
def test_wildcard_function_code(): | ||
rule = DataRule(endpoint, slave_ids=[1], function_codes=None, addresses=[1]) | ||
assert rule.match(slave_id=1, function_code=1, address=1) | ||
|
||
|
||
def test_wildcard_address(): | ||
rule = DataRule(endpoint, slave_ids=[1], function_codes=[1], addresses=None) | ||
assert rule.match(slave_id=1, function_code=1, address=1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters