Skip to content

Commit 4ac299f

Browse files
committed
PYTHON-2827 Versioned API migration example for ecosystem docs (#687)
(cherry picked from commit f86b2c6)
1 parent caf9b32 commit 4ac299f

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

test/test_examples.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1137,6 +1137,47 @@ def test_versioned_api(self):
11371137
uri, server_api=ServerApi("1", deprecation_errors=True))
11381138
# End Versioned API Example 4
11391139

1140+
@client_context.require_version_min(4, 7)
1141+
def test_versioned_api_migration(self):
1142+
# SERVER-58785
1143+
if (client_context.is_topology_type(["sharded"]) and
1144+
not client_context.version.at_least(5, 0, 2)):
1145+
self.skipTest("This test needs MongoDB 5.0.2 or newer")
1146+
1147+
client = rs_client(server_api=ServerApi("1", strict=True))
1148+
client.db.sales.drop()
1149+
1150+
# Start Versioned API Example 5
1151+
def strptime(s):
1152+
return datetime.datetime.strptime(s, "%Y-%m-%dT%H:%M:%SZ")
1153+
client.db.sales.insert_many([
1154+
{"_id": 1, "item": "abc", "price": 10, "quantity": 2, "date": strptime("2021-01-01T08:00:00Z")},
1155+
{"_id": 2, "item": "jkl", "price": 20, "quantity": 1, "date": strptime("2021-02-03T09:00:00Z")},
1156+
{"_id": 3, "item": "xyz", "price": 5, "quantity": 5, "date": strptime("2021-02-03T09:05:00Z")},
1157+
{"_id": 4, "item": "abc", "price": 10, "quantity": 10, "date": strptime("2021-02-15T08:00:00Z")},
1158+
{"_id": 5, "item": "xyz", "price": 5, "quantity": 10, "date": strptime("2021-02-15T09:05:00Z")},
1159+
{"_id": 6, "item": "xyz", "price": 5, "quantity": 5, "date": strptime("2021-02-15T12:05:10Z")},
1160+
{"_id": 7, "item": "xyz", "price": 5, "quantity": 10, "date": strptime("2021-02-15T14:12:12Z")},
1161+
{"_id": 8, "item": "abc", "price": 10, "quantity": 5, "date": strptime("2021-03-16T20:20:13Z")}
1162+
])
1163+
# End Versioned API Example 5
1164+
1165+
with self.assertRaisesRegex(
1166+
OperationFailure, "Provided apiStrict:true, but the command "
1167+
"count is not in API Version 1"):
1168+
client.db.command('count', 'sales', query={})
1169+
# Start Versioned API Example 6
1170+
# pymongo.errors.OperationFailure: Provided apiStrict:true, but the command count is not in API Version 1, full error: {'ok': 0.0, 'errmsg': 'Provided apiStrict:true, but the command count is not in API Version 1', 'code': 323, 'codeName': 'APIStrictError'}
1171+
# End Versioned API Example 6
1172+
1173+
# Start Versioned API Example 7
1174+
client.db.sales.count_documents({})
1175+
# End Versioned API Example 7
1176+
1177+
# Start Versioned API Example 8
1178+
# 8
1179+
# End Versioned API Example 8
1180+
11401181

11411182
if __name__ == "__main__":
11421183
unittest.main()

0 commit comments

Comments
 (0)