Skip to content

Commit

Permalink
test: Replace freezegun with time-machine (#1997)
Browse files Browse the repository at this point in the history
  • Loading branch information
edgarrmondragon authored Oct 5, 2023
1 parent b4f9ac5 commit cb56f86
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 31 deletions.
2 changes: 1 addition & 1 deletion noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@
"pytest",
"pytest-snapshot",
"pytest-durations",
"freezegun",
"pyarrow",
"requests-mock",
"time-machine",
# Cookiecutter tests
"black",
"cookiecutter",
Expand Down
82 changes: 67 additions & 15 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ cookiecutter = ">=2.1.1"
coverage = {extras = ["toml"], version = ">=7.2"}
duckdb = ">=0.8.0"
duckdb-engine = ">=0.9.2"
freezegun = ">=1.2.2"
mypy = ">=1.0"
numpy = [
{ version = "<1.22", python = "<3.8" },
Expand All @@ -112,6 +111,7 @@ numpy = [
pyarrow = ">=11,<13"
pytest-snapshot = ">=0.9.0"
requests-mock = ">=1.10.0"
time-machine = ">=2.10.0"
types-jsonschema = ">=4.17.0.6"
types-python-dateutil = ">=2.8.19"
types-pytz = ">=2022.7.1.2"
Expand Down
14 changes: 11 additions & 3 deletions tests/core/sinks/test_sdc_metadata.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
from __future__ import annotations

from freezegun import freeze_time
import datetime

import time_machine

from tests.conftest import BatchSinkMock, TargetMock


def test_sdc_metadata():
with freeze_time("2023-01-01T00:00:00+00:00"):
with time_machine.travel(
datetime.datetime(2023, 1, 1, tzinfo=datetime.timezone.utc),
tick=False,
):
target = TargetMock()

sink = BatchSinkMock(
Expand All @@ -25,7 +30,10 @@ def test_sdc_metadata():
}
record = record_message["record"]

with freeze_time("2023-01-01T00:05:00+00:00"):
with time_machine.travel(
datetime.datetime(2023, 1, 1, 0, 5, tzinfo=datetime.timezone.utc),
tick=False,
):
sink._add_sdc_metadata_to_record(record, record_message, {})

assert record == {
Expand Down
8 changes: 6 additions & 2 deletions tests/core/test_mapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from __future__ import annotations

import copy
import datetime
import io
import json
import logging
Expand All @@ -11,7 +12,7 @@
from decimal import Decimal

import pytest
from freezegun import freeze_time
import time_machine

from singer_sdk._singerlib import Catalog
from singer_sdk.exceptions import MapExpressionError
Expand Down Expand Up @@ -470,7 +471,10 @@ def _clear_schema_cache() -> None:
get_selected_schema.cache_clear()


@freeze_time("2022-01-01T00:00:00Z")
@time_machine.travel(
datetime.datetime(2022, 1, 1, tzinfo=datetime.timezone.utc),
tick=False,
)
@pytest.mark.snapshot()
@pytest.mark.usefixtures("_clear_schema_cache")
@pytest.mark.parametrize(
Expand Down
8 changes: 6 additions & 2 deletions tests/samples/test_tap_sqlite.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
from __future__ import annotations

import datetime
import json
import typing as t

import pytest
import time_machine
from click.testing import CliRunner
from freezegun import freeze_time

from samples.sample_tap_sqlite import SQLiteTap
from samples.sample_target_csv.csv_target import SampleTargetCSV
Expand Down Expand Up @@ -122,7 +123,10 @@ def test_sync_sqlite_to_csv(sqlite_sample_tap: SQLTap, tmp_path: Path):


@pytest.fixture
@freeze_time("2022-01-01T00:00:00Z")
@time_machine.travel(
datetime.datetime(2022, 1, 1, tzinfo=datetime.timezone.utc),
tick=False,
)
def sqlite_sample_tap_state_messages(sqlite_sample_tap: SQLTap) -> list[dict]:
stdout, _ = tap_sync_test(sqlite_sample_tap)
state_messages = []
Expand Down
36 changes: 29 additions & 7 deletions tests/samples/test_target_csv.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
"""Test tap-to-target sync."""
from __future__ import annotations

import datetime
import json
import shutil
import typing as t
import uuid
from pathlib import Path

import pytest
import time_machine
from click.testing import CliRunner
from freezegun import freeze_time

from samples.sample_mapper.mapper import StreamTransform
from samples.sample_tap_countries.countries_tap import SampleTapCountries
Expand Down Expand Up @@ -77,12 +78,33 @@ def test_target_batching():

buf, _ = tap_sync_test(tap)

mocked_starttime = "2012-01-01 12:00:00"
mocked_jumptotime2 = "2012-01-01 12:31:00"
mocked_jumptotime3 = "2012-01-01 13:02:00"
mocked_starttime = datetime.datetime(
2012,
1,
1,
12,
0,
tzinfo=datetime.timezone.utc,
)
mocked_jumptotime2 = datetime.datetime(
2012,
1,
1,
12,
31,
tzinfo=datetime.timezone.utc,
)
mocked_jumptotime3 = datetime.datetime(
2012,
1,
1,
13,
2,
tzinfo=datetime.timezone.utc,
)
countries_record_count = 257

with freeze_time(mocked_starttime):
with time_machine.travel(mocked_starttime, tick=False):
target = TargetMock(config={})
target.max_parallelism = 1 # Limit unit test to 1 process
assert target.num_records_processed == 0
Expand All @@ -96,7 +118,7 @@ def test_target_batching():
assert len(target.records_written) == 0 # Drain not yet called
assert len(target.state_messages_written) == 0 # Drain not yet called

with freeze_time(mocked_jumptotime2):
with time_machine.travel(mocked_jumptotime2, tick=False):
buf.seek(0)
target_sync_test(target, buf, finalize=False)

Expand All @@ -105,7 +127,7 @@ def test_target_batching():
assert len(target.records_written) == countries_record_count + 1
assert len(target.state_messages_written) == 1

with freeze_time(mocked_jumptotime3):
with time_machine.travel(mocked_jumptotime3, tick=False):
buf.seek(0)
target_sync_test(target, buf, finalize=False)

Expand Down

0 comments on commit cb56f86

Please sign in to comment.