From 77f216f2b3b2e2fb91f0d319a552497564d187b8 Mon Sep 17 00:00:00 2001 From: largittehuna Date: Fri, 23 Jan 2026 00:02:36 +0100 Subject: [PATCH 1/8] Refactor data : Refactor related code, and add tests for non-null database columns --- tests/test_models.py | 117 ++++++++++++++++++++++++++++++++++++------- 1 file changed, 100 insertions(+), 17 deletions(-) diff --git a/tests/test_models.py b/tests/test_models.py index c4a02ab..ae8a5f4 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -1,41 +1,124 @@ +import pytest from app.models import Account, Article, Comment -def test_create_account(db_session): - account = Account(account_username="pytest_user_account", account_password="123456789", account_email="test_account@example.com", account_role="user") +@pytest.fixture +def account_data(): + return { + "account_username": "pytest_user_account", + "account_password": "123456789", + "account_email": "test_account@example.com", + "account_role": "user" + } + +@pytest.fixture +def article_data(): + return { + "article_title": "Test Article", + "article_content": "This is a test article." + } + + +def test_create_account(db_session, account_data): + account = Account( + account_username=account_data["account_username"], + account_password=account_data["account_password"], + account_email=account_data["account_email"], + account_role=account_data["account_role"] + ) + db_session.add(account) db_session.commit() - result = db_session.query(Account).filter_by(account_username="pytest_user_account").first() + + result = ( + db_session.query(Account) + .filter_by(account_username=account_data["account_username"]) + .first() + ) + assert result is not None + assert result.account_username is not None assert result.account_password is not None + assert result.account_role is not None + assert result.account_username == "pytest_user_account" + assert result.account_password == "123456789" assert result.account_role == "user" -def test_create_article(db_session): - author = Account(account_username="pytest_author_article", account_password="123456789", account_email="author_article@test.com", account_role="author") +def test_create_article(db_session, account_data, article_data): + author = Account( + account_username=account_data["account_username"], + account_password=account_data["account_password"], + account_email=account_data["account_email"], + account_role="author" + ) + db_session.add(author) db_session.commit() - article = Article(article_author_id=author.account_id, article_title="Titre article", article_content="Contenu article") + + article = Article( + article_author_id=author.account_id, + article_title=article_data["article_title"], + article_content=article_data["article_content"] + ) + db_session.add(article) db_session.commit() - result = db_session.query(Article).filter_by(article_title="Titre article").first() + + result = ( + db_session.query(Article) + .filter_by(article_title=article_data["article_title"]) + .first() + ) + assert result is not None - assert result.article_author.account_username == "pytest_author_article" - assert result.article_author.account_password is not None + assert result.article_author_id is not None + assert result.article_title is not None + assert result.article_content is not None + assert result.article_author.account_username == "pytest_user_account" + assert result.article_author.account_password == "123456789" + assert result.article_author.account_role == "author" + +def test_create_comment(db_session, account_data, article_data): + author = Account( + account_username=account_data["account_username"], + account_password=account_data["account_password"], + account_email= account_data["account_email"], + account_role="author" + ) + + user = Account( + account_username="Bob", + account_password="2789@_124BBt", + account_email= "bob@humour.fr", + account_role=account_data["account_role"] + ) -def test_create_comment(db_session): - author = Account(account_username="pytest_author_comment", account_password="123456789", account_email="author_comment@test.com", account_role="author") - user = Account(account_username="pytest_user_comment", account_password="123456789", account_email="user_comment@test.com", account_role="user") db_session.add_all([author, user]) db_session.commit() - article = Article(article_author_id=author.account_id, article_title="Titre comment", article_content="Contenu comment") + + article = Article( + article_author_id = author.account_id, + article_title= article_data["article_title"], + article_content=article_data["article_content"] + ) + db_session.add(article) db_session.commit() - comment = Comment(comment_article_id=article.article_id, comment_written_account_id=user.account_id, comment_content="Bravo !") + + comment = Comment( + comment_article_id=article.article_id, + comment_written_account_id=user.account_id, + comment_content="Bravo !" + ) + db_session.add(comment) db_session.commit() + result = db_session.query(Comment).filter_by(comment_content="Bravo !").first() + assert result is not None - assert result.comment_author.account_username == "pytest_user_comment" - assert result.comment_author.account_password is not None - assert result.comment_article.article_title == "Titre comment" + assert result.comment_article_id is not None + assert result.comment_content is not None + assert result.comment_author.account_username == "Bob" + assert result.comment_article.article_title == "Test Article" assert result.comment_article.article_author.account_password is not None From 85f12ae82be521e0376fc4eb4737ee527bc5f4a9 Mon Sep 17 00:00:00 2001 From: largittehuna Date: Fri, 23 Jan 2026 00:06:31 +0100 Subject: [PATCH 2/8] Refactor data : Remove nullable flags from timestamp columns in model classes --- app/models.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/models.py b/app/models.py index 1f691bf..825ec6a 100644 --- a/app/models.py +++ b/app/models.py @@ -14,7 +14,7 @@ class Account(Base): account_password = Column("account_password", Text, nullable=False) account_email = Column("account_email", Text) account_role = Column("account_role", Text, nullable=False) - account_created_at = Column("account_created_at", TIMESTAMP, server_default=func.now(), nullable=False) + account_created_at = Column("account_created_at", TIMESTAMP, server_default=func.now()) articles = relationship("Article", back_populates="article_author", cascade="all, delete-orphan") comments = relationship("Comment", back_populates="comment_author", cascade="all, delete-orphan") @@ -27,7 +27,7 @@ class Article(Base): article_author_id = Column("article_author_id", Integer, ForeignKey("accounts.account_id", ondelete="CASCADE"), nullable=False) article_title = Column("article_title", Text, nullable=False) article_content = Column("article_content", Text, nullable=False) - article_published_at = Column("article_published_at", TIMESTAMP, server_default=func.now(), nullable=False) + article_published_at = Column("article_published_at", TIMESTAMP, server_default=func.now()) article_author = relationship("Account", back_populates="articles") article_comments = relationship("Comment", back_populates="comment_article", cascade="all, delete-orphan") @@ -41,7 +41,7 @@ class Comment(Base): comment_written_account_id = Column("comment_written_account_id", Integer, ForeignKey("accounts.account_id", ondelete="CASCADE"), nullable=False) comment_reply_to = Column("comment_reply_to", Integer, ForeignKey("comments.comment_id"), nullable=True) comment_content = Column("comment_content", Text, nullable=False) - comment_posted_at = Column("comment_posted_at", TIMESTAMP, server_default=func.now(), nullable=False) + comment_posted_at = Column("comment_posted_at", TIMESTAMP, server_default=func.now()) comment_article = relationship("Article", back_populates="article_comments") comment_author = relationship("Account", back_populates="comments") From 76ef9fedd6e80de221155d4599c33ada64760692 Mon Sep 17 00:00:00 2001 From: raydeveloppeur-admin Date: Sun, 25 Jan 2026 02:35:19 +0100 Subject: [PATCH 3/8] Refactor data and add tests : Remove redundant sessionmaker(bind=engine) and bind sessions explicitly per test The global bind on sessionmaker was unnecessary and misleading, since each test already binds the session to its own transactional connection. --- tests/conftest.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/conftest.py b/tests/conftest.py index d621886..6b408ca 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -18,7 +18,7 @@ ) engine = create_engine(database_url) -SessionLocal = sessionmaker(bind=engine) +SessionLocal = sessionmaker() def truncate_all_tables(connection): tables = Base.metadata.sorted_tables From 31d33198c37f138ca0182343b3592a626c69fd16 Mon Sep 17 00:00:00 2001 From: raydeveloppeur-admin Date: Sun, 25 Jan 2026 02:38:06 +0100 Subject: [PATCH 4/8] Refactor data and add tests : Move model class back into conftest.py to centralize imports Added here to avoid direct file dependencies in tests. --- tests/conftest.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/tests/conftest.py b/tests/conftest.py index 6b408ca..caedac3 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -5,7 +5,7 @@ from sqlalchemy import create_engine, text from sqlalchemy.orm import sessionmaker -from app.models import Base +from app.models import Account, Article, Base, Comment file_env = dotenv_values(".env.test") @@ -20,6 +20,15 @@ engine = create_engine(database_url) SessionLocal = sessionmaker() +def account_model(): + return Account + +def article_model(): + return Article + +def comment_model(): + return Comment + def truncate_all_tables(connection): tables = Base.metadata.sorted_tables table_names = ", ".join(f'"{t.name}"' for t in tables) From 3bce5ae7fc26bd5997feda29a762d00c5f2e53c5 Mon Sep 17 00:00:00 2001 From: raydeveloppeur-admin Date: Sun, 25 Jan 2026 05:26:56 +0100 Subject: [PATCH 5/8] =?UTF-8?q?Refactor=20data=20and=20add=20tests=20:=20A?= =?UTF-8?q?dd=20self=E2=80=91referential=20comment=20relationships=20to=20?= =?UTF-8?q?support=20replying=20to=20user=20comments?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/models.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/app/models.py b/app/models.py index 825ec6a..2d6ed78 100644 --- a/app/models.py +++ b/app/models.py @@ -43,6 +43,16 @@ class Comment(Base): comment_content = Column("comment_content", Text, nullable=False) comment_posted_at = Column("comment_posted_at", TIMESTAMP, server_default=func.now()) - comment_article = relationship("Article", back_populates="article_comments") - comment_author = relationship("Account", back_populates="comments") - replies = relationship("Comment", backref="parent", remote_side=[comment_id]) + comment_article = relationship(argument="Article", back_populates="article_comments") + comment_author = relationship(argument="Account", back_populates="comments") + reply_to_comment = relationship( + argument="Comment", + remote_side=[comment_id], + back_populates="comment_replies", + uselist=False, + ) + comment_replies = relationship( + argument="Comment", + back_populates="reply_to_comment", + cascade="all, delete-orphan", + ) From 11a4c421a7c23200ce4fd5f3754948d02910eacd Mon Sep 17 00:00:00 2001 From: raydeveloppeur-admin Date: Sun, 25 Jan 2026 05:27:58 +0100 Subject: [PATCH 6/8] Refactor data and add tests : Remove duplicated test logic and introduce more test cases --- tests/test_models.py | 239 +++++++++++++++++++++++++++---------------- 1 file changed, 153 insertions(+), 86 deletions(-) diff --git a/tests/test_models.py b/tests/test_models.py index ae8a5f4..234b993 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -1,124 +1,191 @@ import pytest -from app.models import Account, Article, Comment - - -@pytest.fixture -def account_data(): - return { - "account_username": "pytest_user_account", - "account_password": "123456789", - "account_email": "test_account@example.com", - "account_role": "user" - } - -@pytest.fixture -def article_data(): - return { - "article_title": "Test Article", - "article_content": "This is a test article." - } - - -def test_create_account(db_session, account_data): - account = Account( - account_username=account_data["account_username"], - account_password=account_data["account_password"], - account_email=account_data["account_email"], - account_role=account_data["account_role"] +import sqlalchemy + +from tests.conftest import account_model, article_model, comment_model + + +def make_account( + account_username="Xxx__D4RK_V4D0R__xxX", + account_password="987654321abcdefg@", + account_email="C4T@exemple.com", + account_role="user", +): + Account = account_model() + return Account( + account_username=account_username, + account_password=account_password, + account_email=account_email, + account_role=account_role, ) +def make_article( + article_author_id, + article_title="Luke, I'm your father !", + article_content=( + 'On the platform, Darth Vader stepped forward and spoke the truth: ' + '"Luke, I am your father." Shocked, Luke backed away, unable to accept it.' + ), +): + Article = article_model() + return Article( + article_author_id=article_author_id, + article_title=article_title, + article_content=article_content, + ) + +def make_comment( + comment_article_id, + comment_written_account_id, + comment_content="Bravo !", +): + Comment = comment_model() + return Comment( + comment_article_id=comment_article_id, + comment_written_account_id=comment_written_account_id, + comment_content=comment_content, + ) + + +def test_create_account(db_session): + account = make_account() db_session.add(account) db_session.commit() - + Account = account_model() result = ( db_session.query(Account) - .filter_by(account_username=account_data["account_username"]) + .filter_by(account_username=account.account_username) .first() ) - - assert result is not None - assert result.account_username is not None - assert result.account_password is not None - assert result.account_role is not None - assert result.account_username == "pytest_user_account" - assert result.account_password == "123456789" + assert result.account_username == "Xxx__D4RK_V4D0R__xxX" + assert result.account_password == "987654321abcdefg@" + assert result.account_email == "C4T@exemple.com" assert result.account_role == "user" -def test_create_article(db_session, account_data, article_data): - author = Account( - account_username=account_data["account_username"], - account_password=account_data["account_password"], - account_email=account_data["account_email"], - account_role="author" - ) - +def test_create_article(db_session): + author = make_account(account_role="author") db_session.add(author) db_session.commit() - - article = Article( - article_author_id=author.account_id, - article_title=article_data["article_title"], - article_content=article_data["article_content"] - ) - + article = make_article(article_author_id=author.account_id) db_session.add(article) db_session.commit() - + Article = article_model() result = ( db_session.query(Article) - .filter_by(article_title=article_data["article_title"]) + .filter_by(article_title=article.article_title) .first() ) - - assert result is not None - assert result.article_author_id is not None - assert result.article_title is not None - assert result.article_content is not None - assert result.article_author.account_username == "pytest_user_account" - assert result.article_author.account_password == "123456789" - assert result.article_author.account_role == "author" - -def test_create_comment(db_session, account_data, article_data): - author = Account( - account_username=account_data["account_username"], - account_password=account_data["account_password"], - account_email= account_data["account_email"], - account_role="author" + expected_article_content = ( + 'On the platform, Darth Vader stepped forward and spoke the truth: ' + '"Luke, I am your father." Shocked, Luke backed away, unable to accept it.' ) + assert result.article_author_id == 1 + assert result.article_title == "Luke, I'm your father !" + assert result.article_content == expected_article_content + assert result.article_author.account_role == "author" - user = Account( +def test_create_comment(db_session): + author = make_account(account_role="author") + user = make_account( account_username="Bob", account_password="2789@_124BBt", - account_email= "bob@humour.fr", - account_role=account_data["account_role"] + account_email="bob@funny.com" ) - db_session.add_all([author, user]) db_session.commit() - - article = Article( - article_author_id = author.account_id, - article_title= article_data["article_title"], - article_content=article_data["article_content"] + article = make_article(article_author_id=author.account_id) + db_session.add(article) + db_session.commit() + comment = make_comment(comment_article_id=article.article_id, comment_written_account_id=user.account_id) + db_session.add(comment) + db_session.commit() + Comment = comment_model() + result = ( + db_session.query(Comment) + .filter_by(comment_content=comment.comment_content) + .first() ) + assert result.comment_article_id == 1 + assert result.comment_written_account_id == 2 + assert result.comment_author.account_username == "Bob" + assert result.comment_author.account_password == "2789@_124BBt" + assert result.comment_author.account_email == "bob@funny.com" + assert result.comment_content == "Bravo !" +def test_create_comment_reply_to(db_session): + author = make_account(account_role="author") + user = make_account( + account_username="Bob", + account_password="2789@_124BBt", + account_email="bob@funny.com" + ) + db_session.add_all([author, user]) + db_session.commit() + article = make_article(article_author_id=author.account_id) db_session.add(article) db_session.commit() - - comment = Comment( + parent_comment = make_comment( comment_article_id=article.article_id, comment_written_account_id=user.account_id, comment_content="Bravo !" ) + db_session.add(parent_comment) + db_session.commit() + reply_comment = make_comment( + comment_article_id=article.article_id, + comment_written_account_id=user.account_id, + comment_content="Thank you !" + ) + reply_comment.comment_reply_to = parent_comment.comment_id + db_session.add(reply_comment) + db_session.commit() + Comment = comment_model() + result = ( + db_session.query(Comment) + .filter_by(comment_content=reply_comment.comment_content) + .first() + ) + assert result.comment_reply_to == 1 + assert result.reply_to_comment.comment_id == 1 + assert result.comment_content == "Thank you !" - db_session.add(comment) +def test_account_username_unique(db_session): + first = make_account() + db_session.add(first) db_session.commit() + second = make_account() + db_session.add(second) + with pytest.raises(sqlalchemy.exc.IntegrityError): + db_session.commit() - result = db_session.query(Comment).filter_by(comment_content="Bravo !").first() +def test_account_missing_username(db_session): + account = make_account(account_username=None) + db_session.add(account) + with pytest.raises(sqlalchemy.exc.IntegrityError): + db_session.commit() + +def test_account_role_invalid(db_session): + account = make_account(account_role="superadmin") + db_session.add(account) + with pytest.raises(sqlalchemy.exc.IntegrityError): + db_session.commit() + +def test_article_missing_title(db_session): + author = make_account(account_role="author") + db_session.add(author) + db_session.commit() + Article = article_model() + article = Article(article_author_id=author.account_id, article_title=None,) + db_session.add(article) + with pytest.raises(sqlalchemy.exc.IntegrityError): + db_session.commit() + +def test_article_missing_content(db_session): + author = make_account(account_role="author") + db_session.add(author) + db_session.commit() + Article = article_model() + article = Article(article_author_id=author.account_id, article_content=None) + db_session.add(article) + with pytest.raises(sqlalchemy.exc.IntegrityError): + db_session.commit() - assert result is not None - assert result.comment_article_id is not None - assert result.comment_content is not None - assert result.comment_author.account_username == "Bob" - assert result.comment_article.article_title == "Test Article" - assert result.comment_article.article_author.account_password is not None From 3bb80b473b2d4f01fe2466e0297060c28bc6e818 Mon Sep 17 00:00:00 2001 From: raydeveloppeur-admin Date: Sun, 25 Jan 2026 05:28:50 +0100 Subject: [PATCH 7/8] Refactor data and add tests : Apply linting --- app/models.py | 89 +++++++++++++++++++++++++++++++++++------------ tests/conftest.py | 1 - 2 files changed, 66 insertions(+), 24 deletions(-) diff --git a/app/models.py b/app/models.py index 2d6ed78..647686d 100644 --- a/app/models.py +++ b/app/models.py @@ -1,47 +1,90 @@ -from sqlalchemy import TIMESTAMP, CheckConstraint, Column, ForeignKey, Integer, Text, func +from sqlalchemy import ( + TIMESTAMP, + CheckConstraint, + Column, + ForeignKey, + Integer, + Text, + func, +) from sqlalchemy.orm import declarative_base, relationship Base = declarative_base() + class Account(Base): __tablename__ = "accounts" __table_args__ = ( - CheckConstraint("account_role IN ('admin', 'author', 'user')", name="accounts_role_check"), + CheckConstraint( + sqltext="account_role IN ('admin', 'author', 'user')", name="accounts_role_check" + ), ) - account_id = Column("account_id", Integer, primary_key=True, autoincrement=True) - account_username = Column("account_username", Text, unique=True, nullable=False) - account_password = Column("account_password", Text, nullable=False) - account_email = Column("account_email", Text) - account_role = Column("account_role", Text, nullable=False) - account_created_at = Column("account_created_at", TIMESTAMP, server_default=func.now()) + account_id = Column(name="account_id", type_=Integer, primary_key=True, autoincrement=True) + account_username = Column(name="account_username", type_=Text, unique=True, nullable=False) + account_password = Column(name="account_password", type_=Text, nullable=False) + account_email = Column(name="account_email", type_=Text) + account_role = Column(name="account_role", type_=Text, nullable=False) + account_created_at = Column( + name="account_created_at", type_=TIMESTAMP, server_default=func.now() + ) - articles = relationship("Article", back_populates="article_author", cascade="all, delete-orphan") - comments = relationship("Comment", back_populates="comment_author", cascade="all, delete-orphan") + articles = relationship( + argument="Article", back_populates="article_author", cascade="all, delete-orphan" + ) + comments = relationship( + argument="Comment", back_populates="comment_author", cascade="all, delete-orphan" + ) class Article(Base): __tablename__ = "articles" - article_id = Column("article_id", Integer, primary_key=True, autoincrement=True) - article_author_id = Column("article_author_id", Integer, ForeignKey("accounts.account_id", ondelete="CASCADE"), nullable=False) - article_title = Column("article_title", Text, nullable=False) - article_content = Column("article_content", Text, nullable=False) - article_published_at = Column("article_published_at", TIMESTAMP, server_default=func.now()) + article_id = Column(name="article_id", type_=Integer, primary_key=True, autoincrement=True) + article_author_id = Column( + ForeignKey("accounts.account_id", ondelete="CASCADE"), + name="article_author_id", + type_=Integer, + nullable=False, + ) + article_title = Column(name="article_title", type_=Text, nullable=False) + article_content = Column(name="article_content", type_=Text, nullable=False) + article_published_at = Column( + name="article_published_at", type_=TIMESTAMP, server_default=func.now() + ) - article_author = relationship("Account", back_populates="articles") - article_comments = relationship("Comment", back_populates="comment_article", cascade="all, delete-orphan") + article_author = relationship(argument="Account", back_populates="articles") + article_comments = relationship( + argument="Comment", back_populates="comment_article", cascade="all, delete-orphan" + ) class Comment(Base): __tablename__ = "comments" - comment_id = Column("comment_id", Integer, primary_key=True, autoincrement=True) - comment_article_id = Column("comment_article_id", Integer, ForeignKey("articles.article_id", ondelete="CASCADE"), nullable=False) - comment_written_account_id = Column("comment_written_account_id", Integer, ForeignKey("accounts.account_id", ondelete="CASCADE"), nullable=False) - comment_reply_to = Column("comment_reply_to", Integer, ForeignKey("comments.comment_id"), nullable=True) - comment_content = Column("comment_content", Text, nullable=False) - comment_posted_at = Column("comment_posted_at", TIMESTAMP, server_default=func.now()) + comment_id = Column(name="comment_id", type_=Integer, primary_key=True, autoincrement=True) + comment_article_id = Column( + ForeignKey("articles.article_id", ondelete="CASCADE"), + name="comment_article_id", + type_=Integer, + nullable=False, + ) + comment_written_account_id = Column( + ForeignKey("accounts.account_id", ondelete="CASCADE"), + name="comment_written_account_id", + type_=Integer, + nullable=False, + ) + comment_reply_to = Column( + ForeignKey("comments.comment_id"), + name="comment_reply_to", + type_=Integer, + nullable=True, + ) + comment_content = Column(name="comment_content", type_=Text, nullable=False) + comment_posted_at = Column( + name="comment_posted_at", type_=TIMESTAMP, server_default=func.now() + ) comment_article = relationship(argument="Article", back_populates="article_comments") comment_author = relationship(argument="Account", back_populates="comments") diff --git a/tests/conftest.py b/tests/conftest.py index caedac3..ffbcf48 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -39,7 +39,6 @@ def db_session(): with engine.begin() as connection: truncate_all_tables(connection) session = SessionLocal(bind=connection) - try: yield session finally: From 1f975e803e9f346fe73d08e176cc5b75d3b2ac72 Mon Sep 17 00:00:00 2001 From: raydeveloppeur-admin Date: Sun, 25 Jan 2026 05:30:46 +0100 Subject: [PATCH 8/8] Refactor data and add tests : Rewrite comments to focus on intent instead of describing the code --- tests/conftest.py | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index ffbcf48..6060013 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -8,15 +8,10 @@ from app.models import Account, Article, Base, Comment file_env = dotenv_values(".env.test") - -# Database selection logic: -# 1. Local environment: use TEST_DATABASE_URL from .env.test -# 2. Optional override: use TEST_DATABASE_URL from os.environ if provided -database_url = ( - file_env.get("TEST_DATABASE_URL") - or os.getenv("TEST_DATABASE_URL") -) - +# CI (GitHub Actions) provides TEST_DATABASE_URL via environment variables. +# Locally, we fall back to .env.test for a dedicated test database. +# This keeps the test configuration consistent across environments without changing the code. +database_url = file_env.get("TEST_DATABASE_URL") or os.getenv("TEST_DATABASE_URL") engine = create_engine(database_url) SessionLocal = sessionmaker()