|
| 1 | +# Copyright 2021-present MongoDB, Inc. |
| 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 | +from __future__ import annotations |
| 15 | + |
| 16 | +import sys |
| 17 | +import unittest |
| 18 | + |
| 19 | +sys.path[0:0] = [""] |
| 20 | + |
| 21 | +from test.asynchronous import AsyncIntegrationTest |
| 22 | +from test.asynchronous.unified_format import UnifiedSpecTestMixinV1 |
| 23 | + |
| 24 | +_IS_SYNC = False |
| 25 | + |
| 26 | + |
| 27 | +class TestCreateEntities(AsyncIntegrationTest): |
| 28 | + async def test_store_events_as_entities(self): |
| 29 | + self.scenario_runner = UnifiedSpecTestMixinV1() |
| 30 | + spec = { |
| 31 | + "description": "blank", |
| 32 | + "schemaVersion": "1.2", |
| 33 | + "createEntities": [ |
| 34 | + { |
| 35 | + "client": { |
| 36 | + "id": "client0", |
| 37 | + "storeEventsAsEntities": [ |
| 38 | + { |
| 39 | + "id": "events1", |
| 40 | + "events": [ |
| 41 | + "PoolCreatedEvent", |
| 42 | + ], |
| 43 | + } |
| 44 | + ], |
| 45 | + } |
| 46 | + }, |
| 47 | + ], |
| 48 | + "tests": [{"description": "foo", "operations": []}], |
| 49 | + } |
| 50 | + self.scenario_runner.TEST_SPEC = spec |
| 51 | + await self.scenario_runner.asyncSetUp() |
| 52 | + await self.scenario_runner.run_scenario(spec["tests"][0]) |
| 53 | + await self.scenario_runner.entity_map["client0"].close() |
| 54 | + final_entity_map = self.scenario_runner.entity_map |
| 55 | + self.assertIn("events1", final_entity_map) |
| 56 | + self.assertGreater(len(final_entity_map["events1"]), 0) |
| 57 | + for event in final_entity_map["events1"]: |
| 58 | + self.assertIn("PoolCreatedEvent", event["name"]) |
| 59 | + |
| 60 | + async def test_store_all_others_as_entities(self): |
| 61 | + self.scenario_runner = UnifiedSpecTestMixinV1() |
| 62 | + spec = { |
| 63 | + "description": "Find", |
| 64 | + "schemaVersion": "1.2", |
| 65 | + "createEntities": [ |
| 66 | + { |
| 67 | + "client": { |
| 68 | + "id": "client0", |
| 69 | + "uriOptions": {"retryReads": True}, |
| 70 | + } |
| 71 | + }, |
| 72 | + {"database": {"id": "database0", "client": "client0", "databaseName": "dat"}}, |
| 73 | + { |
| 74 | + "collection": { |
| 75 | + "id": "collection0", |
| 76 | + "database": "database0", |
| 77 | + "collectionName": "dat", |
| 78 | + } |
| 79 | + }, |
| 80 | + ], |
| 81 | + "tests": [ |
| 82 | + { |
| 83 | + "description": "test loops", |
| 84 | + "operations": [ |
| 85 | + { |
| 86 | + "name": "loop", |
| 87 | + "object": "testRunner", |
| 88 | + "arguments": { |
| 89 | + "storeIterationsAsEntity": "iterations", |
| 90 | + "storeSuccessesAsEntity": "successes", |
| 91 | + "storeFailuresAsEntity": "failures", |
| 92 | + "storeErrorsAsEntity": "errors", |
| 93 | + "numIterations": 5, |
| 94 | + "operations": [ |
| 95 | + { |
| 96 | + "name": "insertOne", |
| 97 | + "object": "collection0", |
| 98 | + "arguments": {"document": {"_id": 1, "x": 44}}, |
| 99 | + }, |
| 100 | + { |
| 101 | + "name": "insertOne", |
| 102 | + "object": "collection0", |
| 103 | + "arguments": {"document": {"_id": 2, "x": 44}}, |
| 104 | + }, |
| 105 | + ], |
| 106 | + }, |
| 107 | + } |
| 108 | + ], |
| 109 | + } |
| 110 | + ], |
| 111 | + } |
| 112 | + |
| 113 | + await self.client.dat.dat.delete_many({}) |
| 114 | + self.scenario_runner.TEST_SPEC = spec |
| 115 | + await self.scenario_runner.asyncSetUp() |
| 116 | + await self.scenario_runner.run_scenario(spec["tests"][0]) |
| 117 | + await self.scenario_runner.entity_map["client0"].close() |
| 118 | + entity_map = self.scenario_runner.entity_map |
| 119 | + self.assertEqual(len(entity_map["errors"]), 4) |
| 120 | + for error in entity_map["errors"]: |
| 121 | + self.assertEqual(error["type"], "DuplicateKeyError") |
| 122 | + self.assertEqual(entity_map["failures"], []) |
| 123 | + self.assertEqual(entity_map["successes"], 2) |
| 124 | + self.assertEqual(entity_map["iterations"], 5) |
| 125 | + |
| 126 | + |
| 127 | +if __name__ == "__main__": |
| 128 | + unittest.main() |
0 commit comments