|
| 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 datetime |
| 16 | +from unittest.mock import MagicMock |
| 17 | + |
| 18 | +import pytest |
| 19 | + |
| 20 | +import bigframes.bigquery.obj as obj |
| 21 | +import bigframes.operations as ops |
| 22 | +import bigframes.series as series |
| 23 | + |
| 24 | + |
| 25 | +def test_fetch_metadata_op_structure(): |
| 26 | + op = ops.obj_fetch_metadata_op |
| 27 | + assert op.name == "obj_fetch_metadata" |
| 28 | + |
| 29 | +def test_get_access_url_op_structure(): |
| 30 | + op = ops.ObjGetAccessUrl(mode="r") |
| 31 | + assert op.name == "obj_get_access_url" |
| 32 | + assert op.mode == "r" |
| 33 | + assert op.duration is None |
| 34 | + |
| 35 | +def test_get_access_url_with_duration_op_structure(): |
| 36 | + op = ops.ObjGetAccessUrl(mode="rw", duration=3600000000) |
| 37 | + assert op.name == "obj_get_access_url" |
| 38 | + assert op.mode == "rw" |
| 39 | + assert op.duration == 3600000000 |
| 40 | + |
| 41 | +def test_make_ref_op_structure(): |
| 42 | + op = ops.obj_make_ref_op |
| 43 | + assert op.name == "obj_make_ref" |
| 44 | + |
| 45 | +def test_make_ref_json_op_structure(): |
| 46 | + op = ops.obj_make_ref_json_op |
| 47 | + assert op.name == "obj_make_ref_json" |
| 48 | + |
| 49 | +def test_fetch_metadata_calls_apply_unary_op(): |
| 50 | + s = MagicMock(spec=series.Series) |
| 51 | + |
| 52 | + obj.fetch_metadata(s) |
| 53 | + |
| 54 | + s._apply_unary_op.assert_called_once() |
| 55 | + args, _ = s._apply_unary_op.call_args |
| 56 | + assert args[0] == ops.obj_fetch_metadata_op |
| 57 | + |
| 58 | +def test_get_access_url_calls_apply_unary_op_without_duration(): |
| 59 | + s = MagicMock(spec=series.Series) |
| 60 | + |
| 61 | + obj.get_access_url(s, mode="r") |
| 62 | + |
| 63 | + s._apply_unary_op.assert_called_once() |
| 64 | + args, _ = s._apply_unary_op.call_args |
| 65 | + assert isinstance(args[0], ops.ObjGetAccessUrl) |
| 66 | + assert args[0].mode == "r" |
| 67 | + assert args[0].duration is None |
| 68 | + |
| 69 | +def test_get_access_url_calls_apply_unary_op_with_duration(): |
| 70 | + s = MagicMock(spec=series.Series) |
| 71 | + duration = datetime.timedelta(hours=1) |
| 72 | + |
| 73 | + obj.get_access_url(s, mode="rw", duration=duration) |
| 74 | + |
| 75 | + s._apply_unary_op.assert_called_once() |
| 76 | + args, kwargs = s._apply_unary_op.call_args |
| 77 | + assert isinstance(args[0], ops.ObjGetAccessUrl) |
| 78 | + assert args[0].mode == "rw" |
| 79 | + # 1 hour = 3600 seconds = 3600 * 1000 * 1000 microseconds |
| 80 | + assert args[0].duration == 3600000000 |
| 81 | + |
| 82 | +def test_make_ref_calls_apply_binary_op_with_authorizer(): |
| 83 | + uri = MagicMock(spec=series.Series) |
| 84 | + auth = MagicMock(spec=series.Series) |
| 85 | + |
| 86 | + obj.make_ref(uri, authorizer=auth) |
| 87 | + |
| 88 | + uri._apply_binary_op.assert_called_once() |
| 89 | + args, _ = uri._apply_binary_op.call_args |
| 90 | + assert args[0] == auth |
| 91 | + assert args[1] == ops.obj_make_ref_op |
| 92 | + |
| 93 | +def test_make_ref_calls_apply_unary_op_without_authorizer(): |
| 94 | + json_val = MagicMock(spec=series.Series) |
| 95 | + |
| 96 | + obj.make_ref(json_val) |
| 97 | + |
| 98 | + json_val._apply_unary_op.assert_called_once() |
| 99 | + args, _ = json_val._apply_unary_op.call_args |
| 100 | + assert args[0] == ops.obj_make_ref_json_op |
0 commit comments