Skip to content

Commit

Permalink
constraint violations as string
Browse files Browse the repository at this point in the history
  • Loading branch information
tilen1976 committed Dec 16, 2024
1 parent c178949 commit 0afc902
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 34 deletions.
6 changes: 3 additions & 3 deletions src/dapla_metadata/variable_definitions/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ def __init__(self, response_body: str) -> None:
self.status = data.get("status", "Unknown status")
if data.get("title") == "Constraint Violation":
violations = data.get("violations", [])
self.detail = [
f"{violation.get('field', 'Unknown field')}: {violation.get('message', 'No message provided')}"
self.detail = "".join(
f"\n{violation.get('field', 'Unknown field')}: {violation.get('message', 'No message provided')}"
for violation in violations
]
)

else:
self.detail = data.get("detail", "No detail provided")
Expand Down
19 changes: 0 additions & 19 deletions src/dapla_metadata/variable_definitions/vardef.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@
from dapla_metadata.variable_definitions.generated.vardef_client.api.variable_definitions_api import (
VariableDefinitionsApi,
)
from dapla_metadata.variable_definitions.generated.vardef_client.models.contact import (
Contact,
)
from dapla_metadata.variable_definitions.generated.vardef_client.models.draft import (
Draft,
)
Expand Down Expand Up @@ -173,19 +170,3 @@ def get_variable_definition(
date_of_validity=date_of_validity,
),
)

draft = Draft(
name={"nb": "testing"},
short_name="test",
definition={"nb": "def testing"},
classification_reference="91",
unit_types=["haha"],
subject_fields=["a"],
contains_sensitive_personal_information=True,
measurement_type="01",
valid_from=date(2024, 11, 1),
external_reference_uri="http://www.example.com",
comment=None,
related_variable_definition_uris=["http://www.example.com"],
contact=Contact(title={"nb": "title"}, email="[email protected]"),
)
1 change: 0 additions & 1 deletion tests/utils/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@
"message": "Invalid Dapla team"
},
{
"field": "updateVariableDefinitionById.updateDraft.owner.team",
"message": "must not be empty"
}
]
Expand Down
21 changes: 10 additions & 11 deletions tests/variable_definitions/vardef_client/test_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,34 +50,33 @@ def test_constraint_violation():
response_body = CONSTRAINT_VIOLATION_BODY
exc = VardefClientException(response_body)
assert exc.status == BAD_REQUEST_STATUS
assert (
exc.detail[0]
== "updateVariableDefinitionById.updateDraft.owner.team: Invalid Dapla team"
assert exc.detail == (
"\nupdateVariableDefinitionById.updateDraft.owner.team: Invalid Dapla team"
"\nupdateVariableDefinitionById.updateDraft.owner.team: must not be empty"
)


def test_constraint_violation_missing_messages():
response_body = CONSTRAINT_VIOLATION_BODY_MISSING_MESSAGES
exc = VardefClientException(response_body)
assert exc.status == BAD_REQUEST_STATUS
assert (
exc.detail[0]
== "updateVariableDefinitionById.updateDraft.owner.team: No message provided"
assert exc.detail == (
"\nupdateVariableDefinitionById.updateDraft.owner.team: No message provided"
"\nupdateVariableDefinitionById.updateDraft.owner.team: No message provided"
)


def test_constraint_violation_empty_violations():
response_body = CONSTRAINT_VIOLATION_BODY_MISSING_VIOLATIONS
exc = VardefClientException(response_body)
assert exc.status == BAD_REQUEST_STATUS
assert str(exc) == "Status 400: []"
assert str(exc) == "Status 400: "


def test_constraint_violation_empty_field():
response_body = CONSTRAINT_VIOLATION_BODY_MISSING_FIELD
exc = VardefClientException(response_body)
assert exc.status == BAD_REQUEST_STATUS
assert exc.detail == [
"Unknown field: Invalid Dapla team",
"updateVariableDefinitionById.updateDraft.owner.team: must not be empty",
]
assert exc.detail == (
"\nUnknown field: Invalid Dapla team\nUnknown field: must not be empty"
)

0 comments on commit 0afc902

Please sign in to comment.