Skip to content

Commit

Permalink
fixed date handling in open survey fetch (#2152)
Browse files Browse the repository at this point in the history
  • Loading branch information
saravanpa-aot authored Sep 8, 2023
1 parent 4cf9bde commit cd886f2
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 6 deletions.
6 changes: 3 additions & 3 deletions met-api/src/met_api/models/survey.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from datetime import datetime
from typing import Optional

from sqlalchemy import ForeignKey, and_, asc, desc, or_
from sqlalchemy import ForeignKey, and_, asc, desc, func, or_
from sqlalchemy.dialects import postgresql
from sqlalchemy.sql import text

Expand Down Expand Up @@ -43,11 +43,11 @@ class Survey(BaseModel): # pylint: disable=too-few-public-methods
@classmethod
def get_open(cls, survey_id) -> Survey:
"""Get an open survey."""
now = datetime.now()
now = datetime.now().date() # Get the current date without the timestamp
survey: Survey = db.session.query(Survey).filter_by(id=survey_id) \
.join(Engagement) \
.filter_by(status_id=Status.Published.value) \
.filter(and_(Engagement.start_date <= now, Engagement.end_date >= now)) \
.filter(and_(func.date(Engagement.start_date) <= now, func.date(Engagement.end_date) >= now)) \
.join(EngagementStatus) \
.first()
return survey
Expand Down
4 changes: 2 additions & 2 deletions met-api/src/met_api/services/email_verification_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def create(cls, email_verification: EmailVerificationSchema,
"""Create an email verification."""
cls.validate_fields(email_verification)
email_address: str = email_verification.get('email_address')
survey = SurveyModel.get_open(email_verification.get('survey_id'))
survey = SurveyModel.find_by_id(email_verification.get('survey_id'))
engagement: EngagementModel = EngagementModel.find_by_id(
survey.engagement_id)
if engagement.is_internal and not email_address.endswith(INTERNAL_EMAIL_DOMAIN):
Expand Down Expand Up @@ -100,7 +100,7 @@ def _send_verification_email(email_verification: dict, subscription_type) -> Non
"""Send an verification email.Throws error if fails."""
survey_id = email_verification.get('survey_id')
email_to = email_verification.get('email_address')
survey: SurveyModel = SurveyModel.get_open(survey_id)
survey: SurveyModel = SurveyModel.find_by_id(survey_id)

if not survey:
raise ValueError('Survey not found')
Expand Down
39 changes: 38 additions & 1 deletion met-api/tests/unit/models/test_survey.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,17 @@
Test suite to ensure that the Survey model routines are working as expected.
"""
from datetime import datetime, timedelta

from faker import Faker
from freezegun import freeze_time

from met_api.constants.engagement_status import Status
from met_api.models import Survey as SurveyModel
from met_api.models import db
from tests.utilities.factory_scenarios import TestEngagementInfo
from tests.utilities.factory_utils import factory_engagement_model, factory_survey_model


fake = Faker()


Expand All @@ -46,3 +49,37 @@ def test_get_open_survey(session):
db.session.commit()
survey_new_1 = SurveyModel.get_open(survey.id)
assert survey_new_1 is not None


def test_get_open_survey_time_based(session):
"""Assert that an open survey can be retrieved."""
now = datetime.now()
eng_info: dict = TestEngagementInfo.engagement1
eng_info['start_date'] = now - timedelta(days=1, hours=1)
eng_info['end_date'] = now # ends today
eng_info['status'] = Status.Published.value

engagement = factory_engagement_model(eng_info)
survey = factory_survey_model()
survey.engagement_id = engagement.id

# Commit the survey and engagement to the session
db.session.add(engagement)
db.session.add(survey)
db.session.commit()

# Call the get_open method and assert that the survey is retrieved.
survey_new = SurveyModel.get_open(survey.id)
assert survey_new is not None, 'survey fetchable on the day of closure'

# Move time forward by 1 day
day_after_time_delay = now + timedelta(days=1)
with freeze_time(day_after_time_delay):
survey_new = SurveyModel.get_open(survey.id)
assert survey_new is None, 'survey is not fetchable after one day of closure.'

# Move time backward by 1 day
day_before_time_delay = now - timedelta(days=1)
with freeze_time(day_before_time_delay):
survey_new = SurveyModel.get_open(survey.id)
assert survey_new is not None, 'survey fetchable since one day before closure.'

0 comments on commit cd886f2

Please sign in to comment.