Skip to content

Commit 86a2920

Browse files
pabrrsBrobin
authored andcommitted
Max length restrictions (#46)
* max_length error tracking, on hold UUID unsubscriptable * removing __init__ files from code coverage and max_length issue fix * add more tests and constants default values for tests * .coveragerc troubles fixes
1 parent 4b06545 commit 86a2920

File tree

5 files changed

+89
-14
lines changed

5 files changed

+89
-14
lines changed

.coveragerc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[run]
2+
omit =
3+
*__init__.py*

django_seed/guessers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def __init__(self, faker):
2525

2626
def guess_format(self, name):
2727
"""
28-
Returns a faker method baed on the field's name
28+
Returns a faker method based on the field's name
2929
:param name:
3030
"""
3131
name = name.lower()

django_seed/seeder.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
from django_seed.exceptions import SeederException
77
from django_seed.guessers import NameGuesser, FieldTypeGuesser
88

9-
109
class ModelSeeder(object):
1110
def __init__(self, model):
1211
"""
@@ -87,10 +86,19 @@ def turn_off_auto_add(model):
8786
manager = self.model.objects.db_manager(using=using)
8887
turn_off_auto_add(manager.model)
8988

90-
obj = manager.create(**{
89+
faker_data = {
9190
field: format_field(field_format, inserted_entities)
9291
for field, field_format in self.field_formatters.items()
93-
})
92+
}
93+
94+
# max length restriction check
95+
for data_field in faker_data:
96+
field = self.model._meta.get_field(data_field)
97+
98+
if field.max_length and isinstance(faker_data[data_field], str):
99+
faker_data[data_field] = faker_data[data_field][:field.max_length]
100+
101+
obj = manager.create(**faker_data)
94102

95103
return obj.pk
96104

@@ -124,7 +132,7 @@ def add_entity(self, model, number, customFieldFormatters=None):
124132
model.field_formatters = model.guess_field_formatters(self.faker)
125133
if customFieldFormatters:
126134
model.field_formatters.update(customFieldFormatters)
127-
135+
128136
klass = model.model
129137
self.entities[klass] = model
130138
self.quantities[klass] = number

django_seed/tests.py

Lines changed: 72 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323

2424
fake = Faker()
2525

26+
DEF_LD = "default long description"
27+
DEF_SD = "default short description"
28+
2629
@contextmanager
2730
def django_setting(name, value):
2831
"""
@@ -69,7 +72,6 @@ class Player(models.Model):
6972
friends = models.PositiveIntegerField()
7073
balance = models.FloatField()
7174

72-
7375
class Action(models.Model):
7476
ACTION_FIRE ='fire'
7577
ACTION_MOVE ='move'
@@ -87,12 +89,18 @@ class Action(models.Model):
8789
target = models.ForeignKey(to=Player,on_delete=models.CASCADE, related_name='enemy_actions+', null=True)
8890

8991
class Product(models.Model):
90-
9192
name = models.CharField(max_length=100)
92-
short_description = models.CharField(max_length=100, default='default short description')
93-
description = models.TextField(default='default long description')
93+
short_description = models.CharField(max_length=100, default=DEF_SD)
94+
description = models.TextField(default=DEF_LD)
9495
enabled = models.BooleanField(default=True)
9596

97+
class Customer(models.Model):
98+
name = models.CharField(max_length=255)
99+
country = models.CharField(max_length=30)
100+
address = models.CharField(max_length=50)
101+
created_at = models.DateTimeField(auto_now=False, auto_now_add=True)
102+
comments = models.TextField(max_length=500)
103+
96104

97105
class NameGuesserTestCase(TestCase):
98106

@@ -279,7 +287,7 @@ def test_default_value_guessed_by_field_type(self):
279287

280288
product = Product.objects.get(id=_id[Product][0])
281289

282-
self.assertEquals(product.short_description, 'default short description')
290+
self.assertEquals(product.short_description, DEF_SD)
283291
self.assertTrue(product.enabled)
284292

285293
def test_default_value_guessed_by_field_name(self):
@@ -293,4 +301,62 @@ def test_default_value_guessed_by_field_name(self):
293301

294302
product = Product.objects.get(id=_id[Product][0])
295303

296-
self.assertEquals(product.description, 'default long description')
304+
self.assertEquals(product.description, DEF_LD)
305+
306+
class LengthRulesTestCase(TestCase):
307+
308+
def test_max_length(self):
309+
faker = fake
310+
seeder = Seeder(faker)
311+
312+
name_max_len = Customer._meta.get_field('name').max_length
313+
country_max_len = Customer._meta.get_field('country').max_length
314+
address_max_len = Customer._meta.get_field('address').max_length
315+
comments_max_len = Customer._meta.get_field('comments').max_length
316+
317+
rand = random.randint(1, 10)
318+
319+
data = {
320+
'name': 'x' * (name_max_len + rand),
321+
'country': 'p' * (country_max_len + rand),
322+
'address': 't' * (address_max_len + rand),
323+
'comments': 'o' * (comments_max_len + rand),
324+
}
325+
326+
seeder.add_entity(Customer, 1, data)
327+
_id = seeder.execute()
328+
329+
customer = Customer.objects.get(id=_id[Customer][0])
330+
331+
self.assertTrue(len(customer.name) <= name_max_len,
332+
"name with length {}, does not respect max length restriction of {}"
333+
.format(len(customer.name), name_max_len))
334+
335+
self.assertTrue(len(customer.country) <= country_max_len,
336+
"country with length {}, does not respect max length restriction of {}"
337+
.format(len(customer.name), country_max_len))
338+
339+
self.assertTrue(len(customer.address) <= address_max_len,
340+
"address with length {}, does not respect max length restriction of {}"
341+
.format(len(customer.name), address_max_len))
342+
343+
self.assertTrue(len(customer.comments) <= comments_max_len,
344+
"comments with length {}, does not respect max length restriction of {}"
345+
.format(len(customer.comments), comments_max_len))
346+
347+
348+
349+
350+
def test_default_with_max_length(self):
351+
faker = fake
352+
seeder = Seeder(faker)
353+
354+
seeder.add_entity(Product, 1)
355+
356+
_id = seeder.execute()
357+
358+
product = Product.objects.get(id=_id[Product][0])
359+
360+
self.assertTrue(len(DEF_LD) == len(product.description))
361+
362+

runtests.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,4 @@ def runtests():
4343

4444

4545
if __name__ == '__main__':
46-
runtests()
47-
48-
46+
runtests()

0 commit comments

Comments
 (0)