|
| 1 | +"""SAMM Meta Model functions test suite.""" |
| 2 | + |
| 3 | +from unittest import mock |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from rdflib.term import URIRef |
| 8 | + |
| 9 | +from esmf_aspect_meta_model_python.samm_meta_model import SammUnitsGraph |
| 10 | + |
| 11 | + |
| 12 | +class TestSammCli: |
| 13 | + """SAMM Units Graph tests.""" |
| 14 | + |
| 15 | + @mock.patch("esmf_aspect_meta_model_python.samm_meta_model.SammUnitsGraph._get_units") |
| 16 | + @mock.patch("esmf_aspect_meta_model_python.samm_meta_model.SammUnitsGraph._validate_path") |
| 17 | + @mock.patch("esmf_aspect_meta_model_python.samm_meta_model.SammUnitsGraph._get_file_path") |
| 18 | + def test_init(self, get_file_path_mock, validate_path_mock, get_units_mock): |
| 19 | + get_file_path_mock.return_value = "unit_file_path" |
| 20 | + get_units_mock.return_value = "graph" |
| 21 | + result = SammUnitsGraph() |
| 22 | + |
| 23 | + assert result.graph == "graph" |
| 24 | + get_file_path_mock.assert_called_once() |
| 25 | + validate_path_mock.assert_called_once() |
| 26 | + get_units_mock.assert_called_once() |
| 27 | + |
| 28 | + @mock.patch("esmf_aspect_meta_model_python.samm_meta_model.SammUnitsGraph._get_units") |
| 29 | + @mock.patch("esmf_aspect_meta_model_python.samm_meta_model.SammUnitsGraph._validate_path") |
| 30 | + @mock.patch("esmf_aspect_meta_model_python.samm_meta_model.join") |
| 31 | + @mock.patch("esmf_aspect_meta_model_python.samm_meta_model.Path") |
| 32 | + def test_get_file_path(self, path_mock, join_mock, _, get_units_mock): |
| 33 | + base_path_mock = mock.MagicMock() |
| 34 | + base_path_mock.parents = ["parent", "child"] |
| 35 | + path_mock.return_value = path_mock |
| 36 | + path_mock.resolve.return_value = base_path_mock |
| 37 | + join_mock.return_value = "file_path" |
| 38 | + get_units_mock.return_value = "graph" |
| 39 | + result = SammUnitsGraph() |
| 40 | + |
| 41 | + assert result.unit_file_path == "file_path" |
| 42 | + path_mock.assert_called_once() |
| 43 | + path_mock.resolve.assert_called_once() |
| 44 | + join_mock.assert_called_once_with("parent", SammUnitsGraph.UNIT_FILE_PATH) |
| 45 | + |
| 46 | + @mock.patch("esmf_aspect_meta_model_python.samm_meta_model.SammUnitsGraph._get_units") |
| 47 | + @mock.patch("esmf_aspect_meta_model_python.samm_meta_model.exists") |
| 48 | + @mock.patch("esmf_aspect_meta_model_python.samm_meta_model.SammUnitsGraph._get_file_path") |
| 49 | + def test_validate_path(self, get_file_path_mock, exists_mock, get_units_mock): |
| 50 | + get_file_path_mock.return_value = "unit_file_path" |
| 51 | + exists_mock.return_value = True |
| 52 | + get_units_mock.return_value = "graph" |
| 53 | + result = SammUnitsGraph() |
| 54 | + |
| 55 | + assert result is not None |
| 56 | + exists_mock.assert_called_once_with("unit_file_path") |
| 57 | + |
| 58 | + @mock.patch("esmf_aspect_meta_model_python.samm_meta_model.exists") |
| 59 | + @mock.patch("esmf_aspect_meta_model_python.samm_meta_model.SammUnitsGraph._get_file_path") |
| 60 | + def test_validate_path_with_exception(self, get_file_path_mock, exists_mock): |
| 61 | + get_file_path_mock.return_value = "unit_file_path" |
| 62 | + exists_mock.return_value = False |
| 63 | + with pytest.raises(ValueError) as error: |
| 64 | + SammUnitsGraph() |
| 65 | + |
| 66 | + assert str(error.value) == "There is no such file unit_file_path" |
| 67 | + exists_mock.assert_called_once_with("unit_file_path") |
| 68 | + |
| 69 | + @mock.patch("esmf_aspect_meta_model_python.samm_meta_model.rdflib.Graph") |
| 70 | + @mock.patch("esmf_aspect_meta_model_python.samm_meta_model.SammUnitsGraph._validate_path") |
| 71 | + @mock.patch("esmf_aspect_meta_model_python.samm_meta_model.SammUnitsGraph._get_file_path") |
| 72 | + def test_get_units(self, get_file_path_mock, validate_path_mock, rdflib_graph_mock): |
| 73 | + get_file_path_mock.return_value = "unit_file_path" |
| 74 | + graph_mock = mock.MagicMock() |
| 75 | + rdflib_graph_mock.return_value = graph_mock |
| 76 | + result = SammUnitsGraph() |
| 77 | + |
| 78 | + assert result._graph == graph_mock |
| 79 | + rdflib_graph_mock.assert_called_once() |
| 80 | + graph_mock.parse.assert_called_once_with("unit_file_path", format="turtle") |
| 81 | + |
| 82 | + @mock.patch("esmf_aspect_meta_model_python.samm_meta_model.SammUnitsGraph._get_units") |
| 83 | + @mock.patch("esmf_aspect_meta_model_python.samm_meta_model.SammUnitsGraph._validate_path") |
| 84 | + @mock.patch("esmf_aspect_meta_model_python.samm_meta_model.SammUnitsGraph._get_file_path") |
| 85 | + def test_get_nested_data_unit(self, get_file_path_mock, _, get_units_mock): |
| 86 | + get_file_path_mock.return_value = "unit_file_path" |
| 87 | + get_units_mock.return_value = "graph" |
| 88 | + units_graph = SammUnitsGraph() |
| 89 | + result = units_graph._get_nested_data("prefix#Unit") |
| 90 | + |
| 91 | + assert result == ("Unit", "prefix#Unit") |
| 92 | + |
| 93 | + @mock.patch("esmf_aspect_meta_model_python.samm_meta_model.SammUnitsGraph.get_info") |
| 94 | + @mock.patch("esmf_aspect_meta_model_python.samm_meta_model.SammUnitsGraph._get_units") |
| 95 | + @mock.patch("esmf_aspect_meta_model_python.samm_meta_model.SammUnitsGraph._validate_path") |
| 96 | + @mock.patch("esmf_aspect_meta_model_python.samm_meta_model.SammUnitsGraph._get_file_path") |
| 97 | + def test_get_nested_data_not_unit(self, get_file_path_mock, _, get_units_mock, get_info_mock): |
| 98 | + get_file_path_mock.return_value = "unit_file_path" |
| 99 | + get_units_mock.return_value = "graph" |
| 100 | + get_info_mock.return_value = "nested_value" |
| 101 | + units_graph = SammUnitsGraph() |
| 102 | + result = units_graph._get_nested_data("prefix#unitType") |
| 103 | + |
| 104 | + assert result == ("unitType", "nested_value") |
| 105 | + get_info_mock.assert_called_once_with("unit:unitType") |
| 106 | + |
| 107 | + @mock.patch("esmf_aspect_meta_model_python.samm_meta_model.SammUnitsGraph._get_nested_data") |
| 108 | + @mock.patch("esmf_aspect_meta_model_python.samm_meta_model.isinstance") |
| 109 | + @mock.patch("esmf_aspect_meta_model_python.samm_meta_model.SammUnitsGraph._get_units") |
| 110 | + @mock.patch("esmf_aspect_meta_model_python.samm_meta_model.SammUnitsGraph._validate_path") |
| 111 | + @mock.patch("esmf_aspect_meta_model_python.samm_meta_model.SammUnitsGraph._get_file_path") |
| 112 | + def test_get_info(self, get_file_path_mock, _, get_units_mock, isinstance_mock, get_nested_data_mock): |
| 113 | + get_file_path_mock.return_value = "unit_file_path" |
| 114 | + isinstance_mock.side_effect = (False, URIRef, URIRef) |
| 115 | + get_nested_data_mock.side_effect = [("type_key", "type_description"), ("sub_unit", "sub_unit_description")] |
| 116 | + row_1_mock = mock.MagicMock() |
| 117 | + row_1_mock.key = "prefix#unitType" |
| 118 | + row_1_mock.value = "unit_1" |
| 119 | + row_2_mock = mock.MagicMock() |
| 120 | + row_2_mock.key = "prefix#type" |
| 121 | + row_2_mock.value = "unit_2" |
| 122 | + row_3_mock = mock.MagicMock() |
| 123 | + row_3_mock.key = "prefix#otherUnit" |
| 124 | + row_3_mock.value = "unit_3" |
| 125 | + graph_mock = mock.MagicMock() |
| 126 | + graph_mock.query.return_value = [row_1_mock, row_2_mock, row_3_mock] |
| 127 | + get_units_mock.return_value = graph_mock |
| 128 | + units_graph = SammUnitsGraph() |
| 129 | + result = units_graph.get_info("unit:unit_name") |
| 130 | + |
| 131 | + assert "unitType" in result |
| 132 | + assert result["unitType"] == "unit_1" |
| 133 | + assert "otherUnit" in result |
| 134 | + assert len(result["otherUnit"]) == 1 |
| 135 | + assert "sub_unit" in result["otherUnit"][0] |
| 136 | + assert result["otherUnit"][0]["sub_unit"] == "sub_unit_description" |
| 137 | + graph_mock.query.assert_called_once_with("SELECT ?key ?value WHERE {unit:unit_name ?key ?value .}") |
| 138 | + isinstance_mock.assert_has_calls( |
| 139 | + [ |
| 140 | + mock.call("unit_1", URIRef), |
| 141 | + mock.call("unit_2", URIRef), |
| 142 | + mock.call("unit_3", URIRef), |
| 143 | + ] |
| 144 | + ) |
| 145 | + get_nested_data_mock.assert_has_calls([mock.call("unit_2"), mock.call("unit_3")]) |
0 commit comments