|
| 1 | +# Copyright 2025 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 | +from unittest import mock |
| 16 | + |
| 17 | +import pandas as pd |
| 18 | +import pytest |
| 19 | + |
| 20 | +import bigframes |
| 21 | +import bigframes.pandas as bpd |
| 22 | + |
| 23 | +pytest.skip("Skipping blob tests due to b/481790217", allow_module_level=True) |
| 24 | + |
| 25 | + |
| 26 | +idisplay = pytest.importorskip("IPython.display") |
| 27 | + |
| 28 | + |
| 29 | +def test_blob_create_from_uri_str( |
| 30 | + bq_connection: str, session: bigframes.Session, images_uris |
| 31 | +): |
| 32 | + uri_series = bpd.Series(images_uris, session=session) |
| 33 | + blob_series = uri_series.str.to_blob(connection=bq_connection) |
| 34 | + |
| 35 | + pd_blob_df = blob_series.struct.explode().to_pandas() |
| 36 | + expected_pd_df = pd.DataFrame( |
| 37 | + { |
| 38 | + "uri": images_uris, |
| 39 | + "version": [None, None], |
| 40 | + "authorizer": [bq_connection.casefold(), bq_connection.casefold()], |
| 41 | + "details": [None, None], |
| 42 | + } |
| 43 | + ) |
| 44 | + |
| 45 | + pd.testing.assert_frame_equal( |
| 46 | + pd_blob_df, expected_pd_df, check_dtype=False, check_index_type=False |
| 47 | + ) |
| 48 | + |
| 49 | + |
| 50 | +def test_blob_create_from_glob_path( |
| 51 | + bq_connection: str, session: bigframes.Session, images_gcs_path, images_uris |
| 52 | +): |
| 53 | + blob_df = session.from_glob_path( |
| 54 | + images_gcs_path, connection=bq_connection, name="blob_col" |
| 55 | + ) |
| 56 | + pd_blob_df = ( |
| 57 | + blob_df["blob_col"] |
| 58 | + .struct.explode() |
| 59 | + .to_pandas() |
| 60 | + .sort_values("uri") |
| 61 | + .reset_index(drop=True) |
| 62 | + ) |
| 63 | + |
| 64 | + expected_df = pd.DataFrame( |
| 65 | + { |
| 66 | + "uri": images_uris, |
| 67 | + "version": [None, None], |
| 68 | + "authorizer": [bq_connection.casefold(), bq_connection.casefold()], |
| 69 | + "details": [None, None], |
| 70 | + } |
| 71 | + ) |
| 72 | + |
| 73 | + pd.testing.assert_frame_equal( |
| 74 | + pd_blob_df, expected_df, check_dtype=False, check_index_type=False |
| 75 | + ) |
| 76 | + |
| 77 | + |
| 78 | +def test_blob_create_read_gbq_object_table( |
| 79 | + bq_connection: str, session: bigframes.Session, images_gcs_path, images_uris |
| 80 | +): |
| 81 | + obj_table = session._create_object_table(images_gcs_path, bq_connection) |
| 82 | + |
| 83 | + blob_df = session.read_gbq_object_table(obj_table, name="blob_col") |
| 84 | + pd_blob_df = ( |
| 85 | + blob_df["blob_col"] |
| 86 | + .struct.explode() |
| 87 | + .to_pandas() |
| 88 | + .sort_values("uri") |
| 89 | + .reset_index(drop=True) |
| 90 | + ) |
| 91 | + expected_df = pd.DataFrame( |
| 92 | + { |
| 93 | + "uri": images_uris, |
| 94 | + "version": [None, None], |
| 95 | + "authorizer": [bq_connection.casefold(), bq_connection.casefold()], |
| 96 | + "details": [None, None], |
| 97 | + } |
| 98 | + ) |
| 99 | + |
| 100 | + pd.testing.assert_frame_equal( |
| 101 | + pd_blob_df, expected_df, check_dtype=False, check_index_type=False |
| 102 | + ) |
| 103 | + |
| 104 | + |
| 105 | +def test_display_images(monkeypatch, images_mm_df: bpd.DataFrame): |
| 106 | + mock_display = mock.Mock() |
| 107 | + monkeypatch.setattr(idisplay, "display", mock_display) |
| 108 | + |
| 109 | + images_mm_df["blob_col"].blob.display() |
| 110 | + |
| 111 | + for call in mock_display.call_args_list: |
| 112 | + args, _ = call |
| 113 | + arg = args[0] |
| 114 | + assert isinstance(arg, idisplay.Image) |
| 115 | + |
| 116 | + |
| 117 | +def test_display_nulls( |
| 118 | + monkeypatch, |
| 119 | + bq_connection: str, |
| 120 | + session: bigframes.Session, |
| 121 | +): |
| 122 | + uri_series = bpd.Series([None, None, None], dtype="string", session=session) |
| 123 | + blob_series = uri_series.str.to_blob(connection=bq_connection) |
| 124 | + mock_display = mock.Mock() |
| 125 | + monkeypatch.setattr(idisplay, "display", mock_display) |
| 126 | + |
| 127 | + blob_series.blob.display() |
| 128 | + |
| 129 | + for call in mock_display.call_args_list: |
| 130 | + args, _ = call |
| 131 | + arg = args[0] |
| 132 | + assert arg == "<NA>" |
0 commit comments