-
Notifications
You must be signed in to change notification settings - Fork 8
[THIS-1095] 🤕Handle integer string #1594
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
01b8c34
tests: added failing test
doctrino 26151f2
feat: added utilty method
doctrino 0f5c3c4
fix
doctrino 18e735c
tests: finished test
doctrino 6ead141
tests: both execution paths
doctrino bb7027c
tests: failing test cases
doctrino e62a54b
fix; better regex
doctrino File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
tests/tests_unit/test_data_model/test_importers/test_dms_api_importer.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| from collections.abc import Iterable | ||
| from pathlib import Path | ||
| from unittest.mock import MagicMock | ||
|
|
||
| import pytest | ||
|
|
||
| from cognite.neat._data_model.exporters import DMSAPIYAMLExporter | ||
| from cognite.neat._data_model.importers import DMSAPIImporter | ||
| from cognite.neat._data_model.models.dms import RequestSchema | ||
|
|
||
|
|
||
| def valid_dms_yaml_formats_roundtrip() -> Iterable[tuple]: | ||
| yield pytest.param( | ||
| { | ||
| "dataModel": """ space: my_space | ||
| externalId: MyModel | ||
| version: 1_0_0 | ||
| views: | ||
| - space: my_space | ||
| externalId: MyView | ||
| version: 1_0_0""", | ||
| "views": """- space: my_space | ||
| externalId: MyView | ||
| version: 1_0_0 | ||
| properties: | ||
| name: | ||
| container: | ||
| space: my_space | ||
| externalId: MyContainer | ||
| containerPropertyIdentifier: name | ||
| """, | ||
| "containers": """- space: my_space | ||
| externalId: MyContainer | ||
| properties: | ||
| name: | ||
| type: | ||
| type: text | ||
| """, | ||
| }, | ||
| { | ||
| "MyModel.datamodel.yaml": """space: my_space | ||
| externalId: MyModel | ||
| version: '1_0_0' | ||
| views: | ||
| - space: my_space | ||
| externalId: MyView | ||
| version: '1_0_0' | ||
| type: view | ||
| """, | ||
| "views/MyView.view.yaml": """space: my_space | ||
| externalId: MyView | ||
| version: '1_0_0' | ||
| properties: | ||
| name: | ||
| container: | ||
| space: my_space | ||
| externalId: MyContainer | ||
| type: container | ||
| containerPropertyIdentifier: name | ||
| """, | ||
| "containers/MyContainer.container.yaml": """space: my_space | ||
| externalId: MyContainer | ||
| properties: | ||
| name: | ||
| type: | ||
| type: text | ||
| """, | ||
| }, | ||
| id="Handle integer in version field", | ||
| ) | ||
|
|
||
|
|
||
| class TestImportYAMLAPIFormat: | ||
| @pytest.mark.parametrize("source, expected", list(valid_dms_yaml_formats_roundtrip())) | ||
| def test_roundtrip_single_input_file(self, source: dict[str, str], expected: dict[str, str]) -> None: | ||
| source_content: list[str] = [] | ||
| for key, value in source.items(): | ||
| source_content.extend([f"{key}:", value]) | ||
| yaml_file = MagicMock(spec=Path) | ||
| yaml_file.suffix = ".yaml" | ||
| yaml_file.read_text.return_value = "\n".join(source_content) | ||
|
|
||
| data_model = DMSAPIImporter.from_yaml(yaml_file).to_data_model() | ||
|
|
||
| self.assert_written_output(data_model, expected) | ||
|
|
||
| @pytest.mark.parametrize("source, expected", list(valid_dms_yaml_formats_roundtrip())) | ||
| def test_roundtrip_directory_input(self, source: dict[str, str], expected: dict[str, str]) -> None: | ||
| yaml_dir = MagicMock(spec=Path) | ||
| yaml_dir.is_dir.return_value = True | ||
| yaml_dir.rglob.return_value = [ | ||
| # File kind is singular. | ||
| self._make_mock_file(kind.removesuffix("s"), content) | ||
| for kind, content in source.items() | ||
| ] | ||
|
|
||
| data_model = DMSAPIImporter.from_yaml(yaml_dir).to_data_model() | ||
|
|
||
| self.assert_written_output(data_model, expected) | ||
|
|
||
| def _make_mock_file(self, kind: str, content: str) -> MagicMock: | ||
| yaml_file = MagicMock(spec=Path) | ||
| yaml_file.suffix = ".yaml" | ||
| yaml_file.read_text.return_value = content | ||
| yaml_file.stem = f"my.{kind}" | ||
| yaml_file.name = f"{yaml_file.stem}{yaml_file.suffix}" | ||
| return yaml_file | ||
|
|
||
| def assert_written_output(self, data_model: RequestSchema, expected: dict[str, str]) -> None: | ||
| written_files: dict[str, str] = {} | ||
|
|
||
| def make_mock_path(name: str = "root") -> MagicMock: | ||
| mock = MagicMock(spec=Path) | ||
| mock.suffix = "" | ||
| mock.write_text = MagicMock(side_effect=lambda content, **_: written_files.update({name: content})) | ||
| mock.__truediv__ = lambda self, other: make_mock_path(str(other)) | ||
| return mock | ||
|
|
||
| yaml_dir = make_mock_path() | ||
| DMSAPIYAMLExporter().export_to_file(data_model, yaml_dir) | ||
| assert expected == written_files |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| from collections.abc import Iterable | ||
|
|
||
| import pytest | ||
|
|
||
| from cognite.neat._utils.text import quote_int_value_by_key_in_yaml | ||
|
|
||
|
|
||
| def quote_key_in_yaml_test_cases() -> Iterable[tuple]: | ||
| yield pytest.param( | ||
| """space: my_space | ||
| externalID: myModel | ||
| version: 3_0_2""", | ||
| '''space: my_space | ||
| externalID: myModel | ||
| version: "3_0_2"''', | ||
| id="Single data model", | ||
| ) | ||
|
|
||
| yield pytest.param( | ||
| """- space: my_space | ||
| externalId: myModel | ||
| version: 1_000 | ||
| - space: my_other_space | ||
| externalId: myOtherModel | ||
| version: 2_000 | ||
| """, | ||
| """- space: my_space | ||
| externalId: myModel | ||
| version: "1_000" | ||
| - space: my_other_space | ||
| externalId: myOtherModel | ||
| version: "2_000" | ||
| """, | ||
| id="Two Data Models", | ||
| ) | ||
|
|
||
| yield pytest.param( | ||
| """space: my_space | ||
| externalID: myModel | ||
| version: '3_0_2'""", | ||
| """space: my_space | ||
| externalID: myModel | ||
| version: '3_0_2'""", | ||
| id="Single data model with single quoted version", | ||
| ) | ||
|
|
||
| yield pytest.param( | ||
| """- space: my_space | ||
| externalId: myModel | ||
| version: '1_000' | ||
| - space: my_other_space | ||
| externalId: myOtherModel | ||
| version: '2_000' | ||
| """, | ||
| """- space: my_space | ||
| externalId: myModel | ||
| version: '1_000' | ||
| - space: my_other_space | ||
| externalId: myOtherModel | ||
| version: '2_000' | ||
| """, | ||
| id="Two Data Models with single quoted version", | ||
| ) | ||
|
|
||
| yield pytest.param( | ||
| '''space: my_space | ||
| externalID: myModel | ||
| version: "3_0_2"''', | ||
| '''space: my_space | ||
| externalID: myModel | ||
| version: "3_0_2"''', | ||
| id="Single data model with double quoted version", | ||
| ) | ||
|
|
||
| yield pytest.param( | ||
| """- space: my_space | ||
| externalId: myModel | ||
| version: "1_000" | ||
| - space: my_other_space | ||
| externalId: myOtherModel | ||
| version: "2_000" | ||
| """, | ||
| """- space: my_space | ||
| externalId: myModel | ||
| version: "1_000" | ||
| - space: my_other_space | ||
| externalId: myOtherModel | ||
| version: "2_000" | ||
| """, | ||
| id="Two Data Models with double quoted version", | ||
| ) | ||
|
|
||
| version_prop = """ | ||
| externalId: CogniteSourceSystem | ||
| properties: | ||
| version: | ||
| container: | ||
| externalId: CogniteSourceSystem | ||
| space: sp_core_model | ||
| type: container | ||
| """ | ||
| yield pytest.param( | ||
| version_prop, | ||
| version_prop, | ||
| id="Version property untouched", | ||
| ) | ||
| yield pytest.param( | ||
| """version: 1_0_0 # My comment""", """version: "1_0_0" # My comment""", id="Handle comment after version" | ||
| ) | ||
| yield pytest.param( | ||
| """version: 1 # My "quoted" comment""", | ||
| """version: "1" # My "quoted" comment""", | ||
| id="Handle comment with quotes after version", | ||
| ) | ||
|
|
||
|
|
||
| class TestQuoteKeyInYAML: | ||
| @pytest.mark.parametrize("raw, expected", list(quote_key_in_yaml_test_cases())) | ||
| def test_quote_key_in_yaml(self, raw: str, expected: str) -> None: | ||
| assert quote_int_value_by_key_in_yaml(raw, key="version") == expected |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It doesn't seem like we successfully handle floats with this regex, see the image below, this is what happens when I load a yaml with
version: 2.1.1