|
| 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 __future__ import annotations |
| 16 | + |
| 17 | +import pickle |
| 18 | +from unittest import mock |
| 19 | + |
| 20 | +from google.adk.sessions.database_session_service import DynamicPickleType |
| 21 | +import pytest |
| 22 | +from sqlalchemy import create_engine |
| 23 | +from sqlalchemy.dialects import mysql |
| 24 | + |
| 25 | + |
| 26 | +@pytest.fixture |
| 27 | +def pickle_type(): |
| 28 | + """Fixture for DynamicPickleType instance.""" |
| 29 | + return DynamicPickleType() |
| 30 | + |
| 31 | + |
| 32 | +def test_load_dialect_impl_mysql(pickle_type): |
| 33 | + """Test that MySQL dialect uses LONGBLOB.""" |
| 34 | + # Mock the MySQL dialect |
| 35 | + mock_dialect = mock.Mock() |
| 36 | + mock_dialect.name = "mysql" |
| 37 | + |
| 38 | + # Mock the return value of type_descriptor |
| 39 | + mock_longblob_type = mock.Mock() |
| 40 | + mock_dialect.type_descriptor.return_value = mock_longblob_type |
| 41 | + |
| 42 | + impl = pickle_type.load_dialect_impl(mock_dialect) |
| 43 | + |
| 44 | + # Verify type_descriptor was called once with mysql.LONGBLOB |
| 45 | + mock_dialect.type_descriptor.assert_called_once_with(mysql.LONGBLOB) |
| 46 | + # Verify the return value is what we expect |
| 47 | + assert impl == mock_longblob_type |
| 48 | + |
| 49 | + |
| 50 | +def test_load_dialect_impl_spanner(pickle_type): |
| 51 | + """Test that Spanner dialect uses SpannerPickleType.""" |
| 52 | + # Mock the spanner dialect |
| 53 | + mock_dialect = mock.Mock() |
| 54 | + mock_dialect.name = "spanner+spanner" |
| 55 | + |
| 56 | + with mock.patch( |
| 57 | + "google.cloud.sqlalchemy_spanner.sqlalchemy_spanner.SpannerPickleType" |
| 58 | + ) as mock_spanner_type: |
| 59 | + pickle_type.load_dialect_impl(mock_dialect) |
| 60 | + mock_dialect.type_descriptor.assert_called_once_with(mock_spanner_type) |
| 61 | + |
| 62 | + |
| 63 | +def test_load_dialect_impl_default(pickle_type): |
| 64 | + """Test that other dialects use default PickleType.""" |
| 65 | + engine = create_engine("sqlite:///:memory:") |
| 66 | + dialect = engine.dialect |
| 67 | + impl = pickle_type.load_dialect_impl(dialect) |
| 68 | + # Should return the default impl (PickleType) |
| 69 | + assert impl == pickle_type.impl |
| 70 | + |
| 71 | + |
| 72 | +@pytest.mark.parametrize( |
| 73 | + "dialect_name", |
| 74 | + [ |
| 75 | + pytest.param("mysql", id="mysql"), |
| 76 | + pytest.param("spanner+spanner", id="spanner"), |
| 77 | + ], |
| 78 | +) |
| 79 | +def test_process_bind_param_pickle_dialects(pickle_type, dialect_name): |
| 80 | + """Test that MySQL and Spanner dialects pickle the value.""" |
| 81 | + mock_dialect = mock.Mock() |
| 82 | + mock_dialect.name = dialect_name |
| 83 | + |
| 84 | + test_data = {"key": "value", "nested": [1, 2, 3]} |
| 85 | + result = pickle_type.process_bind_param(test_data, mock_dialect) |
| 86 | + |
| 87 | + # Should be pickled bytes |
| 88 | + assert isinstance(result, bytes) |
| 89 | + # Should be able to unpickle back to original |
| 90 | + assert pickle.loads(result) == test_data |
| 91 | + |
| 92 | + |
| 93 | +def test_process_bind_param_default(pickle_type): |
| 94 | + """Test that other dialects return value as-is.""" |
| 95 | + mock_dialect = mock.Mock() |
| 96 | + mock_dialect.name = "sqlite" |
| 97 | + |
| 98 | + test_data = {"key": "value"} |
| 99 | + result = pickle_type.process_bind_param(test_data, mock_dialect) |
| 100 | + |
| 101 | + # Should return value unchanged (SQLAlchemy's PickleType handles it) |
| 102 | + assert result == test_data |
| 103 | + |
| 104 | + |
| 105 | +def test_process_bind_param_none(pickle_type): |
| 106 | + """Test that None values are handled correctly.""" |
| 107 | + mock_dialect = mock.Mock() |
| 108 | + mock_dialect.name = "mysql" |
| 109 | + |
| 110 | + result = pickle_type.process_bind_param(None, mock_dialect) |
| 111 | + assert result is None |
| 112 | + |
| 113 | + |
| 114 | +@pytest.mark.parametrize( |
| 115 | + "dialect_name", |
| 116 | + [ |
| 117 | + pytest.param("mysql", id="mysql"), |
| 118 | + pytest.param("spanner+spanner", id="spanner"), |
| 119 | + ], |
| 120 | +) |
| 121 | +def test_process_result_value_pickle_dialects(pickle_type, dialect_name): |
| 122 | + """Test that MySQL and Spanner dialects unpickle the value.""" |
| 123 | + mock_dialect = mock.Mock() |
| 124 | + mock_dialect.name = dialect_name |
| 125 | + |
| 126 | + test_data = {"key": "value", "nested": [1, 2, 3]} |
| 127 | + pickled_data = pickle.dumps(test_data) |
| 128 | + |
| 129 | + result = pickle_type.process_result_value(pickled_data, mock_dialect) |
| 130 | + |
| 131 | + # Should be unpickled back to original |
| 132 | + assert result == test_data |
| 133 | + |
| 134 | + |
| 135 | +def test_process_result_value_default(pickle_type): |
| 136 | + """Test that other dialects return value as-is.""" |
| 137 | + mock_dialect = mock.Mock() |
| 138 | + mock_dialect.name = "sqlite" |
| 139 | + |
| 140 | + test_data = {"key": "value"} |
| 141 | + result = pickle_type.process_result_value(test_data, mock_dialect) |
| 142 | + |
| 143 | + # Should return value unchanged (SQLAlchemy's PickleType handles it) |
| 144 | + assert result == test_data |
| 145 | + |
| 146 | + |
| 147 | +def test_process_result_value_none(pickle_type): |
| 148 | + """Test that None values are handled correctly.""" |
| 149 | + mock_dialect = mock.Mock() |
| 150 | + mock_dialect.name = "mysql" |
| 151 | + |
| 152 | + result = pickle_type.process_result_value(None, mock_dialect) |
| 153 | + assert result is None |
| 154 | + |
| 155 | + |
| 156 | +@pytest.mark.parametrize( |
| 157 | + "dialect_name", |
| 158 | + [ |
| 159 | + pytest.param("mysql", id="mysql"), |
| 160 | + pytest.param("spanner+spanner", id="spanner"), |
| 161 | + ], |
| 162 | +) |
| 163 | +def test_roundtrip_pickle_dialects(pickle_type, dialect_name): |
| 164 | + """Test full roundtrip for MySQL and Spanner: bind -> result.""" |
| 165 | + mock_dialect = mock.Mock() |
| 166 | + mock_dialect.name = dialect_name |
| 167 | + |
| 168 | + original_data = { |
| 169 | + "string": "test", |
| 170 | + "number": 42, |
| 171 | + "list": [1, 2, 3], |
| 172 | + "nested": {"a": 1, "b": 2}, |
| 173 | + } |
| 174 | + |
| 175 | + # Simulate bind (Python -> DB) |
| 176 | + bound_value = pickle_type.process_bind_param(original_data, mock_dialect) |
| 177 | + assert isinstance(bound_value, bytes) |
| 178 | + |
| 179 | + # Simulate result (DB -> Python) |
| 180 | + result_value = pickle_type.process_result_value(bound_value, mock_dialect) |
| 181 | + assert result_value == original_data |
0 commit comments