import io
import yaml


def assert_related_images_names(csv_content, pullspec_name_map):
    """
        Assert if names in relatedImages section were constructed as expected
        """
    related_images = []
    csv_stream = io.StringIO(csv_content)
    related_images_section = yaml.safe_load(csv_stream)['spec'].get('relatedImages', [])
    related_images.extend(related_images_section)

    def get_names(pullspec_):
        return {
            ri['name']
            for ri in related_images
            if pullspec_ == ri['image']
        }

    all_expected_names = set()
    for pullspec, exp_names in pullspec_name_map.items():
        got_names = get_names(pullspec)
        all_expected_names.update(exp_names)
        assert exp_names == got_names, (
            f"Unexpected names for pullspec {pullspec}: "
            f"expected {exp_names}; got: {got_names}"
        )

    # check if coverage is complete
    all_names = {ri['name'] for ri in related_images}
    assert all_names == all_expected_names, (
        f"Incomplete coverage: all names defined in CSV: "
        f"{all_names} differs from excepted names {all_expected_names}"
    )