Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/stringify codes #459

Merged
merged 3 commits into from
Sep 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions Documentation/debug_n_transform_scripts/stringify_rule_codes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import json
import re


def extract_rule_code(filepath):
pattern = r"rule_(\d+)\.py"
match = re.search(pattern, filepath)
if match:
return match.group(1)
else:
return None


with open("files_failed.json", "r") as f:
filepaths = json.load(f)
# filepath = "C:\\Users\\tambe.tabitha\\CIN-validator\\cin_validator\\rules\\cin2022_23\\rule_8940.py"

for filepath in filepaths:
print(f"+++++++++++++++++++++++++++++++{filepath}+++++++++++++++++++++++++++++++")
try:
rule_code = extract_rule_code(filepath)

with open(filepath, "r") as f:
file_text = f.read()
file_text = file_text.replace(f"{rule_code}", f"'{rule_code}'")
with open(filepath, "w") as f:
f.write(file_text)
print(f"updated {rule_code} to '{rule_code}'")
except Exception as e:
print(f"Error updating {filepath}: {e}")
continue
19 changes: 18 additions & 1 deletion cin_validator/__main__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import datetime
import importlib
import json
import os
import xml.etree.ElementTree as ET
from pathlib import Path
Expand Down Expand Up @@ -144,7 +145,23 @@ def test_cmd(rule, ruleset):
for p in module_folder.glob("*.py")
if p.stem != "__init__"
]
pytest.main(test_files)

failed_files = []
for file_path in test_files:
result = pytest.main([file_path])
if result != pytest.ExitCode.OK:
failed_files.append(file_path)

with open("files_failed.json", "w") as f:
json.dump(failed_files, f)
# pytest.main(test_files)


@cli.command(name="retest")
def retest():
with open("files_failed.json", "r") as f:
filepaths = json.load(f)
pytest.main(filepaths)


