-
Notifications
You must be signed in to change notification settings - Fork 63
Implement a new Interface function for datastore_before_update #275
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 8 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
ceb2bfa
Implement a new Interface function for datastore_before_update
avdata99 60411e9
Add test
avdata99 e97e1da
Just to run test in fork
avdata99 2ffb422
Add missing notification
avdata99 3097811
Improve test
avdata99 aaf3900
Change param to be clearer
avdata99 8b2a706
Consistency for calls
avdata99 b442503
ro observations
avdata99 588581c
Update docstring
avdata99 b6a8b2e
drop one line
avdata99 6e13330
fix docstring
avdata99 99c70f3
Not match is the exact use case to detect this!
avdata99 94d7b68
Update tests
avdata99 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,7 @@ | ||
| date,temperature,city | ||
| 2011-01-01,1,Galway | ||
| 2011-01-02,-1,Galway | ||
| 2011-01-03,0,Galway | ||
| 2011-01-01,6,Berkeley | ||
| 2011-01-02,8,Berkeley | ||
| 2011-01-03,5,Berkeley |
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 |
|---|---|---|
|
|
@@ -2,6 +2,7 @@ | |
| from __future__ import print_function | ||
| from __future__ import absolute_import | ||
| import os | ||
| from unittest import mock | ||
| import pytest | ||
| import six | ||
| import sqlalchemy as sa | ||
|
|
@@ -1094,6 +1095,107 @@ def test_shapefile_zip_python3(self, Session): | |
| ] | ||
|
|
||
|
|
||
| class TestDatastoreBeforeUpdateHook(TestLoadBase): | ||
| """ Verify that loader.load_csv / loader.load_table invoke | ||
| IXloader.datastore_before_update with the documented payload shape. | ||
| """ | ||
|
|
||
| def test_fires_on_new_table(self): | ||
| """ First load of a resource: no prior DataStore table, so | ||
| existing_fields is None and new_headers reflects the file columns. | ||
| """ | ||
| resource = factories.Resource() | ||
| with mock.patch.object(loader, '_notify_datastore_before_update') as notify: | ||
| loader.load_csv( | ||
| get_sample_filepath("simple.csv"), | ||
| resource_id=resource['id'], | ||
| mimetype="text/csv", | ||
| logger=logger, | ||
| ) | ||
|
|
||
| notify.assert_called_once() | ||
| resource_id, existing_fields, new_headers = notify.call_args.args | ||
| assert resource_id == resource['id'] | ||
| assert not existing_fields | ||
| assert [h['id'] for h in new_headers] == ['date', 'temperature', 'place'] | ||
|
|
||
| def test_fires_on_reload_same_columns(self): | ||
| """ Reload with the same file: existing_fields and new_headers | ||
| expose the same column ids, so a consumer computing | ||
| ``set(existing) ^ set(new)`` sees no diff. | ||
| """ | ||
| resource = factories.Resource() | ||
| resource_id = resource['id'] | ||
| loader.load_csv( | ||
| get_sample_filepath("simple.csv"), | ||
| resource_id=resource_id, | ||
| mimetype="text/csv", | ||
| logger=logger, | ||
| ) | ||
|
|
||
| with mock.patch.object(loader, '_notify_datastore_before_update') as notify: | ||
| loader.load_csv( | ||
| get_sample_filepath("simple.csv"), | ||
| resource_id=resource_id, | ||
| mimetype="text/csv", | ||
| logger=logger, | ||
| ) | ||
|
|
||
| notify.assert_called_once() | ||
| called_resource_id, existing_fields, new_headers = notify.call_args.args | ||
| assert called_resource_id == resource_id | ||
| assert [f['id'] for f in existing_fields] == ['date', 'temperature', 'place'] | ||
| assert [h['id'] for h in new_headers] == ['date', 'temperature', 'place'] | ||
|
|
||
| def test_fires_on_reload_with_changed_columns(self): | ||
| """ Reload the resource with a renamed column (place -> city) and | ||
| verify the hook exposes both sides BEFORE the DataStore is updated, | ||
| so downstream plugins can diff them and log a 'columns changed' | ||
| activity. | ||
| """ | ||
| resource = factories.Resource() | ||
| resource_id = resource['id'] | ||
| loader.load_csv( | ||
| get_sample_filepath("simple.csv"), | ||
| resource_id=resource_id, | ||
| mimetype="text/csv", | ||
| logger=logger, | ||
| ) | ||
|
|
||
| with mock.patch.object(loader, '_notify_datastore_before_update') as notify: | ||
| loader.load_csv( | ||
| get_sample_filepath("simple2.csv"), | ||
| resource_id=resource_id, | ||
| mimetype="text/csv", | ||
| logger=logger, | ||
| ) | ||
|
|
||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why two blank lines?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. One line removed here |
||
| notify.assert_called_once() | ||
| called_resource_id, existing_fields, new_headers = notify.call_args.args | ||
| assert called_resource_id == resource_id | ||
|
|
||
| old_ids = [f['id'] for f in existing_fields] | ||
| new_ids = [h['id'] for h in new_headers] | ||
| assert old_ids == ['date', 'temperature', 'place'] | ||
| assert new_ids == ['date', 'temperature', 'city'] | ||
|
|
||
| def test_fires_for_load_table(self): | ||
| resource = factories.Resource() | ||
| with mock.patch.object(loader, '_notify_datastore_before_update') as notify: | ||
| loader.load_table( | ||
| get_sample_filepath("simple.xls"), | ||
| resource_id=resource['id'], | ||
| mimetype="xls", | ||
| logger=logger, | ||
| ) | ||
|
|
||
| notify.assert_called_once() | ||
| resource_id, existing_fields, new_headers = notify.call_args.args | ||
| assert resource_id == resource['id'] | ||
| assert not existing_fields | ||
| assert [h['id'] for h in new_headers] == ['date', 'temperature', 'place'] | ||
|
|
||
|
|
||
| class TestLoadTabulator(TestLoadBase): | ||
| def test_simple(self, Session): | ||
| csv_filepath = get_sample_filepath("simple.xls") | ||
|
|
||
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.
I don't think the existing_fields object is the same after removing
_id.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.
Updated here 6e13330