Skip to content

Commit 6c89f35

Browse files
committed
add unit test
1 parent 93b9994 commit 6c89f35

File tree

1 file changed

+160
-0
lines changed

1 file changed

+160
-0
lines changed
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
import pytest
2+
from pytest_httpx import HTTPXMock
3+
4+
from infrahub_sdk import InfrahubClient
5+
from tests.unit.sdk.conftest import BothClients
6+
7+
client_types = ["standard", "sync"]
8+
9+
10+
@pytest.fixture
11+
async def mock_diff_tree_query(httpx_mock: HTTPXMock, client: InfrahubClient) -> HTTPXMock:
12+
response = {
13+
"data": {
14+
"DiffTree": {
15+
"nodes": [
16+
{
17+
"attributes": [],
18+
"kind": "TestCar",
19+
"label": "nolt #444444",
20+
"num_added": 0,
21+
"num_removed": 0,
22+
"num_updated": 1,
23+
"relationships": [
24+
{
25+
"cardinality": "ONE",
26+
"elements": [{"num_added": 0, "num_removed": 0, "num_updated": 1, "status": "UPDATED"}],
27+
"name": "owner",
28+
"num_added": 0,
29+
"num_removed": 0,
30+
"num_updated": 1,
31+
"status": "UPDATED",
32+
}
33+
],
34+
"status": "UPDATED",
35+
"uuid": "17fbadf0-6637-4fa2-43e6-1677ea170e0f",
36+
},
37+
{
38+
"attributes": [],
39+
"kind": "TestPerson",
40+
"label": "Jane",
41+
"num_added": 0,
42+
"num_removed": 0,
43+
"num_updated": 1,
44+
"relationships": [
45+
{
46+
"cardinality": "MANY",
47+
"elements": [{"num_added": 0, "num_removed": 3, "num_updated": 0, "status": "REMOVED"}],
48+
"name": "cars",
49+
"num_added": 0,
50+
"num_removed": 1,
51+
"num_updated": 0,
52+
"status": "UPDATED",
53+
}
54+
],
55+
"status": "UPDATED",
56+
"uuid": "17fbadf0-634f-05a8-43e4-1677e744d4c0",
57+
},
58+
{
59+
"attributes": [
60+
{"name": "name", "num_added": 0, "num_removed": 0, "num_updated": 1, "status": "UPDATED"}
61+
],
62+
"kind": "TestPerson",
63+
"label": "Jonathan",
64+
"num_added": 0,
65+
"num_removed": 0,
66+
"num_updated": 2,
67+
"relationships": [
68+
{
69+
"cardinality": "MANY",
70+
"elements": [{"num_added": 3, "num_removed": 0, "num_updated": 0, "status": "ADDED"}],
71+
"name": "cars",
72+
"num_added": 1,
73+
"num_removed": 0,
74+
"num_updated": 0,
75+
"status": "UPDATED",
76+
}
77+
],
78+
"status": "UPDATED",
79+
"uuid": "17fbadf0-6243-5d3c-43ee-167718ff8dac",
80+
},
81+
]
82+
}
83+
}
84+
}
85+
86+
httpx_mock.add_response(
87+
method="POST",
88+
json=response,
89+
match_headers={"X-Infrahub-Tracker": "query-difftree"},
90+
)
91+
return httpx_mock
92+
93+
94+
@pytest.mark.parametrize("client_type", client_types)
95+
async def test_diffsummary(clients: BothClients, mock_diff_tree_query, client_type):
96+
if client_type == "standard":
97+
node_diffs = await clients.standard.get_diff_summary(
98+
branch="branch2",
99+
tracker="query-difftree",
100+
)
101+
else:
102+
node_diffs = clients.sync.get_diff_summary(
103+
branch="branch2",
104+
tracker="query-difftree",
105+
)
106+
107+
assert len(node_diffs) == 3
108+
assert {
109+
"branch": "branch2",
110+
"kind": "TestCar",
111+
"id": "17fbadf0-6637-4fa2-43e6-1677ea170e0f",
112+
"action": "None",
113+
"display_label": "nolt #444444",
114+
"elements": [
115+
{
116+
"action": "UPDATED",
117+
"element_type": "RELATIONSHIP_ONE",
118+
"name": "owner",
119+
"summary": {"added": 0, "removed": 0, "updated": 1},
120+
}
121+
],
122+
} in node_diffs
123+
assert {
124+
"branch": "branch2",
125+
"kind": "TestPerson",
126+
"id": "17fbadf0-634f-05a8-43e4-1677e744d4c0",
127+
"action": "None",
128+
"display_label": "Jane",
129+
"elements": [
130+
{
131+
"action": "UPDATED",
132+
"element_type": "RELATIONSHIP_MANY",
133+
"name": "cars",
134+
"summary": {"added": 0, "removed": 1, "updated": 0},
135+
"peers": [{"action": "REMOVED", "summary": {"added": 0, "removed": 3, "updated": 0}}],
136+
}
137+
],
138+
} in node_diffs
139+
assert {
140+
"branch": "branch2",
141+
"kind": "TestPerson",
142+
"id": "17fbadf0-6243-5d3c-43ee-167718ff8dac",
143+
"action": "None",
144+
"display_label": "Jonathan",
145+
"elements": [
146+
{
147+
"action": "UPDATED",
148+
"element_type": "ATTRIBUTE",
149+
"name": "name",
150+
"summary": {"added": 0, "removed": 0, "updated": 1},
151+
},
152+
{
153+
"action": "UPDATED",
154+
"element_type": "RELATIONSHIP_MANY",
155+
"name": "cars",
156+
"summary": {"added": 1, "removed": 0, "updated": 0},
157+
"peers": [{"action": "ADDED", "summary": {"added": 3, "removed": 0, "updated": 0}}],
158+
},
159+
],
160+
} in node_diffs

0 commit comments

Comments
 (0)