Skip to content

Commit 3a3aaee

Browse files
committed
PYTHON-2033 Unified Test Format (#519)
(cherry picked from commit 6b01235)
1 parent 3e60563 commit 3a3aaee

File tree

166 files changed

+8646
-94
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

166 files changed

+8646
-94
lines changed

test/__init__.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -613,6 +613,16 @@ def is_topology_type(self, topologies):
613613
return True
614614
if 'sharded' in topologies and self.is_mongos:
615615
return True
616+
if 'sharded-replicaset' in topologies and self.is_mongos:
617+
shards = list(client_context.client.config.shards.find())
618+
for shard in shards:
619+
# For a 3-member RS-backed sharded cluster, shard['host']
620+
# will be 'replicaName/ip1:port1,ip2:port2,ip3:port3'
621+
# Otherwise it will be 'ip1:port1'
622+
host_spec = shard['host']
623+
if not len(host_spec.split('/')) > 1:
624+
return False
625+
return True
616626
return False
617627

618628
def require_cluster_type(self, topologies=[]):

test/test_unified_format.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# Copyright 2020-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+
15+
import os
16+
import sys
17+
18+
sys.path[0:0] = [""]
19+
20+
from bson import ObjectId
21+
22+
from test import unittest
23+
from test.unified_format import generate_test_classes, MatchEvaluatorUtil
24+
25+
26+
_TEST_PATH = os.path.join(
27+
os.path.dirname(os.path.realpath(__file__)), 'unified-test-format')
28+
29+
30+
globals().update(generate_test_classes(
31+
os.path.join(_TEST_PATH, 'valid-pass'),
32+
module=__name__,
33+
class_name_prefix='UnifiedTestFormat',
34+
expected_failures=[
35+
'Client side error in command starting transaction', # PYTHON-1894
36+
]))
37+
38+
39+
globals().update(generate_test_classes(
40+
os.path.join(_TEST_PATH, 'valid-fail'),
41+
module=__name__,
42+
class_name_prefix='UnifiedTestFormat',
43+
bypass_test_generation_errors=True,
44+
expected_failures=[
45+
'.*', # All tests expected to fail
46+
]))
47+
48+
49+
class TestMatchEvaluatorUtil(unittest.TestCase):
50+
def setUp(self):
51+
self.match_evaluator = MatchEvaluatorUtil(self)
52+
53+
def test_unsetOrMatches(self):
54+
spec = {'$$unsetOrMatches': {'y': {'$$unsetOrMatches': 2}}}
55+
for actual in [{}, {'y': 2}, None]:
56+
self.match_evaluator.match_result(spec, actual)
57+
58+
spec = {'x': {'$$unsetOrMatches': {'y': {'$$unsetOrMatches': 2}}}}
59+
for actual in [{}, {'x': {}}, {'x': {'y': 2}}]:
60+
self.match_evaluator.match_result(spec, actual)
61+
62+
def test_type(self):
63+
self.match_evaluator.match_result(
64+
{'operationType': 'insert',
65+
'ns': {'db': 'change-stream-tests', 'coll': 'test'},
66+
'fullDocument': {'_id': {'$$type': 'objectId'}, 'x': 1}},
67+
{'operationType': 'insert',
68+
'fullDocument': {'_id': ObjectId('5fc93511ac93941052098f0c'), 'x': 1},
69+
'ns': {'db': 'change-stream-tests', 'coll': 'test'}})
70+
71+
72+
if __name__ == "__main__":
73+
unittest.main()
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
{
2+
"description": "example-insertOne",
3+
"schemaVersion": "1.0",
4+
"runOnRequirements": [
5+
{
6+
"minServerVersion": "2.6"
7+
}
8+
],
9+
"createEntities": [
10+
{
11+
"client": {
12+
"id": "client0",
13+
"observeEvents": [
14+
"commandStartedEvent"
15+
]
16+
}
17+
},
18+
{
19+
"database": {
20+
"id": "database0",
21+
"client": "client0",
22+
"databaseName": "test"
23+
}
24+
},
25+
{
26+
"collection": {
27+
"id": "collection0",
28+
"database": "database0",
29+
"collectionName": "coll"
30+
}
31+
}
32+
],
33+
"initialData": [
34+
{
35+
"collectionName": "coll",
36+
"databaseName": "test",
37+
"documents": [
38+
{
39+
"_id": 1
40+
}
41+
]
42+
}
43+
],
44+
"tests": [
45+
{
46+
"description": "insertOne",
47+
"operations": [
48+
{
49+
"object": "collection0",
50+
"name": "insertOne",
51+
"arguments": {
52+
"document": {
53+
"_id": 2
54+
}
55+
},
56+
"expectResult": {
57+
"insertedId": {
58+
"$$unsetOrMatches": 2
59+
}
60+
}
61+
}
62+
],
63+
"expectEvents": [
64+
{
65+
"client": "client0",
66+
"events": [
67+
{
68+
"commandStartedEvent": {
69+
"commandName": "insert",
70+
"databaseName": "test",
71+
"command": {
72+
"insert": "coll",
73+
"documents": [
74+
{
75+
"_id": 2
76+
}
77+
]
78+
}
79+
}
80+
}
81+
]
82+
}
83+
],
84+
"outcome": [
85+
{
86+
"collectionName": "coll",
87+
"databaseName": "test",
88+
"documents": [
89+
{
90+
"_id": 1
91+
},
92+
{
93+
"_id": 2
94+
}
95+
]
96+
}
97+
]
98+
}
99+
]
100+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{
2+
"description": "collectionData-additionalProperties",
3+
"schemaVersion": "1.0",
4+
"createEntities": [
5+
{
6+
"client": {
7+
"id": "client0"
8+
}
9+
},
10+
{
11+
"database": {
12+
"id": "database0",
13+
"client": "client0",
14+
"databaseName": "foo"
15+
}
16+
},
17+
{
18+
"collection": {
19+
"id": "collection0",
20+
"database": "database0",
21+
"collectionName": "foo",
22+
"foo": 0
23+
}
24+
}
25+
],
26+
"initialData": [
27+
{
28+
"collectionName": "foo",
29+
"databaseName": "foo",
30+
"documents": [],
31+
"foo": 0
32+
}
33+
],
34+
"tests": [
35+
{
36+
"description": "foo",
37+
"operations": []
38+
}
39+
]
40+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"description": "collectionData-collectionName-required",
3+
"schemaVersion": "1.0",
4+
"createEntities": [
5+
{
6+
"client": {
7+
"id": "client0"
8+
}
9+
},
10+
{
11+
"database": {
12+
"id": "database0",
13+
"client": "client0",
14+
"databaseName": "foo"
15+
}
16+
},
17+
{
18+
"collection": {
19+
"id": "collection0",
20+
"database": "database0",
21+
"collectionName": "foo",
22+
"foo": 0
23+
}
24+
}
25+
],
26+
"initialData": [
27+
{
28+
"databaseName": "foo",
29+
"documents": []
30+
}
31+
],
32+
"tests": [
33+
{
34+
"description": "foo",
35+
"operations": []
36+
}
37+
]
38+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
{
2+
"description": "collectionData-collectionName-type",
3+
"schemaVersion": "1.0",
4+
"createEntities": [
5+
{
6+
"client": {
7+
"id": "client0"
8+
}
9+
},
10+
{
11+
"database": {
12+
"id": "database0",
13+
"client": "client0",
14+
"databaseName": "foo"
15+
}
16+
},
17+
{
18+
"collection": {
19+
"id": "collection0",
20+
"database": "database0",
21+
"collectionName": "foo",
22+
"foo": 0
23+
}
24+
}
25+
],
26+
"initialData": [
27+
{
28+
"collectionName": 0,
29+
"databaseName": "foo",
30+
"documents": []
31+
}
32+
],
33+
"tests": [
34+
{
35+
"description": "foo",
36+
"operations": []
37+
}
38+
]
39+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"description": "collectionData-databaseName-required",
3+
"schemaVersion": "1.0",
4+
"createEntities": [
5+
{
6+
"client": {
7+
"id": "client0"
8+
}
9+
},
10+
{
11+
"database": {
12+
"id": "database0",
13+
"client": "client0",
14+
"databaseName": "foo"
15+
}
16+
},
17+
{
18+
"collection": {
19+
"id": "collection0",
20+
"database": "database0",
21+
"collectionName": "foo",
22+
"foo": 0
23+
}
24+
}
25+
],
26+
"initialData": [
27+
{
28+
"collectionName": "foo",
29+
"documents": []
30+
}
31+
],
32+
"tests": [
33+
{
34+
"description": "foo",
35+
"operations": []
36+
}
37+
]
38+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
{
2+
"description": "collectionData-databaseName-type",
3+
"schemaVersion": "1.0",
4+
"createEntities": [
5+
{
6+
"client": {
7+
"id": "client0"
8+
}
9+
},
10+
{
11+
"database": {
12+
"id": "database0",
13+
"client": "client0",
14+
"databaseName": "foo"
15+
}
16+
},
17+
{
18+
"collection": {
19+
"id": "collection0",
20+
"database": "database0",
21+
"collectionName": "foo",
22+
"foo": 0
23+
}
24+
}
25+
],
26+
"initialData": [
27+
{
28+
"collectionName": "foo",
29+
"databaseName": 0,
30+
"documents": []
31+
}
32+
],
33+
"tests": [
34+
{
35+
"description": "foo",
36+
"operations": []
37+
}
38+
]
39+
}

0 commit comments

Comments
 (0)