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

Add tests for timezone-aware datetimes #98

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
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
54 changes: 54 additions & 0 deletions template_app/tests/models/test_timezone_datetimes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
from datetime import datetime, timedelta, timezone
import uuid

from baselayer.app.models import DBSession, User, init_db
from baselayer.app.env import load_config

cfg = load_config(config_files=["test_config.yaml"])
init_db(**cfg["database"])


def test_input_timezone_is_respected():
# This is 2021-03-24 2PM (PDT) - Los Angeles time
pdt_2pm = datetime.fromisoformat("2021-03-24T14:00:00-07:00")

# Create a new User with a created_at at 2021-03-24 2PM (PDT)
username = uuid.uuid4().hex
user = User(username=username, created_at=pdt_2pm)
DBSession.add(user)
DBSession.commit()
assert user.created_at == pdt_2pm

# Since the User was created at 2PM (PDT), which is 7 hours behind UTC,
# the database should have stored 9PM that day as the created_at.
# So if we filter by created_at < 7PM on that day, we should not see the
# new user.
#
# If the created_at field were using default behavior instead
# of the custom TZDateTime type, the timezone info inputed above would
# just be ignored and 2PM put in as the time instead of being used to
# convert to a correct UTC timestamp before being added to the database.
# So, if this query returns the user, we know timezone info was ignored on
# SQLAlchemy datetime inputs.
utc_7pm = datetime.fromisoformat("2021-03-24T19:00:00")
users = (
DBSession.query(User)
.filter(User.username == username, User.created_at < utc_7pm)
.all()
)

assert len(users) == 0


def test_retrieved_datetimes_have_timezone_info():
# Create a new User
username = uuid.uuid4().hex
user = User(username=username)
DBSession.add(user)
DBSession.commit()

# Verify that datetimes retrieved from the database are tagged with timezone info
user = DBSession.query(User).filter(User.username == username).first()
assert user is not None
assert user.created_at.tzinfo is not None
assert user.created_at.tzinfo == timezone.utc