From 97237f8e5da8af59308a90dc9526c78cc788c43a Mon Sep 17 00:00:00 2001 From: Kenneth Kehl <@kkehl@flexion.us> Date: Thu, 10 Oct 2024 14:35:43 -0700 Subject: [PATCH] try to fix test code --- .ds.baseline | 4 +- tests/app/dao/test_services_dao.py | 69 +++++++++++++++++++----------- 2 files changed, 46 insertions(+), 27 deletions(-) diff --git a/.ds.baseline b/.ds.baseline index 01fb2bf3d..ee20a155d 100644 --- a/.ds.baseline +++ b/.ds.baseline @@ -239,7 +239,7 @@ "filename": "tests/app/dao/test_services_dao.py", "hashed_secret": "5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8", "is_verified": false, - "line_number": 285, + "line_number": 290, "is_secret": false } ], @@ -384,5 +384,5 @@ } ] }, - "generated_at": "2024-10-10T21:03:06Z" + "generated_at": "2024-10-10T21:35:38Z" } diff --git a/tests/app/dao/test_services_dao.py b/tests/app/dao/test_services_dao.py index 7e01e8e1e..df08ce06e 100644 --- a/tests/app/dao/test_services_dao.py +++ b/tests/app/dao/test_services_dao.py @@ -95,6 +95,11 @@ def _get_service_query_count(): return db.session.execute(stmt).scalar() +def _get_service_history_query_count(): + stmt = select(Service.get_history_model()) + return db.session.execute(stmt).scalar() + + def _get_first_service(): stmt = select(Service).limit(1) service = db.session.execute(stmt).scalars().first() @@ -152,7 +157,7 @@ def test_create_service_with_organization(notify_db_session): dao_create_service(service, user) assert _get_service_query_count() == 1 service_db = _get_first_service() - organization = Organization.query.get(organization.id) + organization = db.session.get(Organization, organization.id) assert service_db.name == "service_name" assert service_db.id == service.id assert service_db.email_from == "email_from" @@ -183,7 +188,7 @@ def test_fetch_service_by_id_with_api_keys(notify_db_session): dao_create_service(service, user) assert _get_service_query_count() == 1 service_db = _get_first_service() - organization = Organization.query.get(organization.id) + organization = db.session.get(Organization, organization.id) assert service_db.name == "service_name" assert service_db.id == service.id assert service_db.email_from == "email_from" @@ -689,7 +694,7 @@ def test_removing_all_permission_returns_service_with_no_permissions(notify_db_s def test_create_service_creates_a_history_record_with_current_data(notify_db_session): user = create_user() assert _get_service_query_count() == 0 - assert Service.get_history_model().query.count() == 0 + assert _get_service_history_query_count() == 0 service = Service( name="service_name", email_from="email_from", @@ -699,7 +704,7 @@ def test_create_service_creates_a_history_record_with_current_data(notify_db_ses ) dao_create_service(service, user) assert _get_service_query_count() == 1 - assert Service.get_history_model().query.count() == 1 + assert _get_service_history_query_count() == 1 service_from_db = _get_first_service() service_history = Service.get_history_model().query.first() @@ -715,7 +720,7 @@ def test_create_service_creates_a_history_record_with_current_data(notify_db_ses def test_update_service_creates_a_history_record_with_current_data(notify_db_session): user = create_user() assert _get_service_query_count() == 0 - assert Service.get_history_model().query.count() == 0 + assert _get_service_history_query_count() == 0 service = Service( name="service_name", email_from="email_from", @@ -727,13 +732,13 @@ def test_update_service_creates_a_history_record_with_current_data(notify_db_ses assert _get_service_query_count() == 1 assert _get_first_service().version == 1 - assert Service.get_history_model().query.count() == 1 + assert _get_service_history_query_count() == 1 service.name = "updated_service_name" dao_update_service(service) assert _get_service_query_count() == 1 - assert Service.get_history_model().query.count() == 2 + assert _get_service_history_query_count() == 2 service_from_db = _get_first_service() @@ -757,7 +762,7 @@ def test_update_service_permission_creates_a_history_record_with_current_data( ): user = create_user() assert _get_service_query_count() == 0 - assert Service.get_history_model().query.count() == 0 + assert _get_service_history_query_count() == 0 service = Service( name="service_name", email_from="email_from", @@ -783,7 +788,7 @@ def test_update_service_permission_creates_a_history_record_with_current_data( dao_update_service(service) assert _get_service_query_count() == 1 - assert Service.get_history_model().query.count() == 2 + assert _get_service_history_query_count() == 2 service_from_db = _get_first_service() @@ -805,7 +810,7 @@ def test_update_service_permission_creates_a_history_record_with_current_data( dao_update_service(service) assert _get_service_query_count() == 1 - assert Service.get_history_model().query.count() == 3 + assert _get_service_history_query_count() == 3 service_from_db = _get_first_service() assert service_from_db.version == 3 @@ -830,7 +835,7 @@ def test_update_service_permission_creates_a_history_record_with_current_data( def test_create_service_and_history_is_transactional(notify_db_session): user = create_user() assert _get_service_query_count() == 0 - assert Service.get_history_model().query.count() == 0 + assert _get_service_history_query_count() == 0 service = Service( name=None, email_from="email_from", @@ -848,7 +853,7 @@ def test_create_service_and_history_is_transactional(notify_db_session): ) assert _get_service_query_count() == 0 - assert Service.get_history_model().query.count() == 0 + assert _get_service_history_query_count() == 0 def test_delete_service_and_associated_objects(notify_db_session): @@ -874,21 +879,35 @@ def test_delete_service_and_associated_objects(notify_db_session): ) delete_service_and_all_associated_db_objects(service) - assert VerifyCode.query.count() == 0 - assert ApiKey.query.count() == 0 - assert ApiKey.get_history_model().query.count() == 0 - assert Template.query.count() == 0 - assert TemplateHistory.query.count() == 0 - assert Job.query.count() == 0 - assert Notification.query.count() == 0 - assert Permission.query.count() == 0 - assert User.query.count() == 0 - assert InvitedUser.query.count() == 0 + stmt = select(VerifyCode) + assert db.session.execute(stmt).scalar() == 0 + stmt = select(ApiKey) + assert db.session.execute(stmt).scalar() == 0 + stmt = select(ApiKey.get_history_model()) + assert db.session.execute(stmt).scalar() == 0 + stmt = select(Template) + assert db.session.execute(stmt).scalar() == 0 + stmt = select(TemplateHistory) + assert db.session.execute(stmt).scalar() == 0 + stmt = select(Job) + assert db.session.execute(stmt).scalar() == 0 + stmt = select(Notification) + assert db.session.execute(stmt).scalar() == 0 + stmt = select(Permission) + assert db.session.execute(stmt).scalar() == 0 + stmt = select(User) + assert db.session.execute(stmt).scalar() == 0 + stmt = select(InvitedUser) + assert db.session.execute(stmt).scalar() == 0 + assert _get_service_query_count() == 0 - assert Service.get_history_model().query.count() == 0 - assert ServicePermission.query.count() == 0 + assert _get_service_history_query_count() == 0 + stmt = select(ServicePermission) + assert db.session.execute(stmt).scalar() == 0 + # the organization hasn't been deleted - assert Organization.query.count() == 1 + stmt = select(Organization) + assert db.session.execute(stmt).scalar() == 1 def test_add_existing_user_to_another_service_doesnot_change_old_permissions(