-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest.py
More file actions
398 lines (340 loc) · 20.9 KB
/
Copy pathtest.py
File metadata and controls
398 lines (340 loc) · 20.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
from dataclasses import dataclass
from typing import List
import json
import unittest
import random
import string
from pymongo import MongoClient
from json_parser import JsonParser
from schema_migration import SchemaMigration
SOURCE_RU_URI: str = ""
DEST_VCORE_URI: str = ""
@dataclass
class CollectionConfigSection:
include: List[str]
exclude: List[str]
migrate_shard_key: bool
drop_if_exists: bool
optimize_compound_indexes: bool
class TestSchemaMigration(unittest.TestCase):
@classmethod
def setUpClass(cls):
"""
Set up test fixtures for the TestSchemaMigration class.
"""
cls.source_client = MongoClient(SOURCE_RU_URI)
cls.dest_client = MongoClient(DEST_VCORE_URI)
cls.db_name = cls._generate_random_string(5)
@classmethod
def tearDownClass(cls):
"""
Clean up test fixtures for the TestSchemaMigration class.
"""
cls.source_client.close()
cls.dest_client.close()
def tearDown(self):
"""
Clean up resources after each test.
"""
# Drop the test database
self.source_client.drop_database(self.db_name)
self.dest_client.drop_database(self.db_name)
def test_optimize_compound_indexes_true(self):
"""
Test that the migrate_schema method optimizes compound indexes.
"""
# Create the source collection and index information
source_collection = self.source_client[self.db_name]["test_optimize"]
source_collection.create_index([("a", 1), ("b", 1), ("c", 1), ("d", 1)])
source_collection.create_index([("a", 1), ("b", 1), ("c", 1)])
source_collection.create_index([("b", 1), ("c", 1)])
source_collection.create_index([("c", 1), ("d", 1)])
source_collection.create_index([("b", 1), ("d", 1)])
source_collection.create_index([("d", 1)])
collection_config_sections = []
collection_config_sections.append(CollectionConfigSection([f'{self.db_name}.*'], [], False, False, True))
migrate_all_config = json.loads(self._generate_config(collection_config_sections))
collection_configs = JsonParser(migrate_all_config, self.source_client).parse_json()
# Create a SchemaMigration instance and call migrate_schema
schema_migration = SchemaMigration()
schema_migration.migrate_schema(self.source_client, self.dest_client, collection_configs)
# Verify that the indexes were optimized in the destination collection
dest_collection = self.dest_client[self.db_name]["test_optimize"]
dest_indexes = dest_collection.index_information()
self.assertIn("a_1_b_1_c_1_d_1", dest_indexes, "Compound index on 'a', 'b', 'c', 'd' was not migrated successfully.")
self.assertIn("b_1_d_1", dest_indexes, "Compound index on 'b' and 'd' was not migrated successfully.")
self.assertIn("d_1", dest_indexes, "Compound index on 'd' was not migrated successfully.")
self.assertNotIn("a_1_b_1_c_1", dest_indexes, "Compound index on 'a', 'b', 'c' was not optimized successfully.")
self.assertNotIn("b_1_c_1", dest_indexes, "Compound index on 'b' and 'c' was not optimized successfully.")
self.assertNotIn("c_1_d_1", dest_indexes, "Compound index on 'c' and 'd' was not optimized successfully.")
def test_optimize_compound_indexes_false(self):
"""
Test that the migrate_schema method doesn't optimizes compound indexes.
"""
# Create the source collection and index information
source_collection = self.source_client[self.db_name]["test_optimize"]
source_collection.create_index([("a", 1), ("b", 1), ("c", 1), ("d", 1)])
source_collection.create_index([("a", 1), ("b", 1), ("c", 1)])
source_collection.create_index([("b", 1), ("c", 1)])
source_collection.create_index([("c", 1), ("d", 1)])
source_collection.create_index([("b", 1), ("d", 1)])
source_collection.create_index([("d", 1)])
collection_config_sections = []
collection_config_sections.append(CollectionConfigSection([f'{self.db_name}.*'], [], False, False, False))
migrate_all_config = json.loads(self._generate_config(collection_config_sections))
collection_configs = JsonParser(migrate_all_config, self.source_client).parse_json()
# Create a SchemaMigration instance and call migrate_schema
schema_migration = SchemaMigration()
schema_migration.migrate_schema(self.source_client, self.dest_client, collection_configs)
# Verify that the indexes were not optimized in the destination collection
dest_collection = self.dest_client[self.db_name]["test_optimize"]
dest_indexes = dest_collection.index_information()
self.assertIn("a_1_b_1_c_1_d_1", dest_indexes, "Compound index on 'a', 'b', 'c', 'd' was not migrated successfully.")
self.assertIn("a_1_b_1_c_1", dest_indexes, "Compound index on 'a', 'b', 'c' was not migrated successfully.")
self.assertIn("b_1_c_1", dest_indexes, "Compound index on 'b' and 'c' was not migrated successfully.")
self.assertIn("c_1_d_1", dest_indexes, "Compound index on 'c' and 'd' was not migrated successfully.")
self.assertIn("b_1_d_1", dest_indexes, "Compound index on 'b' and 'd' was not migrated successfully.")
self.assertIn("d_1", dest_indexes, "Compound index on 'd' was not migrated successfully.")
def test_optimize_compound_indexes_filters_index_with_options(self):
"""
Test that the migrate_schema method doesn't optimizes compound indexes when it has options.
"""
# Create the source collection and index information
self.source_client[self.db_name].command({
'customAction': 'CreateCollection',
'collection': 'test_optimize',
'indexes': [{ 'key': { 'a': 1, 'b': 1, 'c': 1 }, 'name': 'a_1_b_1_c_1', 'unique': True }]
})
self.source_client[self.db_name]['test_optimize'].create_index([("a", 1), ("b", 1), ("c", 1), ("d", 1)])
collection_config_sections = []
collection_config_sections.append(CollectionConfigSection([f'{self.db_name}.*'], [], False, False, True))
migrate_all_config = json.loads(self._generate_config(collection_config_sections))
collection_configs = JsonParser(migrate_all_config, self.source_client).parse_json()
# Create a SchemaMigration instance and call migrate_schema
schema_migration = SchemaMigration()
schema_migration.migrate_schema(self.source_client, self.dest_client, collection_configs)
# Verify that the indexes were created in the destination collection
dest_collection = self.dest_client[self.db_name]["test_optimize"]
dest_indexes = dest_collection.index_information()
self.assertIn("a_1_b_1_c_1_d_1", dest_indexes, "Compound index on 'a', 'b', 'c', 'd' was not migrated successfully.")
self.assertIn("a_1_b_1_c_1", dest_indexes, "Compound index on 'a', 'b', 'c' was not migrated successfully.")
self.assertEqual(dest_indexes["a_1_b_1_c_1"]["unique"], True, "Unique option is not set.")
def test_optimize_compound_indexes_filters_index_with_other_indexes(self):
"""
Test that the migrate_schema method doesn't optimizes compound indexes when it has options.
"""
# Create the source collection and index information
self.source_client[self.db_name].command({
'customAction': 'CreateCollection',
'collection': 'test_optimize',
'indexes': [
{ 'key': { 'a': 1, 'b': 1, 'c': 1 }, 'name': 'a_1_b_1_c_1', 'unique': True },
{ 'key': { 'b': 1, 'c': 1 }, 'name': 'b_1_c_1', 'unique': True, 'partialFilterExpression': {"a": {"$gt": 0}}}
]
})
source_collection = self.source_client[self.db_name]['test_optimize']
source_collection.create_index([("a", 1), ("b", 1), ("c", 1), ("d", 1)])
source_collection.create_index([("a", 1), ("b", 1)])
source_collection.create_index([("d", 1)], expireAfterSeconds=10)
collection_config_sections = []
collection_config_sections.append(CollectionConfigSection([f'{self.db_name}.*'], [], False, False, True))
migrate_all_config = json.loads(self._generate_config(collection_config_sections))
collection_configs = JsonParser(migrate_all_config, self.source_client).parse_json()
# Create a SchemaMigration instance and call migrate_schema
schema_migration = SchemaMigration()
schema_migration.migrate_schema(self.source_client, self.dest_client, collection_configs)
# Verify that the indexes in destination
dest_collection = self.dest_client[self.db_name]["test_optimize"]
dest_indexes = dest_collection.index_information()
self.assertIn("a_1_b_1_c_1_d_1", dest_indexes, "Compound index on 'a', 'b', 'c', 'd' was not migrated successfully.")
self.assertIn("a_1_b_1_c_1", dest_indexes, "Compound index on 'a', 'b', 'c' was not migrated successfully.")
self.assertEqual(dest_indexes["a_1_b_1_c_1"]["unique"], True, "Unique option is not set.")
self.assertIn("b_1_c_1", dest_indexes, "Compound index on 'b' and 'c' was not migrated successfully.")
self.assertTrue("partialFilterExpression" in dest_indexes["b_1_c_1"], "Partial filter expression is not set.")
self.assertIn("d_1", dest_indexes, "Compound index on 'd' was not migrated successfully.")
self.assertEqual(dest_indexes["d_1"]["expireAfterSeconds"], 10, "TTL index on 'd' field has incorrect expireAfterSeconds.")
def test_ts_ttl_throws_error(self):
"""
Test that the migrate_schema method throws an error when a TTL index is created on a _ts field.
"""
# Create the source collection and _ts ttl index information
source_collection = self.source_client[self.db_name]["test_ttl"]
source_collection.create_index([("_ts", 1)], expireAfterSeconds=10)
collection_config_sections = []
collection_config_sections.append(CollectionConfigSection([f'{self.db_name}.*'], [], False, False, False))
migrate_all_config = json.loads(self._generate_config(collection_config_sections))
collection_configs = JsonParser(migrate_all_config, self.source_client).parse_json()
# Create a SchemaMigration instance and call migrate_schema
schema_migration = SchemaMigration()
with self.assertRaises(ValueError):
schema_migration.migrate_schema(self.source_client, self.dest_client, collection_configs)
def test_ttl_index_migration(self):
"""
Test that the migrate_schema method is successfulwhen a TTL index is not on a _ts field.
"""
# Create the source collection and ttl index information
source_collection = self.source_client[self.db_name]["test_ttl"]
source_collection.create_index([("abc", 1)], expireAfterSeconds=10)
collection_config_sections = []
collection_config_sections.append(CollectionConfigSection([f'{self.db_name}.*'], [], False, False, False))
migrate_all_config = json.loads(self._generate_config(collection_config_sections))
collection_configs = JsonParser(migrate_all_config, self.source_client).parse_json()
# Create a SchemaMigration instance and call migrate_schema
schema_migration = SchemaMigration()
schema_migration.migrate_schema(self.source_client, self.dest_client, collection_configs)
# Verify that the index was created in the destination collection
dest_collection = self.dest_client[self.db_name]["test_ttl"]
dest_indexes = dest_collection.index_information()
self.assertIn("abc_1", dest_indexes, "TTL index on 'abc' field was not migrated successfully.")
dest_index_info = dest_indexes["abc_1"]
self.assertEqual(dest_index_info["expireAfterSeconds"], 10, "TTL index on 'abc' field has incorrect expireAfterSeconds.")
def test_migrate_shard_key_set_true(self):
"""
Test that the migrate_schema method correctly migrates the shard key.
"""
# Create sharded source collection
self.source_client[self.db_name].command({
'customAction': 'CreateCollection',
'collection': 'test_shard_key',
'shardKey': '_id'
})
collection_config_sections = []
collection_config_sections.append(CollectionConfigSection([f'{self.db_name}.*'], [], True, False, False))
migrate_all_config = json.loads(self._generate_config(collection_config_sections))
collection_configs = JsonParser(migrate_all_config, self.source_client).parse_json()
# Create a SchemaMigration instance and call migrate_schema
schema_migration = SchemaMigration()
schema_migration.migrate_schema(self.source_client, self.dest_client, collection_configs)
# Verify that the shard key was migrated correctly
shard_key_info = self.dest_client[self.db_name].command('listCollections')['cursor']['firstBatch'][0]['info'].get('shardKey')
self.assertIsNotNone(shard_key_info, 'Shard key was not migrated to the destination collection.')
self.assertEqual(shard_key_info, {'_id': 'hashed'}, 'Shard key in the destination collection is incorrect.')
def test_migrate_shard_key_set_false(self):
"""
Test that the migrate_schema method doesn't migrate the shard key.
"""
# Create sharded source collection
self.source_client[self.db_name].command({
'customAction': 'CreateCollection',
'collection': 'test_shard_key',
'shardKey': '_id'
})
collection_config_sections = []
collection_config_sections.append(CollectionConfigSection([f'{self.db_name}.*'], [], False, False, False))
migrate_all_config = json.loads(self._generate_config(collection_config_sections))
collection_configs = JsonParser(migrate_all_config, self.source_client).parse_json()
# Create a SchemaMigration instance and call migrate_schema
schema_migration = SchemaMigration()
schema_migration.migrate_schema(self.source_client, self.dest_client, collection_configs)
# Verify that the shard key was not migrated
shard_key_info = self.dest_client[self.db_name].command('listCollections')['cursor']['firstBatch'][0]['info'].get('shardKey')
self.assertIsNone(shard_key_info, 'Shard key was migrated to the destination collection.')
def test_drop_if_exists_set_true(self):
"""
Test that the migrate_schema method drops the collection in target.
"""
# Create the dest collection with an index
dest_collection = self.dest_client[self.db_name]["test_drop"]
dest_collection.create_index([("foo", 1)])
# Create the source collection with an index
source_collection = self.source_client[self.db_name]["test_drop"]
source_collection.create_index([("bar", 1)])
collection_config_sections = []
collection_config_sections.append(CollectionConfigSection([f'{self.db_name}.*'], [], False, True, False))
migrate_all_config = json.loads(self._generate_config(collection_config_sections))
collection_configs = JsonParser(migrate_all_config, self.source_client).parse_json()
# Create a SchemaMigration instance and call migrate_schema
schema_migration = SchemaMigration()
schema_migration.migrate_schema(self.source_client, self.dest_client, collection_configs)
# Verify that the target has been dropped and recreated
dest_collection = self.dest_client[self.db_name]["test_drop"]
dest_indexes = dest_collection.index_information()
self.assertIn("bar_1", dest_indexes, "bar_1 was not migrated successfully.")
self.assertNotIn("foo_1", dest_indexes, "foo_1 was not dropped.")
def test_drop_if_exists_set_false(self):
"""
Test that the migrate_schema method drops the collection in target.
"""
# Create the dest collection with an index
dest_collection = self.dest_client[self.db_name]["test_drop"]
dest_collection.create_index([("foo", 1)])
# Create the source collection with an index
source_collection = self.source_client[self.db_name]["test_drop"]
source_collection.create_index([("bar", 1)])
collection_config_sections = []
collection_config_sections.append(CollectionConfigSection([f'{self.db_name}.*'], [], False, False, False))
migrate_all_config = json.loads(self._generate_config(collection_config_sections))
collection_configs = JsonParser(migrate_all_config, self.source_client).parse_json()
# Create a SchemaMigration instance and call migrate_schema
schema_migration = SchemaMigration()
schema_migration.migrate_schema(self.source_client, self.dest_client, collection_configs)
# Verify that the target has not been dropped
dest_collection = self.dest_client[self.db_name]["test_drop"]
dest_indexes = dest_collection.index_information()
self.assertIn("bar_1", dest_indexes, "bar_1 was not migrated successfully.")
self.assertIn("foo_1", dest_indexes, "foo_1 was dropped.")
def test_verify_configurations_apply_within_sections(self):
"""
Test that the migrate_schema method applies configurations within sections.
"""
# Create the source collections and index information
source_collection_1 = self.source_client[self.db_name]["test_config_1"]
source_collection_1.create_index([("foo", 1)])
source_collection_2 = self.source_client[self.db_name]["test_config_2"]
source_collection_2.create_index([("foo", 1)])
# Create the destination collection
dest_collection_1 = self.dest_client[self.db_name]["test_config_1"]
dest_collection_1.create_index([("bar", 1)])
dest_collection_2 = self.dest_client[self.db_name]["test_config_2"]
dest_collection_2.create_index([("bar", 1)])
collection_config_sections = []
collection_config_sections.append(CollectionConfigSection([f'{self.db_name}.test_config_1'], [], False, True, False))
collection_config_sections.append(CollectionConfigSection([f'{self.db_name}.test_config_2'], [], False, False, False))
migrate_all_config = json.loads(self._generate_config(collection_config_sections))
collection_configs = JsonParser(migrate_all_config, self.source_client).parse_json()
# Create a SchemaMigration instance and call migrate_schema
schema_migration = SchemaMigration()
schema_migration.migrate_schema(self.source_client, self.dest_client, collection_configs)
# Verify that test_config_1 was dropped and test_config_2 was not
dest_collection_1 = self.dest_client[self.db_name]["test_config_1"]
dest_indexes_1 = dest_collection_1.index_information()
self.assertIn("foo_1", dest_indexes_1, "foo_1 was not migrated successfully.")
self.assertNotIn("bar_1", dest_indexes_1, "bar_1 was not dropped.")
dest_collection_2 = self.dest_client[self.db_name]["test_config_2"]
dest_indexes_2 = dest_collection_2.index_information()
self.assertIn("foo_1", dest_indexes_2, "foo_1 was not migrated successfully.")
self.assertIn("bar_1", dest_indexes_2, "bar_1 was dropped.")
@staticmethod
def _generate_random_string(length=10):
"""
Generate a random string of the specified length.
:param length: Length of the random string (default is 10)
:return: Randomly generated string
"""
characters = string.ascii_letters
return ''.join(random.choice(characters) for _ in range(length))
def _generate_config(
self,
collection_config_sections: List[CollectionConfigSection]) -> str:
"""
Generate a configuration dictionary for testing.
:param include: List of collections to include
:param exclude: List of collections to exclude
:param migrate_shard_key: Boolean for migrating shard key
:param drop_if_exists: Boolean for dropping collection if it exists
:param optimize_compound_indexes: Boolean for optimizing compound indexes
:return: Configuration dictionary
"""
collection_configs = {
"sections": []
}
for collection_config_section in collection_config_sections:
collection_configs["sections"].append({
"include": collection_config_section.include,
"exclude": collection_config_section.exclude,
"migrate_shard_key": str(collection_config_section.migrate_shard_key).lower(),
"drop_if_exists": str(collection_config_section.drop_if_exists).lower(),
"optimize_compound_indexes": str(collection_config_section.optimize_compound_indexes).lower()
})
return json.dumps(collection_configs)
unittest.main()