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 \
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 \
def test_get_first_page_selection(resource, github_args_dict, owner_repo_dict,
                                  github_schema):
    context = m.ChainMap(github_args_dict, owner_repo_dict,
                         dict(parent_after="test_parent_after_cursor"))
    selection = m.multi_upsert_kwargs(
        m.get_first_page_selection_updates(resource, context),
        resource.base_graphql)
    assert_selection_is_sane(selection, github_schema)
    if len(resource.children):  # a root resource
        assert "after" not in str(selection)
def test_get_first_page_selection_against_fixtures(resource, github_args_dict,
                                                   owner_repo_dict):
    context = m.ChainMap(github_args_dict, owner_repo_dict,
                         dict(parent_after="test_parent_after_cursor"))
    expected_serialized = load_graphql_fixture(
        f"{resource.kind.name}_first_selection")
    serialized = str(
        m.multi_upsert_kwargs(
            m.get_first_page_selection_updates(resource, context),
            resource.base_graphql))
    assert serialized == expected_serialized
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
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
def test_get_next_requests_for_initial_requests(
    logger,
    github_args_dict,
    owner_repo_dict,
    github_resource_types,
    expected_request_resources,
):
    context = m.ChainMap(github_args_dict, owner_repo_dict,
                         {"github_query_type": github_resource_types})
    initial_requests = list(m.get_next_requests(logger, context, None))
    assert len(initial_requests) == len(expected_request_resources)
    for r, er in zip(initial_requests, expected_request_resources):
        assert r.resource.kind == er
        assert r.page_number == 0

        expected_serialized = load_graphql_fixture(
            f"{er.name}_first_selection")
        assert str(r.graphql) == str(expected_serialized)