-
Notifications
You must be signed in to change notification settings - Fork 53
feat(schema-config): add schema config import tool #8452
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
Open
grantfitzsimmons
wants to merge
29
commits into
main
Choose a base branch
from
issue-6155-2
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
5e92876
feat(schema-config): add atomic schema import endpoint
grantfitzsimmons e5c70d7
feat(schema-config): add schema import controls
grantfitzsimmons 4c1fe2b
fix(schema-config): tolerate legacy import values
grantfitzsimmons 29a7568
fix(schema-config): build localized import parents
grantfitzsimmons 31afb51
fix(schema-config): validate import file structure
grantfitzsimmons 714f512
refactor(schema-config): reuse localized schema config label
grantfitzsimmons 527de3f
fix(schema-config): skip unavailable import references
grantfitzsimmons 1c608a0
fix(schema-config): validate import language
grantfitzsimmons 5279a97
Lint code with ESLint and Prettier
grantfitzsimmons 4a65bd4
fix(schema-config): contain invalid import errors
grantfitzsimmons c9f5de4
Merge branch 'issue-6155-2' of https://github.com/specify/specify7 in…
grantfitzsimmons 41379d2
fix(schema-config): separate import warnings
grantfitzsimmons ac5c556
fix(schema-config): finalize text
grantfitzsimmons 56be230
Lint code with ESLint and Prettier
grantfitzsimmons 00f4b02
fix schema localization import review comments
grantfitzsimmons abb176a
Merge remote-tracking branch 'origin/main' into issue-6155-2
Copilot f69ccaa
Lint code with ESLint and Prettier
Copilot e9ccbfc
Fix: Normalize locale keys before the lookup
CarolineDenis 2664772
Merge branch 'main' into issue-6155-2
melton-jason 99f9d05
Update specifyweb/backend/context/views.py
CarolineDenis ef3cee2
Fix: Serialize localized-string lookup and creation.
CarolineDenis 6d19ebc
Feat: Separate import errors from post-import refresh errors.
CarolineDenis d9758f6
Feat: Reject imports with a source-language mismatch
CarolineDenis 27e7cfc
Potential fix for pull request finding 'CodeQL / Empty except'
CarolineDenis c83e2a4
Fix: Filter imported aggregators against DataObjFormatters.
CarolineDenis 46a3579
Fix: Validate item formats against UIFormatters.
CarolineDenis 13760c1
Fix: Use the correct formatter set for each schema level.
CarolineDenis a2db7e8
Fix: Enforce the Schema Config tool permission on the import endpoint.
CarolineDenis 615780f
Update specifyweb/backend/context/views.py
CarolineDenis 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
292 changes: 292 additions & 0 deletions
292
specifyweb/backend/context/tests/test_schema_localization_import.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,292 @@ | ||
| import json | ||
| import threading | ||
| from concurrent.futures import ThreadPoolExecutor | ||
| from unittest.mock import patch | ||
|
|
||
| from django.db import close_old_connections | ||
| from django.test import Client | ||
|
|
||
| from specifyweb.backend.context.views import _schema_import_string | ||
| from specifyweb.specify import models | ||
| from specifyweb.specify.tests.test_api import ApiTests, ApiTransactionTests | ||
|
|
||
|
|
||
| class SchemaLocalizationImportTests(ApiTests): | ||
| def setUp(self): | ||
| super().setUp() | ||
| self.container = models.Splocalecontainer.objects.create( | ||
| discipline=self.discipline, name='Accession', schematype=0 | ||
| ) | ||
| self.item = models.Splocalecontaineritem.objects.create( | ||
| container=self.container, name='accessionnumber' | ||
| ) | ||
| self.client = Client() | ||
| self.client.force_login(self.specifyuser) | ||
| self.client.cookies['collection'] = str(self.collection.id) | ||
|
|
||
| def test_export_includes_source_language(self): | ||
| response = self.client.get( | ||
| '/context/schema_localization.json?lang=en-US&export=true' | ||
| ) | ||
|
|
||
| self.assertEqual(response.status_code, 200) | ||
| export = response.json() | ||
| self.assertEqual(export['language'], 'en-us') | ||
| self.assertIn('accession', export['schema']) | ||
|
|
||
| def test_imports_schema_values_and_skips_unknown_entries(self): | ||
| response = self.client.post( | ||
| '/context/schema_localization_import.json', | ||
| data=json.dumps({ | ||
| 'language': 'en', | ||
| 'schema': { | ||
| 'accession': { | ||
| 'format': 'Accession', | ||
| 'name': 'Imported Accession', | ||
| 'items': { | ||
| 'accessionnumber': { | ||
| 'isHidden': True, | ||
| 'name': 'Imported Number', | ||
| 'pickListName': 'Unavailable Picklist', | ||
| 'webLinkName': 'Unavailable Web Link', | ||
| }, | ||
| 'removedfield': {'isHidden': True}, | ||
| }, | ||
| }, | ||
| 'removedtable': {'isHidden': True}, | ||
| }, | ||
| }), | ||
| content_type='application/json', | ||
| ) | ||
|
|
||
| self.assertEqual(response.status_code, 200) | ||
| self.container.refresh_from_db() | ||
| self.item.refresh_from_db() | ||
| self.assertEqual(self.container.format, 'Accession') | ||
| self.assertTrue(self.item.ishidden) | ||
| self.assertIsNone(self.item.picklistname) | ||
| self.assertIsNone(self.item.weblinkname) | ||
| self.assertEqual( | ||
| models.Splocaleitemstr.objects.get( | ||
| containername=self.container, language='en' | ||
| ).text, | ||
| 'Imported Accession', | ||
| ) | ||
| self.assertEqual( | ||
| models.Splocaleitemstr.objects.get( | ||
| itemname=self.item, language='en' | ||
| ).text, | ||
| 'Imported Number', | ||
| ) | ||
|
|
||
| def test_import_updates_existing_countryless_string(self): | ||
| string = models.Splocaleitemstr.objects.create( | ||
| containername=self.container, | ||
| language='en', | ||
| country='', | ||
| text='Existing Accession', | ||
| ) | ||
|
|
||
| response = self.client.post( | ||
| '/context/schema_localization_import.json', | ||
| data=json.dumps({ | ||
| 'language': 'en', | ||
| 'schema': {'accession': {'name': 'Updated Accession'}}, | ||
| }), | ||
| content_type='application/json', | ||
| ) | ||
|
|
||
| self.assertEqual(response.status_code, 200) | ||
| self.assertEqual( | ||
| models.Splocaleitemstr.objects.filter( | ||
| containername=self.container, language='en' | ||
| ).count(), | ||
| 1, | ||
| ) | ||
| string.refresh_from_db() | ||
| self.assertEqual(string.text, 'Updated Accession') | ||
|
|
||
| def test_import_updates_existing_case_insensitive_country_string(self): | ||
| string = models.Splocaleitemstr.objects.create( | ||
| containername=self.container, | ||
| language='en', | ||
| country='US', | ||
| text='Existing US Accession', | ||
| ) | ||
|
|
||
| response = self.client.post( | ||
| '/context/schema_localization_import.json', | ||
| data=json.dumps({ | ||
| 'language': 'en-US', | ||
| 'schema': {'accession': {'name': 'Updated US Accession'}}, | ||
| }), | ||
| content_type='application/json', | ||
| ) | ||
|
|
||
| self.assertEqual(response.status_code, 200) | ||
| self.assertEqual( | ||
| models.Splocaleitemstr.objects.filter( | ||
| containername=self.container, language='en' | ||
| ).count(), | ||
| 1, | ||
| ) | ||
| string.refresh_from_db() | ||
| self.assertEqual(string.text, 'Updated US Accession') | ||
|
|
||
| def test_imports_export_when_source_language_matches(self): | ||
| response = self.client.post( | ||
| '/context/schema_localization_import.json', | ||
| data=json.dumps({ | ||
| 'language': 'en-US', | ||
| 'schema': { | ||
| 'language': 'en-us', | ||
| 'schema': {'accession': {'name': 'Imported Accession'}}, | ||
| }, | ||
| }), | ||
| content_type='application/json', | ||
| ) | ||
|
|
||
| self.assertEqual(response.status_code, 200) | ||
| self.assertEqual( | ||
| models.Splocaleitemstr.objects.get( | ||
| containername=self.container, | ||
| language='en', | ||
| country='us', | ||
| ).text, | ||
| 'Imported Accession', | ||
| ) | ||
|
|
||
| def test_rejects_export_when_source_language_differs(self): | ||
| response = self.client.post( | ||
| '/context/schema_localization_import.json', | ||
| data=json.dumps({ | ||
| 'language': 'fr', | ||
| 'schema': { | ||
| 'language': 'en', | ||
| 'schema': {'accession': {'name': 'Should Not Import'}}, | ||
| }, | ||
| }), | ||
| content_type='application/json', | ||
| ) | ||
|
|
||
| self.assertEqual(response.status_code, 400) | ||
| self.assertFalse( | ||
| models.Splocaleitemstr.objects.filter( | ||
| containername=self.container, | ||
| text='Should Not Import', | ||
| ).exists() | ||
| ) | ||
|
|
||
| def test_invalid_values_do_not_write(self): | ||
| response = self.client.post( | ||
| '/context/schema_localization_import.json', | ||
| data=json.dumps({ | ||
| 'schema': {'accession': {'isHidden': 'yes'}}, | ||
| 'language': 'en', | ||
| }), | ||
| content_type='application/json', | ||
| ) | ||
|
|
||
| self.assertEqual(response.status_code, 400) | ||
| self.container.refresh_from_db() | ||
| self.assertIsNone(self.container.format) | ||
|
|
||
| def test_rejects_non_schema_json(self): | ||
| response = self.client.post( | ||
| '/context/schema_localization_import.json', | ||
| data=json.dumps({'not': 'a schema'}), | ||
| content_type='application/json', | ||
| ) | ||
|
|
||
| self.assertEqual(response.status_code, 400) | ||
| self.container.refresh_from_db() | ||
| self.assertIsNone(self.container.format) | ||
|
|
||
| def test_rejects_invalid_language(self): | ||
| for language in ('en-us-extra', '@@', 'en-$%'): | ||
| with self.subTest(language=language): | ||
| response = self.client.post( | ||
| '/context/schema_localization_import.json', | ||
| data=json.dumps({ | ||
| 'language': language, | ||
| 'schema': {'accession': {'name': 'Should Not Import'}}, | ||
| }), | ||
| content_type='application/json', | ||
| ) | ||
|
|
||
| self.assertEqual(response.status_code, 400) | ||
| self.assertFalse( | ||
| models.Splocaleitemstr.objects.filter( | ||
| containername=self.container, text='Should Not Import' | ||
| ).exists() | ||
| ) | ||
|
|
||
| def test_rejects_non_object_data_for_known_table(self): | ||
| response = self.client.post( | ||
| '/context/schema_localization_import.json', | ||
| data=json.dumps({ | ||
| 'language': 'en', | ||
| 'schema': {'accession': 'invalid'}, | ||
| }), | ||
| content_type='application/json', | ||
| ) | ||
|
|
||
| self.assertEqual(response.status_code, 400) | ||
|
|
||
|
|
||
| class ConcurrentSchemaLocalizationImportTests(ApiTransactionTests): | ||
| def setUp(self): | ||
| super().setUp() | ||
| self.container = models.Splocalecontainer.objects.create( | ||
| discipline=self.discipline, name='Accession', schematype=0 | ||
| ) | ||
| self.clients = [self._make_client(), self._make_client()] | ||
|
|
||
| def _make_client(self): | ||
| client = Client() | ||
| client.force_login(self.specifyuser) | ||
| client.cookies['collection'] = str(self.collection.id) | ||
| return client | ||
|
|
||
| def test_concurrent_imports_do_not_create_duplicate_strings(self): | ||
| payload = json.dumps({ | ||
| 'language': 'en-US', | ||
| 'schema': {'accession': {'name': 'Imported Accession'}}, | ||
| }) | ||
| barrier = threading.Barrier(2) | ||
|
|
||
| def synchronize_import(*args): | ||
| _schema_import_string(*args) | ||
| if args[3] is not None: | ||
| try: | ||
| barrier.wait(timeout=1) | ||
| except threading.BrokenBarrierError: | ||
| # Best-effort synchronization for concurrency overlap in this test; | ||
| # if one worker times out or exits early, continue without failing here. | ||
| pass | ||
|
|
||
| def import_schema(client): | ||
| close_old_connections() | ||
| try: | ||
| return client.post( | ||
| '/context/schema_localization_import.json', | ||
| data=payload, | ||
| content_type='application/json', | ||
| ) | ||
| finally: | ||
| close_old_connections() | ||
|
|
||
| with patch( | ||
| 'specifyweb.backend.context.views._schema_import_string', | ||
| side_effect=synchronize_import, | ||
| ), ThreadPoolExecutor(max_workers=2) as executor: | ||
| responses = list(executor.map(import_schema, self.clients)) | ||
|
|
||
| self.assertEqual([response.status_code for response in responses], [200, 200]) | ||
| strings = models.Splocaleitemstr.objects.filter( | ||
| containername=self.container, | ||
| language='en', | ||
| country__iexact='us', | ||
| ).filter(variant__isnull=True) | ||
| self.assertEqual(strings.count(), 1) | ||
| self.assertEqual(strings.get().text, 'Imported Accession') | ||
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.