Skip to content
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

fix more sqlalchemy #1417

Open
wants to merge 100 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
100 commits
Select commit Hold shift + click to select a range
6d8fdab
fix more sqlalchemy
Nov 14, 2024
7b83ea3
fix more
Nov 14, 2024
310386a
hmmm
Nov 14, 2024
5276ba4
hmmm
Nov 14, 2024
441b75d
hmmm
Nov 14, 2024
b708498
hmmm
Nov 14, 2024
f16c814
revert one test
Nov 14, 2024
0177fdc
hmmm
Nov 14, 2024
8c7cb44
more tests
Nov 15, 2024
b0bf042
more
Nov 15, 2024
704fff7
more
Nov 15, 2024
8f211cc
more
Nov 15, 2024
d1ebcba
more
Nov 15, 2024
1311598
more
Nov 15, 2024
3673a92
fix more
Nov 15, 2024
ee00100
fix tests
Nov 15, 2024
5c6a5f9
fix tests
Nov 15, 2024
5f4da49
more
Nov 15, 2024
98ee86f
more
Nov 15, 2024
703a29f
more
Nov 15, 2024
1f0a64d
more
Nov 15, 2024
555cf5d
more
Nov 15, 2024
3eadfb2
fix style
Nov 15, 2024
bc4d4c9
more
Nov 15, 2024
5b667a1
more
Nov 18, 2024
6c44f81
more
Nov 18, 2024
2eb692a
more
Nov 18, 2024
4ef1847
fix permission_dao
Nov 18, 2024
3168f28
more
Nov 18, 2024
4f3b99b
more
Nov 18, 2024
c77382c
more
Nov 18, 2024
da57887
fix
Nov 18, 2024
2c3c107
revert bad changes
Nov 18, 2024
ce42803
fix
Nov 18, 2024
92bf9c5
more
Nov 18, 2024
25d2901
try fixing pagination
Nov 18, 2024
4539e5c
try fixing pagination
Nov 18, 2024
71e4794
try fixing pagination
Nov 18, 2024
552d464
try fixing pagination
Nov 18, 2024
e7abb06
try fixing pagination
Nov 18, 2024
83a7df6
try fixing pagination
Nov 18, 2024
c3de127
try fixing pagination
Nov 18, 2024
d513c5e
debug
Nov 18, 2024
14bfcd3
debug
Nov 18, 2024
d2d5fa7
fix
Nov 18, 2024
c92a760
fix
Nov 18, 2024
4078a7e
fix
Nov 18, 2024
53f3948
fix
Nov 18, 2024
ce5b9cf
fix
Nov 18, 2024
38e7672
fix
Nov 18, 2024
43a1969
fix
Nov 18, 2024
2abb14d
fix
Nov 18, 2024
84ebf11
fix
Nov 18, 2024
27c9885
fix
Nov 18, 2024
f9cf3f3
fix scheduled tasks
Nov 19, 2024
b33e2ca
fix scheduled tasks
Nov 19, 2024
9d257eb
fix scheduled tasks
Nov 19, 2024
c45a5a3
fix
Nov 19, 2024
8945f84
fix
Nov 19, 2024
4d544ef
fix
Nov 19, 2024
bfcc8ac
fix
Nov 19, 2024
67aa1e6
fix
Nov 19, 2024
0d44f29
fix
Nov 19, 2024
32e73f6
revert
Nov 19, 2024
63ff83b
fix
Nov 19, 2024
a53c067
uploads
Nov 19, 2024
0c9995d
uploads
Nov 19, 2024
b388d9f
fix notifications
Nov 19, 2024
76dc06c
fix notifications
Nov 19, 2024
8b74448
fix notifications
Nov 19, 2024
331f0eb
fix notifications
Nov 19, 2024
fd7b3b9
fix notifications
Nov 19, 2024
a3658ce
fix notifications
Nov 19, 2024
4a03f5b
remove print statements
Nov 19, 2024
1a1de39
change querie to stmt
Nov 21, 2024
d283613
code review feedback and merge from main
Nov 21, 2024
6e23d0b
merge from main
Nov 22, 2024
9e5b21e
merge from main
Nov 26, 2024
8cd0bd8
code review feedback
Dec 19, 2024
096ec68
code review feedback
Dec 19, 2024
e4782e4
code review feedback
Dec 19, 2024
f4ce3d1
code review feedback
Dec 19, 2024
6591693
code review feedback
Dec 19, 2024
3388371
fix filter_bys
Dec 19, 2024
83193c2
fix fragile filter approach
Dec 19, 2024
db16f94
noqa the x == False for sqlalchemy
Dec 19, 2024
772f78d
noqa the x == False for sqlalchemy
Dec 19, 2024
5f9b4bc
noqa the x == False for sqlalchemy
Dec 19, 2024
67d8974
noqa the x == False for sqlalchemy
Dec 19, 2024
440bf85
noqa the x == False for sqlalchemy
Dec 19, 2024
c29fb78
noqa the x == False for sqlalchemy
Dec 19, 2024
9954ac4
noqa the x == False for sqlalchemy
Dec 19, 2024
605782c
noqa the x == False for sqlalchemy
Dec 19, 2024
bb1d81b
noqa the x == False for sqlalchemy
Dec 19, 2024
5a94229
noqa the x == False for sqlalchemy
Dec 19, 2024
8f572bb
noqa the x == False for sqlalchemy
Dec 19, 2024
f809a06
noqa the x == False for sqlalchemy
Dec 19, 2024
a77343e
noqa the x == False for sqlalchemy
Dec 19, 2024
3c0621f
more code review feedback
Dec 20, 2024
d8b7b06
merge from main
Dec 23, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .ds.baseline
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@
"filename": "tests/app/service/test_rest.py",
"hashed_secret": "5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8",
"is_verified": false,
"line_number": 1284,
"line_number": 1285,
"is_secret": false
}
],
Expand Down Expand Up @@ -384,5 +384,5 @@
}
]
},
"generated_at": "2024-10-31T21:25:32Z"
"generated_at": "2024-11-14T22:52:47Z"
}
11 changes: 7 additions & 4 deletions tests/app/celery/test_reporting_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import pytest
from freezegun import freeze_time
from sqlalchemy import select
from sqlalchemy import func, select

