MCPcopy
hub / github.com/redis/redis-py / test_json_with_jsonpath

Method test_json_with_jsonpath

tests/test_search.py:2587–2633  ·  view source on GitHub ↗
(self, client)

Source from the content-addressed store, hash-verified

2585 @pytest.mark.redismod
2586 @skip_ifmodversion_lt("2.2.0", "search")
2587 def test_json_with_jsonpath(self, client):
2588 definition = IndexDefinition(index_type=IndexType.JSON)
2589 client.ft().create_index(
2590 (
2591 TextField('$["prod:name"]', as_name="name"),
2592 TextField("$.prod:name", as_name="name_unsupported"),
2593 ),
2594 definition=definition,
2595 )
2596
2597 client.json().set("doc:1", Path.root_path(), {"prod:name": "RediSearch"})
2598
2599 if expects_resp2_shape(client) or expects_unified_shape(client):
2600 # query for a supported field succeeds
2601 res = client.ft().search(Query("@name:RediSearch"))
2602 assert res.total == 1
2603 assert res.docs[0].id == "doc:1"
2604 assert res.docs[0].json == '{"prod:name":"RediSearch"}'
2605
2606 # query for an unsupported field
2607 res = client.ft().search("@name_unsupported:RediSearch")
2608 assert res.total == 1
2609
2610 # return of a supported field succeeds
2611 res = client.ft().search(Query("@name:RediSearch").return_field("name"))
2612 assert res.total == 1
2613 assert res.docs[0].id == "doc:1"
2614 assert res.docs[0].name == "RediSearch"
2615 elif expects_resp3_shape(client):
2616 # query for a supported field succeeds
2617 res = client.ft().search(Query("@name:RediSearch"))
2618 assert res["total_results"] == 1
2619 assert res["results"][0]["id"] == "doc:1"
2620 assert (
2621 res["results"][0]["extra_attributes"]["$"]
2622 == '{"prod:name":"RediSearch"}'
2623 )
2624
2625 # query for an unsupported field
2626 res = client.ft().search("@name_unsupported:RediSearch")
2627 assert res["total_results"] == 1
2628
2629 # return of a supported field succeeds
2630 res = client.ft().search(Query("@name:RediSearch").return_field("name"))
2631 assert res["total_results"] == 1
2632 assert res["results"][0]["id"] == "doc:1"
2633 assert res["results"][0]["extra_attributes"]["name"] == "RediSearch"
2634
2635
2636class TestProfile(SearchTestsBase):

Callers

nothing calls this directly

Calls 13

IndexDefinitionClass · 0.90
TextFieldClass · 0.90
QueryClass · 0.90
expects_resp2_shapeFunction · 0.85
expects_unified_shapeFunction · 0.85
expects_resp3_shapeFunction · 0.85
create_indexMethod · 0.80
root_pathMethod · 0.80
return_fieldMethod · 0.80
ftMethod · 0.45
setMethod · 0.45
jsonMethod · 0.45

Tested by

no test coverage detected