@cli.command(name="xmltocsv")
Expand Down
1 change: 1 addition & 0 deletions cin_validator/cin_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,7 @@ def __init__(
self.full_issue_df.drop_duplicates(
["child_id", "rule_code", "columns_affected", "row_id"], inplace=True
)
self.full_issue_df.reset_index(drop=True, inplace=True)

def get_rules_to_run(
self, registry, selected_rules: Optional[list[str]] = None
Expand Down
4 changes: 2 additions & 2 deletions cin_validator/rule_engine/__registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@


def rule_definition(
code: int,
code: str,
module: CINTable,
rule_type: RuleType = RuleType.ERROR,
message: Optional[str] = None,
Expand All @@ -29,7 +29,7 @@ def wrapper(*args, **kwargs):
return func(*args, **kwargs)

definition = RuleDefinition(
code=code,
code=str(code),
func=func,
rule_type=rule_type,
module=module,
Expand Down
4 changes: 2 additions & 2 deletions cin_validator/rules/cin2022_23/rule_100.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@


@rule_definition(
code=100,
code="100",
module=CINTable.Header,
message="Reference Date is incorrect",
affected_fields=[ReferenceDate],
Expand Down Expand Up @@ -72,5 +72,5 @@ def test_validate():
]

# Checks rule code and message are correct.
assert result.definition.code == 100
assert result.definition.code == "100"
assert result.definition.message == "Reference Date is incorrect"
4 changes: 2 additions & 2 deletions cin_validator/rules/cin2022_23/rule_1103.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@


@rule_definition(
code=1103,
code="1103",
module=CINTable.Assessments,
message="The assessment start date cannot be before the referral date",
affected_fields=[
Expand Down Expand Up @@ -209,7 +209,7 @@ def test_validate():
)
assert issue_rows.equals(expected_df)

assert result.definition.code == 1103
assert result.definition.code == "1103"
assert (
result.definition.message
== "The assessment start date cannot be before the referral date"
Expand Down
4 changes: 2 additions & 2 deletions cin_validator/rules/cin2022_23/rule_1104.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@


@rule_definition(
code=1104,
code="1104",
module=CINTable.Section47,
message="The date of the initial child protection conference cannot be before the referral date",
affected_fields=[
Expand Down Expand Up @@ -200,7 +200,7 @@ def test_validate():
)
assert issue_rows.equals(expected_df)

assert result.definition.code == 1104
assert result.definition.code == "1104"
assert (
result.definition.message
== "The date of the initial child protection conference cannot be before the referral date"
Expand Down
6 changes: 3 additions & 3 deletions cin_validator/rules/cin2022_23/rule_1105.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""
Rule number: 1105
Rule number: '1105'
Module: Child protection plans
Rule details: Where present, the <CPPStartDate> (N00105) must be on or after the <CINReferralDate> (N00100)
Rule message: The child protection plan start date cannot be before the referral date
Expand Down Expand Up @@ -29,7 +29,7 @@


@rule_definition(
code=1105,
code="1105",
module=CINTable.ChildProtectionPlans,
message="The child protection plan start date cannot be before the referral date",
affected_fields=[
Expand Down Expand Up @@ -208,7 +208,7 @@ def test_validate():

assert issue_rows.equals(expected_df)

assert result.definition.code == 1105
assert result.definition.code == "1105"
assert (
result.definition.message
== "The child protection plan start date cannot be before the referral date"
Expand Down
4 changes: 2 additions & 2 deletions cin_validator/rules/cin2022_23/rule_1510.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@


@rule_definition(
code=1510,
code="1510",
module=CINTable.ChildIdentifiers,
message="UPN invalid (wrong check letter at character 1)",
affected_fields=[UPN],
Expand Down Expand Up @@ -107,7 +107,7 @@ def test_validate():
IssueLocator(CINTable.ChildIdentifiers, UPN, 6),
]

assert result.definition.code == 1510
assert result.definition.code == "1510"
assert (
result.definition.message == "UPN invalid (wrong check letter at character 1)"
)
6 changes: 3 additions & 3 deletions cin_validator/rules/cin2022_23/rule_1520.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""
Rule number: 1520
Rule number: '1520'
Module: Child idenitifiers
Rule details: Each pupil <UPN> (N00001) must be unique across all pupils in the extract.
Note: This rule should be evaluated at LA-level for imported data
Expand All @@ -17,7 +17,7 @@


@rule_definition(
code=1520,
code="1520",
module=CINTable.ChildIdentifiers,
message="More than one record with the same UPN.",
affected_fields=[UPN],
Expand Down Expand Up @@ -125,5 +125,5 @@ def test_validate():
# Check that the rule definition is what you wrote in the context above.

# replace 8840 with the rule code and put the appropriate message in its place too.
assert result.definition.code == 1520
assert result.definition.code == "1520"
assert result.definition.message == "More than one record with the same UPN."
5 changes: 2 additions & 3 deletions cin_validator/rules/cin2022_23/rule_1530.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@


@rule_definition(
code=1530,
code="1530",
module=CINTable.ChildIdentifiers,
message="UPN invalid (characters 2-4 not a recognised LA code)",
affected_fields=[UPN],
Expand Down Expand Up @@ -98,7 +98,6 @@ def validate(


def test_validate():

child_identifiers = pd.DataFrame(
{
"UPN": [
Expand Down Expand Up @@ -127,7 +126,7 @@ def test_validate():
IssueLocator(CINTable.ChildIdentifiers, UPN, 6),
]

assert result.definition.code == 1530
assert result.definition.code == "1530"
assert (
result.definition.message
== "UPN invalid (characters 2-4 not a recognised LA code)"
Expand Down
4 changes: 2 additions & 2 deletions cin_validator/rules/cin2022_23/rule_1540.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@


@rule_definition(
code=1540,
code="1540",
module=CINTable.ChildIdentifiers,
message="UPN invalid (characters 5-12 not all numeric)",
affected_fields=[UPN],
Expand Down Expand Up @@ -57,5 +57,5 @@ def test_validate():
IssueLocator(CINTable.ChildIdentifiers, UPN, 3),
]

assert result.definition.code == 1540
assert result.definition.code == "1540"
assert result.definition.message == "UPN invalid (characters 5-12 not all numeric)"
4 changes: 2 additions & 2 deletions cin_validator/rules/cin2022_23/rule_1550.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@


@rule_definition(
code=1550,
code="1550",
module=CINTable.ChildIdentifiers,
message="UPN invalid (character 13 not a recognised value)",
affected_fields=[UPN],
Expand Down Expand Up @@ -85,7 +85,7 @@ def test_validate():
IssueLocator(CINTable.ChildIdentifiers, UPN, 2),
]

assert result.definition.code == 1550
assert result.definition.code == "1550"
assert (
result.definition.message == "UPN invalid (character 13 not a recognised value)"
)
5 changes: 3 additions & 2 deletions cin_validator/rules/cin2022_23/rule_2883.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@

Section47 = CINTable.Section47


# define characteristics of rule
@rule_definition(
code=2883,
code="2883",
# module is table that seems central to the condition.
module=CINTable.ChildProtectionPlans,
message="There are more child protection plans starting than initial conferences taking place",
Expand Down Expand Up @@ -150,6 +151,6 @@ def test_validate():

issues = result.la_issues
assert issues == (
2883,
"2883",
"There are more child protection plans starting than initial conferences taking place",
)
5 changes: 2 additions & 3 deletions cin_validator/rules/cin2022_23/rule_2884.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@


@rule_definition(
code=2884,
code="2884",
module=CINTable.Section47,
message="An initial child protection conference is recorded at both the S47 and CIN Details level and it should only be recorded in one",
affected_fields=[
Expand All @@ -25,7 +25,6 @@
def validate(
data_container: Mapping[CINTable, pd.DataFrame], rule_context: RuleContext
):

df_47 = data_container[Section47].copy()
df_cin = data_container[CINdetails].copy()

Expand Down Expand Up @@ -221,7 +220,7 @@ def test_validate():
)
assert issue_rows.equals(expected_df)

assert result.definition.code == 2884
assert result.definition.code == "2884"
assert (
result.definition.message
== "An initial child protection conference is recorded at both the S47 and CIN Details level and it should only be recorded in one"
Expand Down
6 changes: 3 additions & 3 deletions cin_validator/rules/cin2022_23/rule_2885.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@

# define characteristics of rule
@rule_definition(
# write the rule code here, in place of 2885
code=2885,
# write the rule code here, in place of '2885'
code="2885",
# replace ChildProtectionPlans with the value in the module column of the excel sheet corresponding to this rule .
# Note that even if multiple tables are involved, one table will be named in the module column.
module=CINTable.ChildProtectionPlans,
Expand Down Expand Up @@ -606,7 +606,7 @@ def test_validate():
assert issue_rows0.equals(expected_df0)

# Confirm that the rule details were properly pushed through.
assert result.definition.code == 2885
assert result.definition.code == "2885"
assert (
result.definition.message
== "Child protection plan shown as starting a different day to the initial child protection conference."
Expand Down
5 changes: 2 additions & 3 deletions cin_validator/rules/cin2022_23/rule_2889.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,14 @@


@rule_definition(
code=2889,
code="2889",
module=CINTable.Section47,
message="The S47 start date cannot be before the referral date.",
affected_fields=[S47ActualStartDate, CINreferralDate],
)
def validate(
data_container: Mapping[CINTable, pd.DataFrame], rule_context: RuleContext
):

df_s47 = data_container[Section47]
df_cin = data_container[CINdetails]

Expand Down Expand Up @@ -162,7 +161,7 @@ def test_validate():
)
assert issue_rows.equals(expected_df)

assert result.definition.code == 2889
assert result.definition.code == "2889"
assert (
result.definition.message
== "The S47 start date cannot be before the referral date."
Expand Down
4 changes: 2 additions & 2 deletions cin_validator/rules/cin2022_23/rule_2990.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

# define characteristics of rule
@rule_definition(
code=2990,
code="2990",
module=CINTable.CINdetails,
message="Activity is recorded against a case marked as ‘Case closed after assessment, no further action’ or 'case closed after assessment, referred to early help'.",
affected_fields=[
Expand Down Expand Up @@ -382,7 +382,7 @@ def test_validate():
)
assert issue_rows.equals(expected_df)

assert result.definition.code == 2990
assert result.definition.code == "2990"
assert (
result.definition.message
== "Activity is recorded against a case marked as ‘Case closed after assessment, no further action’ or 'case closed after assessment, referred to early help'."
Expand Down
4 changes: 2 additions & 2 deletions cin_validator/rules/cin2022_23/rule_4000.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@


@rule_definition(
code=4000,
code="4000",
module=CINTable.CINplanDates,
message="CIN Plan details provided for a referral with no further action",
affected_fields=[
Expand Down Expand Up @@ -156,7 +156,7 @@ def test_validate():
)
assert issue_rows.equals(expected_df)

assert result.definition.code == 4000
assert result.definition.code == "4000"
assert (
result.definition.message
== "CIN Plan details provided for a referral with no further action"
Expand Down
Loading
Loading