Skip to content

Commit

Permalink
Merge pull request #153 from PoByBolek/allow-string-masks-as-lessons-…
Browse files Browse the repository at this point in the history
…for-unique-fields

Allow `Mask` objects as lessons for unique fields
  • Loading branch information
paulocheque committed Aug 18, 2023
2 parents ce0d3de + 9d24a0a commit afab4e3
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 3 deletions.
7 changes: 5 additions & 2 deletions django_dynamic_fixture/ddf.py
Original file line number Diff line number Diff line change
Expand Up @@ -685,7 +685,10 @@ def teach(self, model_class, ddf_lesson=None, **kwargs):
if field_name in self._DDF_CONFIGS:
continue
field = get_field_by_name_or_raise(model_class, field_name)
fixture = kwargs[field_name]
if field.unique and not (isinstance(fixture, (DynamicFixture, Copier, DataFixture)) or callable(fixture)):
if field.unique and not _is_dynamic_value(kwargs[field_name]):
raise InvalidConfigurationError('It is not possible to store static values for fields with unique=True (%s). Try using a lambda function instead.' % get_unique_field_name(field))
library.add_configuration(model_class, kwargs, name=ddf_lesson)


def _is_dynamic_value(fixture):
return isinstance(fixture, (DynamicFixture, Copier, DataFixture, Mask)) or callable(fixture)
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
from django.core.exceptions import ImproperlyConfigured


from django.contrib.gis.geos import *
try:
from django.contrib.gis.geos import *
from django.contrib.gis.db import models as geomodels
except ImproperlyConfigured:
pass # environment without geo libs
Expand Down
8 changes: 8 additions & 0 deletions django_dynamic_fixture/models_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,14 @@ class Meta:
app_label = 'django_dynamic_fixture'


class ModelWithUniqueCharField(models.Model):
text_unique = models.CharField(max_length=20, unique=True)

class Meta:
verbose_name = 'Unique char field'
app_label = 'django_dynamic_fixture'


class ModelForDDFSetup(models.Model):
integer = models.IntegerField(null=True)

Expand Down
12 changes: 12 additions & 0 deletions django_dynamic_fixture/tests/test_ddf_teaching_and_lessons.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# -*- coding: utf-8 -*-
import re

from django.test import TestCase
import pytest

Expand Down Expand Up @@ -37,6 +39,16 @@ def test_it_must_raise_an_error_if_try_to_set_a_static_value_to_a_field_with_uni
with pytest.raises(InvalidConfigurationError):
self.ddf.teach(ModelForLibrary, integer_unique=1000)

def test_it_allows_to_use_masks_as_lessons_for_unique_integer_fields(self):
self.ddf.teach(ModelForLibrary, integer_unique=Mask('1###'))
instance = self.ddf.get(ModelForLibrary)
assert 1000 <= int(instance.integer_unique) <= 1999

def test_it_allows_to_use_masks_as_lessons_for_unique_char_fields(self):
self.ddf.teach(ModelWithUniqueCharField, text_unique=Mask('---- ### __'))
instance = self.ddf.get(ModelWithUniqueCharField)
assert re.match(r'[A-Z]{4} [0-9]{3} [a-z]{2}', instance.text_unique)

def test_it_must_accept_dynamic_values_for_fields_with_unicity(self):
self.ddf.teach(ModelForLibrary, integer_unique=lambda field: 1000)

Expand Down

0 comments on commit afab4e3

Please sign in to comment.