|
1 | | -from sphinxcontrib.test_reports.functions import tr_link |
| 1 | +from pathlib import Path |
| 2 | + |
| 3 | +import importlib.util |
| 4 | +import sys |
| 5 | + |
| 6 | + |
| 7 | +def import_from_path(path, name): |
| 8 | + spec = importlib.util.spec_from_file_location(name, path) |
| 9 | + module = importlib.util.module_from_spec(spec) |
| 10 | + sys.modules[name] = module |
| 11 | + spec.loader.exec_module(module) |
| 12 | + return module |
| 13 | + |
| 14 | + |
| 15 | +# (!) We need to import the 'tr_link' method from the local module, |
| 16 | +# otherwise the method from the installed 'sphinxcontrib' package will be used. |
| 17 | +local_functions_module = import_from_path( |
| 18 | + Path(__file__).parent.parent.joinpath( |
| 19 | + "sphinxcontrib/test_reports/functions/__init__.py" |
| 20 | + ), |
| 21 | + "functions", |
| 22 | +) |
| 23 | +tr_link = local_functions_module.tr_link |
2 | 24 |
|
3 | 25 |
|
4 | 26 | def test_tr_link_option_not_in_need(): |
5 | 27 | """ |
6 | 28 | Return an empty string when the specified test option is missing from the need. |
7 | 29 | """ |
8 | | - assert tr_link(app=None, need={}, needs={}, test_option="a", target_option="b") == "" |
| 30 | + assert ( |
| 31 | + tr_link(app=None, need={}, needs={}, test_option="a", target_option="b") == "" |
| 32 | + ) |
| 33 | + |
9 | 34 |
|
10 | 35 | def test_tr_link_no_target_option_in_needs(): |
11 | 36 | """ |
12 | 37 | Return an empty list when the target option is missing in all items of needs. |
13 | 38 | """ |
14 | | - assert tr_link(app=None, need={"a": "1"}, needs={"x": {"id": "123"}}, test_option="a", target_option="b") == [] |
| 39 | + assert ( |
| 40 | + tr_link( |
| 41 | + app=None, |
| 42 | + need={"a": "1"}, |
| 43 | + needs={"x": {"id": "123"}}, |
| 44 | + test_option="a", |
| 45 | + target_option="b", |
| 46 | + ) |
| 47 | + == [] |
| 48 | + ) |
| 49 | + |
15 | 50 |
|
16 | 51 | def test_tr_link_no_match(): |
17 | 52 | """ |
18 | | - Returns an empty list when no matching value for the test option is found in any of the target options within needs. |
| 53 | + Returns an empty list when no matching value for the test option is found |
| 54 | + in any of the target options within needs. |
19 | 55 | """ |
20 | | - assert tr_link(app=None, need={"a": "1"}, needs={"x": {"b": "2", "id": "123"}}, test_option="a", target_option="b") == [] |
| 56 | + assert ( |
| 57 | + tr_link( |
| 58 | + app=None, |
| 59 | + need={"a": "1"}, |
| 60 | + needs={"x": {"b": "2", "id": "123"}}, |
| 61 | + test_option="a", |
| 62 | + target_option="b", |
| 63 | + ) |
| 64 | + == [] |
| 65 | + ) |
| 66 | + |
21 | 67 |
|
22 | 68 | def test_tr_link_match(): |
23 | 69 | """ |
24 | 70 | Returns a list of ids when there is a matching value in both need and needs. |
25 | 71 | """ |
26 | | - assert tr_link(app=None, need={"a": "1"}, needs={"x": {"b": "1", "id": "123"}}, test_option="a", target_option="b") == ["123"] |
| 72 | + assert tr_link( |
| 73 | + app=None, |
| 74 | + need={"a": "1"}, |
| 75 | + needs={"x": {"b": "1", "id": "123"}}, |
| 76 | + test_option="a", |
| 77 | + target_option="b", |
| 78 | + ) == ["123"] |
| 79 | + |
27 | 80 |
|
28 | 81 | def test_tr_link_none_or_empty(): |
29 | 82 | """ |
30 | 83 | 'None' and empty string values are not considered as valid matches. |
31 | 84 | """ |
32 | 85 | need = {"a": None, "b": ""} |
33 | | - needs = {"x": {"c": None, "id": "111"}, "y": {"c": "valid", "id": "222"}, "z": {"c": "", "id": "333"}} |
34 | | - assert tr_link(app=None, need=need, needs=needs, test_option="b", target_option="c") == [] |
35 | | - assert tr_link(app=None, need=need, needs=needs, test_option="a", target_option="c") == [] |
| 86 | + needs = { |
| 87 | + "x": {"c": None, "id": "111"}, |
| 88 | + "y": {"c": "valid", "id": "222"}, |
| 89 | + "z": {"c": "", "id": "333"}, |
| 90 | + } |
| 91 | + assert ( |
| 92 | + tr_link(app=None, need=need, needs=needs, test_option="b", target_option="c") |
| 93 | + == [] |
| 94 | + ) |
| 95 | + assert ( |
| 96 | + tr_link(app=None, need=need, needs=needs, test_option="a", target_option="c") |
| 97 | + == [] |
| 98 | + ) |
| 99 | + |
| 100 | + |
| 101 | +def test_tr_link_regex_match(): |
| 102 | + """ |
| 103 | + Returns a list of ids when the test option value containing an asterisk (*) |
| 104 | + correctly matches target options using regular expression patterns. |
| 105 | + """ |
| 106 | + needs = { |
| 107 | + "x": {"b": "abc123", "id": "111"}, |
| 108 | + "y": {"b": "def456", "id": "222"}, |
| 109 | + "z": {"b": "ghi789", "id": "333"}, |
| 110 | + } |
| 111 | + need = {"a": "abc.*"} |
| 112 | + assert tr_link( |
| 113 | + app=None, need=need, needs=needs, test_option="a", target_option="b" |
| 114 | + ) == ["111"] |
| 115 | + |
| 116 | + |
| 117 | +def test_tr_link_regex_no_match(): |
| 118 | + """ |
| 119 | + Returns an empty list when the test option value containing an asterisk (*) |
| 120 | + does not match any target options using regular expression patterns. |
| 121 | + """ |
| 122 | + needs = {"x": {"b": "abc123", "id": "111"}, "y": {"b": "def456", "id": "222"}} |
| 123 | + need = {"a": "xyz.*"} |
| 124 | + assert ( |
| 125 | + tr_link(app=None, need=need, needs=needs, test_option="a", target_option="b") |
| 126 | + == [] |
| 127 | + ) |
0 commit comments