|
| 1 | +# Copyright 2024 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 pytest |
| 16 | + |
| 17 | +import bigframes.bigquery as bbq |
| 18 | +import bigframes.pandas as bpd |
| 19 | + |
| 20 | + |
| 21 | +@pytest.mark.parametrize( |
| 22 | + ("data", "expected"), |
| 23 | + [ |
| 24 | + pytest.param( |
| 25 | + [1, 2, 3, 3, 2], [{"value": 3, "count": 2}, {"value": 2, "count": 2}] |
| 26 | + ), |
| 27 | + pytest.param( |
| 28 | + ["apple", "apple", "pear", "pear", "pear", "banana"], |
| 29 | + [{"value": "pear", "count": 3}, {"value": "apple", "count": 2}], |
| 30 | + ), |
| 31 | + pytest.param( |
| 32 | + [True, False, True, False, True], |
| 33 | + [{"value": True, "count": 3}, {"value": False, "count": 2}], |
| 34 | + ), |
| 35 | + pytest.param( |
| 36 | + [], |
| 37 | + [], |
| 38 | + ), |
| 39 | + pytest.param( |
| 40 | + [[1, 2], [1], [1, 2]], |
| 41 | + [], |
| 42 | + marks=pytest.mark.xfail(raises=TypeError), |
| 43 | + ), |
| 44 | + ], |
| 45 | + ids=["int64", "string", "bool", "null", "array"], |
| 46 | +) |
| 47 | +def test_approx_top_count_w_dtypes(data, expected): |
| 48 | + s = bpd.Series(data) |
| 49 | + result = bbq.approx_top_count(s, number=2) |
| 50 | + assert result == expected |
| 51 | + |
| 52 | + |
| 53 | +@pytest.mark.parametrize( |
| 54 | + ("number", "expected"), |
| 55 | + [ |
| 56 | + pytest.param( |
| 57 | + 0, |
| 58 | + [], |
| 59 | + marks=pytest.mark.xfail(raises=ValueError), |
| 60 | + ), |
| 61 | + pytest.param(1, [{"value": 3, "count": 2}]), |
| 62 | + pytest.param( |
| 63 | + 4, |
| 64 | + [ |
| 65 | + {"value": 3, "count": 2}, |
| 66 | + {"value": 2, "count": 2}, |
| 67 | + {"value": 1, "count": 1}, |
| 68 | + ], |
| 69 | + ), |
| 70 | + ], |
| 71 | + ids=["zero", "one", "full"], |
| 72 | +) |
| 73 | +def test_approx_top_count_w_numbers(number, expected): |
| 74 | + s = bpd.Series([1, 2, 3, 3, 2]) |
| 75 | + result = bbq.approx_top_count(s, number=number) |
| 76 | + assert result == expected |
0 commit comments