remove all() from statement

This commit is contained in:
Kenneth Kehl
2024-10-15 13:13:42 -07:00
parent 2cff3fa0fd
commit 9c95e588d1
2 changed files with 17 additions and 14 deletions

View File

@@ -426,7 +426,7 @@ def fetch_stats_for_all_services_by_date_range(
def fetch_monthly_template_usage_for_service(start_date, end_date, service_id): def fetch_monthly_template_usage_for_service(start_date, end_date, service_id):
# services_dao.replaces dao_fetch_monthly_historical_usage_by_template_for_service # services_dao.replaces dao_fetch_monthly_historical_usage_by_template_for_service
stats = ( stats = (
db.session.query( select(
FactNotificationStatus.template_id.label("template_id"), FactNotificationStatus.template_id.label("template_id"),
Template.name.label("name"), Template.name.label("name"),
Template.template_type.label("template_type"), Template.template_type.label("template_type"),
@@ -461,7 +461,7 @@ def fetch_monthly_template_usage_for_service(start_date, end_date, service_id):
month = get_month_from_utc_column(Notification.created_at) month = get_month_from_utc_column(Notification.created_at)
stats_for_today = ( stats_for_today = (
db.session.query( select(
Notification.template_id.label("template_id"), Notification.template_id.label("template_id"),
Template.name.label("name"), Template.name.label("name"),
Template.template_type.label("template_type"), Template.template_type.label("template_type"),
@@ -490,7 +490,7 @@ def fetch_monthly_template_usage_for_service(start_date, end_date, service_id):
all_stats_table = stats.union_all(stats_for_today).subquery() all_stats_table = stats.union_all(stats_for_today).subquery()
query = ( query = (
db.session.query( select(
all_stats_table.c.template_id, all_stats_table.c.template_id,
all_stats_table.c.name, all_stats_table.c.name,
all_stats_table.c.template_type, all_stats_table.c.template_type,
@@ -511,12 +511,12 @@ def fetch_monthly_template_usage_for_service(start_date, end_date, service_id):
) )
else: else:
query = stats query = stats
return query.all() return db.session.execute(query).all()
def get_total_notifications_for_date_range(start_date, end_date): def get_total_notifications_for_date_range(start_date, end_date):
query = ( query = (
db.session.query( select(
FactNotificationStatus.local_date.label("local_date"), FactNotificationStatus.local_date.label("local_date"),
func.sum( func.sum(
case( case(
@@ -550,12 +550,12 @@ def get_total_notifications_for_date_range(start_date, end_date):
FactNotificationStatus.local_date >= start_date, FactNotificationStatus.local_date >= start_date,
FactNotificationStatus.local_date <= end_date, FactNotificationStatus.local_date <= end_date,
) )
return query.all() return db.session.execute(query).all()
def fetch_monthly_notification_statuses_per_service(start_date, end_date): def fetch_monthly_notification_statuses_per_service(start_date, end_date):
return ( stmt = (
db.session.query( select(
func.date_trunc("month", FactNotificationStatus.local_date) func.date_trunc("month", FactNotificationStatus.local_date)
.cast(Date) .cast(Date)
.label("date_created"), .label("date_created"),
@@ -648,5 +648,5 @@ def fetch_monthly_notification_statuses_per_service(start_date, end_date):
Service.id, Service.id,
FactNotificationStatus.notification_type, FactNotificationStatus.notification_type,
) )
.all()
) )
return db.session.execute(stmt).all()

View File

@@ -3,7 +3,9 @@ from uuid import UUID
import pytest import pytest
from freezegun import freeze_time from freezegun import freeze_time
from sqlalchemy import func, select
from app import db
from app.dao.fact_notification_status_dao import ( from app.dao.fact_notification_status_dao import (
fetch_monthly_notification_statuses_per_service, fetch_monthly_notification_statuses_per_service,
fetch_monthly_template_usage_for_service, fetch_monthly_template_usage_for_service,
@@ -1126,9 +1128,10 @@ def test_update_fact_notification_status_respects_gmt_bst(
process_day, NotificationType.SMS, sample_service.id process_day, NotificationType.SMS, sample_service.id
) )
assert ( stmt = (
FactNotificationStatus.query.filter_by( select(func.count())
service_id=sample_service.id, local_date=process_day .select_from(FactNotificationStatus)
).count() .filter_by(service_id=sample_service.id, local_date=process_day)
== expected_count
) )
result = db.session.execute(stmt)
assert result.rowcount == expected_count