|
| 1 | +import os |
| 2 | +from unittest.mock import AsyncMock, MagicMock, patch |
| 3 | + |
| 4 | +import pytest |
| 5 | + |
| 6 | +from scripts.cosmosdb_migration import CosmosDBMigrator, migrate_cosmosdb_data |
| 7 | + |
| 8 | +# Sample old format item |
| 9 | +TEST_OLD_ITEM = { |
| 10 | + "id": "123", |
| 11 | + "entra_oid": "OID_X", |
| 12 | + "title": "This is a test message", |
| 13 | + "timestamp": 123456789, |
| 14 | + "answers": [ |
| 15 | + [ |
| 16 | + "What does a Product Manager do?", |
| 17 | + { |
| 18 | + "delta": {"role": "assistant"}, |
| 19 | + "session_state": "143c0240-b2ee-4090-8e90-2a1c58124894", |
| 20 | + "message": { |
| 21 | + "content": "A Product Manager is responsible for product strategy and execution.", |
| 22 | + "role": "assistant", |
| 23 | + }, |
| 24 | + }, |
| 25 | + ], |
| 26 | + [ |
| 27 | + "What about a Software Engineer?", |
| 28 | + { |
| 29 | + "delta": {"role": "assistant"}, |
| 30 | + "session_state": "243c0240-b2ee-4090-8e90-2a1c58124894", |
| 31 | + "message": { |
| 32 | + "content": "A Software Engineer writes code to create applications.", |
| 33 | + "role": "assistant", |
| 34 | + }, |
| 35 | + }, |
| 36 | + ], |
| 37 | + ], |
| 38 | +} |
| 39 | + |
| 40 | + |
| 41 | +class MockAsyncPageIterator: |
| 42 | + """Helper class to mock an async page from CosmosDB""" |
| 43 | + |
| 44 | + def __init__(self, items): |
| 45 | + self.items = items |
| 46 | + |
| 47 | + def __aiter__(self): |
| 48 | + return self |
| 49 | + |
| 50 | + async def __anext__(self): |
| 51 | + if not self.items: |
| 52 | + raise StopAsyncIteration |
| 53 | + return self.items.pop(0) |
| 54 | + |
| 55 | + |
| 56 | +class MockCosmosDBResultsIterator: |
| 57 | + """Helper class to mock a paginated query result from CosmosDB""" |
| 58 | + |
| 59 | + def __init__(self, data=[]): |
| 60 | + self.data = data |
| 61 | + self.continuation_token = None |
| 62 | + |
| 63 | + def by_page(self, continuation_token=None): |
| 64 | + """Return a paged iterator""" |
| 65 | + self.continuation_token = "next_token" if not continuation_token else continuation_token + "_next" |
| 66 | + # Return an async iterator that contains pages |
| 67 | + return MockPagesAsyncIterator(self.data) |
| 68 | + |
| 69 | + |
| 70 | +class MockPagesAsyncIterator: |
| 71 | + """Helper class to mock an iterator of pages""" |
| 72 | + |
| 73 | + def __init__(self, data): |
| 74 | + self.data = data |
| 75 | + self.continuation_token = "next_token" |
| 76 | + |
| 77 | + def __aiter__(self): |
| 78 | + return self |
| 79 | + |
| 80 | + async def __anext__(self): |
| 81 | + if not self.data: |
| 82 | + raise StopAsyncIteration |
| 83 | + # Return a page, which is an async iterator of items |
| 84 | + return MockAsyncPageIterator([self.data.pop(0)]) |
| 85 | + |
| 86 | + |
| 87 | +@pytest.mark.asyncio |
| 88 | +async def test_migrate_method(): |
| 89 | + """Test the migrate method of CosmosDBMigrator""" |
| 90 | + # Create mock objects |
| 91 | + mock_container = MagicMock() |
| 92 | + mock_database = MagicMock() |
| 93 | + mock_client = MagicMock() |
| 94 | + |
| 95 | + # Set up the query_items mock to return our test item |
| 96 | + mock_container.query_items.return_value = MockCosmosDBResultsIterator([TEST_OLD_ITEM]) |
| 97 | + |
| 98 | + # Set up execute_item_batch as a spy to capture calls |
| 99 | + execute_batch_mock = AsyncMock() |
| 100 | + mock_container.execute_item_batch = execute_batch_mock |
| 101 | + |
| 102 | + # Set up the database mock to return our container mocks |
| 103 | + mock_database.get_container_client.side_effect = lambda container_name: mock_container |
| 104 | + |
| 105 | + # Set up the client mock |
| 106 | + mock_client.get_database_client.return_value = mock_database |
| 107 | + |
| 108 | + # Create the migrator with our mocks |
| 109 | + migrator = CosmosDBMigrator("dummy_account", "dummy_db") |
| 110 | + migrator.client = mock_client |
| 111 | + migrator.database = mock_database |
| 112 | + migrator.old_container = mock_container |
| 113 | + migrator.new_container = mock_container |
| 114 | + |
| 115 | + # Call the migrate method |
| 116 | + await migrator.migrate() |
| 117 | + |
| 118 | + # Verify query_items was called with the right parameters |
| 119 | + mock_container.query_items.assert_called_once_with(query="SELECT * FROM c") |
| 120 | + |
| 121 | + # Verify execute_item_batch was called |
| 122 | + execute_batch_mock.assert_called_once() |
| 123 | + |
| 124 | + # Extract the arguments from the call |
| 125 | + call_args = execute_batch_mock.call_args[1] |
| 126 | + batch_operations = call_args["batch_operations"] |
| 127 | + partition_key = call_args["partition_key"] |
| 128 | + |
| 129 | + # Verify the partition key |
| 130 | + assert partition_key == ["OID_X", "123"] |
| 131 | + |
| 132 | + # We should have 3 operations: 1 for session and 2 for message pairs |
| 133 | + assert len(batch_operations) == 3 |
| 134 | + |
| 135 | + # Verify session item |
| 136 | + session_operation = batch_operations[0] |
| 137 | + assert session_operation[0] == "upsert" |
| 138 | + session_item = session_operation[1][0] |
| 139 | + assert session_item["id"] == "123" |
| 140 | + assert session_item["session_id"] == "123" |
| 141 | + assert session_item["entra_oid"] == "OID_X" |
| 142 | + assert session_item["title"] == "This is a test message" |
| 143 | + assert session_item["timestamp"] == 123456789 |
| 144 | + assert session_item["type"] == "session" |
| 145 | + assert session_item["version"] == "cosmosdb-v2" |
| 146 | + |
| 147 | + # Verify first message pair |
| 148 | + message1_operation = batch_operations[1] |
| 149 | + assert message1_operation[0] == "upsert" |
| 150 | + message1_item = message1_operation[1][0] |
| 151 | + assert message1_item["id"] == "123-0" |
| 152 | + assert message1_item["session_id"] == "123" |
| 153 | + assert message1_item["entra_oid"] == "OID_X" |
| 154 | + assert message1_item["question"] == "What does a Product Manager do?" |
| 155 | + assert message1_item["type"] == "message_pair" |
| 156 | + assert message1_item["order"] == 0 |
| 157 | + |
| 158 | + # Verify second message pair |
| 159 | + message2_operation = batch_operations[2] |
| 160 | + assert message2_operation[0] == "upsert" |
| 161 | + message2_item = message2_operation[1][0] |
| 162 | + assert message2_item["id"] == "123-1" |
| 163 | + assert message2_item["session_id"] == "123" |
| 164 | + assert message2_item["entra_oid"] == "OID_X" |
| 165 | + assert message2_item["question"] == "What about a Software Engineer?" |
| 166 | + assert message2_item["type"] == "message_pair" |
| 167 | + assert message2_item["order"] == 1 |
| 168 | + |
| 169 | + |
| 170 | +@pytest.mark.asyncio |
| 171 | +async def test_migrate_cosmosdb_data(monkeypatch): |
| 172 | + """Test the main migrate_cosmosdb_data function""" |
| 173 | + with patch.dict(os.environ, clear=True): |
| 174 | + monkeypatch.setenv("USE_CHAT_HISTORY_COSMOS", "true") |
| 175 | + monkeypatch.setenv("AZURE_COSMOSDB_ACCOUNT", "dummy_account") |
| 176 | + monkeypatch.setenv("AZURE_CHAT_HISTORY_DATABASE", "dummy_db") |
| 177 | + |
| 178 | + # Create a mock for the CosmosDBMigrator |
| 179 | + with patch("scripts.cosmosdb_migration.CosmosDBMigrator") as mock_migrator_class: |
| 180 | + # Set up the mock for the migrator instance |
| 181 | + mock_migrator = AsyncMock() |
| 182 | + mock_migrator_class.return_value = mock_migrator |
| 183 | + |
| 184 | + # Call the function |
| 185 | + await migrate_cosmosdb_data() |
| 186 | + |
| 187 | + # Verify the migrator was created with the right parameters |
| 188 | + mock_migrator_class.assert_called_once_with("dummy_account", "dummy_db") |
| 189 | + |
| 190 | + # Verify migrate and close were called |
| 191 | + mock_migrator.migrate.assert_called_once() |
| 192 | + mock_migrator.close.assert_called_once() |
0 commit comments