Skip to content

Commit

Permalink
Merge pull request #492 from data-to-insight/486-error-code-8945q-add…
Browse files Browse the repository at this point in the history
…ed-2024-v11

add rule 8945Q for 2024_25
  • Loading branch information
WillLP-code authored Dec 19, 2024
2 parents 425c2c6 + 29d18c8 commit 56cd68c
Showing 1 changed file with 61 additions and 0 deletions.
61 changes: 61 additions & 0 deletions cin_validator/rules/cin2024_25/rule_8945Q.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
from typing import Mapping

import pandas as pd

from cin_validator.rule_engine import (
CINTable,
IssueLocator,
RuleContext,
RuleType,
rule_definition,
)
from cin_validator.test_engine import run_rule

Assessments = CINTable.Assessments
AssessmentFactors = Assessments.AssessmentFactors


# define characteristics of rule
@rule_definition(
code="8945Q",
rule_type=RuleType.QUERY,
module=CINTable.Assessments,
message="Please check and either amend data or provide a reason: the assessment factors code '18A' should not be used ('18B' or '18C' should be used instead)",
affected_fields=[AssessmentFactors],
)
def validate(
data_container: Mapping[CINTable, pd.DataFrame], rule_context: RuleContext
):
df = data_container[Assessments].copy()

with_af = df[df[AssessmentFactors].notna()]
failing_indices = with_af[with_af[AssessmentFactors].str.upper() == "18A"].index

rule_context.push_issue(
table=Assessments, field=AssessmentFactors, row=failing_indices
)


def test_validate():
sample_assessments = pd.DataFrame(
{"AssessmentFactors": [pd.NA, "18a", "18A", "18B", "18C"]}
)

# Run rule function passing in our sample data
result = run_rule(validate, {Assessments: sample_assessments})

issues = list(result.issues)
# Intended fail points in data.
assert len(issues) == 2
# Intended failures of test data by index.
assert issues == [
IssueLocator(CINTable.Assessments, AssessmentFactors, 1),
IssueLocator(CINTable.Assessments, AssessmentFactors, 2),
]

# Checks rule code and message are correct.
assert result.definition.code == "8945Q"
assert (
result.definition.message
== "Please check and either amend data or provide a reason: the assessment factors code '18A' should not be used ('18B' or '18C' should be used instead)"
)

0 comments on commit 56cd68c

Please sign in to comment.