def test_check_all_files_with_combined_coverage() -> None:
    report = create_report(
        meta=ReportMetadata(branch_coverage=True),
        files={
            "a.py":
            FileCoverageModel(summary=CoverageSummaryModel(
                covered_lines=5,
                num_statements=5,
                covered_branches=1,
                num_branches=5,
            )),
            "b.py":
            FileCoverageModel(summary=CoverageSummaryModel(
                covered_lines=5,
                num_statements=5,
                covered_branches=3,
                num_branches=5,
            )),
        },
    )

    assert (check_all(
        report, Config(file_combined_coverage_min=Decimal("60.0"))) == Pass())
    assert check_all(
        report, Config(file_combined_coverage_min=Decimal("80.0"))) == Fail([
            'File: "a.py" failed COMBINED line plus branch coverage metric' +
            ", expected 80.0, was 60.0000"
        ])
def test_check_all_files_with_branch_coverage() -> None:
    report = create_report(
        meta=ReportMetadata(branch_coverage=True),
        files={
            "a.py":
            FileCoverageModel(summary=CoverageSummaryModel(
                covered_lines=5,
                num_statements=5,
                covered_branches=1,
                num_branches=2,
            )),
            "b.py":
            FileCoverageModel(summary=CoverageSummaryModel(
                covered_lines=5,
                num_statements=5,
                covered_branches=3,
                num_branches=4,
            )),
        },
    )

    assert check_all(
        report, Config(file_branch_coverage_min=Decimal("50.0"))) == Pass()
    assert check_all(
        report, Config(file_branch_coverage_min=Decimal("75.0"))
    ) == Fail([
        'File: "a.py" failed BRANCH coverage metric, expected 75.0, was 50.0000'
    ])
def test_module_level_config() -> None:
    report = create_report(
        meta=ReportMetadata(branch_coverage=True),
        files={
            "src/model/a.py":
            FileCoverageModel(summary=CoverageSummaryModel(
                covered_lines=5,
                num_statements=5,
                covered_branches=1,
                num_branches=2,
            )),
            "src/model/b.py":
            FileCoverageModel(summary=CoverageSummaryModel(
                covered_lines=5,
                num_statements=5,
                covered_branches=3,
                num_branches=4,
            )),
            "src/cli/command.py":
            FileCoverageModel(summary=CoverageSummaryModel(
                covered_lines=5,
                num_statements=5,
                covered_branches=4,
                num_branches=4,
            )),
        },
    )

    assert (check_all(
        report,
        Config(modules={
            "src/model/":
            ModuleConfig(file_branch_coverage_min=Decimal("50.0"))
        }),
    ) == Pass())
    assert (check_all(
        report,
        Config(
            modules={
                "src/model/":
                ModuleConfig(file_branch_coverage_min=Decimal("75.0")),
                "src/model/a":
                ModuleConfig(file_branch_coverage_min=Decimal("50.0")),
            }),
    ) == Pass())
    assert check_all(
        report,
        Config(
            modules={
                "src/model/":
                ModuleConfig(file_branch_coverage_min=Decimal("80.0")),
                "src/model/a":
                ModuleConfig(file_branch_coverage_min=Decimal("50.0")),
            }),
    ) == Fail([
        'File: "src/model/b.py" failed BRANCH coverage metric' +
        ", expected 80.0, was 75.0000"
    ])
Example #4
0
def test_report_parse__full() -> None:
    assert ReportModel.parse({
        "meta": {
            "branch_coverage": True,
        },
        "files": {
            "src/__init__.py": {
                "summary": {
                    "covered_lines": 1,
                    "num_statements": 2,
                    "num_branches": 3,
                    "covered_branches": 4,
                },
            },
            "src/main.py": {
                "summary": {
                    "covered_lines": 5,
                    "num_statements": 6,
                    "num_branches": 7,
                    "covered_branches": 8,
                },
            },
        },
        "totals": {
            "covered_lines": 9,
            "num_statements": 10,
            "num_branches": 11,
            "covered_branches": 12,
        },
    }) == ReportModel(
        meta=ReportMetadata(branch_coverage=True),
        files={
            "src/__init__.py":
            FileCoverageModel(summary=CoverageSummaryModel(
                covered_lines=1,
                num_statements=2,
                num_branches=3,
                covered_branches=4,
            )),
            "src/main.py":
            FileCoverageModel(summary=CoverageSummaryModel(
                covered_lines=5,
                num_statements=6,
                num_branches=7,
                covered_branches=8,
            )),
        },
        totals=CoverageSummaryModel(
            covered_lines=9,
            num_statements=10,
            num_branches=11,
            covered_branches=12,
        ),
    )
def test_checking_branch_coverage_fails_without_branch_report() -> None:
    report = create_report(meta=ReportMetadata(branch_coverage=False))
    expected_error_message = "missing number of branches or number of branches covered"

    with pytest.raises(ValueError) as e:
        check_all(report, Config(branch_coverage_min=Decimal("50.0")))
    assert str(e.value) == expected_error_message

    with pytest.raises(ValueError) as e:
        check_all(report, Config(combined_coverage_min=Decimal("50.0")))
    assert str(e.value) == expected_error_message

    with pytest.raises(ValueError) as e:
        check_all(report, Config(file_branch_coverage_min=Decimal("75.0")))
    assert str(e.value) == expected_error_message
def create_report(
    files: Optional[Mapping[str, FileCoverageModel]] = None,
    meta: ReportMetadata = ReportMetadata(branch_coverage=False),
    totals: CoverageSummaryModel = CoverageSummaryModel(
        covered_lines=4,
        num_statements=4,
    ),
) -> ReportModel:
    if files is None:
        files = {"src/main.py": FileCoverageModel(summary=totals)}
    return ReportModel(
        meta=meta,
        files=files,
        totals=totals,
    )
def test_check_totals_with_branch_coverage() -> None:
    report = create_report(
        meta=ReportMetadata(branch_coverage=True),
        totals=CoverageSummaryModel(
            covered_lines=5,
            num_statements=5,
            covered_branches=3,
            num_branches=4,
        ),
    )
    assert (check_all(
        report,
        Config(branch_coverage_min=Decimal("75.0")),
    ) == Pass())
    assert (check_all(
        report,
        Config(branch_coverage_min=Decimal("75.001")),
    ) == Fail(
        ["Total branch coverage metric failed, expected 75.001, was 75.0000"]))