Example #1
0
def test_response_fetching_results(resource):
    r = m.Response(
        resource=resource,
        json=load_json_fixture(
            f"{resource.kind.name}_first_page_response_next_page"),
    )
    if r.resource.kind == m.RepoLangs.kind:
        assert r.end_cursor == "Y3Vyc29yOnYyOpHORMtSuQ=="
        assert r.num_results == 2
        assert r.total_results == 10
    elif r.resource.kind == m.RepoManifests.kind:
        assert r.end_cursor == "MQ"
        assert r.num_results == 1
        assert r.total_results == 1
    elif r.resource.kind == m.RepoManifestDeps.kind:
        assert r.end_cursor == "Mg"
        assert r.num_results == 2
        assert r.total_results == 2
    elif r.resource.kind == m.RepoVulnAlerts.kind:
        assert r.end_cursor == "Y3Vyc29yOnYyOpHOCOpjzQ=="
        assert r.num_results == 4
        assert r.total_results == 5
    elif r.resource.kind == m.RepoVulnAlertVulns.kind:
        assert r.end_cursor == "Mg"
        assert r.num_results == 2
        assert r.total_results == 2
    else:
        raise NotImplementedError()
    assert r.log_str
Example #2
0
def test_get_next_requests_only_returns_requests_for_enabled_resource(
        logger, github_args_dict, owner_repo_dict, github_schema):
    last_resource = m.RepoManifests
    context = m.ChainMap(
        owner_repo_dict,
        github_args_dict,
        {
            "github_query_type": [m.RepoManifests],
            "parent_after":
            "test_parent_after_cursor",  # only for nested pages
        },
    )
    last_exchange = m.RequestResponseExchange(
        request=m.Request(
            resource=last_resource,
            selection_updates=m.get_first_page_selection_updates(
                last_resource, m.ChainMap(context, owner_repo_dict)),
        ),
        response=m.Response(
            resource=last_resource,
            json=load_json_fixture(
                f"{last_resource.kind.name}_first_page_response_next_page"),
        ),
    )
    next_requests = list(m.get_next_requests(logger, context, last_exchange))
    assert len(next_requests) == 1
    r = next_requests[0]
    assert r.resource == m.RepoManifests
    assert_selection_is_sane(r.graphql, github_schema)
    assert str(r.graphql) == load_graphql_fixture(
        f"{r.resource.kind.name}_next_selection"
    ), f"did not matched expected serialized \
Example #3
0
def test_get_next_requests_for_last_page_returns_no_more_requests_for_resource(
        logger, github_args_dict, owner_repo_dict, last_resource,
        github_schema):
    context = m.ChainMap(
        owner_repo_dict,
        github_args_dict,
        {
            "github_query_type": all_resource_kinds,
            "parent_after":
            "test_parent_first_page_after_cursor",  # only for nested pages
        },
    )
    last_exchange = m.RequestResponseExchange(
        request=m.Request(
            resource=last_resource,
            selection_updates=m.get_first_page_selection_updates(
                last_resource, context),
        ),
        response=m.Response(
            resource=last_resource,
            json=load_json_fixture(
                f"{last_resource.kind.name}_first_page_response_no_next_page"),
        ),
    )
    next_requests = list(m.get_next_requests(logger, context, last_exchange))
    for r in next_requests:
        assert r.resource != last_resource
        assert r.page_number == 0
        assert_selection_is_sane(r.graphql, github_schema)
        assert str(r.graphql) == load_graphql_fixture(
            f"{r.resource.kind.name}_nested_first_selection"
        ), f"did not matched expected serialized \
Example #4
0
def test_get_next_requests_does_not_grow_request_selection_updates(
        logger, github_args_dict, owner_repo_dict, last_resource,
        github_schema):
    context = m.ChainMap(
        owner_repo_dict,
        github_args_dict,
        {
            "github_query_type": all_resource_kinds,
            "parent_after":
            "test_parent_after_cursor",  # only for nested pages
        },
    )
    first_exchange = m.RequestResponseExchange(
        request=m.Request(
            resource=last_resource,
            selection_updates=m.get_first_page_selection_updates(
                last_resource, context),
        ),
        response=m.Response(
            resource=last_resource,
            json=load_json_fixture(
                f"{last_resource.kind.name}_first_page_response_next_page"),
        ),
    )
    r = next(m.get_next_requests(logger, context, first_exchange))
    assert r.page_number == 1
    assert_selection_is_sane(r.graphql, github_schema)
    assert r.resource not in last_resource.children
    assert len(r.selection_updates) == len(r.resource.first_page_diffs) + 1

    second_exchange = m.RequestResponseExchange(
        request=r,
        response=m.Response(
            resource=last_resource,
            json=load_json_fixture(
                f"{last_resource.kind.name}_first_page_response_next_page"),
        ),
    )
    r = next(m.get_next_requests(logger, context, second_exchange))
    assert r.page_number == 2
    assert_selection_is_sane(r.graphql, github_schema)
    assert r.resource not in last_resource.children
    for update in r.selection_updates:
        print(update)
    assert len(r.selection_updates) == len(r.resource.first_page_diffs) + 1
Example #5
0
def test_response_properties():
    r = m.Response(resource=m.RepoManifests)
    assert r.end_cursor is None
    assert r.num_results is None
    assert r.total_results is None
    assert r.log_str == "invalid response!"

    r = m.Response(resource=m.RepoManifests, json="[]")
    assert r.end_cursor is None
    assert r.num_results is None
    assert r.total_results is None
    assert r.log_str == "invalid response!"

    r = m.Response(resource=m.RepoManifests, json={})
    assert r.end_cursor is None
    assert r.num_results is 0
    assert r.total_results is 0
    assert r.log_str == "0 of 0"
Example #6
0
def test_get_next_requests_returns_more_pages_of_the_same_resource_and_linked_resources(
        logger, github_args_dict, owner_repo_dict, last_resource,
        github_schema):
    context = m.ChainMap(
        owner_repo_dict,
        github_args_dict,
        {
            "github_query_type": all_resource_kinds,
            "parent_after":
            "test_parent_after_cursor",  # only for nested pages
        },
    )
    last_exchange = m.RequestResponseExchange(
        request=m.Request(
            resource=last_resource,
            selection_updates=m.get_first_page_selection_updates(
                last_resource, context),
        ),
        response=m.Response(
            resource=last_resource,
            json=load_json_fixture(
                f"{last_resource.kind.name}_first_page_response_next_page"),
        ),
    )
    next_requests = list(m.get_next_requests(logger, context, last_exchange))
    assert len(next_requests) == 1 + len(last_resource.children)
    for r in next_requests:
        assert_selection_is_sane(r.graphql, github_schema)

        if r.resource in last_resource.children:
            assert r.page_number == 0
            assert str(r.graphql) == load_graphql_fixture(
                f"{r.resource.kind.name}_nested_first_selection"
            ), f"did not matched expected serialized \
gql for next {r.resource.kind} from {last_exchange.request.resource.kind}"

            assert len(r.selection_updates) == len(r.resource.first_page_diffs)
        else:
            assert r.page_number == 1
            assert str(r.graphql) == load_graphql_fixture(
                f"{r.resource.kind.name}_next_selection"
            ), f"did not matched expected serialized \
gql for next {r.resource.kind} from {last_exchange.request.resource.kind}"

            assert len(
                r.selection_updates) == len(r.resource.first_page_diffs) + 1