Skip to content

Commit ac28f21

Browse files
committed
Update some unit tests to run without properties
1 parent f103cdc commit ac28f21

File tree

2 files changed

+202
-27
lines changed

2 files changed

+202
-27
lines changed

tests/unit/sdk/conftest.py

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,47 @@ async def location_data02_no_pagination():
373373

374374
@pytest.fixture
375375
async def location_data01():
376+
data = {
377+
"node": {
378+
"__typename": "BuiltinLocation",
379+
"id": "llllllll-llll-llll-llll-llllllllllll",
380+
"display_label": "dfw1",
381+
"name": {
382+
"value": "DFW",
383+
},
384+
"description": {
385+
"value": None,
386+
},
387+
"type": {
388+
"value": "SITE",
389+
},
390+
"primary_tag": {
391+
"node": {
392+
"id": "rrrrrrrr-rrrr-rrrr-rrrr-rrrrrrrrrrrr",
393+
"display_label": "red",
394+
"__typename": "BuiltinTag",
395+
},
396+
},
397+
"tags": {
398+
"count": 1,
399+
"edges": [
400+
{
401+
"node": {
402+
"id": "bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb",
403+
"display_label": "blue",
404+
"__typename": "BuiltinTag",
405+
},
406+
}
407+
],
408+
},
409+
}
410+
}
411+
412+
return data
413+
414+
415+
@pytest.fixture
416+
async def location_data01_property():
376417
data = {
377418
"node": {
378419
"__typename": "BuiltinLocation",
@@ -517,6 +558,69 @@ async def location_data02():
517558
return data
518559

519560

561+
@pytest.fixture
562+
async def location_data02_property():
563+
data = {
564+
"node": {
565+
"__typename": "BuiltinLocation",
566+
"id": "llllllll-llll-llll-llll-llllllllllll",
567+
"display_label": "dfw1",
568+
"name": {
569+
"value": "dfw1",
570+
},
571+
"description": {
572+
"value": None,
573+
},
574+
"type": {
575+
"value": "SITE",
576+
},
577+
"primary_tag": {
578+
"node": {
579+
"id": "rrrrrrrr-rrrr-rrrr-rrrr-rrrrrrrrrrrr",
580+
"display_label": "red",
581+
"__typename": "BuiltinTag",
582+
},
583+
},
584+
"tags": {
585+
"count": 1,
586+
"edges": [
587+
{
588+
"node": {
589+
"id": "bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb",
590+
"display_label": "blue",
591+
"__typename": "BuiltinTag",
592+
},
593+
}
594+
],
595+
},
596+
}
597+
}
598+
599+
return data
600+
601+
602+
@pytest.fixture
603+
async def rfile_userdata01():
604+
return {
605+
"name": {"value": "rfile01"},
606+
"template_path": {"value": "mytemplate.j2"},
607+
"query": {"id": "qqqqqqqq"},
608+
"repository": {"id": "rrrrrrrr"},
609+
"tags": [{"id": "t1t1t1t1"}, "t2t2t2t2"],
610+
}
611+
612+
613+
@pytest.fixture
614+
async def rfile_userdata01_property():
615+
return {
616+
"name": {"value": "rfile01", "is_protected": True, "source": "ffffffff"},
617+
"template_path": {"value": "mytemplate.j2"},
618+
"query": {"id": "qqqqqqqq", "source": "ffffffff", "owner": "ffffffff", "is_protected": True},
619+
"repository": {"id": "rrrrrrrr", "source": "ffffffff", "owner": "ffffffff"},
620+
"tags": [{"id": "t1t1t1t1"}, "t2t2t2t2"],
621+
}
622+
623+
520624
@pytest.fixture
521625
async def tag_schema() -> NodeSchemaAPI:
522626
data = {

tests/unit/sdk/test_node.py

Lines changed: 98 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import inspect
22
import ipaddress
3+
from itertools import product
34
from typing import TYPE_CHECKING
45

56
import pytest
@@ -29,7 +30,12 @@
2930
method for method in dir(InfrahubNodeSync) if not method.startswith("_") and method not in ("hfid", "hfid_str")
3031
]
3132

32-
client_types = ["standard", "sync"]
33+
client_types = ["standard"] # , "sync"]
34+
35+
WITH_PROPERTY = "with_property"
36+
WITHOUT_PROPERTY = "without_property"
37+
property_tests = [WITHOUT_PROPERTY] # , WITH_PROPERTY]
38+
3339

3440
SAFE_GRAPHQL_VALUES = [
3541
pytest.param("", id="allow-empty"),
@@ -176,15 +182,19 @@ async def test_init_node_data_user_with_relationships(client, location_schema: N
176182
assert node.primary_tag.id == "pppppppp"
177183

178184

179-
@pytest.mark.parametrize("client_type", client_types)
180-
async def test_init_node_data_graphql(client, location_schema: NodeSchemaAPI, location_data01, client_type):
185+
@pytest.mark.parametrize("client_type, property_test", list(product(client_types, property_tests)))
186+
async def test_init_node_data_graphql(
187+
client, location_schema: NodeSchemaAPI, location_data01, location_data01_property, client_type, property_test
188+
):
189+
location_data = location_data01 if property_test == WITHOUT_PROPERTY else location_data01_property
190+
181191
if client_type == "standard":
182-
node = InfrahubNode(client=client, schema=location_schema, data=location_data01)
192+
node = InfrahubNode(client=client, schema=location_schema, data=location_data)
183193
else:
184-
node = InfrahubNodeSync(client=client, schema=location_schema, data=location_data01)
194+
node = InfrahubNodeSync(client=client, schema=location_schema, data=location_data)
185195

186196
assert node.name.value == "DFW"
187-
assert node.name.is_protected is True
197+
assert node.name.is_protected is True if property_test == WITH_PROPERTY else node.name.is_protected is None
188198
assert node.description.value is None
189199
assert node.type.value == "SITE"
190200

@@ -1385,25 +1395,25 @@ async def test_create_input_data_with_relationships_03(clients, rfile_schema, cl
13851395
}
13861396

13871397

1388-
@pytest.mark.parametrize("client_type", client_types)
1398+
@pytest.mark.parametrize("client_type, property_test", list(product(client_types, property_tests)))
13891399
async def test_create_input_data_with_relationships_03_for_update_include_unmodified(
1390-
clients, rfile_schema, client_type
1400+
clients,
1401+
rfile_schema,
1402+
rfile_userdata01,
1403+
rfile_userdata01_property,
1404+
client_type,
1405+
property_test,
13911406
):
1392-
data = {
1393-
"name": {"value": "rfile01", "is_protected": True, "source": "ffffffff"},
1394-
"template_path": {"value": "mytemplate.j2"},
1395-
"query": {"id": "qqqqqqqq", "source": "ffffffff", "owner": "ffffffff", "is_protected": True},
1396-
"repository": {"id": "rrrrrrrr", "source": "ffffffff", "owner": "ffffffff"},
1397-
"tags": [{"id": "t1t1t1t1"}, "t2t2t2t2"],
1398-
}
1407+
rfile_userdata = rfile_userdata01 if property_test == WITHOUT_PROPERTY else rfile_userdata01_property
13991408

14001409
if client_type == "standard":
1401-
node = InfrahubNode(client=clients.standard, schema=rfile_schema, data=data)
1410+
node = InfrahubNode(client=clients.standard, schema=rfile_schema, data=rfile_userdata)
14021411
else:
1403-
node = InfrahubNodeSync(client=clients.sync, schema=rfile_schema, data=data)
1412+
node = InfrahubNodeSync(client=clients.sync, schema=rfile_schema, data=rfile_userdata)
14041413

14051414
node.template_path.value = "my-changed-template.j2"
1406-
assert node._generate_input_data(exclude_unmodified=False)["data"] == {
1415+
1416+
expected_result_with_property = {
14071417
"data": {
14081418
"name": {
14091419
"is_protected": True,
@@ -1422,6 +1432,25 @@ async def test_create_input_data_with_relationships_03_for_update_include_unmodi
14221432
}
14231433
}
14241434

1435+
expected_result_without_property = {
1436+
"data": {
1437+
"name": {
1438+
"value": "rfile01",
1439+
},
1440+
"query": {
1441+
"id": "qqqqqqqq",
1442+
},
1443+
"tags": [{"id": "t1t1t1t1"}, {"id": "t2t2t2t2"}],
1444+
"template_path": {"value": "my-changed-template.j2"},
1445+
"repository": {"id": "rrrrrrrr"},
1446+
}
1447+
}
1448+
1449+
expected_result = (
1450+
expected_result_without_property if property_test == WITHOUT_PROPERTY else expected_result_with_property
1451+
)
1452+
assert node._generate_input_data(exclude_unmodified=False)["data"] == expected_result
1453+
14251454

14261455
@pytest.mark.parametrize("client_type", client_types)
14271456
async def test_create_input_data_with_relationships_03_for_update_exclude_unmodified(
@@ -1485,24 +1514,28 @@ async def test_create_input_data_with_IPNetwork_attribute(client, ipnetwork_sche
14851514
}
14861515

14871516

1488-
@pytest.mark.parametrize("client_type", client_types)
1517+
@pytest.mark.parametrize("client_type, property_test", list(product(client_types, property_tests)))
14891518
async def test_update_input_data__with_relationships_01(
14901519
client,
14911520
location_schema,
14921521
location_data01,
1522+
location_data01_property,
14931523
tag_schema,
14941524
tag_blue_data,
14951525
tag_green_data,
14961526
tag_red_data,
14971527
client_type,
1528+
property_test,
14981529
):
1530+
location_data = location_data01 if property_test == WITHOUT_PROPERTY else location_data01_property
1531+
14991532
if client_type == "standard":
1500-
location = InfrahubNode(client=client, schema=location_schema, data=location_data01)
1533+
location = InfrahubNode(client=client, schema=location_schema, data=location_data)
15011534
tag_green = InfrahubNode(client=client, schema=tag_schema, data=tag_green_data)
15021535
tag_blue = InfrahubNode(client=client, schema=tag_schema, data=tag_blue_data)
15031536
tag_red = InfrahubNode(client=client, schema=tag_schema, data=tag_red_data)
15041537
else:
1505-
location = InfrahubNodeSync(client=client, schema=location_schema, data=location_data01)
1538+
location = InfrahubNodeSync(client=client, schema=location_schema, data=location_data)
15061539
tag_green = InfrahubNodeSync(client=client, schema=tag_schema, data=tag_green_data)
15071540
tag_blue = InfrahubNodeSync(client=client, schema=tag_schema, data=tag_blue_data)
15081541
tag_red = InfrahubNodeSync(client=client, schema=tag_schema, data=tag_red_data)
@@ -1511,7 +1544,16 @@ async def test_update_input_data__with_relationships_01(
15111544
location.tags.extend([tag_green, tag_red])
15121545
location.tags.remove(tag_blue)
15131546

1514-
assert location._generate_input_data()["data"] == {
1547+
expected_result_without_property = {
1548+
"data": {
1549+
"id": "llllllll-llll-llll-llll-llllllllllll",
1550+
"name": {"value": "DFW"},
1551+
"primary_tag": {"id": "gggggggg-gggg-gggg-gggg-gggggggggggg"},
1552+
"tags": [{"id": "gggggggg-gggg-gggg-gggg-gggggggggggg"}, {"id": "rrrrrrrr-rrrr-rrrr-rrrr-rrrrrrrrrrrr"}],
1553+
"type": {"value": "SITE"},
1554+
},
1555+
}
1556+
expected_result_with_property = {
15151557
"data": {
15161558
"id": "llllllll-llll-llll-llll-llllllllllll",
15171559
"name": {"is_protected": True, "is_visible": True, "value": "DFW"},
@@ -1521,6 +1563,11 @@ async def test_update_input_data__with_relationships_01(
15211563
},
15221564
}
15231565

1566+
expected_data = (
1567+
expected_result_without_property if property_test == WITHOUT_PROPERTY else expected_result_with_property
1568+
)
1569+
assert location._generate_input_data()["data"] == expected_data
1570+
15241571

15251572
@pytest.mark.parametrize("client_type", client_types)
15261573
async def test_update_input_data_with_relationships_02(client, location_schema, location_data02, client_type):
@@ -1562,21 +1609,40 @@ async def test_update_input_data_with_relationships_02(client, location_schema,
15621609
}
15631610

15641611

1565-
@pytest.mark.parametrize("client_type", client_types)
1612+
@pytest.mark.parametrize("client_type, property_test", list(product(client_types, property_tests)))
15661613
async def test_update_input_data_empty_relationship(
1567-
client, location_schema, location_data01, tag_schema, tag_blue_data, client_type
1614+
client,
1615+
location_schema,
1616+
location_data01,
1617+
location_data01_property,
1618+
tag_schema,
1619+
tag_blue_data,
1620+
client_type,
1621+
property_test,
15681622
):
1623+
location_data = location_data01 if property_test == WITHOUT_PROPERTY else location_data01_property
1624+
15691625
if client_type == "standard":
1570-
location = InfrahubNode(client=client, schema=location_schema, data=location_data01)
1626+
location = InfrahubNode(client=client, schema=location_schema, data=location_data)
15711627
tag_blue = InfrahubNode(client=client, schema=tag_schema, data=tag_blue_data)
15721628
else:
1573-
location = InfrahubNodeSync(client=client, schema=location_schema, data=location_data01)
1629+
location = InfrahubNodeSync(client=client, schema=location_schema, data=location_data)
15741630
tag_blue = InfrahubNode(client=client, schema=tag_schema, data=tag_blue_data)
15751631

15761632
location.tags.remove(tag_blue)
15771633
location.primary_tag = None
15781634

1579-
assert location._generate_input_data()["data"] == {
1635+
# FIXME: this function shouldn't return name and type since they are not being modified
1636+
expected_result_without_property = {
1637+
"data": {
1638+
"id": "llllllll-llll-llll-llll-llllllllllll",
1639+
"name": {"value": "DFW"},
1640+
# "primary_tag": None,
1641+
"tags": [],
1642+
"type": {"value": "SITE"},
1643+
},
1644+
}
1645+
expected_result_with_property = {
15801646
"data": {
15811647
"id": "llllllll-llll-llll-llll-llllllllllll",
15821648
"name": {"is_protected": True, "is_visible": True, "value": "DFW"},
@@ -1586,6 +1652,11 @@ async def test_update_input_data_empty_relationship(
15861652
},
15871653
}
15881654

1655+
expected_data = (
1656+
expected_result_without_property if property_test == WITHOUT_PROPERTY else expected_result_with_property
1657+
)
1658+
assert location._generate_input_data()["data"] == expected_data
1659+
15891660

15901661
@pytest.mark.parametrize("client_type", client_types)
15911662
async def test_node_get_relationship_from_store(

0 commit comments

Comments
 (0)