Skip to content

Commit

Permalink
Add DataSourceVersionsSynchronization.clean_data_source_versions()
Browse files Browse the repository at this point in the history
  • Loading branch information
kemar committed Dec 24, 2024
1 parent fd68304 commit 7a67d92
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Generated by Django 4.2.17 on 2024-12-24 14:43
# Generated by Django 4.2.17 on 2024-12-24 15:02

from django.conf import settings
from django.db import migrations, models
Expand Down Expand Up @@ -62,7 +62,7 @@ class Migration(migrations.Migration):
(
"source_version_to_compare_with",
models.ForeignKey(
help_text="The pyramid as a comparison.",
help_text="The version of the pyramid to use as a comparison.",
on_delete=django.db.models.deletion.CASCADE,
related_name="synchronized_as_source_version_to_compare_with",
to="iaso.sourceversion",
Expand All @@ -71,7 +71,7 @@ class Migration(migrations.Migration):
(
"source_version_to_update",
models.ForeignKey(
help_text="The pyramid for which we want to generate change requests.",
help_text="The version of the pyramid for which we want to generate change requests.",
on_delete=django.db.models.deletion.CASCADE,
related_name="synchronized_as_source_version_to_update",
to="iaso.sourceversion",
Expand Down
19 changes: 17 additions & 2 deletions iaso/models/data_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from django.contrib.auth.models import AnonymousUser, User
from django.contrib.postgres.fields import ArrayField
from django.core.exceptions import ValidationError
from django.db import models
from django.utils.translation import gettext_lazy as _

Expand Down Expand Up @@ -206,13 +207,13 @@ class DataSourceVersionsSynchronization(models.Model):
SourceVersion,
on_delete=models.CASCADE,
related_name="synchronized_as_source_version_to_update",
help_text=_("The pyramid for which we want to generate change requests."),
help_text=_("The version of the pyramid for which we want to generate change requests."),
)
source_version_to_compare_with = models.ForeignKey(
SourceVersion,
on_delete=models.CASCADE,
related_name="synchronized_as_source_version_to_compare_with",
help_text=_("The pyramid as a comparison."),
help_text=_("The version of the pyramid to use as a comparison."),
)

# The exact JSON format is defined in `iaso.diffing.dumper.DataSourceVersionsSynchronizationEncoder`.
Expand Down Expand Up @@ -249,6 +250,20 @@ class Meta:
def __str__(self) -> str:
return self.name

def save(self, *args, **kwargs):
self.clean()
super().save(*args, **kwargs)

def clean(self, *args, **kwargs):
super().clean()
self.clean_data_source_versions()

def clean_data_source_versions(self) -> None:
if self.source_version_to_update.data_source_id != self.source_version_to_compare_with.data_source_id:
raise ValidationError({"__all__": "The two versions to compare must be linked to the same data source."})
if self.source_version_to_update.pk == self.source_version_to_compare_with.pk:
raise ValidationError({"__all__": "The two versions to compare must be different."})

@property
def total_change_requests(self):
return self.count_create + self.count_update
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import time_machine

from django.contrib.gis.geos import MultiPolygon, Polygon
from django.core.exceptions import ValidationError

from iaso import models as m
from iaso.test import TestCase
Expand Down Expand Up @@ -167,6 +168,37 @@ def test_create(self):
self.assertEqual(data_source_sync.created_at, self.DT)
self.assertEqual(data_source_sync.updated_at, self.DT)

def test_clean_data_source_versions(self):
other_data_source = m.DataSource.objects.create(name="Other data source")
other_source_version = m.SourceVersion.objects.create(
data_source=other_data_source, number=1, description="Other data source version"
)
kwargs = {
"name": "Foo",
"source_version_to_update": self.source_version_to_update,
"source_version_to_compare_with": other_source_version,
"account": self.account,
"created_by": self.user,
}
data_source_sync = m.DataSourceVersionsSynchronization(**kwargs)

with self.assertRaises(ValidationError) as error:
data_source_sync.clean_data_source_versions()
self.assertIn("The two versions to compare must be linked to the same data source.", error.exception.messages)

kwargs = {
"name": "Foo",
"source_version_to_update": self.source_version_to_update,
"source_version_to_compare_with": self.source_version_to_update,
"account": self.account,
"created_by": self.user,
}
data_source_sync = m.DataSourceVersionsSynchronization(**kwargs)

with self.assertRaises(ValidationError) as error:
data_source_sync.clean_data_source_versions()
self.assertIn("The two versions to compare must be different.", error.exception.messages)

def test_create_json_diff(self):
"""
Test that `create_json_diff()` works as expected.
Expand Down

0 comments on commit 7a67d92

Please sign in to comment.