Repository: redis/redis-py
File: tests/test_asyncio/test_search.py (Python)

Code before:

        # create withsuffixtrie index (text fields)
        assert await decoded_r.ft().create_index(TextField("t", withsuffixtrie=True))
        await waitForIndex(decoded_r, getattr(decoded_r.ft(), "index_name", "idx"))
        info = await decoded_r.ft().info()
        assert "WITHSUFFIXTRIE" in info["attributes"][0]["flags"]
        assert await decoded_r.ft().dropindex("idx")

        # create withsuffixtrie index (tag field)
        assert await decoded_r.ft().create_index(TagField("t", withsuffixtrie=True))
        await waitForIndex(decoded_r, getattr(decoded_r.ft(), "index_name", "idx"))
        info = await decoded_r.ft().info()
        assert "WITHSUFFIXTRIE" in info["attributes"][0]["flags"]


@pytest.mark.redismod
@skip_ifmodversion_lt("2.10.05", "search")
async def test_aggregations_add_scores(decoded_r: redis.Redis):
    assert await decoded_r.ft().create_index(
        (
            TextField("name", sortable=True, weight=5.0),
            NumericField("age", sortable=True),
        )
    )

    assert await decoded_r.ft().client.hset(
        "doc1", mapping={"name": "bar", "age": "25"}
    )
    assert await decoded_r.ft().client.hset(
        "doc2", mapping={"name": "foo", "age": "19"}
    )

    req = aggregations.AggregateRequest("*").add_scores()
    res = await decoded_r.ft().aggregate(req)
    assert len(res.rows) == 2
    assert res.rows[0] == ["__score", "0.2"]
    assert res.rows[1] == ["__score", "0.2"]


@pytest.mark.redismod
@skip_if_redis_enterprise()
async def test_search_commands_in_pipeline(decoded_r: redis.Redis):
    p = await decoded_r.ft().pipeline()
    p.create_index((TextField("txt"),))
    p.hset("doc1", mapping={"txt": "foo bar"})
    p.hset("doc2", mapping={"txt": "foo bar"})
    q = Query("foo bar").with_payloads()
    await p.search(q)
    res = await p.execute()
    if is_resp2_connection(decoded_r):
        assert res[:3] == ["OK", True, True]

Review comment (suggestion):
`decoded_r.ft().client` is redundant. You should just use `decoded_r`, which means "Redis Client with decode_responses=True"

Code after:

        # create withsuffixtrie index (text fields)
        assert await decoded_r.ft().create_index(TextField("t", withsuffixtrie=True))
        await waitForIndex(decoded_r, getattr(decoded_r.ft(), "index_name", "idx"))
        info = await decoded_r.ft().info()
        assert "WITHSUFFIXTRIE" in info["attributes"][0]["flags"]
        assert await decoded_r.ft().dropindex("idx")

        # create withsuffixtrie index (tag field)
        assert await decoded_r.ft().create_index(TagField("t", withsuffixtrie=True))
        await waitForIndex(decoded_r, getattr(decoded_r.ft(), "index_name", "idx"))
        info = await decoded_r.ft().info()
        assert "WITHSUFFIXTRIE" in info["attributes"][0]["flags"]


@pytest.mark.redismod
@skip_ifmodversion_lt("2.10.05", "search")
async def test_aggregations_add_scores(decoded_r: redis.Redis):
    assert await decoded_r.ft().create_index(
        (
            TextField("name", sortable=True, weight=5.0),
            NumericField("age", sortable=True),
        )
    )

    assert await decoded_r.hset("doc1", mapping={"name": "bar", "age": "25"})
    assert await decoded_r.hset("doc2", mapping={"name": "foo", "age": "19"})

    req = aggregations.AggregateRequest("*").add_scores()
    res = await decoded_r.ft().aggregate(req)

    if isinstance(res, dict):
        assert len(res["results"]) == 2
        assert res["results"][0]["extra_attributes"] == {"__score": "0.2"}
        assert res["results"][1]["extra_attributes"] == {"__score": "0.2"}
    else:
        assert len(res.rows) == 2
        assert res.rows[0] == ["__score", "0.2"]
        assert res.rows[1] == ["__score", "0.2"]


@pytest.mark.redismod
@skip_if_redis_enterprise()
async def test_search_commands_in_pipeline(decoded_r: redis.Redis):
    p = await decoded_r.ft().pipeline()
    p.create_index((TextField("txt"),))
    p.hset("doc1", mapping={"txt": "foo bar"})
    p.hset("doc2", mapping={"txt": "foo bar"})
    q = Query("foo bar").with_payloads()
    await p.search(q)
    res = await p.execute()