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

Create control share validation #511

Merged
merged 1 commit into from
Aug 23, 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
9 changes: 9 additions & 0 deletions conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,15 @@ def basic_template_form(
action=fake.sentence(),
identifier="y-tunnus",
)
field_factory(
label="Hallintaosuus",
section=company_applicant_section,
type=basic_field_types["textbox"],
hint_text=fake.sentence(),
validation=fake.sentence(),
action=fake.sentence(),
identifier="hallintaosuus",
)

# Subsection for company's contact person information
contact_person_company_applicant_section = section_factory(
Expand Down
7 changes: 6 additions & 1 deletion forms/serializers/form.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@
from ..enums import FormState
from ..models import Answer, Choice, Entry, Field, FieldType, Form, Section
from ..models.form import Attachment, EntrySection
from ..validators.answer import FieldRegexValidator, RequiredFormFieldValidator
from ..validators.answer import (
ControlShareValidation,
FieldRegexValidator,
RequiredFormFieldValidator,
)


class RecursiveSerializer(serializers.Serializer):
Expand Down Expand Up @@ -485,6 +489,7 @@ class Meta:
FieldRegexValidator(
"[0-9]{7}-?[0-9]{1}$", "invalid_company_id", "y-tunnus"
),
ControlShareValidation(),
]

def get_information_checks(self, obj):
Expand Down
21 changes: 18 additions & 3 deletions forms/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,13 @@ def test_answer_post(
},
{
"sections": {},
"fields": {"business-id": {"value": "", "extraValue": ""}},
"fields": {
"business-id": {
"value": "",
"extraValue": "",
"hallintaosuus": {"value": "1/1", "extraValue": ""},
}
},
},
],
"hakijan-tiedot": {
Expand Down Expand Up @@ -171,7 +177,13 @@ def test_answer_post(
"company-information": [
{
"sections": {},
"fields": {"company-name": {"value": "jee", "extraValue": ""}},
"fields": {
"company-name": {
"value": "jee",
"extraValue": "",
"hallintaosuus": {"value": "1/1", "extraValue": ""},
}
},
},
],
"hakijan-tiedot": {
Expand Down Expand Up @@ -334,7 +346,10 @@ def test_attachment_post(
},
{
"sections": {},
"fields": {"business-id": {"value": "", "extraValue": ""}},
"fields": {
"business-id": {"value": "", "extraValue": ""},
"hallintaosuus": {"value": "1/1", "extraValue": ""},
},
},
],
"contact-person": {
Expand Down
62 changes: 62 additions & 0 deletions forms/tests/test_serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ def test_all_required_fields_answered_validator(
parent__isnull=True
)[1]

entries["hakijan-tiedot"]["sections"]["company-information"]["fields"][
"hallintaosuus"
] = {"value": "1/1", "extraValue": ""}

entries[empty_section.identifier]["sections"]["person-information"] = list()
entries[empty_section.identifier]["sections"]["person-information"].append(
{
Expand Down Expand Up @@ -139,6 +143,9 @@ def test_social_security_validator(
entries["hakijan-tiedot"]["sections"]["company-information"]["fields"][
"y-tunnus"
] = {"value": "1234567-8", "extraValue": ""}
entries["hakijan-tiedot"]["sections"]["company-information"]["fields"][
"hallintaosuus"
] = {"value": "1/1", "extraValue": ""}

answer_data = {
"form": basic_template_form.id,
Expand Down Expand Up @@ -179,6 +186,9 @@ def test_company_id_validator(
entries["hakijan-tiedot"]["sections"]["company-information"]["fields"][
"y-tunnus"
] = {"value": "1234567-8", "extraValue": ""}
entries["hakijan-tiedot"]["sections"]["company-information"]["fields"][
"hallintaosuus"
] = {"value": "1/1", "extraValue": ""}

answer_data = {
"form": basic_template_form.id,
Expand All @@ -202,6 +212,58 @@ def test_company_id_validator(
assert val_error.value.args[0]["non_field_errors"][0].code == "invalid_company_id"


@pytest.mark.django_db
def test_control_share(
django_db_setup, basic_template_form, plot_search_target, admin_user
):
entries = {}
for section in basic_template_form.sections.filter(parent__isnull=True):
entries[section.identifier] = dict()
generate_entries(section, entries[section.identifier])
entries["hakijan-tiedot"]["sections"]["company-information"]["sections"][
"contact-person"
]["fields"]["henkilotunnus"] = {"value": "010181-900C", "extraValue": ""}
entries["hakijan-tiedot"]["sections"]["company-information"]["fields"][
"y-tunnus"
] = {"value": "1234567-8", "extraValue": ""}
entries["hakijan-tiedot"]["sections"]["company-information"]["fields"][
"hallintaosuus"
] = {"value": "1/3", "extraValue": ""}
entries["hakijan-tiedot"]["sections"].update(
{
"company-information[0]": {
"fields": {
"hallintaosuus": {"value": "2/3", "extraValue": "",}, # noqa: E231
}
}
}
)

answer_data = {
"form": basic_template_form.id,
"user": admin_user.id,
"entries": entries,
"targets": [plot_search_target.pk,], # noqa: E231
}

answer_serializer = AnswerSerializer(data=answer_data)
# test that a correctly formatted ssn passes the validator
assert answer_serializer.is_valid()

answer_data["entries"]["hakijan-tiedot"]["sections"]["company-information[0]"][
"fields"
]["hallintaosuus"] = {"value": "1/3", "extraValue": ""}
answer_serializer = AnswerSerializer(data=answer_data)

# test that a incorrectly formatted ssn is caught by the validator
Copy link
Contributor

@PasiVuohijoki PasiVuohijoki Aug 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment is copypasted from previous test. Maybe something like "test that control share validator returns validation error if the sum is not 1"

with pytest.raises(ValidationError) as val_error:
answer_serializer.is_valid(True)
assert (
val_error.value.args[0]["non_field_errors"][0].code
== "control share is not even"
)


@pytest.mark.django_db
def test_target_status(
django_db_setup,
Expand Down
22 changes: 22 additions & 0 deletions forms/validators/answer.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,25 @@ def required_validator(
self.required_validator(
entries[entry], required_fields, section_identifier=section_identifier
)


class ControlShareValidation:
def __call__(self, value):
result = 0
for control_share in self.control_share_finder_generator(value["entries"]):
values = control_share.split("/")
result += int(values[0]) / int(values[1])
if round(result, 10) != 1:
raise ValidationError(code="control share is not even")

def control_share_finder_generator(self, entries):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ooo, nice generator function :D

if not isinstance(entries, Iterable) or isinstance(entries, str):
return
if "hallintaosuus" in entries:
yield entries["hallintaosuus"]["value"]
if isinstance(entries, list):
for entry in entries:
yield from self.control_share_finder_generator(entry)
return
for entry in entries:
yield from self.control_share_finder_generator(entries[entry])
Loading