-
Notifications
You must be signed in to change notification settings - Fork 10
Testing: overriding env with test env #284
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
Closed
Closed
Changes from all commits
Commits
Show all changes
43 commits
Select commit
Hold shift + click to select a range
ac7495d
overriding env with test env
nishika26 bdb0459
env file example
nishika26 ec994bc
Merge branch 'main' into enhancement/env_test
nishika26 4703a57
stricter overriding for test db
nishika26 e290dd7
ci
nishika26 bbdf4de
ci clarification
nishika26 b7f26cd
Merge branch 'main' into enhancement/env_test
nishika26 0bc016d
updating env variables before importing
nishika26 08ed5c6
Merge branch 'main' into enhancement/env_test
nishika26 c0e37f0
adding test db to ci
nishika26 cc29969
adding test db to ci
nishika26 be834e7
adding test db to ci
nishika26 07cfecd
adding test db to ci
nishika26 1516fb1
adding test db to ci
nishika26 3a2bf4c
adding test db to ci
nishika26 bb9ecd9
Merge branch 'main' into enhancement/env_test
AkhileshNegi 0c680fa
coderabbit suggestions
945bf3d
coderabbit suggestions
292a548
coderabbit suggestions
bc4d30e
cleanups
1157fe2
fixing few testcases
7528612
few more cleanups
0fcd10c
creating both env files
4f2375c
cleanup CI
f8615e7
using testdb
3c42875
updating DB
d663406
using env.test as .env
0ec01d7
cleaning up env variable
ae259aa
updating env.example
de5a19c
increasing pool size
21497d1
cleanup file
aa14f7e
cleanup file
f69f428
cleanup env
9ecc5cb
cleaning up imports
5398d21
revert changes for storage.py
724a229
removing redundant code
074e753
fixing engine
72847f8
cleanups for PEP8
bf6c039
cleanups
ca60538
removing duplicate setting engine
1239e2a
removing duplicate setting engine
874f177
added few comments
4733796
Merge branch 'main' into enhancement/env_test
AkhileshNegi 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| ENVIRONMENT=local | ||
|
|
||
| PROJECT_NAME="AI Platform" | ||
| STACK_NAME=ai-platform | ||
|
|
||
| #Backend | ||
| SECRET_KEY=changethis | ||
| FIRST_SUPERUSER=superuser@example.com | ||
| FIRST_SUPERUSER_PASSWORD=changethis | ||
| EMAIL_TEST_USER="test@example.com" | ||
|
|
||
| # Postgres | ||
|
|
||
| POSTGRES_SERVER=localhost | ||
| POSTGRES_PORT=5432 | ||
| POSTGRES_DB=ai_platform_test | ||
| POSTGRES_USER=postgres | ||
| POSTGRES_PASSWORD=postgres | ||
|
|
||
| # AWS | ||
|
|
||
| AWS_ACCESS_KEY_ID= | ||
| AWS_SECRET_ACCESS_KEY= | ||
| AWS_DEFAULT_REGION=ap-south-1 | ||
| AWS_S3_BUCKET_PREFIX="bucket-prefix-name" |
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 |
|---|---|---|
|
|
@@ -6,7 +6,7 @@ node_modules/ | |
| /playwright/.cache/ | ||
|
|
||
| # Environments | ||
| .env | ||
| .env* | ||
| .venv | ||
| env/ | ||
| venv/ | ||
|
|
||
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
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 |
|---|---|---|
| @@ -1,27 +1,60 @@ | ||
| import logging | ||
| from collections.abc import Generator | ||
|
|
||
| import pytest | ||
| from dotenv import find_dotenv | ||
| from fastapi.testclient import TestClient | ||
| from sqlmodel import Session | ||
| from sqlalchemy import event | ||
| from collections.abc import Generator | ||
| from sqlmodel import Session | ||
|
|
||
| from app.core.config import settings | ||
| from app.core.db import engine | ||
| from app.api.deps import get_db | ||
| from app.core.config import settings | ||
| from app.main import app | ||
| from app.models import APIKeyPublic | ||
| from app.tests.utils.user import authentication_token_from_email | ||
| from app.tests.utils.utils import get_superuser_token_headers, get_api_key_by_email | ||
| from app.seed_data.seed_data import seed_database | ||
| from app.tests.utils.user import authentication_token_from_email | ||
| from app.tests.utils.utils import ( | ||
| get_superuser_token_headers, | ||
| get_api_key_by_email, | ||
| load_environment, | ||
| ) | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| @pytest.fixture(scope="session", autouse=True) | ||
| def seed_baseline(): | ||
| # Load test environment to ensure correct bucket name | ||
| path = find_dotenv(".env.test") | ||
| load_environment(path) | ||
|
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. @AkhileshNegi I have modified seed_baseline function with
And this is my output After loading also settings is having old reference of env variables. db should be aipostgres_test. |
||
|
|
||
| # Import engine after environment is loaded | ||
| from app.core.db import engine | ||
|
|
||
| with Session(engine) as session: | ||
| logger.info("Seeding baseline data...") | ||
| seed_database(session) # deterministic baseline | ||
| yield | ||
|
|
||
|
|
||
| @pytest.fixture(scope="function", autouse=True) | ||
| def cleanup_sessions(): | ||
| """Clean up any lingering sessions after each test.""" | ||
| yield | ||
| # Force cleanup of any remaining connections in the pool | ||
| from app.core.db import engine | ||
|
|
||
| engine.dispose() | ||
|
|
||
|
|
||
| @pytest.fixture(scope="function") | ||
| def db() -> Generator[Session, None, None]: | ||
| from app.core.db import engine | ||
|
|
||
| connection = engine.connect() | ||
| transaction = connection.begin() | ||
| session = Session(bind=connection) | ||
|
|
||
| nested = session.begin_nested() | ||
|
|
||
| @event.listens_for(session, "after_transaction_end") | ||
| def restart_savepoint(sess, trans): | ||
| if trans.nested and not trans._parent.nested: | ||
|
|
@@ -32,14 +65,7 @@ def restart_savepoint(sess, trans): | |
| finally: | ||
| session.close() | ||
| transaction.rollback() | ||
| connection.close() | ||
|
|
||
|
|
||
| @pytest.fixture(scope="session", autouse=True) | ||
| def seed_baseline(): | ||
| with Session(engine) as session: | ||
| seed_database(session) # deterministic baseline | ||
| yield | ||
| connection.close() # Explicitly close the connection | ||
|
|
||
|
|
||
| @pytest.fixture(scope="function") | ||
|
|
||
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.
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.
This will use
.envfile, if there is no.env.testavailable.