|
| 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() |
0 commit comments