from app import db
from app.celery.reporting_tasks import (
Expand Down Expand Up @@ -363,9 +363,12 @@ def test_create_nightly_billing_for_day_use_BST(
rate_multiplier=1.0,
billable_units=4,
)

assert Notification.query.count() == 3
assert FactBilling.query.count() == 0
stmt = select(func.count()).select_from(Notification)
count = db.session.execute(stmt).scalar() or 0
assert count == 3
stmt = select(func.count()).select_from(FactBilling)
count = db.session.execute(stmt).scalar() or 0
assert count == 0

create_nightly_billing_for_day("2018-03-25")
records = FactBilling.query.order_by(FactBilling.local_date).all()
Expand Down
52 changes: 31 additions & 21 deletions tests/app/celery/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ def test_should_send_template_to_correct_sms_task_and_persist(
encryption.encrypt(notification),
)

persisted_notification = Notification.query.one()
persisted_notification = _get_notification_query_one()
assert persisted_notification.to == "1"
assert persisted_notification.template_id == sample_template_with_placeholders.id
assert (
Expand All @@ -431,6 +431,11 @@ def test_should_send_template_to_correct_sms_task_and_persist(
)


def _get_notification_query_one():
stmt = select(Notification)
return db.session.execute(stmt).scalars().one()


def test_should_save_sms_if_restricted_service_and_valid_number(
notify_db_session, mocker
):
Expand All @@ -451,7 +456,7 @@ def test_should_save_sms_if_restricted_service_and_valid_number(
encrypt_notification,
)

persisted_notification = Notification.query.one()
persisted_notification = _get_notification_query_one()
assert persisted_notification.to == "1"
assert persisted_notification.template_id == template.id
assert persisted_notification.template_version == template.version
Expand Down Expand Up @@ -490,7 +495,7 @@ def test_save_email_should_save_default_email_reply_to_text_on_notification(
encryption.encrypt(notification),
)

persisted_notification = Notification.query.one()
persisted_notification = _get_notification_query_one()
assert persisted_notification.reply_to_text == "reply_to@digital.fake.gov"


Expand All @@ -510,7 +515,7 @@ def test_save_sms_should_save_default_sms_sender_notification_reply_to_text_on(
encryption.encrypt(notification),
)

persisted_notification = Notification.query.one()
persisted_notification = _get_notification_query_one()
assert persisted_notification.reply_to_text == "12345"


Expand All @@ -534,6 +539,11 @@ def test_should_not_save_sms_if_restricted_service_and_invalid_number(
assert _get_notification_query_count() == 0


def _get_notification_query_all():
stmt = select(Notification)
return db.session.execute(stmt).scalars().all()


def _get_notification_query_count():
stmt = select(func.count()).select_from(Notification)
return db.session.execute(stmt).scalar() or 0
Expand Down Expand Up @@ -577,7 +587,7 @@ def test_should_save_sms_template_to_and_persist_with_job_id(sample_job, mocker)
notification_id,
encryption.encrypt(notification),
)
persisted_notification = Notification.query.one()
persisted_notification = _get_notification_query_one()
assert persisted_notification.to == "1"
assert persisted_notification.job_id == sample_job.id
assert persisted_notification.template_id == sample_job.template.id
Expand Down Expand Up @@ -642,7 +652,7 @@ def test_should_use_email_template_and_persist(
encryption.encrypt(notification),
)

persisted_notification = Notification.query.one()
persisted_notification = _get_notification_query_one()
assert persisted_notification.to == "1"
assert (
persisted_notification.template_id == sample_email_template_with_placeholders.id
Expand Down Expand Up @@ -689,7 +699,7 @@ def test_save_email_should_use_template_version_from_job_not_latest(
encryption.encrypt(notification),
)

persisted_notification = Notification.query.one()
persisted_notification = _get_notification_query_one()
assert persisted_notification.to == "1"
assert persisted_notification.template_id == sample_email_template.id
assert persisted_notification.template_version == version_on_notification
Expand Down Expand Up @@ -718,7 +728,7 @@ def test_should_use_email_template_subject_placeholders(
notification_id,
encryption.encrypt(notification),
)
persisted_notification = Notification.query.one()
persisted_notification = _get_notification_query_one()
assert persisted_notification.to == "1"
assert (
persisted_notification.template_id == sample_email_template_with_placeholders.id
Expand Down Expand Up @@ -759,7 +769,7 @@ def test_save_email_uses_the_reply_to_text_when_provided(sample_email_template,
encryption.encrypt(notification),
sender_id=other_email_reply_to.id,
)
persisted_notification = Notification.query.one()
persisted_notification = _get_notification_query_one()
assert persisted_notification.notification_type == NotificationType.EMAIL
assert persisted_notification.reply_to_text == "other@example.com"

Expand All @@ -784,7 +794,7 @@ def test_save_email_uses_the_default_reply_to_text_if_sender_id_is_none(
encryption.encrypt(notification),
sender_id=None,
)
persisted_notification = Notification.query.one()
persisted_notification = _get_notification_query_one()
assert persisted_notification.notification_type == NotificationType.EMAIL
assert persisted_notification.reply_to_text == "default@example.com"

Expand All @@ -803,7 +813,7 @@ def test_should_use_email_template_and_persist_without_personalisation(
notification_id,
encryption.encrypt(notification),
)
persisted_notification = Notification.query.one()
persisted_notification = _get_notification_query_one()
assert persisted_notification.to == "1"
assert persisted_notification.template_id == sample_email_template.id
assert persisted_notification.created_at >= now
Expand Down Expand Up @@ -936,7 +946,7 @@ def test_save_sms_uses_sms_sender_reply_to_text(mocker, notify_db_session):
encryption.encrypt(notification),
)

persisted_notification = Notification.query.one()
persisted_notification = _get_notification_query_one()
assert persisted_notification.reply_to_text == "+12028675309"


Expand All @@ -962,7 +972,7 @@ def test_save_sms_uses_non_default_sms_sender_reply_to_text_if_provided(
sender_id=new_sender.id,
)

persisted_notification = Notification.query.one()
persisted_notification = _get_notification_query_one()
assert persisted_notification.reply_to_text == "new-sender"


Expand Down Expand Up @@ -1476,12 +1486,12 @@ def test_save_api_email_or_sms(mocker, sample_service, notification_type):

encrypted = encryption.encrypt(data)

assert len(Notification.query.all()) == 0
assert len(_get_notification_query_all()) == 0
if notification_type == NotificationType.EMAIL:
save_api_email(encrypted_notification=encrypted)
else:
save_api_sms(encrypted_notification=encrypted)
notifications = Notification.query.all()
notifications = _get_notification_query_all()
assert len(notifications) == 1
assert str(notifications[0].id) == data["id"]
assert notifications[0].created_at == datetime(2020, 3, 25, 14, 30)
Expand Down Expand Up @@ -1529,20 +1539,20 @@ def test_save_api_email_dont_retry_if_notification_already_exists(
expected_queue = QueueNames.SEND_SMS

encrypted = encryption.encrypt(data)
assert len(Notification.query.all()) == 0
assert len(_get_notification_query_all()) == 0

if notification_type == NotificationType.EMAIL:
save_api_email(encrypted_notification=encrypted)
else:
save_api_sms(encrypted_notification=encrypted)
notifications = Notification.query.all()
notifications = _get_notification_query_all()
assert len(notifications) == 1
# call the task again with the same notification
if notification_type == NotificationType.EMAIL:
save_api_email(encrypted_notification=encrypted)
else:
save_api_sms(encrypted_notification=encrypted)
notifications = Notification.query.all()
notifications = _get_notification_query_all()
assert len(notifications) == 1
assert str(notifications[0].id) == data["id"]
assert notifications[0].created_at == datetime(2020, 3, 25, 14, 30)
Expand Down Expand Up @@ -1606,7 +1616,7 @@ def test_save_tasks_use_cached_service_and_template(
]

# But we save 2 notifications and enqueue 2 tasks
assert len(Notification.query.all()) == 2
assert len(_get_notification_query_all()) == 2
assert len(delivery_mock.call_args_list) == 2


Expand Down Expand Up @@ -1667,12 +1677,12 @@ def create_encrypted_notification():
}
)

assert len(Notification.query.all()) == 0
assert len(_get_notification_query_all()) == 0

for _ in range(3):
task_function(encrypted_notification=create_encrypted_notification())

assert service_dict_mock.call_args_list == [call(str(template.service_id))]

assert len(Notification.query.all()) == 3
assert len(_get_notification_query_all()) == 3
assert len(mock_provider_task.call_args_list) == 3
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,21 @@ def test_move_notifications_does_nothing_if_notification_history_row_already_exi
)

assert _get_notification_count() == 0
history = NotificationHistory.query.all()
history = _get_notification_history_query_all()
assert len(history) == 1
assert history[0].status == NotificationStatus.DELIVERED


def _get_notification_query_all():
stmt = select(Notification)
return db.session.execute(stmt).scalars().all()


def _get_notification_history_query_all():
stmt = select(NotificationHistory)
return db.session.execute(stmt).scalars().all()


def _get_notification_count():
stmt = select(func.count()).select_from(Notification)
return db.session.execute(stmt).scalar() or 0
Expand Down Expand Up @@ -76,8 +86,18 @@ def test_move_notifications_only_moves_notifications_older_than_provided_timesta
)
assert result == 1

