def test_search_content_mixed_fields(populated_repo): """search_content crossing multiple fields and types returns matching units""" crit = Criteria.and_( Criteria.with_field_in("content_type_id", ["rpm", "modulemd"]), Criteria.with_field_in("name", ["bash", "module1"]), ) units = list(populated_repo.search_content(crit)) # Note: sorting different types not natively supported, hence sorting by repr assert sorted(units, key=repr) == [ ModulemdUnit( unit_id="23a7711a-8133-2876-37eb-dcd9e87a1613", name="module1", stream="s1", version=1234, context="a1b2", arch="x86_64", repository_memberships=["repo1"], ), RpmUnit( unit_id="e3e70682-c209-4cac-629f-6fbed82c07cd", name="bash", version="4.0", release="1", arch="x86_64", repository_memberships=["repo1"], ), ]
def test_field_in_str_invalid(): """Criteria.with_field_in raises if provided field value is a string. This is checked specifically because strings are iterable, so it could seem to work, but it's almost certainly an error if the caller provided a string. """ with pytest.raises(ValueError) as exc_info: Criteria.with_field_in("x", "someval") assert "Must be an iterable: 'someval'" in str(exc_info.value)
def test_eng_product_in(): """eng_product is mapped correctly""" crit = Criteria.with_field_in("eng_product_id", [12, 34, 56]) assert filters_for_criteria(crit, Repository) == { "notes.eng_product": { "$in": ["12", "34", "56"] } }
def test_field_in_criteria(): """with_field_in is translated to a mongo fragment as expected.""" assert filters_for_criteria( Criteria.with_field_in("some.field", ["val1", "val2"])) == { "some.field": { "$in": ["val1", "val2"] } }
def test_search_content_mixed_fields(populated_repo): """search_content crossing multiple fields and types returns matching units""" crit = Criteria.and_( Criteria.with_field_in("content_type_id", ["rpm", "modulemd"]), Criteria.with_field_in("name", ["bash", "module1"]), ) units = list(populated_repo.search_content(crit)) # Note: sorting different types not natively supported, hence sorting by repr assert sorted(units, key=repr) == [ ModulemdUnit(name="module1", stream="s1", version=1234, context="a1b2", arch="x86_64"), RpmUnit(name="bash", version="4.0", release="1", arch="x86_64"), ]
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_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" } } }, } }