diff options
| author | Max Nanis | 2026-09-02 14:57:14 -0700 |
|---|---|---|
| committer | Max Nanis | 2026-09-02 14:57:14 -0700 |
| commit | a63de356383f3dc1d92c44e1167b27de05157146 (patch) | |
| tree | fc4d9c0676c9d1fa11c4832ad09a1a7261180fec | |
| parent | ba191f535649f483ae064f47b023ee89141f32c5 (diff) | |
| download | generalresearch-a63de356383f3dc1d92c44e1167b27de05157146.tar.gz generalresearch-a63de356383f3dc1d92c44e1167b27de05157146.zip | |
django migrations functional
| -rw-r--r-- | generalresearch/thl_django/app/manage.py | 4 | ||||
| -rw-r--r-- | generalresearch/thl_django/app/test_settings.py | 17 | ||||
| -rw-r--r-- | test_utils/conftest.py | 148 | ||||
| -rw-r--r-- | tests/test_postgres.py | 21 |
4 files changed, 116 insertions, 74 deletions
diff --git a/generalresearch/thl_django/app/manage.py b/generalresearch/thl_django/app/manage.py index 33f2367..dabd5b3 100644 --- a/generalresearch/thl_django/app/manage.py +++ b/generalresearch/thl_django/app/manage.py @@ -1,11 +1,7 @@ #!/usr/bin/env python -import os import sys if __name__ == "__main__": - os.environ.setdefault( - "DJANGO_SETTINGS_MODULE", "generalresearch.thl_django.app.settings" - ) from django.core.management import execute_from_command_line execute_from_command_line(sys.argv) diff --git a/generalresearch/thl_django/app/test_settings.py b/generalresearch/thl_django/app/test_settings.py new file mode 100644 index 0000000..c5df32a --- /dev/null +++ b/generalresearch/thl_django/app/test_settings.py @@ -0,0 +1,17 @@ +DATABASES = { + "default": { + "ENGINE": "django.db.backends.postgresql", + "NAME": 'unittest-2026-09-02-a0310b', + "USER": 'jenkins', + "PASSWORD": '123456789', + "HOST": 'unittest-postgresql.fmt2.grl.internal', + "PORT": 5432, + } +} +INSTALLED_APPS = ['django.contrib.postgres', 'django.contrib.contenttypes', 'generalresearch.thl_django'] +DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField" +LANGUAGE_CODE = "en-us" +TIME_ZONE = "UTC" +USE_I18N = True +USE_L10N = True +USE_TZ = True diff --git a/test_utils/conftest.py b/test_utils/conftest.py index 146cc49..33a7e77 100644 --- a/test_utils/conftest.py +++ b/test_utils/conftest.py @@ -217,20 +217,22 @@ def gr_repo( @pytest.fixture(scope="session") def django_settings_file( - tmp_path_factory: TempPathFactory, postgres_instance_dict: PostgresDict, -): +) -> Callable[..., Path]: - def _inner(name: str, extra_installed_apps: list[str] | None = None): + def _inner( + settings_dir: Path, extra_installed_apps: list[str] | None = None + ) -> Path: installed_apps = [ "django.contrib.postgres", "django.contrib.contenttypes", - "generalresearchutils.thl_django", - *(extra_installed_apps or []), - ] + ] + (extra_installed_apps or []) + """ + This returns the directory path of where the settings file is in, + not the path of the settings file itself + """ - settings_content = f""" -DATABASES = {{ + settings_content = f"""DATABASES = {{ "default": {{ "ENGINE": "django.db.backends.postgresql", "NAME": {postgres_instance_dict["name"]!r}, @@ -248,15 +250,11 @@ USE_I18N = True USE_L10N = True USE_TZ = True """ - settings_path = tmp_path_factory.mktemp("settings") / f"{name}.py" - settings_path.write_text(settings_content) - - # Django settinsg require python dot syntax, and for the - # file to be in the path.. so we must set this. - sys.path.insert(0, str(tmp_path_factory)) - print("SETTINGS_PATH: ", settings_path) + settings_file_path = settings_dir / "test_settings.py" + settings_file_path.unlink(missing_ok=True) + settings_file_path.write_text(settings_content) - return settings_path + return settings_dir return _inner @@ -267,6 +265,7 @@ def django_db_factory( postgres_instance: PostgresDsn, gr_repo: Callable[..., Path], django_settings_file: Callable[..., Path], + postgres_instance_dict: PostgresDict, tmp_path_factory: TempPathFactory, ) -> Callable[..., PostgresDsn | None]: @@ -279,65 +278,78 @@ def django_db_factory( if _ran.get(django_project, False): print(f"Already ran django_db_factory:{django_project}") return postgres_instance - _ran[django_project] = True - print("DJANGO_PROJECT", django_project) - _settings_name = "thl_django" - _project_path = "generalresearch/thl_django/" - if "gr" in django_project: - _settings_name = "gr_carer" - _project_path = gr_repo() + # This is the generalresearch project root path, it's + # 1 directory up from test_utils/, or tests/ + base_dir = Path(request.config.rootpath).parent + + if django_project == "generalresearch.thl_django": + _cwd = base_dir + _manage_path = "generalresearch.thl_django.app.manage" + _settings_dir = base_dir / "generalresearch/thl_django/app" + _settings_module = "generalresearch.thl_django.app.test_settings" django_settings_file( - name=_settings_name, extra_installed_apps=["gr.common"] + settings_dir=_settings_dir, + extra_installed_apps=[ + "generalresearch.thl_django", + ], ) - else: - django_settings_file(name=_settings_name) - - django_fp = Path(request.config.rootpath).parent / str(_project_path) - env = { - # **os.environ, - "DJANGO_SETTINGS_MODULE": f"settings.{_settings_name}", - "PYTHONPATH": str(django_fp), - } - - if "gr" in django_project: - # print("ENV", env) - res1 = subprocess.run( - [sys.executable, "manage.py", "makemigrations"], - cwd=str(django_fp), - env=env, - capture_output=True, - text=True, - check=True, + + elif django_project == "gr.common": + _cwd = gr_repo() + _manage_path = "gr.app.manage" + _settings_dir = gr_repo() / "gr/app" + _settings_module = "gr.app.test_settings" + django_settings_file( + settings_dir=_settings_dir, extra_installed_apps=["gr.common"] ) - print("RES1:", res1) else: - print("ENV", env) - res1 = subprocess.run( - [ - sys.executable, - "manage.py", - "makemigrations", - ], - cwd=str(django_fp / "app"), - env=env, - capture_output=True, - text=True, - check=True, - ) - print("RES1:", res1) - - # res2 = subprocess.run( - # [sys.executable, "-m", "django", "migrate"], - # env=env, - # cwd=str(_project_path), - # capture_output=True, - # text=True, - # check=True, - # ) - # print("RES2:", res2) + raise ValueError("Not implemented yet.") + + assert _settings_dir + + env = {"DJANGO_SETTINGS_MODULE": str(_settings_module)} + res1 = subprocess.run( + [ + sys.executable, + "-m", + _manage_path, + "makemigrations", + f"--settings={_settings_module}", + ], + cwd=str(_cwd), + env=env, + capture_output=True, + text=True, + check=True, + ) + + if res1.returncode != 0: + print("STDOUT:", res1.stdout) + print("STDERR:", res1.stderr) + res1.check_returncode() + + res2 = subprocess.run( + [ + sys.executable, + "-m", + _manage_path, + "migrate", + f"--settings={_settings_module}", + ], + env=env, + cwd=str(_cwd), + capture_output=True, + text=True, + check=True, + ) + + if res2.returncode != 0: + print("STDOUT:", res2.stdout) + print("STDERR:", res2.stderr) + res2.check_returncode() # 3. Return the Dsn so the factory gives a way to connect return postgres_instance diff --git a/tests/test_postgres.py b/tests/test_postgres.py index 9794321..de3f5d8 100644 --- a/tests/test_postgres.py +++ b/tests/test_postgres.py @@ -70,13 +70,30 @@ class TestPostgresDjangoCreation: assert len(res) == 1 assert res[0]["count"] == 57 + def test_django_tables_only_gr(self, gr_db: PostgresConfig): + """ + IMPORTANT: This can't really run with only the GR tables, + that's because we have most of the database init fixtures + as session scoped; and we can't ensure that this will + run before any test that depends on the core thl + migrations + """ + + res = gr_db.execute_sql_query(query=""" + SELECT COUNT(*) + FROM information_schema.tables + WHERE table_schema = 'public'; + """) + assert len(res) == 1 + assert res[0]["count"] == 65 + def test_django_tables_with_gr( self, thl_web_rw: PostgresConfig, gr_db: PostgresConfig ): res = thl_web_rw.execute_sql_query(query=""" - SELECT COUNT(*) + SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = 'public'; """) assert len(res) == 1 - assert res[0]["count"] > 57 + assert res[0]["count"] == 65 |
