Skip to content

Commit 07bf2d4

Browse files
authored
test: Add data file for JSON dtype system tests (#1136)
1 parent 0dea941 commit 07bf2d4

File tree

4 files changed

+60
-0
lines changed

4 files changed

+60
-0
lines changed

tests/data/json.jsonl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{"rowindex": 0, "json_col": null}
2+
{"rowindex": 1, "json_col": true}
3+
{"rowindex": 2, "json_col": 100}
4+
{"rowindex": 3, "json_col": 0.98}
5+
{"rowindex": 4, "json_col": "a string"}
6+
{"rowindex": 5, "json_col": []}
7+
{"rowindex": 6, "json_col": [1, 2, 3]}
8+
{"rowindex": 7, "json_col": [{"a": 1}, {"a": 2}, {"a": null}, {}]}
9+
{"rowindex": 8, "json_col": {"bool_value": true}}
10+
{"rowindex": 9, "json_col": {"folat_num": 3.14159}}
11+
{"rowindex": 10, "json_col": {"date": "2024-07-16"}}
12+
{"rowindex": 11, "json_col": {"null_filed": null}}
13+
{"rowindex": 12, "json_col": {"int_value": 2, "null_filed": null}}
14+
{"rowindex": 13, "json_col": {"list_data": [10, 20, 30]}}
15+
{"rowindex": 14, "json_col": {"person": {"name": "Alice", "age": 35}}}
16+
{"rowindex": 15, "json_col": {"order": {"items": ["book", "pen"], "total": 15.99}}}

tests/data/json_schema.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[
2+
{
3+
"name": "rowindex",
4+
"type": "INTEGER",
5+
"mode": "REQUIRED"
6+
},
7+
{
8+
"name": "json_col",
9+
"type": "JSON",
10+
"mode": "NULLABLE"
11+
}
12+
]

tests/system/conftest.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,7 @@ def load_test_data_tables(
298298
("nested", "nested_schema.json", "nested.jsonl"),
299299
("nested_structs", "nested_structs_schema.json", "nested_structs.jsonl"),
300300
("repeated", "repeated_schema.json", "repeated.jsonl"),
301+
("json", "json_schema.json", "json.jsonl"),
301302
("penguins", "penguins_schema.json", "penguins.jsonl"),
302303
("time_series", "time_series_schema.json", "time_series.jsonl"),
303304
("hockey_players", "hockey_players.json", "hockey_players.jsonl"),
@@ -384,6 +385,11 @@ def repeated_table_id(test_data_tables) -> str:
384385
return test_data_tables["repeated"]
385386

386387

388+
@pytest.fixture(scope="session")
389+
def json_table_id(test_data_tables) -> str:
390+
return test_data_tables["json"]
391+
392+
387393
@pytest.fixture(scope="session")
388394
def penguins_table_id(test_data_tables) -> str:
389395
return test_data_tables["penguins"]
@@ -481,6 +487,25 @@ def repeated_pandas_df() -> pd.DataFrame:
481487
return df
482488

483489

490+
@pytest.fixture(scope="session")
491+
def json_df(
492+
json_table_id: str, session: bigframes.Session
493+
) -> bigframes.dataframe.DataFrame:
494+
"""Returns a DataFrame containing columns of JSON type."""
495+
return session.read_gbq(json_table_id, index_col="rowindex")
496+
497+
498+
@pytest.fixture(scope="session")
499+
def json_pandas_df() -> pd.DataFrame:
500+
"""Returns a DataFrame containing columns of JSON type."""
501+
df = pd.read_json(
502+
DATA_DIR / "json.jsonl",
503+
lines=True,
504+
)
505+
df = df.set_index("rowindex")
506+
return df
507+
508+
484509
@pytest.fixture(scope="session")
485510
def scalars_df_default_index(
486511
scalars_df_index: bigframes.dataframe.DataFrame,

tests/system/small/test_series.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,13 @@ def test_get_column(scalars_dfs, col_name, expected_dtype):
278278
assert series_pandas.shape[0] == scalars_pandas_df.shape[0]
279279

280280

281+
def test_get_column_w_json(json_df, json_pandas_df):
282+
series = json_df["json_col"]
283+
series_pandas = series.to_pandas()
284+
assert series.dtype == pd.StringDtype(storage="pyarrow")
285+
assert series_pandas.shape[0] == json_pandas_df.shape[0]
286+
287+
281288
def test_series_get_column_default(scalars_dfs):
282289
scalars_df, _ = scalars_dfs
283290
result = scalars_df.get(123123123123123, "default_val")

0 commit comments

Comments
 (0)