def test_reportv1_object_array_minitems_audience(repo_json, report_v1_json):
    report = report_v1_json(audience=[])
    repo = repo_json(reports=[report])
    errors = validate_with_schema(repo)

    assert errors is not None
    assert '[] is too short' == errors.splitlines()[0]
def test_reportv2_object_audience_string_items(repo_json, report_v2_json):
    report = report_v2_json(audience=[1])
    repo = repo_json(reports=[report])
    errors = validate_with_schema(repo)

    assert errors is not None
    assert "1 is not of type 'string'" == errors.splitlines()[0]
def test_repo_object_field_types(repo_json, field):
    repo = repo_json()
    repo[field] = 1
    errors = validate_with_schema(repo)

    assert errors is not None
    assert "1 is not of type 'string'" == errors.splitlines()[0]
def test_repo_object_required_fields(repo_json, field):
    repo = repo_json()
    repo.pop(field)
    errors = validate_with_schema(repo)

    assert errors is not None
    assert f"'{field}' is a required property" == errors.splitlines()[0]
def test_parameter_object_minitem_choices_field(repo_json, report_v2_json):
    report = report_v2_json()
    report['parameters'][0]['choices'] = []
    repo = repo_json(reports=[report])
    errors = validate_with_schema(repo)

    assert errors is not None
    assert "[] is too short" == errors.splitlines()[0]
def test_reportv1_object_number_minimum(repo_json, report_v1_json, field):
    report = report_v1_json()
    report[field] = 0
    repo = repo_json(reports=[report])
    errors = validate_with_schema(repo)

    assert errors is not None
    assert '0 is less than the minimum of 1' == errors.splitlines()[0]
def test_parameter_object_required_boolean_field(repo_json, report_v2_json):
    report = report_v2_json()
    report['parameters'][0]['required'] = 1
    repo = repo_json(reports=[report])
    errors = validate_with_schema(repo)

    assert errors is not None
    assert "1 is not of type 'boolean'" == errors.splitlines()[0]
def test_reportv1_object_string_fields(repo_json, report_v1_json, field):
    report = report_v1_json()
    report[field] = 1
    repo = repo_json(reports=[report])
    errors = validate_with_schema(repo)

    assert errors is not None
    assert "1 is not of type 'string'" == errors.splitlines()[0]
def test_reportv2_object_report_spec(repo_json, report_v2_json):
    report = report_v2_json()
    report['report_spec'] = '5'
    repo = repo_json(reports=[report])
    errors = validate_with_schema(repo)

    assert errors is not None
    assert "'5' is not one of ['1', '2']" == errors.splitlines()[0]
def test_reportv2_object_array_minitems(repo_json, report_v2_json, field):
    report = report_v2_json()
    report[field] = []
    repo = repo_json(reports=[report])
    errors = validate_with_schema(repo)

    assert errors is not None
    assert '[] is too short' == errors.splitlines()[0]
def test_reportv2_object_array_fields(repo_json, report_v2_json, field):
    report = report_v2_json()
    report[field] = 'string'
    repo = repo_json(reports=[report])
    errors = validate_with_schema(repo)

    assert errors is not None
    assert "'string' is not of type 'array'" == errors.splitlines()[0]
def test_parameter_object_required_fields(repo_json, report_v2_json, field):
    report = report_v2_json()
    report['parameters'][0].pop(field)
    repo = repo_json(reports=[report])
    errors = validate_with_schema(repo)

    assert errors is not None
    assert f"'{field}' is a required property" == errors.splitlines()[0]
def test_parameter_object_choices_array_field(repo_json, report_v2_json):
    report = report_v2_json()
    report['parameters'][0]['choices'] = 1
    repo = repo_json(reports=[report])
    errors = validate_with_schema(repo)

    assert errors is not None
    assert "1 is not of type 'array'" == errors.splitlines()[0]
def test_choice_object_string_fields(param_json, repo_json, report_v2_json, field):
    params = [param_json(
        choices=[
            {'value': 'v1', 'label': 'l1'},
        ],
    )]
    report = report_v2_json(parameters=params)
    report['parameters'][0]['choices'][0][field] = 1
    repo = repo_json(reports=[report])
    errors = validate_with_schema(repo)

    assert errors is not None
    assert "1 is not of type 'string'" == errors.splitlines()[0]
