-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #492 from data-to-insight/486-error-code-8945q-add…
…ed-2024-v11 add rule 8945Q for 2024_25
- Loading branch information
Showing
1 changed file
with
61 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)" | ||
) |