Skip to content

Commit

Permalink
Minor rewrite db init tests
Browse files Browse the repository at this point in the history
  • Loading branch information
uittenbroekrobbert committed Jul 11, 2024
1 parent ba6f74c commit 35bfe85
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 74 deletions.
139 changes: 95 additions & 44 deletions tests/core/test_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
from unittest.mock import MagicMock

import pytest
from sqlmodel import Session, select
import tad.core.db as testdb
from sqlmodel import Session, SQLModel, select
from tad.core.config import Settings
from tad.core.db import (
add_demo_statuses,
Expand All @@ -16,10 +17,10 @@

from tests.constants import (
default_status,
default_task,
default_user,
)
from tests.database_test_utils import DatabaseTestUtils
from tests.function_calls_utils import count_calls

logger = logging.getLogger(__name__)

Expand All @@ -35,76 +36,126 @@ def test_check_database():


@pytest.mark.parametrize(
("patch_settings", "caplog"),
"patch_settings",
[
({"ENVIRONMENT": "demo", "AUTO_CREATE_SCHEMA": True}, None),
({"ENVIRONMENT": "demo", "AUTO_CREATE_SCHEMA": False}, None),
({"ENVIRONMENT": "production"}, None),
({"ENVIRONMENT": "demo", "AUTO_CREATE_SCHEMA": True}),
({"ENVIRONMENT": "demo", "AUTO_CREATE_SCHEMA": False}),
({"ENVIRONMENT": "production"}),
],
indirect=True,
)
def test_init_database(patch_settings: Settings, caplog: pytest.LogCaptureFixture):
def test_init_database(patch_settings: Settings):
remove_old_demo_objects_orig = testdb.remove_old_demo_objects
testdb.remove_old_demo_objects = MagicMock()
create_all_orig = SQLModel.metadata.create_all
SQLModel.metadata.create_all = MagicMock()

init_db()

if patch_settings.ENVIRONMENT == "demo":
assert [record.message for record in caplog.records if "Creating demo data" in record.message]
assert testdb.remove_old_demo_objects.called
if patch_settings.AUTO_CREATE_SCHEMA is True:
assert [record.message for record in caplog.records if "Creating database schema" in record.message]
assert [record.message for record in caplog.records if "Finished initializing database" in record.message]
assert SQLModel.metadata.create_all.called

testdb.remove_old_demo_objects = remove_old_demo_objects_orig
SQLModel.metadata.create_all = create_all_orig


def test_remove_old_demo_objects(db: DatabaseTestUtils, monkeypatch: pytest.MonkeyPatch):
def test_remove_old_demo_objects(db: DatabaseTestUtils):
org_delete = db.get_session().delete
db_session = db.get_session()
db_session.delete = MagicMock()

user = User(name="Robbert", avatar=None)
status = Status(name="Todo", sort_order=1)
task = Task(title="First task", description="This is the first task", sort_order=1, status_id=status.id)

db.given([user, status, task])
session_delete_counter = count_calls(monkeypatch, db.get_session(), db.get_session().delete)

remove_old_demo_objects(db.get_session())
assert session_delete_counter.get() == 3
assert db_session.delete.call_count == 3

db.get_session().delete = org_delete


def test_remove_old_demo_objects_nothing_to_delete(db: DatabaseTestUtils):
org_delete = db.get_session().delete
db_session = db.get_session()
db_session.delete = MagicMock()

session_delete_counter = count_calls(monkeypatch, db.get_session(), db.get_session().delete)
remove_old_demo_objects(db.get_session())
assert session_delete_counter.get() == 0
assert db_session.delete.call_count == 0

db.get_session().delete = org_delete

def test_add_demo_user(db: DatabaseTestUtils, monkeypatch: pytest.MonkeyPatch):
user_names = [default_user().name]

session_add_counter = count_calls(monkeypatch, db.get_session(), db.get_session().add)
add_demo_users(db.get_session(), user_names)
assert db.exists(User, User.name, user_names[0])
assert session_add_counter.get() == 1
# test again, the user already exists so new new user should be created
session_add_counter = count_calls(monkeypatch, db.get_session(), db.get_session().add)
def test_add_demo_user(db: DatabaseTestUtils):
user_names = [default_user().name]
add_demo_users(db.get_session(), user_names)
assert db.exists(User, User.name, user_names[0])
assert session_add_counter.get() == 0


def test_add_demo_status(db: DatabaseTestUtils, monkeypatch: pytest.MonkeyPatch):
session_add_counter = count_calls(monkeypatch, db.get_session(), db.get_session().add)
add_demo_statuses(db.get_session(), [default_status().name])
assert db.exists(Status, Status.name, default_status().name)
assert session_add_counter.get() == 1
# test again, the status already exists so no new status should be created
session_add_counter = count_calls(monkeypatch, db.get_session(), db.get_session().add)
def test_add_demo_user_nothing_to_add(db: DatabaseTestUtils):
db.given([default_user()])

orig_add = db.get_session().add
db_session = db.get_session()
db_session.add = MagicMock()

add_demo_users(db.get_session(), [default_user().name])

assert db_session.add.call_count == 0

db.get_session().add = orig_add


def test_add_demo_status(db: DatabaseTestUtils):
add_demo_statuses(db.get_session(), [default_status().name])
assert db.exists(Status, Status.name, default_status().name)
assert session_add_counter.get() == 0


def test_add_demo_tasks(db: DatabaseTestUtils, monkeypatch: pytest.MonkeyPatch):
def test_add_demo_status_nothing_to_add(db: DatabaseTestUtils):
db.given([default_status()])
session_add_counter = count_calls(monkeypatch, db.get_session(), db.get_session().add)

orig_add = db.get_session().add
db_session = db.get_session()
db_session.add = MagicMock()

add_demo_statuses(db.get_session(), [default_status().name])

assert db_session.add.call_count == 0
db.get_session().add = orig_add


def test_add_demo_tasks(db: DatabaseTestUtils):
add_demo_tasks(db.get_session(), default_status(), 3)
assert session_add_counter.get() == 3
assert db.exists(Task, Task.title, "Example task 1")
# test again, the tasks already exist so no new cards should be created
session_add_counter = count_calls(monkeypatch, db.get_session(), db.get_session().add)
assert db.exists(Task, Task.title, "Example task 2")
assert db.exists(Task, Task.title, "Example task 3")


def test_add_demo_tasks_nothing_to_add(db: DatabaseTestUtils):
db.given(
[
default_task(title="Example task 1"),
default_task(title="Example task 2"),
default_task(title="Example task 3"),
]
)

orig_add = db.get_session().add
db_session = db.get_session()
db_session.add = MagicMock()

add_demo_tasks(db.get_session(), default_status(), 3)
assert session_add_counter.get() == 0
assert db.exists(Task, Task.title, "Example task 1")
# test without status
session_add_counter = count_calls(monkeypatch, db.get_session(), db.get_session().add)
add_demo_tasks(db.get_session(), None, 1)
assert session_add_counter.get() == 0
assert db_session.add.call_count == 0
db.get_session().add = orig_add


def test_add_demo_tasks_no_status(db: DatabaseTestUtils):
orig_add = db.get_session().add
db_session = db.get_session()
db_session.add = MagicMock()

add_demo_tasks(db.get_session(), None, 3)
assert db_session.add.call_count == 0
db.get_session().add = orig_add
30 changes: 0 additions & 30 deletions tests/function_calls_utils.py

This file was deleted.

0 comments on commit 35bfe85

Please sign in to comment.