def test_choice_object_required_fields(repo_json, report_v2_json, param_json, field):
    params = [param_json(
        choices=[
            {'value': 'v1', 'label': 'l1'},
        ],
    )]
    report = report_v2_json(parameters=params)
    report['parameters'][0]['choices'][0].pop(field)
    repo = repo_json(reports=[report])
    errors = validate_with_schema(repo)

    assert errors is not None
    assert f"'{field}' is a required property" == errors.splitlines()[0]
def test_schema_ok(
    repo_json, report_v1_json, report_v2_json, renderer_json,
    renderer_type, template, args,
):
    report_v1 = report_v1_json()
    report_v2 = report_v2_json(renderers=[
        renderer_json(
            type=renderer_type,
            description=f'{renderer_type.upper()} renderer.',
            template=template,
            args=args,
        ),
    ])
    repo = repo_json(reports=[report_v1, report_v2])

    assert validate_with_schema(repo) is None
Пример #17
0
def validate_project(project_dir):
    click.secho(f'Validating project {project_dir}...\n', fg='blue')

    data = _file_descriptor_validations(project_dir)

    errors = validate_with_schema(data)
    if errors:
        raise ClickException(f'Invalid `reports.json`: {errors}')

    report_project = parse(project_dir, data)

    errors = validate(report_project)
    if errors:
        raise ClickException(f'Invalid `reports.json`: {",".join(errors)}')

    for report in report_project.reports:
        _entrypoint_validations(project_dir, report.entrypoint, report.report_spec)

    click.secho(f'Report Project {project_dir} has been successfully validated.', fg='green')
Пример #18
0
def add_report(project_dir, package_name):
    if not os.path.isdir(f'{project_dir}/{package_name}'):
        raise ClickException(
            f'The directory package called `{package_name}` does not exist,'
            '\nPlease, create it or choose an existing one using `-n` option.',
        )

    # Required: check if the project descriptor is valid
    project_desc = _file_descriptor_validations(project_dir)
    errors = validate_with_schema(project_desc)
    if errors:
        raise ClickException(f'Invalid `reports.json`: {errors}')

    click.secho(f'Adding new report to project {project_dir}...\n', fg='blue')

    with tempfile.TemporaryDirectory() as add_report_tmpdir:

        # Instead of using cookiecutter use the internals
        report_dir, report_slug = _custom_cookiecutter(
            PROJECT_REPORT_BOILERPLATE_URL,
            add_report_tmpdir,
            project_dir,
            package_name,
        )
        if os.path.isdir(f'{project_dir}/{package_name}/{report_slug}'):
            raise ClickException(
                f'\nThe report directory called `{project_dir}/{package_name}/{report_slug}` already exists, '
                '\nplease, choose other report name or delete the existing one.',
            )

        shutil.move(
            f'{report_dir}/{package_name}/{report_slug}',
            f'{project_dir}/{package_name}/',
        )

        _add_report_to_descriptor(project_dir, report_dir, package_name)

        click.secho('\nReport has been successfully created.', fg='blue')
Пример #19
0
def load_repo(repo_dir):
    cfg = os.path.join(repo_dir, 'reports.json')
    if not os.path.isfile(cfg):
        raise ClickException(
            f'The directory `{repo_dir}` is not a reports project root directory.',
        )
    try:
        descriptor = json.load(open(cfg, 'r'))
    except json.JSONDecodeError:
        raise ClickException(
            'The reports project descriptor `reports.json` is not a valid json file.',
        )

    errors = validate_with_schema(descriptor)
    if errors:
        raise ClickException(f'Invalid `reports.json`: {errors}')

    repo = parse(repo_dir, descriptor)

    errors = validate(repo)
    if errors:
        raise ClickException(f'Invalid `reports.json`: {",".join(errors)}')

    return repo
def test_repo_object_reports_field(repo_json):
    repo = repo_json(reports='reports')
    errors = validate_with_schema(repo)

    assert errors is not None
    assert "'reports' is not of type 'array'" == errors.splitlines()[0]
def test_repo_object_reports_minitem(repo_json):
    repo = repo_json(reports=[])
    errors = validate_with_schema(repo)

    assert errors is not None
    assert '[] is too short' == errors.splitlines()[0]