Skip to content

Commit

Permalink
fix: use django utils timezone to make timezone aware
Browse files Browse the repository at this point in the history
  • Loading branch information
varshamenon4 committed Jan 8, 2025
1 parent bcf2974 commit 4887120
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 16 deletions.
7 changes: 4 additions & 3 deletions learning_assistant/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
"""
import datetime
import logging
from datetime import datetime, timedelta, timezone
from datetime import datetime, timedelta

from django.conf import settings
from django.contrib.auth import get_user_model
from django.core.cache import cache
from django.utils import timezone
from edx_django_utils.cache import get_cache_key
from jinja2 import BaseLoader, Environment
from opaque_keys import InvalidKeyError
Expand Down Expand Up @@ -291,7 +292,7 @@ def get_or_create_audit_trial(user):
audit_trial, _ = LearningAssistantAuditTrial.objects.get_or_create(
user=user,
defaults={
"start_date": datetime.now(),
"start_date": timezone.now(),
},
)

Expand All @@ -314,7 +315,7 @@ def audit_trial_is_expired(enrollment, audit_trial_data):
* audit_trial_is_expired (boolean): whether the audit trial is expired
"""
upgrade_deadline = enrollment.upgrade_deadline
today = datetime.now(tz=timezone.utc)
today = timezone.now()

# If the upgrade deadline has passed, return True for expired. Upgrade deadline is an optional attribute of a
# CourseEnrollment, so if it's None, then do not return True.
Expand Down
23 changes: 10 additions & 13 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@
Test cases for the learning-assistant api module.
"""
import itertools
from datetime import datetime, timedelta, timezone
import zoneinfo
from datetime import datetime, timedelta
from unittest.mock import MagicMock, patch

import ddt
from django.conf import settings
from django.contrib.auth import get_user_model
from django.core.cache import cache
from django.test import TestCase, override_settings
from django.utils import timezone
from freezegun import freeze_time
from opaque_keys import InvalidKeyError
from opaque_keys.edx.keys import CourseKey, UsageKey
Expand Down Expand Up @@ -593,15 +595,15 @@ class CheckIfAuditTrialIsExpiredTests(TestCase):
Test suite for audit_trial_is_expired.
"""

@freeze_time('2024-01-01 00:00:01')
def setUp(self):
super().setUp()
self.course_key = CourseKey.from_string('course-v1:edx+fake+1')
self.user = User(username='tester', email='[email protected]')
self.user.save()

@freeze_time('2024-01-01 00:00:01 UTC')
def test_upgrade_deadline_expired(self):
today = datetime.now(tz=timezone.utc)
today = timezone.now()
mock_enrollment = MagicMock()
mock_enrollment.upgrade_deadline = today - timedelta(days=1) # yesterday

Expand All @@ -614,9 +616,8 @@ def test_upgrade_deadline_expired(self):

self.assertEqual(audit_trial_is_expired(mock_enrollment, audit_trial_data), True)

@freeze_time('2024-01-01 00:00:01 UTC')
def test_upgrade_deadline_none(self):
today = datetime.now(tz=timezone.utc)
today = datetime.now()
mock_enrollment = MagicMock()
mock_enrollment.upgrade_deadline = None

Expand All @@ -642,15 +643,12 @@ def test_upgrade_deadline_none(self):

@ddt.data(
# exactly the trial deadline
datetime(year=2024, month=1, day=1, tzinfo=timezone.utc) -
timedelta(days=settings.LEARNING_ASSISTANT_AUDIT_TRIAL_LENGTH_DAYS),
datetime(year=2024, month=1, day=1) - timedelta(days=settings.LEARNING_ASSISTANT_AUDIT_TRIAL_LENGTH_DAYS),
# 1 day more than trial deadline
datetime(year=2024, month=1, day=1, tzinfo=timezone.utc) -
timedelta(days=settings.LEARNING_ASSISTANT_AUDIT_TRIAL_LENGTH_DAYS + 1),
datetime(year=2024, month=1, day=1) - timedelta(days=settings.LEARNING_ASSISTANT_AUDIT_TRIAL_LENGTH_DAYS + 1),
)
@freeze_time('2024-01-01 00:00:01 UTC')
def test_audit_trial_expired(self, start_date):
today = datetime.now(tz=timezone.utc)
today = datetime.now()
mock_enrollment = MagicMock()
mock_enrollment.upgrade_deadline = today + timedelta(days=1) # tomorrow

Expand All @@ -662,9 +660,8 @@ def test_audit_trial_expired(self, start_date):

self.assertEqual(audit_trial_is_expired(mock_enrollment, audit_trial_data), True)

@freeze_time('2024-01-01 00:00:01 UTC')
def test_audit_trial_unexpired(self):
today = datetime.now(tz=timezone.utc)
today = datetime.now()
mock_enrollment = MagicMock()
mock_enrollment.upgrade_deadline = today + timedelta(days=1) # tomorrow

Expand Down

0 comments on commit 4887120

Please sign in to comment.