assert Notification.query.one().id == new_notification.id
assert NotificationHistory.query.one().id == old_notification_id
assert _get_notification_query_one().id == new_notification.id
assert _get_notification_history_query_one().id == old_notification_id


def _get_notification_query_one():
stmt = select(Notification)
return db.session.execute(stmt).scalars().one()


def _get_notification_history_query_one():
stmt = select(NotificationHistory)
return db.session.execute(stmt).scalars().one()


def test_move_notifications_keeps_calling_until_no_more_to_delete_and_then_returns_total_deleted(
Expand Down Expand Up @@ -123,7 +143,9 @@ def test_move_notifications_only_moves_for_given_notification_type(sample_servic
)
assert result == 1
assert {x.notification_type for x in Notification.query} == {NotificationType.EMAIL}
assert NotificationHistory.query.one().notification_type == NotificationType.SMS
assert (
_get_notification_history_query_one().notification_type == NotificationType.SMS
)


def test_move_notifications_only_moves_for_given_service(notify_db_session):
Expand All @@ -146,8 +168,8 @@ def test_move_notifications_only_moves_for_given_service(notify_db_session):
)
assert result == 1

assert NotificationHistory.query.one().service_id == service.id
assert Notification.query.one().service_id == other_service.id
assert _get_notification_history_query_one().service_id == service.id
assert _get_notification_query_one().service_id == other_service.id


