diff --git a/libcoveofds/lib/common_checks.py b/libcoveofds/lib/common_checks.py index 7561115..cee61e3 100644 --- a/libcoveofds/lib/common_checks.py +++ b/libcoveofds/lib/common_checks.py @@ -249,11 +249,39 @@ def skip_if_any_related_resources(self) -> bool: return False +class IsNodeUsedInLinkAdditionalCheckForNetwork(AdditionalCheckForNetwork): + def __init__(self, lib_cove_bods_config, schema_object): + super().__init__(lib_cove_bods_config, schema_object) + self._node_ids_used_in_links = [] + + def check_link_first_pass(self, link: dict): + start = link.get("start") + if start and start not in self._node_ids_used_in_links: + self._node_ids_used_in_links.append(start) + end = link.get("end") + if end and end not in self._node_ids_used_in_links: + self._node_ids_used_in_links.append(end) + + def check_node_second_pass(self, node: dict): + id = node.get("id") + if id and id not in self._node_ids_used_in_links: + self._additional_check_results.append( + { + "type": "node_not_used_in_any_links", + "node_id": node.get("id"), + } + ) + + def skip_if_any_related_resources(self) -> bool: + return True + + ADDITIONAL_CHECK_CLASSES_FOR_NETWORK = [ LinksMustHaveValidNodesAdditionalCheckForNetwork, NodesLocationAndLinksRouteAdditionalCheckForNetwork, PhaseReferenceAdditionalCheckForNetwork, NodeInternationalConnectionCountryAdditionalCheckForNetwork, + IsNodeUsedInLinkAdditionalCheckForNetwork, ] diff --git a/tests/fixtures/0_1_0_alpha/end_node_not_found_1.json b/tests/fixtures/0_1_0_alpha/end_node_not_found_1.json index ab04526..e21daa2 100644 --- a/tests/fixtures/0_1_0_alpha/end_node_not_found_1.json +++ b/tests/fixtures/0_1_0_alpha/end_node_not_found_1.json @@ -7,10 +7,6 @@ { "id": "1", "name": "Accra" - }, - { - "id": "2", - "name": "Kumasi" } ], "links": [ diff --git a/tests/fixtures/0_1_0_alpha/node_location_coordinates_incorrect_1.json b/tests/fixtures/0_1_0_alpha/node_location_coordinates_incorrect_1.json index 2c3dcf9..9f50cdc 100644 --- a/tests/fixtures/0_1_0_alpha/node_location_coordinates_incorrect_1.json +++ b/tests/fixtures/0_1_0_alpha/node_location_coordinates_incorrect_1.json @@ -63,6 +63,18 @@ ] ] } + }, + { + "id": "2", + "name": "Kumasi" + } + ], + "links": [ + { + "id": "1", + "name": "Accra to Kumasi", + "start": "1", + "end": "2" } ] } diff --git a/tests/fixtures/0_1_0_alpha/node_location_type_incorrect_1.json b/tests/fixtures/0_1_0_alpha/node_location_type_incorrect_1.json index 33d1fc0..55e1f3f 100644 --- a/tests/fixtures/0_1_0_alpha/node_location_type_incorrect_1.json +++ b/tests/fixtures/0_1_0_alpha/node_location_type_incorrect_1.json @@ -14,6 +14,18 @@ 5.625 ] } + }, + { + "id": "2", + "name": "Kumasi" + } + ], + "links": [ + { + "id": "1", + "name": "Accra to Kumasi", + "start": "1", + "end": "2" } ] } diff --git a/tests/fixtures/0_1_0_alpha/node_not_used_in_any_links_1.json b/tests/fixtures/0_1_0_alpha/node_not_used_in_any_links_1.json new file mode 100644 index 0000000..ce56232 --- /dev/null +++ b/tests/fixtures/0_1_0_alpha/node_not_used_in_any_links_1.json @@ -0,0 +1,27 @@ +{ + "networks": [ + { + "id": "a096d627-72e1-4f9b-b129-951b1737bff4", + "name": "Ghana Fibre Network", + "nodes": [ + { + "id": "1" + }, + { + "id": "2" + }, + { + "id": "3" + } + ], + "links": [ + { + "id": "1", + "name": "Accra to Kumasi", + "start": "1", + "end": "2" + } + ] + } + ] +} \ No newline at end of file diff --git a/tests/fixtures/0_1_0_alpha/start_node_not_found_1.json b/tests/fixtures/0_1_0_alpha/start_node_not_found_1.json index e8f4b6a..f1c9780 100644 --- a/tests/fixtures/0_1_0_alpha/start_node_not_found_1.json +++ b/tests/fixtures/0_1_0_alpha/start_node_not_found_1.json @@ -4,10 +4,6 @@ "id": "a096d627-72e1-4f9b-b129-951b1737bff4", "name": "Ghana Fibre Network", "nodes": [ - { - "id": "1", - "name": "Accra" - }, { "id": "2", "name": "Kumasi" diff --git a/tests/test_api_0_1_0_alpha.py b/tests/test_api_0_1_0_alpha.py index 99b9c19..7f3f6b0 100644 --- a/tests/test_api_0_1_0_alpha.py +++ b/tests/test_api_0_1_0_alpha.py @@ -386,3 +386,28 @@ def test_node_international_connections_country_not_set_1(): "node_id": "1", "type": "node_international_connections_country_not_set", } + + +def test_node_not_used_in_any_links_1(): + cove_temp_folder = tempfile.mkdtemp( + prefix="lib-cove-ofds-tests-", dir=tempfile.gettempdir() + ) + json_filename = os.path.join( + os.path.dirname(os.path.realpath(__file__)), + "fixtures", + "0_1_0_alpha", + "node_not_used_in_any_links_1.json", + ) + + results = ofds_json_output(cove_temp_folder, json_filename) + + assert results["schema_version"] == "0.1.0-alpha" + + assert results["validation_errors_count"] == 0 + + assert results["additional_checks_count"] == 1 + assert results["additional_checks"][0] == { + "network_id": "a096d627-72e1-4f9b-b129-951b1737bff4", + "node_id": "3", + "type": "node_not_used_in_any_links", + }