Exemplo n.º 1
0
async def test_fulltext_query_pg_catalog_lang(container_requester):
    from guillotina.contrib.catalog.pg.utility import PGSearchUtility

    async with container_requester as requester:
        await requester(
            "POST",
            "/db/guillotina/",
            data=json.dumps({
                "@type": "Item",
                "id": "item1",
                "title": "Something Grande Casa foobar"
            }),
        )
        await requester(
            "POST",
            "/db/guillotina/",
            data=json.dumps({
                "@type": "Item",
                "title": "Something Grande",
                "id": "item2"
            }),
        )

        async with requester.db.get_transaction_manager(
        ) as tm, await tm.begin():
            test_utils.login()
            root = await tm.get_root()
            container = await root.async_get("guillotina")

            util = PGSearchUtility()
            await util.initialize()
            results = await util.search(container, {"title": "Grande"})
            assert len(results["items"]) == 2
            results = await util.search(container, {"title": "Casa"})
            assert len(results["items"]) == 1
Exemplo n.º 2
0
async def test_build_pg_query(dummy_guillotina):
    from guillotina.contrib.catalog.pg.utility import PGSearchUtility

    util = PGSearchUtility()
    with mocks.MockTransaction():
        content = test_utils.create_content(Container)
        query = parse_query(content, {"uuid": content.uuid}, util)
        assert content.uuid == query["wheres_arguments"][0]
        assert "json->'uuid'" in query["wheres"][0]
Exemplo n.º 3
0
async def test_parse_metadata(dummy_guillotina):
    from guillotina.contrib.catalog.pg.utility import PGSearchUtility

    util = PGSearchUtility()
    with mocks.MockTransaction():
        content = test_utils.create_content(Container)
        query = parse_query(content, {"_metadata": "foobar"})
        result = util.load_meatdata(query, {
            "foobar": "foobar",
            "blah": "blah"
        })
        assert result == {"foobar": "foobar"}

        query = parse_query(content, {"_metadata_not": "foobar"})
        result = util.load_meatdata(query, {
            "foobar": "foobar",
            "blah": "blah"
        })
        assert result == {"blah": "blah"}
Exemplo n.º 4
0
async def test_date_query_pg_catalog_sort(container_requester):
    from guillotina.contrib.catalog.pg.utility import PGSearchUtility

    async with container_requester as requester:
        await requester(
            "POST",
            "/db/guillotina/",
            data=json.dumps({
                "@type": "Item",
                "id": "item1",
                "title": "Something interesting"
            }),
        )
        await requester(
            "POST",
            "/db/guillotina/",
            data=json.dumps({
                "@type": "Item",
                "title": "Something else something foobar something",
                "id": "item2"
            }),
        )

        async with requester.db.get_transaction_manager(
        ) as tm, await tm.begin():
            test_utils.login()
            root = await tm.get_root()
            container = await root.async_get("guillotina")

            util = PGSearchUtility()
            await util.initialize()
            results = await util.search(container, {
                "creation_date__gt": "1/1/1900",
                "_sort_asc": "creation_date"
            })
            assert len(results["items"]) == 2

            results = await util.search(container, {
                "creation_date__gt": "1/1/3000",
                "_sort_asc": "creation_date"
            })
            assert len(results["items"]) == 0
Exemplo n.º 5
0
async def test_query_pg_catalog(container_requester):
    from guillotina.contrib.catalog.pg.utility import PGSearchUtility

    async with container_requester as requester:
        await requester("POST",
                        "/db/guillotina/",
                        data=json.dumps({
                            "@type": "Item",
                            "title": "Item1",
                            "id": "item1"
                        }))
        await requester("POST",
                        "/db/guillotina/",
                        data=json.dumps({
                            "@type": "Item",
                            "title": "Item2",
                            "id": "item2"
                        }))

        async with requester.db.get_transaction_manager(
        ) as tm, await tm.begin():
            test_utils.login()
            root = await tm.get_root()
            container = await root.async_get("guillotina")

            util = PGSearchUtility()
            await util.initialize()
            results = await util.search(container, {"id": "item1"})
            assert len(results["items"]) == 1

            results = await util.search(container, {"_size": "1"})
            assert len(results["items"]) == 1
            results = await util.search(container, {
                "_size": "1",
                "_from": "1"
            })
            assert len(results["items"]) == 1

            results = await util.query_aggregation(container,
                                                   {"_metadata": "title"})
            assert len(results["items"]) == 2
            assert results["items"][0][0] == "Item1"

            results = await util.query_aggregation(
                container, {"_metadata": ["title", "creators"]})
            assert len(results["items"]) == 2
            assert results["items"][0][1][0] == "root"

            results = await util.query_aggregation(container,
                                                   {"_metadata": ["creators"]})
            assert len(results["items"]) == 1
            assert results["items"][0][0][0] == "root"

            results = await util.query_aggregation(
                container, {
                    "_metadata": ["title", "creators"],
                    "title__eq": "Item2"
                })
            assert len(results["items"]) == 1
            assert results["items"][0][1][0] == "root"

        resp, status = await requester(
            "GET",
            "/db/guillotina/@aggregation?title__eq=Item2&_metadata=title,creators",
        )
        assert status == 200
        assert resp == {
            "title": {
                "items": {
                    "Item2": 1
                },
                "total": 1
            },
            "creators": {
                "items": {
                    "root": 1
                },
                "total": 1
            },
        }