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

Unified to UTC #7665

Merged
merged 8 commits into from
Jun 29, 2023
Merged
Show file tree
Hide file tree
Changes from 7 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
6 changes: 6 additions & 0 deletions .changes/unreleased/Fixes-20230525-091955.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: Fixes
body: Unified to UTC
time: 2023-05-25T09:19:55.865281+09:00
custom:
Author: d-kaneshiro
Issue: "7664"
37 changes: 22 additions & 15 deletions tests/functional/simple_snapshot/test_hard_delete_snapshot.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import os
from datetime import datetime
from datetime import timedelta
from datetime import timezone
import time

from datetime import datetime, timedelta
import pytz
import pytest
from dbt.tests.util import run_dbt, check_relations_equal
from dbt.tests.adapter.utils.test_current_timestamp import is_naive
from tests.functional.simple_snapshot.fixtures import (
models__schema_yml,
models__ref_snapshot_sql,
Expand All @@ -18,14 +15,6 @@
# These tests uses the same seed data, containing 20 records of which we hard delete the last 10.
# These deleted records set the dbt_valid_to to time the snapshot was ran.

# Using replace on a timestamp won't account for hour differences unless given the local timezone.
# We can force python as utc but not postgres fields which need to be handled as local timestamps.
def currenttz():
if time.daylight:
return timezone(timedelta(seconds=-time.altzone), time.tzname[1])
else:
return timezone(timedelta(seconds=-time.timezone), time.tzname[0])


def datetime_snapshot():
NUM_SNAPSHOT_MODELS = 1
Expand Down Expand Up @@ -94,7 +83,16 @@ def test_snapshot_hard_delete(project):
for result in snapshotted[10:]:
# result is a tuple, the dbt_valid_to column is the latest
assert isinstance(result[-1], datetime)
assert result[-1].replace(tzinfo=currenttz()) >= invalidated_snapshot_datetime
# Plenty of wiggle room if clocks aren't perfectly sync'd, etc
# There are two types of datetime objects: naive and aware. In this case, we are testing only with naive objects
# because in the case of aware, it is not possible to compare the time between DB and Python
if is_naive(result[-1]):
tolerance = timedelta(minutes=1)
assert (
result[-1].replace(tzinfo=pytz.UTC) > (invalidated_snapshot_datetime - tolerance)
) and (
result[-1].replace(tzinfo=pytz.UTC) < (invalidated_snapshot_datetime + tolerance)
), f"SQL timestamp {result[-1].replace(tzinfo=pytz.UTC).isoformat()} is not close enough to Python UTC {invalidated_snapshot_datetime.isoformat()}"

# revive records
# Timestamp must have microseconds for tests below to be meaningful
Expand Down Expand Up @@ -133,7 +131,16 @@ def test_snapshot_hard_delete(project):
for result in invalidated_records:
# result is a tuple, the dbt_valid_to column is the latest
assert isinstance(result[1], datetime)
assert result[1].replace(tzinfo=currenttz()) >= invalidated_snapshot_datetime
# Plenty of wiggle room if clocks aren't perfectly sync'd, etc
# There are two types of datetime objects: naive and aware. In this case, we are testing only with naive objects
# because in the case of aware, it is not possible to compare the time between DB and Python
if is_naive(result[1]):
tolerance = timedelta(minutes=1)
assert (
result[1].replace(tzinfo=pytz.UTC) > (invalidated_snapshot_datetime - tolerance)
) and (
result[1].replace(tzinfo=pytz.UTC) < (invalidated_snapshot_datetime + tolerance)
), f"SQL timestamp {result[1].replace(tzinfo=pytz.UTC).isoformat()} is not close enough to Python UTC {invalidated_snapshot_datetime.isoformat()}"

# records which were revived (id = 10, 11)
# dbt_valid_to is null
Expand Down