Skip to content

Commit

Permalink
DOC-3787 add missing code samples for JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
andy-stark-redis committed May 14, 2024
1 parent d93f533 commit 41a91d2
Showing 1 changed file with 38 additions and 3 deletions.
41 changes: 38 additions & 3 deletions doctests/dt_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,21 +227,33 @@
assert res7 == ["Phoebe", "Quaoar"]
# REMOVE_END

# STEP_START filters
# STEP_START filter1
res8 = r.json().get(
"bikes:inventory",
"$..mountain_bikes[?(@.price < 3000 && @.specs.weight < 10)]",
)
print(
res8
) # >>> [{'id': 'bike:2', 'model': 'Quaoar', 'description': "Redesigned for the 2020 model year, this bike impressed our testers and is the best all-around trail bike we've ever tested. The Shimano gear system effectively does away with an external cassette, so is super low maintenance in terms of wear and tear. All in all it's an impressive package for the price, making it very competitive.", 'price': 2072, 'specs': {'material': 'aluminium', 'weight': 7.9}, 'colors': ['black', 'white']}]
# STEP_END

# REMOVE_START
assert res8 == [{'id': 'bike:2', 'model': 'Quaoar', 'description': "Redesigned for the 2020 model year, this bike impressed our testers and is the best all-around trail bike we've ever tested. The Shimano gear system effectively does away with an external cassette, so is super low maintenance in terms of wear and tear. All in all it's an impressive package for the price, making it very competitive.", 'price': 2072, 'specs': {'material': 'aluminium', 'weight': 7.9}, 'colors': ['black', 'white']}]
# REMOVE_END

# STEP_START filter2
# names of bikes made from an alloy
res9 = r.json().get(
"bikes:inventory", "$..[?(@.specs.material == 'alloy')].model"
)
print(res9) # >>> [['Weywot', 'Mimas']]
print(res9) # >>> ['Weywot', 'Mimas']
# STEP_END

# REMOVE_START
assert res9 == ['Weywot', 'Mimas']
# REMOVE_END

# STEP_START filter3
res10 = r.json().get(
"bikes:inventory", "$..[?(@.specs.material =~ '(?i)al')].model"
)
Expand All @@ -252,6 +264,19 @@
assert res10 == ["Quaoar", "Weywot", "Salacia", "Mimas"]
# REMOVE_END

# STEP_START filter4
res11 = r.json().set("bikes:inventory", "$.inventory.mountain_bikes[0].regex_pat", "(?i)al")
res12 = r.json().set("bikes:inventory", "$.inventory.mountain_bikes[1].regex_pat", "(?i)al")
res13 = r.json().set("bikes:inventory", "$.inventory.mountain_bikes[2].regex_pat", "(?i)al")

res14 = r.json().get("bikes:inventory", "$.inventory.mountain_bikes[?(@.specs.material =~ @.regex_pat)].model")
print(res14) # >>> ['Quaoar', 'Weywot']
# STEP_END

# REMOVE_START
assert res14 == ['Quaoar', 'Weywot']
# REMOVE_END

# STEP_START update_bikes
res11 = r.json().get("bikes:inventory", "$..price")
print(res11) # >>> [1920, 2072, 3264, 1475, 3941]
Expand All @@ -267,7 +292,17 @@
assert res12 == [1820, 1972, 3164, 1375, 3841]
# REMOVE_END

# STEP_START update_filters
# STEP_START update_filters1
res16 = r.json().set("bikes:inventory", "$.inventory.*[?(@.price<2000)].price", 1500)
res17 = r.json().get("bikes:inventory", "$..price")
print(res17) # >>> [1500, 2072, 3264, 1500, 3941]
# STEP_END

# REMOVE_START
assert res17 == [1500, 2072, 3264, 1500, 3941]
# REMOVE_END

# STEP_START update_filters2
res14 = r.json().arrappend(
"bikes:inventory", "$.inventory.*[?(@.price<2000)].colors", "pink"
)
Expand Down

0 comments on commit 41a91d2

Please sign in to comment.