Skip to content

PYTHON-5308 Remove SON from doc examples #2280

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 18 additions & 26 deletions test/asynchronous/test_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,54 +182,50 @@ async def test_query_embedded_documents(self):
db = self.db

# Start Example 14
# Subdocument key order matters in a few of these examples so we have
# to use bson.son.SON instead of a Python dict.
from bson.son import SON

await db.inventory.insert_many(
[
{
"item": "journal",
"qty": 25,
"size": SON([("h", 14), ("w", 21), ("uom", "cm")]),
"size": {"h": 14, "w": 21, "uom": "cm"},
"status": "A",
},
{
"item": "notebook",
"qty": 50,
"size": SON([("h", 8.5), ("w", 11), ("uom", "in")]),
"size": {"h": 8.5, "w": 11, "uom": "in"},
"status": "A",
},
{
"item": "paper",
"qty": 100,
"size": SON([("h", 8.5), ("w", 11), ("uom", "in")]),
"size": {"h": 8.5, "w": 11, "uom": "in"},
"status": "D",
},
{
"item": "planner",
"qty": 75,
"size": SON([("h", 22.85), ("w", 30), ("uom", "cm")]),
"size": {"h": 22.85, "w": 30, "uom": "cm"},
"status": "D",
},
{
"item": "postcard",
"qty": 45,
"size": SON([("h", 10), ("w", 15.25), ("uom", "cm")]),
"size": {"h": 10, "w": 15.25, "uom": "cm"},
"status": "A",
},
]
)
# End Example 14

# Start Example 15
cursor = db.inventory.find({"size": SON([("h", 14), ("w", 21), ("uom", "cm")])})
cursor = db.inventory.find({"size": {"h": 14, "w": 21, "uom": "cm"}})
# End Example 15

self.assertEqual(len(await cursor.to_list()), 1)

# Start Example 16
cursor = db.inventory.find({"size": SON([("w", 21), ("h", 14), ("uom", "cm")])})
cursor = db.inventory.find({"size": {"w": 21, "h": 14, "uom": "cm"}})
# End Example 16

self.assertEqual(len(await cursor.to_list()), 0)
Expand Down Expand Up @@ -324,53 +320,49 @@ async def test_query_array_of_documents(self):
db = self.db

# Start Example 29
# Subdocument key order matters in a few of these examples so we have
# to use bson.son.SON instead of a Python dict.
from bson.son import SON

await db.inventory.insert_many(
[
{
"item": "journal",
"instock": [
SON([("warehouse", "A"), ("qty", 5)]),
SON([("warehouse", "C"), ("qty", 15)]),
{"warehouse": "A", "qty": 5},
{"warehouse": "C", "qty": 15},
],
},
{"item": "notebook", "instock": [SON([("warehouse", "C"), ("qty", 5)])]},
{"item": "notebook", "instock": [{"warehouse": "C", "qty": 5}]},
{
"item": "paper",
"instock": [
SON([("warehouse", "A"), ("qty", 60)]),
SON([("warehouse", "B"), ("qty", 15)]),
{"warehouse": "A", "qty": 60},
{"warehouse": "B", "qty": 15},
],
},
{
"item": "planner",
"instock": [
SON([("warehouse", "A"), ("qty", 40)]),
SON([("warehouse", "B"), ("qty", 5)]),
{"warehouse": "A", "qty": 40},
{"warehouse": "B", "qty": 5},
],
},
{
"item": "postcard",
"instock": [
SON([("warehouse", "B"), ("qty", 15)]),
SON([("warehouse", "C"), ("qty", 35)]),
{"warehouse": "B", "qty": 15},
{"warehouse": "C", "qty": 35},
],
},
]
)
# End Example 29

# Start Example 30
cursor = db.inventory.find({"instock": SON([("warehouse", "A"), ("qty", 5)])})
cursor = db.inventory.find({"instock": {"warehouse": "A", "qty": 5}})
# End Example 30

self.assertEqual(len(await cursor.to_list()), 1)

# Start Example 31
cursor = db.inventory.find({"instock": SON([("qty", 5), ("warehouse", "A")])})
cursor = db.inventory.find({"instock": {"qty": 5, "warehouse": "A"}})
# End Example 31

