|  | 
|  | 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 | +import os | 
|  | 16 | +import traceback | 
|  | 17 | +from typing import Generator | 
|  | 18 | +import uuid | 
|  | 19 | + | 
|  | 20 | +from google.cloud import storage | 
|  | 21 | +import pandas as pd | 
|  | 22 | +import pytest | 
|  | 23 | + | 
|  | 24 | +import bigframes | 
|  | 25 | +from bigframes import dtypes | 
|  | 26 | +import bigframes.pandas as bpd | 
|  | 27 | + | 
|  | 28 | + | 
|  | 29 | +@pytest.fixture(scope="session") | 
|  | 30 | +def images_output_folder() -> Generator[str, None, None]: | 
|  | 31 | +    id = uuid.uuid4().hex | 
|  | 32 | +    folder = os.path.join("gs://bigframes_blob_test/", id) | 
|  | 33 | +    yield folder | 
|  | 34 | + | 
|  | 35 | +    # clean up | 
|  | 36 | +    try: | 
|  | 37 | +        cloud_storage_client = storage.Client() | 
|  | 38 | +        bucket = cloud_storage_client.bucket("bigframes_blob_test") | 
|  | 39 | +        blobs = bucket.list_blobs(prefix=id) | 
|  | 40 | +        for blob in blobs: | 
|  | 41 | +            blob.delete() | 
|  | 42 | +    except Exception as exc: | 
|  | 43 | +        traceback.print_exception(type(exc), exc, None) | 
|  | 44 | + | 
|  | 45 | + | 
|  | 46 | +@pytest.fixture(scope="session") | 
|  | 47 | +def images_output_uris(images_output_folder: str) -> list[str]: | 
|  | 48 | +    return [ | 
|  | 49 | +        os.path.join(images_output_folder, "img0.jpg"), | 
|  | 50 | +        os.path.join(images_output_folder, "img1.jpg"), | 
|  | 51 | +    ] | 
|  | 52 | + | 
|  | 53 | + | 
|  | 54 | +def test_blob_image_blur_to_series( | 
|  | 55 | +    images_mm_df: bpd.DataFrame, | 
|  | 56 | +    bq_connection: str, | 
|  | 57 | +    images_output_uris: list[str], | 
|  | 58 | +    session: bigframes.Session, | 
|  | 59 | +): | 
|  | 60 | +    bigframes.options.experiments.blob = True | 
|  | 61 | + | 
|  | 62 | +    series = bpd.Series(images_output_uris, session=session).str.to_blob( | 
|  | 63 | +        connection=bq_connection | 
|  | 64 | +    ) | 
|  | 65 | + | 
|  | 66 | +    actual = images_mm_df["blob_col"].blob.image_blur( | 
|  | 67 | +        (8, 8), dst=series, connection=bq_connection | 
|  | 68 | +    ) | 
|  | 69 | +    expected_df = pd.DataFrame( | 
|  | 70 | +        { | 
|  | 71 | +            "uri": images_output_uris, | 
|  | 72 | +            "version": [None, None], | 
|  | 73 | +            "authorizer": [bq_connection.casefold(), bq_connection.casefold()], | 
|  | 74 | +            "details": [None, None], | 
|  | 75 | +        } | 
|  | 76 | +    ) | 
|  | 77 | +    pd.testing.assert_frame_equal( | 
|  | 78 | +        actual.struct.explode().to_pandas(), | 
|  | 79 | +        expected_df, | 
|  | 80 | +        check_dtype=False, | 
|  | 81 | +        check_index_type=False, | 
|  | 82 | +    ) | 
|  | 83 | + | 
|  | 84 | +    # verify the files exist | 
|  | 85 | +    assert not actual.blob.size().isna().any() | 
|  | 86 | + | 
|  | 87 | + | 
|  | 88 | +def test_blob_image_blur_to_folder( | 
|  | 89 | +    images_mm_df: bpd.DataFrame, | 
|  | 90 | +    bq_connection: str, | 
|  | 91 | +    images_output_folder: str, | 
|  | 92 | +    images_output_uris: list[str], | 
|  | 93 | +): | 
|  | 94 | +    bigframes.options.experiments.blob = True | 
|  | 95 | + | 
|  | 96 | +    actual = images_mm_df["blob_col"].blob.image_blur( | 
|  | 97 | +        (8, 8), dst=images_output_folder, connection=bq_connection | 
|  | 98 | +    ) | 
|  | 99 | +    expected_df = pd.DataFrame( | 
|  | 100 | +        { | 
|  | 101 | +            "uri": images_output_uris, | 
|  | 102 | +            "version": [None, None], | 
|  | 103 | +            "authorizer": [bq_connection.casefold(), bq_connection.casefold()], | 
|  | 104 | +            "details": [None, None], | 
|  | 105 | +        } | 
|  | 106 | +    ) | 
|  | 107 | +    pd.testing.assert_frame_equal( | 
|  | 108 | +        actual.struct.explode().to_pandas(), | 
|  | 109 | +        expected_df, | 
|  | 110 | +        check_dtype=False, | 
|  | 111 | +        check_index_type=False, | 
|  | 112 | +    ) | 
|  | 113 | + | 
|  | 114 | +    # verify the files exist | 
|  | 115 | +    assert not actual.blob.size().isna().any() | 
|  | 116 | + | 
|  | 117 | + | 
|  | 118 | +def test_blob_image_blur_to_bq(images_mm_df: bpd.DataFrame, bq_connection: str): | 
|  | 119 | +    bigframes.options.experiments.blob = True | 
|  | 120 | + | 
|  | 121 | +    actual = images_mm_df["blob_col"].blob.image_blur((8, 8), connection=bq_connection) | 
|  | 122 | + | 
|  | 123 | +    assert isinstance(actual, bpd.Series) | 
|  | 124 | +    assert len(actual) == 2 | 
|  | 125 | +    assert actual.dtype == dtypes.BYTES_DTYPE | 
0 commit comments