|
| 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 json |
| 16 | + |
| 17 | +import geopandas as gpd # type: ignore |
| 18 | +import pandas as pd |
| 19 | +import pytest |
| 20 | + |
| 21 | +import bigframes.bigquery as bbq |
| 22 | +import bigframes.pandas as bpd |
| 23 | + |
| 24 | + |
| 25 | +def _get_series_from_json(json_data): |
| 26 | + sql = " UNION ALL ".join( |
| 27 | + [ |
| 28 | + f"SELECT {id} AS id, JSON '{json.dumps(data)}' AS data" |
| 29 | + for id, data in enumerate(json_data) |
| 30 | + ] |
| 31 | + ) |
| 32 | + df = bpd.read_gbq(sql).set_index("id").sort_index() |
| 33 | + return df["data"] |
| 34 | + |
| 35 | + |
| 36 | +@pytest.mark.parametrize( |
| 37 | + ("json_path", "expected_json"), |
| 38 | + [ |
| 39 | + pytest.param("$.a", [{"a": 10}], id="simple"), |
| 40 | + pytest.param("$.a.b.c", [{"a": {"b": {"c": 10, "d": []}}}], id="nested"), |
| 41 | + ], |
| 42 | +) |
| 43 | +def test_json_set_at_json_path(json_path, expected_json): |
| 44 | + s = _get_series_from_json([{"a": {"b": {"c": "tester", "d": []}}}]) |
| 45 | + actual = bbq.json_set(s, json_path_value_pairs=[(json_path, 10)]) |
| 46 | + |
| 47 | + expected = _get_series_from_json(expected_json) |
| 48 | + pd.testing.assert_series_equal( |
| 49 | + actual.to_pandas(), |
| 50 | + expected.to_pandas(), |
| 51 | + ) |
| 52 | + |
| 53 | + |
| 54 | +@pytest.mark.parametrize( |
| 55 | + ("json_value", "expected_json"), |
| 56 | + [ |
| 57 | + pytest.param(10, [{"a": {"b": 10}}, {"a": {"b": 10}}], id="int"), |
| 58 | + pytest.param(0.333, [{"a": {"b": 0.333}}, {"a": {"b": 0.333}}], id="float"), |
| 59 | + pytest.param("eng", [{"a": {"b": "eng"}}, {"a": {"b": "eng"}}], id="string"), |
| 60 | + pytest.param([1, 2], [{"a": {"b": 1}}, {"a": {"b": 2}}], id="series"), |
| 61 | + ], |
| 62 | +) |
| 63 | +def test_json_set_at_json_value_type(json_value, expected_json): |
| 64 | + s = _get_series_from_json([{"a": {"b": "dev"}}, {"a": {"b": [1, 2]}}]) |
| 65 | + actual = bbq.json_set(s, json_path_value_pairs=[("$.a.b", json_value)]) |
| 66 | + |
| 67 | + expected = _get_series_from_json(expected_json) |
| 68 | + pd.testing.assert_series_equal( |
| 69 | + actual.to_pandas(), |
| 70 | + expected.to_pandas(), |
| 71 | + ) |
| 72 | + |
| 73 | + |
| 74 | +def test_json_set_w_more_pairs(): |
| 75 | + s = _get_series_from_json([{"a": 2}, {"b": 5}, {"c": 1}]) |
| 76 | + actual = bbq.json_set( |
| 77 | + s, json_path_value_pairs=[("$.a", 1), ("$.b", 2), ("$.a", [3, 4, 5])] |
| 78 | + ) |
| 79 | + expected = _get_series_from_json( |
| 80 | + [{"a": 3, "b": 2}, {"a": 4, "b": 2}, {"a": 5, "b": 2, "c": 1}] |
| 81 | + ) |
| 82 | + pd.testing.assert_series_equal( |
| 83 | + actual.to_pandas(), |
| 84 | + expected.to_pandas(), |
| 85 | + ) |
| 86 | + |
| 87 | + |
| 88 | +@pytest.mark.parametrize( |
| 89 | + ("series", "json_path_value_pairs"), |
| 90 | + [ |
| 91 | + pytest.param( |
| 92 | + _get_series_from_json([{"a": 10}]), |
| 93 | + [("$.a", 1, 100)], |
| 94 | + id="invalid_json_path_value_pairs", |
| 95 | + marks=pytest.mark.xfail(raises=ValueError), |
| 96 | + ), |
| 97 | + pytest.param( |
| 98 | + _get_series_from_json([{"a": 10}]), |
| 99 | + [ |
| 100 | + ( |
| 101 | + "$.a", |
| 102 | + bpd.read_pandas( |
| 103 | + gpd.GeoSeries.from_wkt(["POINT (1 2)", "POINT (2 1)"]) |
| 104 | + ), |
| 105 | + ) |
| 106 | + ], |
| 107 | + id="invalid_json_value_type", |
| 108 | + marks=pytest.mark.xfail(raises=TypeError), |
| 109 | + ), |
| 110 | + pytest.param( |
| 111 | + bpd.Series([1, 2]), |
| 112 | + [("$.a", 1)], |
| 113 | + id="invalid_series_type", |
| 114 | + marks=pytest.mark.xfail(raises=TypeError), |
| 115 | + ), |
| 116 | + ], |
| 117 | +) |
| 118 | +def test_json_set_w_invalid(series, json_path_value_pairs): |
| 119 | + bbq.json_set(series, json_path_value_pairs=json_path_value_pairs) |
0 commit comments