forked from tfrancoi/odoo_csv_import
-
Notifications
You must be signed in to change notification settings - Fork 2
feat(orm): --resolve-relation CLI flag + ORM-minimizing docs #203
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
3 commits
Select commit
Hold shift + click to select a range
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
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,92 @@ | ||
| """Tests for the --resolve-relation CLI flag and its parser.""" | ||
|
|
||
| from unittest.mock import MagicMock, patch | ||
|
|
||
| import click | ||
| import pytest | ||
| from click.testing import CliRunner | ||
|
|
||
| from fluvo import __main__ | ||
| from fluvo.__main__ import _parse_resolve_relation_specs | ||
|
|
||
|
|
||
| def test_parse_four_part_spec() -> None: | ||
| """A 4-part spec parses to a dict without a 'to' key.""" | ||
| out = _parse_resolve_relation_specs(("country:res.country:code:country_id",)) | ||
| assert out == [ | ||
| { | ||
| "source_column": "country", | ||
| "model": "res.country", | ||
| "key_field": "code", | ||
| "relation_field": "country_id", | ||
| } | ||
| ] | ||
|
|
||
|
|
||
| def test_parse_five_part_spec_includes_to() -> None: | ||
| """A 5-part spec carries the 'to' target.""" | ||
| out = _parse_resolve_relation_specs(("c:res.country:code:country_id:dbid",)) | ||
| assert out[0]["to"] == "dbid" | ||
|
|
||
|
|
||
| def test_parse_multiple_specs() -> None: | ||
| """Several specs produce several dicts.""" | ||
| out = _parse_resolve_relation_specs( | ||
| ( | ||
| "country:res.country:code:country_id", | ||
| "parent:res.partner:ref:parent_id:xmlid", | ||
| ) | ||
| ) | ||
| assert len(out) == 2 | ||
|
|
||
|
|
||
| def test_parse_rejects_wrong_part_count() -> None: | ||
| """A spec with too few parts is rejected.""" | ||
| with pytest.raises(click.BadParameter): | ||
| _parse_resolve_relation_specs(("country:res.country:code",)) | ||
|
|
||
|
|
||
| def test_parse_rejects_empty_fields() -> None: | ||
| """A spec with an empty required field is rejected.""" | ||
| with pytest.raises(click.BadParameter): | ||
| _parse_resolve_relation_specs(("country::code:country_id",)) | ||
|
|
||
|
|
||
| def test_parse_rejects_invalid_to() -> None: | ||
| """An invalid 'to' value is rejected.""" | ||
| with pytest.raises(click.BadParameter): | ||
| _parse_resolve_relation_specs(("c:res.country:code:country_id:nope",)) | ||
|
|
||
|
|
||
| @patch("fluvo.__main__.run_import") | ||
| def test_cli_resolve_relation_flows_to_run_import(mock_run_import: MagicMock) -> None: | ||
| """--resolve-relation is parsed and passed to run_import as resolve_relations.""" | ||
| mock_run_import.return_value = {"x": 1} | ||
| runner = CliRunner() | ||
| with runner.isolated_filesystem(): | ||
| with open("conn.conf", "w") as f: | ||
| f.write("[Connection]") | ||
| result = runner.invoke( | ||
| __main__.cli, | ||
| [ | ||
| "import", | ||
| "--connection-file", | ||
| "conn.conf", | ||
| "--file", | ||
| "my.csv", | ||
| "--model", | ||
| "res.partner", | ||
| "--resolve-relation", | ||
| "country:res.country:code:country_id", | ||
| ], | ||
| ) | ||
| assert result.exit_code == 0 | ||
| call_kwargs = mock_run_import.call_args.kwargs | ||
| assert call_kwargs["resolve_relations"] == [ | ||
| { | ||
| "source_column": "country", | ||
| "model": "res.country", | ||
| "key_field": "code", | ||
| "relation_field": "country_id", | ||
| } | ||
| ] | ||
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.
To ensure that the new validation for empty fields in the
--resolve-relationspec is thoroughly tested, we should add a test case that verifies that specs with empty fields are rejected.