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

TP2000-143: 500 on Measure Detail view #497

Merged
merged 6 commits into from
Feb 18, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
14 changes: 14 additions & 0 deletions measures/models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from datetime import date
from typing import Set

from django.core.exceptions import ValidationError
from django.db import models

from common.business_rules import UpdateValidity
Expand Down Expand Up @@ -647,6 +648,8 @@ def save(self, *args, force_write=False, **kwargs):
instance = super().save(*args, force_write=force_write, **kwargs)
return self.copy(self.transaction)

self.full_clean()

return super().save(*args, force_write=force_write, **kwargs)

def diff_components(
Expand Down Expand Up @@ -697,6 +700,17 @@ def diff_components(
transaction=workbasket.new_transaction(),
)

def clean(self, *args, **kwargs):
duplicates = Measure.objects.approved_up_to_transaction(
self.transaction,
).filter(sid=self.sid)
if self.version_group:
duplicates = duplicates.exclude(version_group=self.version_group)

if duplicates.exists():
raise ValidationError(f"Measure with sid {self.sid} already exists")
super().clean(*args, **kwargs)


class MeasureComponent(TrackedModel):
"""Contains the duty information or part of the duty information."""
Expand Down
13 changes: 13 additions & 0 deletions measures/tests/test_models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from decimal import Decimal

import pytest
from django.core.exceptions import ValidationError

from common.tests import factories
from common.validators import UpdateType
Expand Down Expand Up @@ -425,3 +426,15 @@ def test_diff_components_delete(

assert deleted.exists()
assert deleted.first().transaction == workbasket.current_transaction


def test_measure_with_duplicate_sid_raises_error():
"""Check that creating a new version doesn't raise an error and that error
is raised when version_group differs."""
model = factories.MeasureFactory.create()
model.new_version(model.transaction.workbasket)

with pytest.raises(ValidationError) as err:
factories.MeasureFactory.create(sid=model.sid)

assert f"Measure with sid {model.sid} already exists" in err.value.messages