Skip to content

Commit 87951c3

Browse files
committed
PYTHON-1674 Refactor spec test runner for transactions
This enables us to reuse the transaction spec runner for tesing retryable reads.
1 parent 7f4c504 commit 87951c3

File tree

5 files changed

+558
-500
lines changed

5 files changed

+558
-500
lines changed

test/test_crud_v1.py

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -48,51 +48,44 @@ class TestAllScenarios(IntegrationTest):
4848
pass
4949

5050

51-
def check_result(expected_result, result):
52-
if isinstance(result, Cursor) or isinstance(result, CommandCursor):
53-
return list(result) == expected_result
54-
55-
elif isinstance(result, _WriteResult):
51+
def check_result(self, expected_result, result):
52+
if isinstance(result, _WriteResult):
5653
for res in expected_result:
5754
prop = camel_to_snake(res)
55+
msg = "%s : %r != %r" % (prop, expected_result, result)
5856
# SPEC-869: Only BulkWriteResult has upserted_count.
59-
if (prop == "upserted_count" and
60-
not isinstance(result, BulkWriteResult)):
57+
if (prop == "upserted_count"
58+
and not isinstance(result, BulkWriteResult)):
6159
if result.upserted_id is not None:
6260
upserted_count = 1
6361
else:
6462
upserted_count = 0
65-
if upserted_count != expected_result[res]:
66-
return False
63+
self.assertEqual(upserted_count, expected_result[res], msg)
6764
elif prop == "inserted_ids":
6865
# BulkWriteResult does not have inserted_ids.
6966
if isinstance(result, BulkWriteResult):
70-
if len(expected_result[res]) != result.inserted_count:
71-
return False
67+
self.assertEqual(len(expected_result[res]),
68+
result.inserted_count)
7269
else:
7370
# InsertManyResult may be compared to [id1] from the
7471
# crud spec or {"0": id1} from the retryable write spec.
7572
ids = expected_result[res]
7673
if isinstance(ids, dict):
7774
ids = [ids[str(i)] for i in range(len(ids))]
78-
if ids != result.inserted_ids:
79-
return False
75+
self.assertEqual(ids, result.inserted_ids, msg)
8076
elif prop == "upserted_ids":
8177
# Convert indexes from strings to integers.
8278
ids = expected_result[res]
8379
expected_ids = {}
8480
for str_index in ids:
8581
expected_ids[int(str_index)] = ids[str_index]
86-
if expected_ids != result.upserted_ids:
87-
return False
88-
elif getattr(result, prop) != expected_result[res]:
89-
return False
90-
return True
82+
self.assertEqual(expected_ids, result.upserted_ids, msg)
83+
else:
84+
self.assertEqual(
85+
getattr(result, prop), expected_result[res], msg)
86+
9187
else:
92-
if expected_result is None:
93-
return result is None
94-
else:
95-
return result == expected_result
88+
self.assertEqual(result, expected_result)
9689

9790

9891
def run_operation(collection, test):
@@ -138,7 +131,11 @@ def run_operation(collection, test):
138131
if operation == "aggregate":
139132
if arguments["pipeline"] and "$out" in arguments["pipeline"][-1]:
140133
out = collection.database[arguments["pipeline"][-1]["$out"]]
141-
return out.find()
134+
result = out.find()
135+
136+
if isinstance(result, Cursor) or isinstance(result, CommandCursor):
137+
return list(result)
138+
142139
return result
143140

144141

@@ -160,7 +157,7 @@ def run_scenario(self):
160157
run_operation(self.db.test, test)
161158
else:
162159
result = run_operation(self.db.test, test)
163-
self.assertTrue(check_result(expected_result, result))
160+
check_result(self, expected_result, result)
164161

165162
# Assert final state is expected.
166163
expected_c = test['outcome'].get('collection')

test/test_retryable_writes.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,7 @@ def run_scenario(self):
123123
# We can't test the expected result when the test should fail because
124124
# the BulkWriteResult is not reported when raising a network error.
125125
if not should_fail:
126-
self.assertTrue(check_result(expected_result, result),
127-
"%r != %r" % (expected_result, result))
126+
check_result(self, expected_result, result)
128127

129128
return run_scenario
130129

0 commit comments

Comments
 (0)