def test_move_notifications_just_deletes_test_key_notifications(sample_template):
Expand Down Expand Up @@ -258,8 +280,8 @@ def test_insert_notification_history_delete_notifications(sample_email_template)
timestamp_to_delete_backwards_from=utc_now() - timedelta(days=1),
)
assert del_count == 8
notifications = Notification.query.all()
history_rows = NotificationHistory.query.all()
notifications = _get_notification_query_all()
history_rows = _get_notification_history_query_all()
assert len(history_rows) == 8
assert ids_to_move == sorted([x.id for x in history_rows])
assert len(notifications) == 3
Expand Down Expand Up @@ -293,8 +315,8 @@ def test_insert_notification_history_delete_notifications_more_notifications_tha
)

assert del_count == 1
notifications = Notification.query.all()
history_rows = NotificationHistory.query.all()
notifications = _get_notification_query_all()
history_rows = _get_notification_history_query_all()
assert len(history_rows) == 1
assert len(notifications) == 2

Expand Down Expand Up @@ -324,8 +346,8 @@ def test_insert_notification_history_delete_notifications_only_insert_delete_for
)

assert del_count == 1
notifications = Notification.query.all()
history_rows = NotificationHistory.query.all()
notifications = _get_notification_query_all()
history_rows = _get_notification_history_query_all()
assert len(notifications) == 1
assert len(history_rows) == 1
assert notifications[0].id == notification_to_stay.id
Expand Down Expand Up @@ -361,8 +383,8 @@ def test_insert_notification_history_delete_notifications_insert_for_key_type(
)

