|
4 | 4 |
|
5 | 5 | from typing_extensions import Optional |
6 | 6 |
|
7 | | -from posit.connect.resources import BaseResource |
| 7 | +from posit.connect.resources import ( |
| 8 | + BaseResource, |
| 9 | + _contains_dict_key_values, |
| 10 | + _matches_exact, |
| 11 | + _matches_pattern, |
| 12 | +) |
8 | 13 |
|
9 | 14 | config = Mock() |
10 | 15 | session = Mock() |
@@ -62,3 +67,74 @@ def test_foo(self): |
62 | 67 | d = {k: v} |
63 | 68 | r = FakeResource(mock.Mock(), **d) |
64 | 69 | assert r.foo == v |
| 70 | + |
| 71 | + |
| 72 | +class TestContainsDictKeyValues: |
| 73 | + def test_empty_value_dict(self): |
| 74 | + r = FakeResource(mock.Mock(), foo={"a": 1, "b": 2}) |
| 75 | + assert _contains_dict_key_values(r, "foo", {}) is True |
| 76 | + |
| 77 | + def test_matching_single_key_value(self): |
| 78 | + r = FakeResource(mock.Mock(), foo={"a": 1, "b": 2}) |
| 79 | + assert _contains_dict_key_values(r, "foo", {"a": 1}) is True |
| 80 | + |
| 81 | + def test_matching_multiple_key_values(self): |
| 82 | + r = FakeResource(mock.Mock(), foo={"a": 1, "b": 2, "c": 3}) |
| 83 | + assert _contains_dict_key_values(r, "foo", {"a": 1, "b": 2}) is True |
| 84 | + |
| 85 | + def test_non_matching_key_value(self): |
| 86 | + r = FakeResource(mock.Mock(), foo={"a": 1, "b": 2}) |
| 87 | + assert _contains_dict_key_values(r, "foo", {"a": 2}) is False |
| 88 | + |
| 89 | + def test_missing_key_in_item(self): |
| 90 | + r = FakeResource(mock.Mock(), foo={"a": 1}) |
| 91 | + assert _contains_dict_key_values(r, "foo", {"b": 2}) is False |
| 92 | + |
| 93 | + def test_missing_field_in_resource(self): |
| 94 | + r = FakeResource(mock.Mock()) |
| 95 | + assert _contains_dict_key_values(r, "nonexistent", {"a": 1}) is False |
| 96 | + |
| 97 | + def test_non_dict_field_value(self): |
| 98 | + r = FakeResource(mock.Mock(), foo="not_a_dict") |
| 99 | + assert _contains_dict_key_values(r, "foo", {"a": 1}) is False |
| 100 | + |
| 101 | + def test_nested_dict_values(self): |
| 102 | + r = FakeResource(mock.Mock(), foo={"nested": {"x": 10}, "simple": "value"}) |
| 103 | + assert _contains_dict_key_values(r, "foo", {"nested": {"x": 10}}) is True |
| 104 | + assert _contains_dict_key_values(r, "foo", {"nested": {"x": 20}}) is False |
| 105 | + |
| 106 | + |
| 107 | +class TestMatchesPattern: |
| 108 | + def test_pattern_matches(self): |
| 109 | + r = FakeResource(mock.Mock(), name="test-app-123") |
| 110 | + assert _matches_pattern(r, "name", r"test-.*-\d+") is True |
| 111 | + |
| 112 | + def test_pattern_does_not_match(self): |
| 113 | + r = FakeResource(mock.Mock(), name="production-app") |
| 114 | + assert _matches_pattern(r, "name", r"test-.*-\d+") is False |
| 115 | + |
| 116 | + def test_missing_field_returns_false(self): |
| 117 | + r = FakeResource(mock.Mock()) |
| 118 | + assert _matches_pattern(r, "nonexistent", ".*") is False |
| 119 | + |
| 120 | + def test_empty_pattern_matches_any_string(self): |
| 121 | + r = FakeResource(mock.Mock(), description="any text here") |
| 122 | + assert _matches_pattern(r, "description", "") is True |
| 123 | + |
| 124 | + def test_partial_match_returns_true(self): |
| 125 | + r = FakeResource(mock.Mock(), title="My Great App") |
| 126 | + assert _matches_pattern(r, "title", "Great") is True |
| 127 | + |
| 128 | + |
| 129 | +class TestMatchesExact: |
| 130 | + def test_exact_match_returns_true(self): |
| 131 | + r = FakeResource(mock.Mock(), name="test-app") |
| 132 | + assert _matches_exact(r, "name", "test-app") is True |
| 133 | + |
| 134 | + def test_no_match_returns_false(self): |
| 135 | + r = FakeResource(mock.Mock(), name="test-app") |
| 136 | + assert _matches_exact(r, "name", "other-app") is False |
| 137 | + |
| 138 | + def test_missing_field_returns_false(self): |
| 139 | + r = FakeResource(mock.Mock()) |
| 140 | + assert _matches_exact(r, "nonexistent", "value") is False |
0 commit comments