diff --git a/users/migrations/0004_alter_userorigin_origin_id.py b/users/migrations/0004_alter_userorigin_origin_id.py new file mode 100644 index 00000000..e885a288 --- /dev/null +++ b/users/migrations/0004_alter_userorigin_origin_id.py @@ -0,0 +1,21 @@ +# Generated by Django 4.2.13 on 2024-07-25 09:41 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("users", "0003_userorigin"), + ] + + operations = [ + migrations.AlterField( + model_name="userorigin", + name="origin_id", + field=models.CharField( + blank=True, default="", max_length=100, verbose_name="Origin ID" + ), + preserve_default=False, + ), + ] diff --git a/users/models.py b/users/models.py index 1dd9cec3..d3cc99c8 100644 --- a/users/models.py +++ b/users/models.py @@ -35,7 +35,7 @@ class UserOrigin(models.Model): user = models.ForeignKey(User, related_name="origins", on_delete=models.CASCADE) data_source = models.ForeignKey("hours.DataSource", on_delete=models.CASCADE) origin_id = models.CharField( - verbose_name=_("Origin ID"), max_length=100, null=True, blank=True + verbose_name=_("Origin ID"), max_length=100, blank=True ) class Meta: diff --git a/users/tests/conftest.py b/users/tests/conftest.py new file mode 100644 index 00000000..c0752e4e --- /dev/null +++ b/users/tests/conftest.py @@ -0,0 +1,6 @@ +from pytest_factoryboy import register + +from hours.tests.conftest import DataSourceFactory, UserFactory + +register(UserFactory) +register(DataSourceFactory) diff --git a/users/tests/test_models.py b/users/tests/test_models.py new file mode 100644 index 00000000..5f5d9546 --- /dev/null +++ b/users/tests/test_models.py @@ -0,0 +1,14 @@ +import pytest +from django.db import IntegrityError + +from users.models import UserOrigin + + +@pytest.mark.django_db +def test_user_origin_shouldnt_allow_duplicate_empty_origin_id(user, data_source): + """Duplicate UserOrigin instances shouldn't get created when data source is + the same but origin_id is empty. + """ + UserOrigin.objects.create(user=user, data_source=data_source) + with pytest.raises(IntegrityError): + UserOrigin.objects.create(user=user, data_source=data_source)