self.assertEqual(len(await cursor.to_list()), 0)
Expand Down
44 changes: 18 additions & 26 deletions test/test_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,54 +182,50 @@ def test_query_embedded_documents(self):
db = self.db

# Start Example 14
# Subdocument key order matters in a few of these examples so we have
# to use bson.son.SON instead of a Python dict.
from bson.son import SON

db.inventory.insert_many(
[
{
"item": "journal",
"qty": 25,
"size": SON([("h", 14), ("w", 21), ("uom", "cm")]),
"size": {"h": 14, "w": 21, "uom": "cm"},
"status": "A",
},
{
"item": "notebook",
"qty": 50,
"size": SON([("h", 8.5), ("w", 11), ("uom", "in")]),
"size": {"h": 8.5, "w": 11, "uom": "in"},
"status": "A",
},
{
"item": "paper",
"qty": 100,
"size": SON([("h", 8.5), ("w", 11), ("uom", "in")]),
"size": {"h": 8.5, "w": 11, "uom": "in"},
"status": "D",
},
{
"item": "planner",
"qty": 75,
"size": SON([("h", 22.85), ("w", 30), ("uom", "cm")]),
"size": {"h": 22.85, "w": 30, "uom": "cm"},
"status": "D",
},
{
"item": "postcard",
"qty": 45,
"size": SON([("h", 10), ("w", 15.25), ("uom", "cm")]),
"size": {"h": 10, "w": 15.25, "uom": "cm"},
"status": "A",
},
]
)
# End Example 14

# Start Example 15
cursor = db.inventory.find({"size": SON([("h", 14), ("w", 21), ("uom", "cm")])})
cursor = db.inventory.find({"size": {"h": 14, "w": 21, "uom": "cm"}})
# End Example 15

self.assertEqual(len(cursor.to_list()), 1)

# Start Example 16
cursor = db.inventory.find({"size": SON([("w", 21), ("h", 14), ("uom", "cm")])})
cursor = db.inventory.find({"size": {"w": 21, "h": 14, "uom": "cm"}})
# End Example 16

self.assertEqual(len(cursor.to_list()), 0)
Expand Down Expand Up @@ -324,53 +320,49 @@ def test_query_array_of_documents(self):
db = self.db

# Start Example 29
# Subdocument key order matters in a few of these examples so we have
# to use bson.son.SON instead of a Python dict.
from bson.son import SON

db.inventory.insert_many(
[
{
"item": "journal",
"instock": [
SON([("warehouse", "A"), ("qty", 5)]),
SON([("warehouse", "C"), ("qty", 15)]),
{"warehouse": "A", "qty": 5},
{"warehouse": "C", "qty": 15},
],
},
{"item": "notebook", "instock": [SON([("warehouse", "C"), ("qty", 5)])]},
{"item": "notebook", "instock": [{"warehouse": "C", "qty": 5}]},
{
"item": "paper",
"instock": [
SON([("warehouse", "A"), ("qty", 60)]),
SON([("warehouse", "B"), ("qty", 15)]),
{"warehouse": "A", "qty": 60},
{"warehouse": "B", "qty": 15},
],
},
{
"item": "planner",
"instock": [
SON([("warehouse", "A"), ("qty", 40)]),
SON([("warehouse", "B"), ("qty", 5)]),
{"warehouse": "A", "qty": 40},
{"warehouse": "B", "qty": 5},
],
},
{
"item": "postcard",
"instock": [
SON([("warehouse", "B"), ("qty", 15)]),
SON([("warehouse", "C"), ("qty", 35)]),
{"warehouse": "B", "qty": 15},
{"warehouse": "C", "qty": 35},
],
},
]
)
# End Example 29

# Start Example 30
cursor = db.inventory.find({"instock": SON([("warehouse", "A"), ("qty", 5)])})
cursor = db.inventory.find({"instock": {"warehouse": "A", "qty": 5}})
# End Example 30

self.assertEqual(len(cursor.to_list()), 1)

# Start Example 31
cursor = db.inventory.find({"instock": SON([("qty", 5), ("warehouse", "A")])})
cursor = db.inventory.find({"instock": {"qty": 5, "warehouse": "A"}})
# End Example 31

self.assertEqual(len(cursor.to_list()), 0)
Expand Down
Loading