|
| 1 | +# Copyright 2026 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 unittest |
| 16 | +from unittest.mock import MagicMock, patch |
| 17 | +from datetime import datetime |
| 18 | + |
| 19 | +from embedding_utils import ( |
| 20 | + get_latest_lock_timestamp, |
| 21 | + get_updated_nodes, |
| 22 | + filter_and_convert_nodes, |
| 23 | + generate_embeddings_partitioned |
| 24 | +) |
| 25 | + |
| 26 | +class TestEmbeddingUtils(unittest.TestCase): |
| 27 | + |
| 28 | + def test_get_latest_lock_timestamp(self): |
| 29 | + mock_database = MagicMock() |
| 30 | + mock_snapshot = MagicMock() |
| 31 | + mock_database.snapshot.return_value.__enter__.return_value = mock_snapshot |
| 32 | + expected_timestamp = datetime(2026, 4, 20, 12, 0, 0) |
| 33 | + mock_snapshot.execute_sql.return_value = [(expected_timestamp,)] |
| 34 | + |
| 35 | + timestamp = get_latest_lock_timestamp(mock_database) |
| 36 | + self.assertEqual(timestamp, expected_timestamp) |
| 37 | + |
| 38 | + def test_get_updated_nodes(self): |
| 39 | + mock_database = MagicMock() |
| 40 | + mock_snapshot = MagicMock() |
| 41 | + mock_database.snapshot.return_value.__enter__.return_value = mock_snapshot |
| 42 | + |
| 43 | + class MockField: |
| 44 | + def __init__(self, name): |
| 45 | + self.name = name |
| 46 | + |
| 47 | + class MockResults: |
| 48 | + def __init__(self, rows, field_names): |
| 49 | + self.rows = rows |
| 50 | + self.fields = [MockField(name) for name in field_names] |
| 51 | + |
| 52 | + def __iter__(self): |
| 53 | + return iter(self.rows) |
| 54 | + |
| 55 | + mock_snapshot.execute_sql.return_value = MockResults( |
| 56 | + rows=[("dc/1", "Node 1", ["Topic"])], |
| 57 | + field_names=["subject_id", "name", "types"] |
| 58 | + ) |
| 59 | + |
| 60 | + nodes = list(get_updated_nodes(mock_database, None, ["Topic"])) |
| 61 | + |
| 62 | + # Verify Spanner call |
| 63 | + mock_snapshot.execute_sql.assert_called_once() |
| 64 | + args, kwargs = mock_snapshot.execute_sql.call_args |
| 65 | + query = args[0] |
| 66 | + self.assertIn("SELECT subject_id, name, types FROM Node", query) |
| 67 | + self.assertIn("TRUE", query) |
| 68 | + self.assertEqual(kwargs["params"], {"node_types": ["Topic"]}) |
| 69 | + |
| 70 | + self.assertEqual(len(nodes), 1) |
| 71 | + self.assertEqual(nodes[0]["subject_id"], "dc/1") |
| 72 | + self.assertEqual(nodes[0]["name"], "Node 1") |
| 73 | + self.assertEqual(nodes[0]["types"], ["Topic"]) |
| 74 | + |
| 75 | + def test_get_updated_nodes_with_timestamp(self): |
| 76 | + mock_database = MagicMock() |
| 77 | + mock_snapshot = MagicMock() |
| 78 | + mock_database.snapshot.return_value.__enter__.return_value = mock_snapshot |
| 79 | + |
| 80 | + class MockField: |
| 81 | + def __init__(self, name): |
| 82 | + self.name = name |
| 83 | + |
| 84 | + class MockResults: |
| 85 | + def __init__(self, rows, field_names): |
| 86 | + self.rows = rows |
| 87 | + self.fields = [MockField(name) for name in field_names] |
| 88 | + |
| 89 | + def __iter__(self): |
| 90 | + return iter(self.rows) |
| 91 | + |
| 92 | + mock_snapshot.execute_sql.return_value = MockResults( |
| 93 | + rows=[("dc/2", "Node 2", ["Topic"])], |
| 94 | + field_names=["subject_id", "name", "types"] |
| 95 | + ) |
| 96 | + |
| 97 | + test_timestamp = datetime(2026, 4, 25, 0, 0, 0) |
| 98 | + nodes = list(get_updated_nodes(mock_database, test_timestamp, ["Topic"])) |
| 99 | + |
| 100 | + # Verify Spanner call |
| 101 | + mock_snapshot.execute_sql.assert_called_once() |
| 102 | + args, kwargs = mock_snapshot.execute_sql.call_args |
| 103 | + query = args[0] |
| 104 | + self.assertIn("SELECT subject_id, name, types FROM Node", query) |
| 105 | + self.assertIn("update_timestamp > @timestamp", query) |
| 106 | + self.assertEqual(kwargs["params"], {"node_types": ["Topic"], "timestamp": test_timestamp}) |
| 107 | + |
| 108 | + self.assertEqual(len(nodes), 1) |
| 109 | + self.assertEqual(nodes[0]["subject_id"], "dc/2") |
| 110 | + |
| 111 | + def test_filter_and_convert_nodes(self): |
| 112 | + nodes = [ |
| 113 | + {"subject_id": "dc/1", "name": "Node 1", "types": ["Topic"]}, |
| 114 | + {"subject_id": "dc/2", "name": None, "types": ["StatisticalVariable"]}, |
| 115 | + {"subject_id": "dc/3", "name": "Node 3", "types": ["Topic", "StatisticalVariable"]}, |
| 116 | + {"subject_id": "dc/4", "name": "", "types": ["StatisticalVariable"]} |
| 117 | + ] |
| 118 | + |
| 119 | + converted = list(filter_and_convert_nodes(nodes)) |
| 120 | + self.assertEqual(len(converted), 2) |
| 121 | + self.assertEqual(converted[0], ("dc/1", "Node 1", ["Topic"])) |
| 122 | + self.assertEqual(converted[1], ("dc/3", "Node 3", ["Topic", "StatisticalVariable"])) |
| 123 | + |
| 124 | + @patch('embedding_utils._BATCH_SIZE', 2) |
| 125 | + def test_generate_embeddings_partitioned(self): |
| 126 | + mock_database = MagicMock() |
| 127 | + |
| 128 | + nodes = [ |
| 129 | + ("dc/1", "Node 1", ["Topic"]), |
| 130 | + ("dc/2", "Node 2", ["Topic"]), |
| 131 | + ("dc/3", "Node 3", ["Topic"]), |
| 132 | + ("dc/4", "Node 4", ["Topic"]), |
| 133 | + ("dc/5", "Node 5", ["Topic"]), |
| 134 | + ("dc/6", "Node 6", ["Topic"]), |
| 135 | + ("dc/7", "Node 7", ["Topic"]), |
| 136 | + ("dc/8", "Node 8", ["Topic"]) |
| 137 | + ] |
| 138 | + |
| 139 | + transactions = [] |
| 140 | + def side_effect(func): |
| 141 | + mock_transaction = MagicMock() |
| 142 | + mock_transaction.execute_update.return_value = 2 |
| 143 | + transactions.append(mock_transaction) |
| 144 | + return func(mock_transaction) |
| 145 | + |
| 146 | + mock_database.run_in_transaction.side_effect = side_effect |
| 147 | + |
| 148 | + affected_rows = generate_embeddings_partitioned(mock_database, nodes) |
| 149 | + self.assertEqual(affected_rows, 8) |
| 150 | + self.assertEqual(mock_database.run_in_transaction.call_count, 4) |
| 151 | + |
| 152 | + # Verify execute_update calls |
| 153 | + self.assertEqual(len(transactions), 4) |
| 154 | + for i, tx in enumerate(transactions): |
| 155 | + tx.execute_update.assert_called_once() |
| 156 | + args, kwargs = tx.execute_update.call_args |
| 157 | + self.assertIn("INSERT OR UPDATE INTO NodeEmbeddings", args[0]) |
| 158 | + |
| 159 | + # Verify batch content |
| 160 | + batch = kwargs["params"]["nodes"] |
| 161 | + self.assertEqual(len(batch), 2) |
| 162 | + self.assertEqual(batch[0][0], f"dc/{i*2 + 1}") |
| 163 | + self.assertEqual(batch[1][0], f"dc/{i*2 + 2}") |
| 164 | + |
| 165 | +if __name__ == '__main__': |
| 166 | + unittest.main() |
0 commit comments