2323
2424fake = Faker ()
2525
26+ DEF_LD = "default long description"
27+ DEF_SD = "default short description"
28+
2629@contextmanager
2730def django_setting (name , value ):
2831 """
@@ -69,7 +72,6 @@ class Player(models.Model):
6972 friends = models .PositiveIntegerField ()
7073 balance = models .FloatField ()
7174
72-
7375class 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
8991class 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
97105class 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+
0 commit comments