-
Notifications
You must be signed in to change notification settings - Fork 17
Populate development dbs with test fixture data #2430
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
KatieB5
merged 1 commit into
main
from
KatieB5/populate-development-dbs-with-test-fixture-data
Apr 25, 2025
Merged
Changes from all commits
Commits
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
120 changes: 120 additions & 0 deletions
120
opencodelists/management/commands/setup_local_dev_databases.py
Jongmassey marked this conversation as resolved.
Show resolved
Hide resolved
|
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 @@ | ||
| import sys | ||
|
|
||
| from django.core.management import BaseCommand, call_command | ||
|
|
||
| from coding_systems.versioning.models import ( | ||
| CodingSystemRelease, | ||
| build_db_path, | ||
| update_coding_system_database_connections, | ||
| ) | ||
| from opencodelists import settings | ||
| from opencodelists.tests.fixtures import build_fixtures | ||
|
|
||
|
|
||
| class Command(BaseCommand): | ||
KatieB5 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| help = """Populates local opencodelists dbs with test fixture data: | ||
| Loads the CodingSystemReleases needed for the snomed and dmd | ||
| fixtures into the Core db (you will be prompted for consent | ||
| to do this), deletes existing test coding system release dbs, | ||
| creates new coding system release dbs, populates these with | ||
| test fixture data, instantiates a universe of fixture | ||
| objects, and creates a superuser. Running this command | ||
| directly is not recommended, instead use `just | ||
| build-dbs-for-local-development`""" | ||
|
|
||
| def handle(self, *args, **options): | ||
| if not settings.DEBUG: | ||
| self.stderr.write( | ||
| "This management command will not run in a production environment." | ||
| ) | ||
| sys.exit(1) | ||
|
|
||
| load_versioning_fixture_answer = input( | ||
| "Running this command will load the CodingSystemReleases needed for the snomed and dmd test fixtures, into the Core db. Any local test coding system release dbs will then be deleted and recreated. Do you want to proceed? (Y/n)" | ||
| ) | ||
|
|
||
| if load_versioning_fixture_answer == "n": | ||
| return | ||
|
|
||
| call_command( | ||
| "loaddata", | ||
| "coding_systems/versioning/fixtures/coding_system_releases.json", | ||
| ) | ||
|
|
||
| self.stdout.write("Deleting local coding system release dbs...") | ||
| for coding_system_release in CodingSystemRelease.objects.all(): | ||
| db_path = build_db_path(coding_system_release) | ||
| if db_path.exists(): | ||
| db_path.unlink() | ||
| self.stdout.write(f"{str(db_path)} has been deleted") | ||
|
|
||
| self.stdout.write( | ||
| "Migrating coding system release dbs and populating them with test fixture data" | ||
| ) | ||
|
|
||
| update_coding_system_database_connections() | ||
Jongmassey marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| # migrate snomedct test db and load test fixtures | ||
| call_command("migrate", "snomedct", database="snomedct_test_20200101") | ||
|
|
||
| call_command( | ||
| "loaddata", | ||
| "coding_systems/snomedct/fixtures/core-model-components.snomedct_test_20200101.json", | ||
| database="snomedct_test_20200101", | ||
| ) | ||
|
|
||
| call_command( | ||
| "loaddata", | ||
| "coding_systems/snomedct/fixtures/tennis-elbow.snomedct_test_20200101.json", | ||
| database="snomedct_test_20200101", | ||
| ) | ||
|
|
||
| call_command( | ||
| "loaddata", | ||
| "coding_systems/snomedct/fixtures/tennis-toe.snomedct_test_20200101.json", | ||
| database="snomedct_test_20200101", | ||
| ) | ||
|
|
||
| # migrate dmd test db and load test fixture | ||
| call_command("migrate", "dmd", database="dmd_test_20200101") | ||
|
|
||
| call_command( | ||
| "loaddata", | ||
| "coding_systems/dmd/fixtures/asthma-medication.dmd_test_20200101.json", | ||
| database="dmd_test_20200101", | ||
| ) | ||
|
|
||
| # migrate bnf test db and load test fixture | ||
| call_command("migrate", "bnf", database="bnf_test_20200101") | ||
|
|
||
| call_command( | ||
| "loaddata", | ||
| "coding_systems/bnf/fixtures/asthma.bnf_test_20200101.json", | ||
| database="bnf_test_20200101", | ||
| ) | ||
|
|
||
| # migrate icd10 test db and load test fixture | ||
| call_command("migrate", "icd10", database="icd10_test_20200101") | ||
| call_command( | ||
| "loaddata", | ||
| "coding_systems/icd10/fixtures/icd10.icd10_test_20200101.json", | ||
| database="icd10_test_20200101", | ||
| ) | ||
|
|
||
| # instantiate the test data universe | ||
| build_fixtures() | ||
|
|
||
| call_command( | ||
| "createsuperuser", | ||
| no_input=True, | ||
| username="localdev", | ||
| email="localdev@example.com", | ||
| ) | ||
|
|
||
| self.stdout.write( | ||
| "Local setup complete! You can now:\n" | ||
| " - Log in as `localdev`\n" | ||
| " - Search for 'arthritis', 'tennis', and 'elbow'\n" | ||
| " - Build codelists with the concepts returned from these searches (see /opencodelists/opencodelists/tests/fixtures.py for more info)\n" | ||
| " - View a BNF codelist, a minimal codelist, and a new-style SNOMED CT codelist" | ||
| ) | ||
14 changes: 14 additions & 0 deletions
14
opencodelists/tests/management/commands/test_setup_local_dev_databases.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,14 @@ | ||
| import pytest | ||
| from django.core.management import call_command | ||
|
|
||
| # from opencodelists.management.commands import setup_local_dev_databases | ||
| from opencodelists import settings | ||
|
|
||
|
|
||
| def test_setup_local_dev_databases_exits_early_when_in_prod_environment(monkeypatch): | ||
| monkeypatch.setattr(settings, "DEBUG", False) | ||
|
|
||
| with pytest.raises(SystemExit) as error: | ||
| call_command("setup_local_dev_databases") | ||
|
|
||
| assert error.value.code == 1 |
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.