assert del_count == 2
notifications = Notification.query.all()
history_rows = NotificationHistory.query.all()
notifications = _get_notification_query_all()
history_rows = _get_notification_history_query_all()
assert len(notifications) == 1
assert with_test_key.id == notifications[0].id
assert len(history_rows) == 2
8 changes: 5 additions & 3 deletions tests/app/dao/test_annual_billing_dao.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import pytest
from freezegun import freeze_time
from sqlalchemy import select

from app import db
from app.dao.annual_billing_dao import (
dao_create_or_update_annual_billing_for_year,
dao_get_free_sms_fragment_limit_for_year,
Expand Down Expand Up @@ -87,7 +89,7 @@ def test_set_default_free_allowance_for_service(

set_default_free_allowance_for_service(service=service, year_start=year)

annual_billing = AnnualBilling.query.all()
annual_billing = db.session.execute(select(AnnualBilling)).scalars().all()

assert len(annual_billing) == 1
assert annual_billing[0].service_id == service.id
Expand All @@ -109,7 +111,7 @@ def test_set_default_free_allowance_for_service_using_correct_year(
@freeze_time("2021-04-01 14:02:00")
def test_set_default_free_allowance_for_service_updates_existing_year(sample_service):
set_default_free_allowance_for_service(service=sample_service, year_start=None)
annual_billing = AnnualBilling.query.all()
annual_billing = db.session.execute(select(AnnualBilling)).scalars().all()
assert not sample_service.organization_type
assert len(annual_billing) == 1
assert annual_billing[0].service_id == sample_service.id
Expand All @@ -118,7 +120,7 @@ def test_set_default_free_allowance_for_service_updates_existing_year(sample_ser
sample_service.organization_type = OrganizationType.FEDERAL

set_default_free_allowance_for_service(service=sample_service, year_start=None)
annual_billing = AnnualBilling.query.all()
annual_billing = db.session.execute(select(AnnualBilling)).scalars().all()
assert len(annual_billing) == 1
assert annual_billing[0].service_id == sample_service.id
assert annual_billing[0].free_sms_fragment_limit == 150000
Loading
Loading