|
| 1 | +# pylint: disable=missing-docstring,redefined-outer-name |
| 2 | +import pytest |
| 3 | +import testing |
| 4 | +from plm import Runner |
| 5 | +from pymongo import MongoClient |
| 6 | + |
| 7 | + |
| 8 | +def perform_with_options(source, plm, phase: Runner.Phase, include_ns=None, exclude_ns=None): |
| 9 | + """Perform the PLM operation with the given options.""" |
| 10 | + plm_options = {} |
| 11 | + if include_ns: |
| 12 | + plm_options["include_namespaces"] = include_ns |
| 13 | + if exclude_ns: |
| 14 | + plm_options["exclude_namespaces"] = exclude_ns |
| 15 | + |
| 16 | + return Runner(source, plm, phase, plm_options) |
| 17 | + |
| 18 | + |
| 19 | +def check_if_target_is_subset(source: MongoClient, target: MongoClient): |
| 20 | + """Check if the target MongoDB is a subset of the source MongoDB.""" |
| 21 | + source_dbs = set(testing.list_databases(source)) |
| 22 | + target_dbs = set(testing.list_databases(target)) |
| 23 | + assert set(target_dbs).issubset(source_dbs) |
| 24 | + |
| 25 | + for db in target_dbs: |
| 26 | + source_colls = set(testing.list_collections(source, db)) |
| 27 | + target_colls = set(testing.list_collections(target, db)) |
| 28 | + assert set(target_colls).issubset(source_colls) |
| 29 | + |
| 30 | + for coll in target_colls: |
| 31 | + testing.compare_namespace(source, target, db, coll) |
| 32 | + |
| 33 | + |
| 34 | +@pytest.mark.parametrize("phase", [Runner.Phase.APPLY, Runner.Phase.CLONE]) |
| 35 | +def test_create_collection_with_include_only(t: testing.Testing, phase: Runner.Phase): |
| 36 | + with perform_with_options( |
| 37 | + t.source, |
| 38 | + t.plm, |
| 39 | + phase, |
| 40 | + include_ns=["db_0.*", "db_1.coll_0", "db_1.coll_1", "db_2.coll_0", "db_2.coll_1"], |
| 41 | + ): |
| 42 | + for db in range(3): |
| 43 | + for coll in range(3): |
| 44 | + t.source["db_1"]["coll_1"].create_index({"i": 1}) |
| 45 | + t.source.admin.command("shardCollection", f"db_{db}.coll_{coll}", key={"_id": 1}) |
| 46 | + t.source[f"db_{db}"][f"coll_{coll}"].insert_one({}) |
| 47 | + |
| 48 | + expected = { |
| 49 | + "db_0.coll_0", |
| 50 | + "db_0.coll_1", |
| 51 | + "db_0.coll_2", |
| 52 | + "db_1.coll_0", |
| 53 | + "db_1.coll_1", |
| 54 | + # "db_1.coll_2", |
| 55 | + "db_2.coll_0", |
| 56 | + "db_2.coll_1", |
| 57 | + # "db_2.coll_2", |
| 58 | + } |
| 59 | + |
| 60 | + assert expected == set(testing.list_all_namespaces(t.target)) |
| 61 | + check_if_target_is_subset(t.source, t.target) |
| 62 | + |
| 63 | + |
| 64 | +@pytest.mark.parametrize("phase", [Runner.Phase.APPLY, Runner.Phase.CLONE]) |
| 65 | +def test_create_collection_with_exclude_only(t: testing.Testing, phase: Runner.Phase): |
| 66 | + with perform_with_options( |
| 67 | + t.source, t.plm, phase, exclude_ns=["db_0.*", "db_1.coll_0", "db_1.coll_1"] |
| 68 | + ): |
| 69 | + for db in range(3): |
| 70 | + for coll in range(3): |
| 71 | + t.source["db_1"]["coll_1"].create_index({"i": 1}) |
| 72 | + t.source.admin.command("shardCollection", f"db_{db}.coll_{coll}", key={"_id": 1}) |
| 73 | + t.source[f"db_{db}"][f"coll_{coll}"].insert_one({}) |
| 74 | + |
| 75 | + expected = { |
| 76 | + # "db_0.coll_0", |
| 77 | + # "db_0.coll_1", |
| 78 | + # "db_0.coll_2", |
| 79 | + # "db_1.coll_0", |
| 80 | + # "db_1.coll_1", |
| 81 | + "db_1.coll_2", |
| 82 | + "db_2.coll_0", |
| 83 | + "db_2.coll_1", |
| 84 | + "db_2.coll_2", |
| 85 | + } |
| 86 | + |
| 87 | + assert expected == set(testing.list_all_namespaces(t.target)) |
| 88 | + check_if_target_is_subset(t.source, t.target) |
| 89 | + |
| 90 | + |
| 91 | +@pytest.mark.parametrize("phase", [Runner.Phase.APPLY, Runner.Phase.CLONE]) |
| 92 | +def test_create_collection(t: testing.Testing, phase: Runner.Phase): |
| 93 | + with perform_with_options( |
| 94 | + t.source, |
| 95 | + t.plm, |
| 96 | + phase, |
| 97 | + include_ns=["db_0.*", "db_1.coll_0", "db_1.coll_1", "db_2.coll_0", "db_2.coll_1"], |
| 98 | + exclude_ns=["db_0.*", "db_1.coll_0", "db_3.coll_1"], |
| 99 | + ): |
| 100 | + for db in range(4): |
| 101 | + for coll in range(3): |
| 102 | + t.source["db_1"]["coll_1"].create_index({"i": 1}) |
| 103 | + t.source.admin.command("shardCollection", f"db_{db}.coll_{coll}", key={"_id": 1}) |
| 104 | + t.source[f"db_{db}"][f"coll_{coll}"].insert_one({}) |
| 105 | + |
| 106 | + expected = { |
| 107 | + # "db_0.coll_0", |
| 108 | + # "db_0.coll_1", |
| 109 | + # "db_0.coll_2", |
| 110 | + |
| 111 | + # "db_1.coll_0", |
| 112 | + "db_1.coll_1", |
| 113 | + # "db_1.coll_2", |
| 114 | + |
| 115 | + "db_2.coll_0", |
| 116 | + "db_2.coll_1", |
| 117 | + # "db_2.coll_2", |
| 118 | + |
| 119 | + "db_3.coll_0", |
| 120 | + # "db_3.coll_1", |
| 121 | + "db_3.coll_2", |
| 122 | + } |
| 123 | + |
| 124 | + assert expected == set(testing.list_all_namespaces(t.target)) |
| 125 | + check_if_target_is_subset(t.source, t.target) |
0 commit comments