def test_complex_type_ids(client):
    """content searches raise if using criteria with unsupported operators on content_type_id"""
    repo = Repository(id="some-repo")
    repo.__dict__["_client"] = client

    with pytest.raises(ValueError) as e:
        repo.search_content(
            Criteria.with_field("content_type_id", Matcher.regex("foobar")))

    assert "unsupported expression for content_type_id" in str(e.value)
def test_search_content_type_id_in_or(client, requests_mocker):
    """Searching with a content_type_id within $or fails as unsupported"""
    repo = Repository(id="some-repo")
    repo.__dict__["_client"] = client

    crit = Criteria.or_(
        Criteria.with_field("name", "hello.txt"),
        Criteria.with_field_in("content_type_id", ["rpm", "iso"]),
    )

    with pytest.raises(ValueError) as e:
        repo.search_content(crit).result()

    assert "Can't serialize criteria for Pulp query; too complicated" in str(
        e.value)
def test_search_fields(client, requests_mocker):
    """Searching with limited fields works correctly"""
    repo = Repository(id="some-repo")
    repo.__dict__["_client"] = client
    requests_mocker.post(
        "https://pulp.example.com/pulp/api/v2/repositories/some-repo/search/units/",
        json=[],
    )

    crit = Criteria.and_(
        Criteria.with_unit_type(FileUnit, unit_fields=["sha256sum"]),
        Criteria.with_field("name", "hello.txt"),
    )

    repo.search_content(crit).result()

    history = requests_mocker.request_history

    # There should have been just one request
    assert len(history) == 1

    request = history[0]
    body = request.json()

    # This should have been the request body.
    assert body == {
        "criteria": {
            "type_ids": ["iso"],
            "skip": 0,
            "limit": 2000,
            "filters": {
                "unit": {
                    "name": {
                        "$eq": "hello.txt"
                    }
                }
            },
            "fields": {
                "unit": ["checksum", "name", "size"]
            },
        }
    }
def test_mixed_search(client, requests_mocker):
    """Searching with a criteria mixing several fields works correctly"""
    repo = Repository(id="some-repo")
    repo.__dict__["_client"] = client
    requests_mocker.post(
        "https://pulp.example.com/pulp/api/v2/repositories/some-repo/search/units/",
        json=[{
            "metadata": {
                "_content_type_id": "iso",
                "name": "hello.txt",
                "size": 23,
                "checksum": "a" * 64,
            }
        }],
    )

    crit = Criteria.and_(
        Criteria.with_field_in("content_type_id", ["rpm", "iso"]),
        Criteria.with_field("name", "hello.txt"),
    )

    files = list(repo.search_content(crit))

    assert files == [FileUnit(path="hello.txt", size=23, sha256sum="a" * 64)]

    history = requests_mocker.request_history

    # There should have been just one request
    assert len(history) == 1

    request = history[0]
    body = request.json()

    # This should have been the request body.
    assert body == {
        "criteria": {
            "type_ids": ["rpm", "iso"],
            "skip": 0,
            "limit": 2000,
            "filters": {
                "unit": {
                    "name": {
                        "$eq": "hello.txt"
                    }
                }
            },
        }
    }