1212# See the License for the specific language governing permissions and
1313# limitations under the License.
1414
15- import db_dtypes # type: ignore
1615import geopandas as gpd # type: ignore
1716import pandas as pd
1817import pyarrow as pa
1918import pytest
2019
2120import bigframes .bigquery as bbq
22- import bigframes .dtypes
21+ import bigframes .dtypes as dtypes
2322import bigframes .pandas as bpd
2423
2524
2625@pytest .mark .parametrize (
2726 ("json_path" , "expected_json" ),
2827 [
29- pytest .param ("$.a" , [{"a" : 10 }], id = "simple" ),
30- pytest .param ("$.a.b.c" , [{"a" : {"b" : {"c" : 10 , "d" : []}}}], id = "nested" ),
28+ pytest .param ("$.a" , [' {"a": 10}' ], id = "simple" ),
29+ pytest .param ("$.a.b.c" , [' {"a": {"b": {"c": 10, "d": []}}}' ], id = "nested" ),
3130 ],
3231)
3332def test_json_set_at_json_path (json_path , expected_json ):
34- original_json = [{"a" : {"b" : {"c" : "tester" , "d" : []}}}]
35- s = bpd .Series (original_json , dtype = db_dtypes . JSONDtype () )
33+ original_json = [' {"a": {"b": {"c": "tester", "d": []}}}' ]
34+ s = bpd .Series (original_json , dtype = dtypes . JSON_DTYPE )
3635 actual = bbq .json_set (s , json_path_value_pairs = [(json_path , 10 )])
3736
38- expected = bpd .Series (expected_json , dtype = db_dtypes . JSONDtype () )
37+ expected = bpd .Series (expected_json , dtype = dtypes . JSON_DTYPE )
3938 pd .testing .assert_series_equal (
4039 actual .to_pandas (),
4140 expected .to_pandas (),
@@ -45,47 +44,49 @@ def test_json_set_at_json_path(json_path, expected_json):
4544@pytest .mark .parametrize (
4645 ("json_value" , "expected_json" ),
4746 [
48- pytest .param (10 , [{"a" : {"b" : 10 }}, {"a" : {"b" : 10 }}], id = "int" ),
49- pytest .param (0.333 , [{"a" : {"b" : 0.333 }}, {"a" : {"b" : 0.333 }}], id = "float" ),
50- pytest .param ("eng" , [{"a" : {"b" : "eng" }}, {"a" : {"b" : "eng" }}], id = "string" ),
51- pytest .param ([1 , 2 ], [{"a" : {"b" : 1 }}, {"a" : {"b" : 2 }}], id = "series" ),
47+ pytest .param (10 , ['{"a": {"b": 10}}' , '{"a": {"b": 10}}' ], id = "int" ),
48+ pytest .param (0.333 , ['{"a": {"b": 0.333}}' , '{"a": {"b": 0.333}}' ], id = "float" ),
49+ pytest .param (
50+ "eng" , ['{"a": {"b": "eng"}}' , '{"a": {"b": "eng"}}' ], id = "string"
51+ ),
52+ pytest .param ([1 , 2 ], ['{"a": {"b": 1}}' , '{"a": {"b": 2}}' ], id = "series" ),
5253 ],
5354)
5455def test_json_set_at_json_value_type (json_value , expected_json ):
55- original_json = [{"a" : {"b" : "dev" }}, {"a" : {"b" : [1 , 2 ]}}]
56- s = bpd .Series (original_json , dtype = db_dtypes . JSONDtype () )
56+ original_json = [' {"a": {"b": "dev"}}' , ' {"a": {"b": [1, 2]}}' ]
57+ s = bpd .Series (original_json , dtype = dtypes . JSON_DTYPE )
5758 actual = bbq .json_set (s , json_path_value_pairs = [("$.a.b" , json_value )])
5859
59- expected = bpd .Series (expected_json , dtype = db_dtypes . JSONDtype () )
60+ expected = bpd .Series (expected_json , dtype = dtypes . JSON_DTYPE )
6061 pd .testing .assert_series_equal (
6162 actual .to_pandas (),
6263 expected .to_pandas (),
6364 )
6465
6566
6667def test_json_set_w_more_pairs ():
67- original_json = [{"a" : 2 }, {"b" : 5 }, {"c" : 1 }]
68- s = bpd .Series (original_json , dtype = db_dtypes . JSONDtype () )
68+ original_json = [' {"a": 2}' , ' {"b": 5}' , ' {"c": 1}' ]
69+ s = bpd .Series (original_json , dtype = dtypes . JSON_DTYPE )
6970 actual = bbq .json_set (
7071 s , json_path_value_pairs = [("$.a" , 1 ), ("$.b" , 2 ), ("$.a" , [3 , 4 , 5 ])]
7172 )
7273
73- expected_json = [{"a" : 3 , "b" : 2 }, {"a" : 4 , "b" : 2 }, {"a" : 5 , "b" : 2 , "c" : 1 }]
74- expected = bpd .Series (expected_json , dtype = db_dtypes . JSONDtype () )
74+ expected_json = [' {"a": 3, "b": 2}' , ' {"a": 4, "b": 2}' , ' {"a": 5, "b": 2, "c": 1}' ]
75+ expected = bpd .Series (expected_json , dtype = dtypes . JSON_DTYPE )
7576 pd .testing .assert_series_equal (
7677 actual .to_pandas (),
7778 expected .to_pandas (),
7879 )
7980
8081
8182def test_json_set_w_invalid_json_path_value_pairs ():
82- s = bpd .Series ([{"a" : 10 }], dtype = db_dtypes . JSONDtype () )
83+ s = bpd .Series ([' {"a": 10}' ], dtype = dtypes . JSON_DTYPE )
8384 with pytest .raises (ValueError ):
8485 bbq .json_set (s , json_path_value_pairs = [("$.a" , 1 , 100 )]) # type: ignore
8586
8687
8788def test_json_set_w_invalid_value_type ():
88- s = bpd .Series ([{"a" : 10 }], dtype = db_dtypes . JSONDtype () )
89+ s = bpd .Series ([' {"a": 10}' ], dtype = dtypes . JSON_DTYPE )
8990 with pytest .raises (TypeError ):
9091 bbq .json_set (
9192 s ,
@@ -101,17 +102,18 @@ def test_json_set_w_invalid_value_type():
101102
102103
103104def test_json_set_w_invalid_series_type ():
105+ s = bpd .Series ([1 , 2 ])
104106 with pytest .raises (TypeError ):
105- bbq .json_set (bpd . Series ([ 1 , 2 ]) , json_path_value_pairs = [("$.a" , 1 )])
107+ bbq .json_set (s , json_path_value_pairs = [("$.a" , 1 )])
106108
107109
108110def test_json_extract_from_json ():
109111 s = bpd .Series (
110- [{"a" : {"b" : [1 , 2 ]}}, {"a" : {"c" : 1 }}, {"a" : {"b" : 0 }}],
111- dtype = db_dtypes . JSONDtype () ,
112+ [' {"a": {"b": [1, 2]}}' , ' {"a": {"c": 1}}' , ' {"a": {"b": 0}}' ],
113+ dtype = dtypes . JSON_DTYPE ,
112114 )
113115 actual = bbq .json_extract (s , "$.a.b" ).to_pandas ()
114- expected = bpd .Series ([[1 , 2 ], None , 0 ], dtype = db_dtypes . JSONDtype () ).to_pandas ()
116+ expected = bpd .Series ([" [1, 2]" , None , "0" ], dtype = dtypes . JSON_DTYPE ).to_pandas ()
115117 pd .testing .assert_series_equal (
116118 actual ,
117119 expected ,
@@ -132,14 +134,15 @@ def test_json_extract_from_string():
132134
133135
134136def test_json_extract_w_invalid_series_type ():
137+ s = bpd .Series ([1 , 2 ])
135138 with pytest .raises (TypeError ):
136- bbq .json_extract (bpd . Series ([ 1 , 2 ]) , "$.a" )
139+ bbq .json_extract (s , "$.a" )
137140
138141
139142def test_json_extract_array_from_json ():
140143 s = bpd .Series (
141- [{"a" : ["ab" , "2" , "3 xy" ]}, {"a" : []}, {"a" : ["4" , "5" ]}, {} ],
142- dtype = db_dtypes . JSONDtype () ,
144+ [' {"a": ["ab", "2", "3 xy"]}' , ' {"a": []}' , ' {"a": ["4", "5"]}' , "{}" ],
145+ dtype = dtypes . JSON_DTYPE ,
143146 )
144147 actual = bbq .json_extract_array (s , "$.a" )
145148
@@ -225,7 +228,7 @@ def test_json_extract_string_array_from_array_strings():
225228
226229def test_json_extract_string_array_as_float_array_from_array_strings ():
227230 s = bpd .Series (["[1, 2.5, 3]" , "[]" , "[4,5]" ])
228- actual = bbq .json_extract_string_array (s , value_dtype = bigframes . dtypes .FLOAT_DTYPE )
231+ actual = bbq .json_extract_string_array (s , value_dtype = dtypes .FLOAT_DTYPE )
229232 expected = bpd .Series ([[1 , 2.5 , 3 ], [], [4 , 5 ]])
230233 pd .testing .assert_series_equal (
231234 actual .to_pandas (),
0 commit comments