Skip to content

Commit f14fb2b

Browse files
test: add dbtime compliance tests (#90)
* fix: address failing compliance tests in DateArray and TimeArray test: add a test session with prerelease versions of dependencies * fix min/max/median for 2D arrays * fixes except for null contains * actually use NaT as 'advertised' * fix!: use `pandas.NaT` for missing values in dbdate and dbtime dtypes This makes them consistent with other date/time dtypes, as well as internally consistent with the advertised `dtype.na_value`. BREAKING-CHANGE: dbdate and dbtime dtypes return NaT instead of None for missing values Release-As: 0.4.0 * more progress towards compliance * address errors in TestMethods * move tests * add prerelease deps * fix: address failing tests with pandas 1.5.0 test: add a test session with prerelease versions of dependencies * fix owlbot config * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * document why microsecond precision is used * use correct units * add box_func tests * typo * fix: avoid TypeError when using sorted search * add unit tests * fix: dbdate and dbtime support set item * add TestMethods * fix: allow comparison with scalar values * correct behavior for comparison with different types and shapes * use same dtype in shape comparison tests * test: add final dbdate compliance tests and sort * test: add dbtime compliance tests Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent efe7e6d commit f14fb2b

File tree

3 files changed

+272
-0
lines changed

3 files changed

+272
-0
lines changed

tests/compliance/time/conftest.py

+134
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
# Copyright 2022 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import datetime
16+
17+
import numpy
18+
import pytest
19+
20+
from db_dtypes import TimeArray, TimeDtype
21+
22+
23+
@pytest.fixture(params=["data", "data_missing"])
24+
def all_data(request, data, data_missing):
25+
"""Parametrized fixture giving 'data' and 'data_missing'"""
26+
if request.param == "data":
27+
return data
28+
elif request.param == "data_missing":
29+
return data_missing
30+
31+
32+
@pytest.fixture
33+
def data():
34+
return TimeArray(
35+
numpy.arange(
36+
datetime.datetime(1970, 1, 1),
37+
datetime.datetime(1970, 1, 2),
38+
datetime.timedelta(microseconds=864_123_456),
39+
dtype="datetime64[ns]",
40+
)
41+
)
42+
43+
44+
@pytest.fixture
45+
def data_for_grouping():
46+
"""
47+
Data for factorization, grouping, and unique tests.
48+
49+
Expected to be like [B, B, NA, NA, A, A, B, C]
50+
51+
Where A < B < C and NA is missing
52+
53+
See:
54+
https://github.com/pandas-dev/pandas/blob/main/pandas/tests/extension/conftest.py
55+
"""
56+
return TimeArray(
57+
[
58+
datetime.time(11, 45, 29, 987_654),
59+
datetime.time(11, 45, 29, 987_654),
60+
None,
61+
None,
62+
datetime.time(0, 1, 2, 345_678),
63+
datetime.time(0, 1, 2, 345_678),
64+
datetime.time(11, 45, 29, 987_654),
65+
datetime.time(23, 59, 59, 999_999),
66+
]
67+
)
68+
69+
70+
@pytest.fixture
71+
def data_for_sorting():
72+
"""
73+
Length-3 array with a known sort order.
74+
75+
This should be three items [B, C, A] with
76+
A < B < C
77+
78+
See:
79+
https://github.com/pandas-dev/pandas/blob/main/pandas/tests/extension/conftest.py
80+
"""
81+
return TimeArray(
82+
[
83+
datetime.time(11, 45, 29, 987_654),
84+
datetime.time(23, 59, 59, 999_999),
85+
datetime.time(0, 1, 2, 345_678),
86+
]
87+
)
88+
89+
90+
@pytest.fixture
91+
def data_missing():
92+
"""Length-2 array with [NA, Valid]
93+
94+
See:
95+
https://github.com/pandas-dev/pandas/blob/main/pandas/tests/extension/conftest.py
96+
"""
97+
return TimeArray([None, datetime.time(13, 7, 42, 123_456)])
98+
99+
100+
@pytest.fixture
101+
def data_missing_for_sorting():
102+
"""
103+
Length-3 array with a known sort order.
104+
105+
This should be three items [B, NA, A] with
106+
A < B and NA missing.
107+
108+
See:
109+
https://github.com/pandas-dev/pandas/blob/main/pandas/tests/extension/conftest.py
110+
"""
111+
return TimeArray(
112+
[datetime.time(13, 7, 42, 123_456), None, datetime.time(1, 2, 3, 456_789)]
113+
)
114+
115+
116+
@pytest.fixture
117+
def data_repeated(data):
118+
"""
119+
Generate many datasets.
120+
121+
See:
122+
https://github.com/pandas-dev/pandas/blob/main/pandas/tests/extension/conftest.py
123+
"""
124+
125+
def gen(count):
126+
for _ in range(count):
127+
yield data
128+
129+
return gen
130+
131+
132+
@pytest.fixture
133+
def dtype():
134+
return TimeDtype()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
# Copyright 2022 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
"""
15+
Tests for extension interface compliance, inherited from pandas.
16+
17+
See:
18+
https://github.com/pandas-dev/pandas/blob/main/pandas/tests/extension/decimal/test_decimal.py
19+
and
20+
https://github.com/pandas-dev/pandas/blob/main/pandas/tests/extension/test_period.py
21+
"""
22+
23+
import pandas
24+
from pandas.tests.extension import base
25+
import pytest
26+
27+
import db_dtypes
28+
29+
30+
# TODO(https://github.com/googleapis/python-db-dtypes-pandas/issues/87): Add
31+
# compliance tests for arithmetic operations.
32+
33+
# TODO(https://github.com/googleapis/python-db-dtypes-pandas/issues/78): Add
34+
# compliance tests for reduction operations.
35+
36+
37+
class TestComparisonOps(base.BaseComparisonOpsTests):
38+
pass
39+
40+
41+
class TestCasting(base.BaseCastingTests):
42+
pass
43+
44+
45+
class TestConstructors(base.BaseConstructorsTests):
46+
pass
47+
48+
49+
class TestDtype(base.BaseDtypeTests):
50+
pass
51+
52+
53+
class TestGetitem(base.BaseGetitemTests):
54+
pass
55+
56+
57+
class TestGroupby(base.BaseGroupbyTests):
58+
pass
59+
60+
61+
class TestIndex(base.BaseIndexTests):
62+
pass
63+
64+
65+
class TestInterface(base.BaseInterfaceTests):
66+
pass
67+
68+
69+
class TestMissing(base.BaseMissingTests):
70+
pass
71+
72+
73+
class TestMethods(base.BaseMethodsTests):
74+
def test_combine_add(self):
75+
pytest.skip("Cannot add dates.")
76+
77+
@pytest.mark.parametrize("dropna", [True, False])
78+
def test_value_counts(self, all_data, dropna):
79+
all_data = all_data[:10]
80+
if dropna:
81+
# Overridden from
82+
# https://github.com/pandas-dev/pandas/blob/main/pandas/tests/extension/base/methods.py
83+
# to avoid difference in dtypes.
84+
other = db_dtypes.TimeArray(all_data[~all_data.isna()])
85+
else:
86+
other = all_data
87+
88+
result = pandas.Series(all_data).value_counts(dropna=dropna).sort_index()
89+
expected = pandas.Series(other).value_counts(dropna=dropna).sort_index()
90+
91+
self.assert_series_equal(result, expected)
92+
93+
94+
class TestParsing(base.BaseParsingTests):
95+
pass
96+
97+
98+
class TestPrinting(base.BasePrintingTests):
99+
pass
100+
101+
102+
class TestReshaping(base.BaseReshapingTests):
103+
pass
104+
105+
106+
class TestSetitem(base.BaseSetitemTests):
107+
pass
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Copyright 2022 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
"""
15+
Tests for extension interface compliance, inherited from pandas.
16+
17+
See:
18+
https://github.com/pandas-dev/pandas/blob/main/pandas/tests/extension/decimal/test_decimal.py
19+
and
20+
https://github.com/pandas-dev/pandas/blob/main/pandas/tests/extension/test_period.py
21+
"""
22+
23+
from pandas.tests.extension import base
24+
import pytest
25+
26+
# NDArrayBacked2DTests suite added in https://github.com/pandas-dev/pandas/pull/44974
27+
pytest.importorskip("pandas", minversion="1.5.0dev")
28+
29+
30+
class Test2DCompat(base.NDArrayBacked2DTests):
31+
pass

0 commit comments

Comments
 (0)