This repository was archived by the owner on Apr 1, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathtest_time.py
More file actions
70 lines (55 loc) · 2.12 KB
/
test_time.py
File metadata and controls
70 lines (55 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# Copyright 2024 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import datetime
import unittest.mock as mock
import google.cloud.bigquery
import pytest
import bigframes.session.time
INITIAL_BQ_TIME = datetime.datetime(
year=2020,
month=4,
day=24,
hour=8,
minute=55,
second=29,
tzinfo=datetime.timezone.utc,
)
@pytest.fixture()
def bq_client():
bqclient = mock.create_autospec(google.cloud.bigquery.Client, instance=True)
def query_and_wait_mock(query, *args, **kwargs):
if query.startswith("SELECT CURRENT_TIMESTAMP()"):
return iter([[INITIAL_BQ_TIME]])
else:
return ValueError(f"mock cannot handle query : {query}")
bqclient.query_and_wait = query_and_wait_mock
return bqclient
def test_bqsyncedclock_get_time(bq_client):
freezegun = pytest.importorskip("freezegun")
# this initial local time is actually irrelevant, only the ticks matter
initial_local_datetime = datetime.datetime(
year=1, month=7, day=12, hour=15, minute=6, second=3
)
with freezegun.freeze_time(initial_local_datetime) as frozen_datetime:
clock = bigframes.session.time.BigQuerySyncedClock(bq_client)
t1 = clock.get_time()
assert t1 == INITIAL_BQ_TIME
frozen_datetime.tick(datetime.timedelta(seconds=3))
t2 = clock.get_time()
assert t2 == INITIAL_BQ_TIME + datetime.timedelta(seconds=3)
frozen_datetime.tick(datetime.timedelta(seconds=23529385))
t3 = clock.get_time()
assert t3 == INITIAL_BQ_TIME + datetime.timedelta(
seconds=3
) + datetime.timedelta(seconds=23529385)