Ticket #1553: fields.py

File fields.py, 19.2 KB (added by Nebojsa Djordjevic <nesh at studioquattro dot co dot yu>, 19 years ago)

unit-tests for model fields (put this in <django root>/test/othertests/fields.py)

Line 
1# test/othertests/fields.py
2from django.core import validators
3from django.db.models.fields import *
4import random
5import unittest
6import datetime, time
7from decimal import Decimal
8import re
9
10errors = 0
11failures = 0
12def run_test(test):
13 if __name__ == '__main__': return
14 global errors, failures
15 suite = unittest.makeSuite(test)
16 ret = unittest.TextTestRunner().run(suite)
17 errors += len(ret.errors)
18 failures += len(ret.failures)
19#
20
21class TestAutoField(unittest.TestCase):
22 def setUp(self):
23 self.klass = AutoField
24 #
25 def test_create(self):
26 """ AutoField:: create """
27 self.assertRaises(AssertionError, self.klass)
28 self.klass(primary_key=True)
29 try:
30 self.klass(primary_key=True, blank=True)
31 self.assert_('No Assertion error for blank=True')
32 except AssertionError:
33 pass
34 try:
35 self.klass(primary_key=True, null=True)
36 self.assert_('No Assertion error for null=True')
37 except AssertionError:
38 pass
39 #
40
41 def test_conversion(self):
42 """ AutoField:: conversion """
43 cf = self.klass(primary_key=True)
44 self.assertEqual(cf.to_python(1), 1)
45 self.assertEqual(cf.to_python('10'), 10)
46 #
47#
48run_test(TestAutoField)
49
50class TestBooleanField(unittest.TestCase):
51 def setUp(self):
52 self.klass = BooleanField
53 #
54 def test_create(self):
55 """ BooleanField:: create """
56 try:
57 self.klass(null=True)
58 self.assert_('No Assertion error for null=True')
59 except AssertionError:
60 pass
61 #
62
63 def test_conversion(self):
64 """ BooleanField:: conversion """
65 cf = self.klass()
66 self.assertEqual(cf.to_python('t'), True)
67 self.assertEqual(cf.to_python('f'), False)
68 self.assertEqual(cf.to_python(True), True)
69 self.assertEqual(cf.to_python(False), False)
70 # XXX currently BooleanField don't accept integers as valid values which IMHO it should
71 #self.assertEqual(cf.to_python(''), False)
72 #self.assertEqual(cf.to_python(4023), True)
73 #self.assertEqual(cf.to_python(-4023), True)
74 #self.assertEqual(cf.to_python(0), False)
75 #
76#
77run_test(TestBooleanField)
78
79class TestNullBooleanField(unittest.TestCase):
80 def setUp(self):
81 self.klass = NullBooleanField
82 #
83
84 def test_conversion_null(self):
85 """ NullBooleanField:: conversion with NULL value """
86 cf = self.klass()
87 self.assertRaises(validators.ValidationError, cf.to_python, None)
88 cf = self.klass(null=True)
89 self.assertEqual(cf.to_python(None), None)
90 #
91
92 def test_conversion(self):
93 """ NullBooleanField:: conversion """
94 cf = self.klass()
95 self.assertEqual(cf.to_python('t'), True)
96 self.assertEqual(cf.to_python('f'), False)
97 self.assertEqual(cf.to_python(True), True)
98 self.assertEqual(cf.to_python(False), False)
99 self.assertEqual(cf.to_python(None), None)
100 # XXX currently BooleanField don't accept integers as valid values which IMHO it should
101 #self.assertEqual(cf.to_python(''), False)
102 #self.assertEqual(cf.to_python(4023), True)
103 #self.assertEqual(cf.to_python(-4023), True)
104 #self.assertEqual(cf.to_python(0), False)
105 #
106#
107run_test(TestNullBooleanField)
108
109class TestCommaSeparatedIntegerField(unittest.TestCase):
110 def setUp(self):
111 self.klass = CommaSeparatedIntegerField
112 #
113
114 def test_conversion_blank(self):
115 """ CommaSeparatedIntegerField:: conversion with blank value """
116 cf = self.klass()
117 self.assertRaises(validators.ValidationError, cf.to_python, '')
118 cf = self.klass(blank=True)
119 self.assertEqual(cf.to_python(''), '')
120 #
121
122 def test_conversion_null(self):
123 """ CommaSeparatedIntegerField:: conversion with NULL value """
124 cf = self.klass()
125 self.assertRaises(validators.ValidationError, cf.to_python, None)
126 cf = self.klass(null=True)
127 self.assertEqual(cf.to_python(None), [])
128 #
129 # I don't need to test null/blank validation, to_pyhon takes care of that
130 def test_validate_maxlength(self):
131 """ CommaSeparatedIntegerField:: maxlength validation """
132 cf = self.klass(maxlength=2)
133 data = [1, 2, 3]
134 self.assertRaises(validators.ValidationError, cf.validate, data, {})
135 cf = self.klass(maxlength=3)
136 cf.validate(data, {})
137 #
138
139 def test_conversion(self):
140 """ CommaSeparatedIntegerField:: conversion """
141 cf = self.klass()
142 self.assertEqual(cf.to_python('1, 2, 3'), [1, 2, 3])
143 self.assertRaises(validators.ValidationError, cf.validate, '1, a, 3', {})
144 #
145#
146run_test(TestCommaSeparatedIntegerField)
147
148class TestCharField(unittest.TestCase):
149 def setUp(self):
150 self.klass = CharField
151 #
152
153 def test_conversion(self):
154 """ CharField:: conversion """
155 cf = self.klass()
156 self.assertEqual(cf.to_python(1), '1')
157 self.assertEqual(cf.to_python('10'), '10')
158 #
159
160 def test_conversion_blank(self):
161 """ CharField:: conversion with blank value """
162 cf = self.klass()
163 self.assertRaises(validators.ValidationError, cf.to_python, '')
164 cf = self.klass(blank=True)
165 self.assertEqual(cf.to_python(''), '')
166 #
167
168 def test_conversion_null(self):
169 """ CharField:: conversion with NULL value """
170 cf = self.klass()
171 self.assertRaises(validators.ValidationError, cf.to_python, None)
172 cf = self.klass(null=True)
173 self.assertEqual(cf.to_python(None), None)
174 #
175 # I don't need to test null/blank validation, to_pyhon takes care of that
176 def test_validate_maxlength(self):
177 """ CharField:: maxlength validation """
178 cf = self.klass(maxlength=10)
179 self.assertRaises(validators.ValidationError, cf.validate, 'a' * 11, {})
180 cf.validate('b'*10, {})
181 #
182#
183run_test(TestCharField)
184
185class TestDateField(unittest.TestCase):
186 # TODO: auto_now stuff tests
187 def setUp(self):
188 self.klass = DateField
189 self.data = '2006-10-15'
190 self.ret = datetime.date(*time.strptime(self.data, '%Y-%m-%d')[:3])
191 #
192
193 def test_conversion(self):
194 """ DateField:: conversion """
195 cf = self.klass()
196 self.assertEqual(cf.to_python(self.data), self.ret)
197 # TODO: support for other date formats??
198 #
199
200 def test_conversion_null(self):
201 """ DateField:: conversion with NULL value """
202 cf = self.klass()
203 self.assertRaises(validators.ValidationError, cf.to_python, None)
204 cf = self.klass(null=True)
205 self.assertEqual(cf.to_python(None), None)
206 #
207#
208run_test(TestDateField)
209
210class TestDateTimeField(unittest.TestCase):
211 # TODO: auto_now stuff tests
212 def setUp(self):
213 self.klass = DateTimeField
214 self.data = '2006-10-15 13:15:23'
215 self.ret = datetime.datetime(*time.strptime(self.data, '%Y-%m-%d %H:%M:%S')[:3])
216 #
217
218 def test_conversion(self):
219 """ DateTimeField:: conversion """
220 cf = self.klass()
221 self.assertEqual(cf.to_python(self.data), self.ret)
222 # TODO: support for other date formats??
223 #
224
225 def test_conversion_null(self):
226 """ DateTimeField:: conversion with NULL value """
227 cf = self.klass()
228 self.assertRaises(validators.ValidationError, cf.to_python, None)
229 cf = self.klass(null=True)
230 self.assertEqual(cf.to_python(None), None)
231 #
232#
233run_test(TestDateTimeField)
234
235
236class TestEmailField(unittest.TestCase):
237 # TODO: auto_now stuff tests
238 def setUp(self):
239 self.klass = EmailField
240 #
241
242 def test_conversion(self):
243 """ EmailField:: conversion """
244 cf = self.klass()
245 self.assertEqual(cf.to_python('a@bc.de'), 'a@bc.de')
246 #
247
248 def test_validate(self):
249 """ EmailField:: validate """
250 cf = self.klass()
251 self.assertRaises(validators.ValidationError, cf.validate, 'a-b-c', {})
252 cf.validate('a@bc.de', {})
253 #
254
255 def test_conversion_blank(self):
256 """ EmailField:: conversion with blank value """
257 cf = self.klass()
258 self.assertRaises(validators.ValidationError, cf.to_python, '')
259 cf = self.klass(blank=True)
260 self.assertEqual(cf.to_python(''), '')
261 #
262
263 def test_conversion_null(self):
264 """ EmailField:: conversion with NULL value """
265 cf = self.klass()
266 self.assertRaises(validators.ValidationError, cf.to_python, None)
267 cf = self.klass(null=True)
268 self.assertEqual(cf.to_python(None), None)
269 #
270#
271run_test(TestEmailField)
272
273# TODO: FileField
274class TestFileField(unittest.TestCase):
275 def setUp(self):
276 self.klass = FileField
277 #
278#
279run_test(TestFileField)
280# TODO: FilePathField
281class TestFilePathField(unittest.TestCase):
282 def setUp(self):
283 self.klass = FilePathField
284 #
285#
286run_test(TestFilePathField)
287
288class TestFloatField(unittest.TestCase):
289 def setUp(self):
290 self.klass = FloatField
291 #
292 def test_conversion(self):
293 """ FloatField:: conversion """
294 cf = self.klass()
295 self.assertEqual(cf.to_python('1.0'), Decimal('1.0'))
296 #
297
298 def test_validate(self):
299 """ FloatField:: validate """
300 cf = self.klass(max_digits=2)
301 self.assertRaises(validators.ValidationError, cf.validate, Decimal('132.21'), {})
302 cf.validate(Decimal('12.1223'), {})
303 #
304
305 def test_conversion_blank(self):
306 """ FloatField:: conversion with blank value """
307 cf = self.klass()
308 self.assertRaises(validators.ValidationError, cf.to_python, '')
309 cf = self.klass(blank=True)
310 self.assertEqual(cf.to_python(''), '')
311 #
312
313 def test_conversion_null(self):
314 """ FloatField:: conversion with NULL value """
315 cf = self.klass()
316 self.assertRaises(validators.ValidationError, cf.to_python, None)
317 cf = self.klass(null=True)
318 self.assertEqual(cf.to_python(None), None)
319 #
320#
321run_test(TestFloatField)
322# TODO: ImageField
323class TestImageField(unittest.TestCase):
324 def setUp(self):
325 self.klass = ImageField
326 #
327#
328run_test(TestImageField)
329
330class TestIntegerField(unittest.TestCase):
331 def setUp(self):
332 self.klass = IntegerField
333 #
334 def test_conversion(self):
335 """ IntegerField:: conversion """
336 cf = self.klass()
337 self.assertEqual(cf.to_python('22'), 22)
338 #
339
340 def test_conversion_blank(self):
341 """ IntegerField:: conversion with blank value """
342 cf = self.klass()
343 self.assertRaises(validators.ValidationError, cf.to_python, '')
344 cf = self.klass(blank=True)
345 self.assertEqual(cf.to_python(''), '')
346 #
347
348 def test_conversion_null(self):
349 """ IntegerField:: conversion with NULL value """
350 cf = self.klass()
351 self.assertRaises(validators.ValidationError, cf.to_python, None)
352 cf = self.klass(null=True)
353 self.assertEqual(cf.to_python(None), None)
354 #
355#
356run_test(TestIntegerField)
357# TODO: IPAddressField
358class TestIPAddressField(unittest.TestCase):
359 def setUp(self):
360 self.klass = IPAddressField
361 #
362 def test_validate(self):
363 """ IPAddressField:: validate """
364 cf = self.klass()
365 self.assertRaises(validators.ValidationError, cf.validate, '12.13.4.257', {})
366 cf.validate('1.2.3.4', {})
367 #
368
369 def test_conversion(self):
370 """ IPAddressField:: conversion """
371 cf = self.klass()
372 self.assertEqual(cf.to_python('1.2.3.4'), '1.2.3.4')
373 #
374
375 def test_conversion_blank(self):
376 """ IPAddressField:: conversion with blank value """
377 cf = self.klass()
378 self.assertRaises(validators.ValidationError, cf.to_python, '')
379 cf = self.klass(blank=True)
380 self.assertEqual(cf.to_python(''), '')
381 #
382
383 def test_conversion_null(self):
384 """ IPAddressField:: conversion with NULL value """
385 cf = self.klass()
386 self.assertRaises(validators.ValidationError, cf.to_python, None)
387 cf = self.klass(null=True)
388 self.assertEqual(cf.to_python(None), None)
389 #
390#
391run_test(TestIPAddressField)
392# TODO: PhoneNumberField
393# XXX Maybe this is more for contrib than trunk?
394class TestPhoneNumberField(unittest.TestCase):
395 def setUp(self):
396 self.klass = PhoneNumberField
397 #
398#
399run_test(TestPhoneNumberField)
400
401class TestPositiveIntegerField(unittest.TestCase):
402 def setUp(self):
403 self.klass = PositiveIntegerField
404 #
405 def test_conversion(self):
406 """ PositiveIntegerField:: conversion """
407 cf = self.klass()
408 self.assertEqual(cf.to_python('22'), 22)
409 #
410
411 def test_validate(self):
412 """ PositiveIntegerField:: validate """
413 cf = self.klass()
414 self.assertRaises(validators.ValidationError, cf.validate, -1, {})
415 cf.validate(1, {})
416 #
417
418 def test_conversion_blank(self):
419 """ PositiveIntegerField:: conversion with blank value """
420 cf = self.klass()
421 self.assertRaises(validators.ValidationError, cf.to_python, '')
422 cf = self.klass(blank=True)
423 self.assertEqual(cf.to_python(''), '')
424 #
425
426 def test_conversion_null(self):
427 """ PositiveIntegerField:: conversion with NULL value """
428 cf = self.klass()
429 self.assertRaises(validators.ValidationError, cf.to_python, None)
430 cf = self.klass(null=True)
431 self.assertEqual(cf.to_python(None), None)
432 #
433#
434run_test(TestPositiveIntegerField)
435
436class TestPositiveSmallIntegerField(unittest.TestCase):
437 def setUp(self):
438 self.klass = PositiveSmallIntegerField
439 #
440
441 def test_conversion(self):
442 """ PositiveSmallIntegerField:: conversion """
443 cf = self.klass()
444 self.assertEqual(cf.to_python('22'), 22)
445 #
446
447 def test_validate(self):
448 """ PositiveSmallIntegerField:: validate """
449 cf = self.klass()
450 self.assertRaises(validators.ValidationError, cf.validate, -1, {})
451 cf.validate(1, {})
452 #
453
454 def test_conversion_blank(self):
455 """ PositiveSmallIntegerField:: conversion with blank value """
456 cf = self.klass()
457 self.assertRaises(validators.ValidationError, cf.to_python, '')
458 cf = self.klass(blank=True)
459 self.assertEqual(cf.to_python(''), '')
460 #
461
462 def test_conversion_null(self):
463 """ PositiveSmallIntegerField:: conversion with NULL value """
464 cf = self.klass()
465 self.assertRaises(validators.ValidationError, cf.to_python, None)
466 cf = self.klass(null=True)
467 self.assertEqual(cf.to_python(None), None)
468 #
469#
470run_test(TestPositiveSmallIntegerField)
471
472class TestSlugField(unittest.TestCase):
473 def setUp(self):
474 self.klass = SlugField
475 #
476 def test_validate(self):
477 """ SlugField:: validate """
478 cf = self.klass()
479 self.assertRaises(validators.ValidationError, cf.validate, 'asAD d-ad_ad', {})
480 cf.validate('valid-slug_', {})
481 #
482
483 def test_conversion_blank(self):
484 """ SlugField:: conversion with blank value """
485 cf = self.klass()
486 self.assertRaises(validators.ValidationError, cf.to_python, '')
487 cf = self.klass(blank=True)
488 self.assertEqual(cf.to_python(''), '')
489 #
490
491 def test_conversion_null(self):
492 """ SlugField:: conversion with NULL value """
493 cf = self.klass()
494 self.assertRaises(validators.ValidationError, cf.to_python, None)
495 cf = self.klass(null=True)
496 self.assertEqual(cf.to_python(None), None)
497 #
498#
499run_test(TestSlugField)
500
501class TestSmallIntegerField(unittest.TestCase):
502 def setUp(self):
503 self.klass = SmallIntegerField
504 #
505
506 def test_conversion(self):
507 """ SmallIntegerField:: conversion """
508 cf = self.klass()
509 self.assertEqual(cf.to_python('22'), 22)
510 #
511
512 def test_conversion_blank(self):
513 """ SmallIntegerField:: conversion with blank value """
514 cf = self.klass()
515 self.assertRaises(validators.ValidationError, cf.to_python, '')
516 cf = self.klass(blank=True)
517 self.assertEqual(cf.to_python(''), '')
518 #
519
520 def test_conversion_null(self):
521 """ SmallIntegerField:: conversion with NULL value """
522 cf = self.klass()
523 self.assertRaises(validators.ValidationError, cf.to_python, None)
524 cf = self.klass(null=True)
525 self.assertEqual(cf.to_python(None), None)
526 #
527#
528run_test(TestSmallIntegerField)
529
530class TestTextField(unittest.TestCase):
531 def setUp(self):
532 self.klass = TextField
533 #
534
535 def test_conversion_blank(self):
536 """ TextField:: conversion with blank value """
537 cf = self.klass()
538 self.assertRaises(validators.ValidationError, cf.to_python, '')
539 cf = self.klass(blank=True)
540 self.assertEqual(cf.to_python(''), '')
541 #
542
543 def test_conversion_null(self):
544 """ TextField:: conversion with NULL value """
545 cf = self.klass()
546 self.assertRaises(validators.ValidationError, cf.to_python, None)
547 cf = self.klass(null=True)
548 self.assertEqual(cf.to_python(None), None)
549 #
550#
551run_test(TestTextField)
552
553class TestTimeField(unittest.TestCase):
554 def setUp(self):
555 self.klass = TimeField
556 self.data = '13:45:16'
557 self.ret = time.strptime(self.data, '%H:%M:%S')
558 #
559
560 def test_conversion(self):
561 """ TestTimeField:: conversion """
562 cf = self.klass()
563 self.assertEqual(cf.to_python(self.data), self.ret)
564 # TODO: support for other date formats??
565 #
566
567 def test_conversion_null(self):
568 """ TestTimeField:: conversion with NULL value """
569 cf = self.klass()
570 self.assertRaises(validators.ValidationError, cf.to_python, None)
571 cf = self.klass(null=True)
572 self.assertEqual(cf.to_python(None), None)
573 #
574#
575run_test(TestTimeField)
576
577class TestURLField(unittest.TestCase):
578 def setUp(self):
579 self.klass = URLField
580 #
581
582 def test_conversion(self):
583 """ URLField:: conversion """
584 cf = self.klass()
585 self.assertEqual(cf.to_python('www.djangoproject.com'), 'www.djangoproject.com')
586 #
587
588 def test_validate(self):
589 """ URLField:: conversion """
590 cf = self.klass()
591 self.assertRaises(validators.ValidationError, cf.to_python, 'this.not.exists')
592 cf.to_python('www.djangoproject.com')
593 #
594
595 def test_conversion_blank(self):
596 """ URLField:: conversion with blank value """
597 cf = self.klass()
598 self.assertRaises(validators.ValidationError, cf.to_python, '')
599 cf = self.klass(blank=True)
600 self.assertEqual(cf.to_python(''), '')
601 #
602
603 def test_conversion_null(self):
604 """ URLField:: conversion with NULL value """
605 cf = self.klass()
606 self.assertRaises(validators.ValidationError, cf.to_python, None)
607 cf = self.klass(null=True)
608 self.assertEqual(cf.to_python(None), None)
609 #
610#
611run_test(TestURLField)
612# TODO: USStateField
613# XXX Maybe this is more for contrib than trunk?
614class TestUSStateField(unittest.TestCase):
615 def setUp(self):
616 self.klass = USStateField
617 #
618#
619run_test(TestUSStateField)
620# TODO: XMLField
621class TestXMLField(unittest.TestCase):
622 def setUp(self):
623 self.klass = XMLField
624 #
625#
626run_test(TestXMLField)
627
628# THE BIG TODO
629# TODO: OrderingField
630# TODO: ForeignKey
631# TODO: OneToOneField
632# TODO: ManyToManyField
633
634if __name__ != '__main__':
635 # for django runtests.py
636 if errors or failures:
637 assert False, 'some tests are failed (E: %d, F:%d)' % (errors, failures)
638else:
639 unittest.main()
Back to Top