Ticket #1553: new-fields.py

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

new unittests (I can't replace old ;( )

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
21# TODO: AutoField
22class TestAutoField(unittest.TestCase):
23 def setUp(self):
24 self.klass = AutoField
25 #
26#
27run_test(TestAutoField)
28
29class TestBooleanField(unittest.TestCase):
30 def setUp(self):
31 self.klass = BooleanField
32 #
33 def test_create(self):
34 """ BooleanField:: create """
35 try:
36 self.klass(null=True)
37 self.assert_('No Assertion error for null=True')
38 except AssertionError:
39 pass
40 #
41
42 def test_conversion(self):
43 """ BooleanField:: conversion """
44 cf = self.klass()
45 self.assertEqual(cf.to_python('t'), True)
46 self.assertEqual(cf.to_python('f'), False)
47 self.assertEqual(cf.to_python(True), True)
48 self.assertEqual(cf.to_python(False), False)
49 self.assertEqual(cf.to_python(''), False)
50 self.assertRaises(validators.ValidationError, cf.to_python, None)
51 self.assertRaises(validators.ValidationError, cf.to_python, 'asd')
52 self.assertRaises(validators.ValidationError, cf.to_python, 10)
53 #
54#
55run_test(TestBooleanField)
56
57class TestNullBooleanField(unittest.TestCase):
58 def setUp(self):
59 self.klass = NullBooleanField
60 #
61
62 def test_conversion(self):
63 """ NullBooleanField:: conversion """
64 cf = self.klass()
65 self.assertEqual(cf.to_python('t'), True)
66 self.assertEqual(cf.to_python('f'), False)
67 self.assertEqual(cf.to_python(True), True)
68 self.assertEqual(cf.to_python(False), False)
69 self.assertEqual(cf.to_python(''), None)
70 self.assertEqual(cf.to_python(None), None)
71 self.assertRaises(validators.ValidationError, cf.to_python, 'asd')
72 self.assertRaises(validators.ValidationError, cf.to_python, 10)
73 #
74#
75run_test(TestNullBooleanField)
76
77class TestCommaSeparatedIntegerField(unittest.TestCase):
78 def setUp(self):
79 self.klass = CommaSeparatedIntegerField
80 #
81
82 def test_validate_maxlength(self):
83 """ CommaSeparatedIntegerField:: maxlength validation """
84 cf = self.klass(maxlength=2)
85 data = [1, 2, 3]
86 self.assertRaises(validators.ValidationError, cf.validate, data, {})
87 cf = self.klass(maxlength=3)
88 cf.validate(data, {})
89 #
90
91 def test_conversion(self):
92 """ CommaSeparatedIntegerField:: conversion """
93 cf = self.klass()
94 self.assertEqual(cf.to_python('1, 2, 3'), [1, 2, 3])
95 self.assertEqual(cf.to_python(''), [])
96 self.assertEqual(cf.to_python(None), [])
97 self.assertRaises(validators.ValidationError, cf.validate, '1, a, 3', {})
98 #
99#
100run_test(TestCommaSeparatedIntegerField)
101
102class TestCharField(unittest.TestCase):
103 def setUp(self):
104 self.klass = CharField
105 #
106
107 def test_conversion(self):
108 """ CharField:: conversion """
109 cf = self.klass()
110 self.assertEqual(cf.to_python(1), '1')
111 self.assertEqual(cf.to_python('10'), '10')
112 self.assertEqual(cf.to_python(''), '')
113 self.assertEqual(cf.to_python(None), None)
114 #
115
116 # I don't need to test null/blank validation, to_pyhon takes care of that
117 def test_validate_maxlength(self):
118 """ CharField:: maxlength validation """
119 cf = self.klass(maxlength=10)
120 self.assertRaises(validators.ValidationError, cf.validate, 'a' * 11, {})
121 cf.validate('b' * 10, {})
122 #
123#
124run_test(TestCharField)
125
126class TestDateField(unittest.TestCase):
127 # TODO: auto_now stuff tests
128 def setUp(self):
129 self.klass = DateField
130 self.data = '2006-10-15'
131 self.ret = datetime.date(*time.strptime(self.data, '%Y-%m-%d')[:3])
132 #
133
134 def test_conversion(self):
135 """ DateField:: conversion """
136 cf = self.klass()
137 self.assert_(isinstance(cf.to_python(self.data), datetime.date))
138 self.assertEqual(cf.to_python(''), None)
139 self.assertEqual(cf.to_python(None), None)
140 # TODO: support for other date formats??
141 #
142#
143run_test(TestDateField)
144
145class TestDateTimeField(unittest.TestCase):
146 # TODO: auto_now stuff tests
147 def setUp(self):
148 self.klass = DateTimeField
149 self.data = '2006-10-15 13:15:23'
150 self.ret = datetime.datetime(*time.strptime(self.data, '%Y-%m-%d %H:%M:%S')[:6])
151 #
152
153 def test_conversion(self):
154 """ DateTimeField:: conversion """
155 cf = self.klass()
156 self.assert_(isinstance(cf.to_python(self.data), datetime.date))
157 self.assertEqual(cf.to_python(''), None)
158 self.assertEqual(cf.to_python(None), None)
159 # TODO: support for other date formats??
160 #
161#
162run_test(TestDateTimeField)
163
164
165class TestEmailField(unittest.TestCase):
166 # TODO: auto_now stuff tests
167 def setUp(self):
168 self.klass = EmailField
169 #
170
171 def test_conversion(self):
172 """ EmailField:: conversion """
173 cf = self.klass()
174 self.assertEqual(cf.to_python('a@bc.de'), 'a@bc.de')
175 self.assertEqual(cf.to_python(''), '')
176 self.assertEqual(cf.to_python(None), None)
177 #
178
179 def test_validate(self):
180 """ EmailField:: validate """
181 cf = self.klass()
182 self.assertRaises(validators.ValidationError, cf.validate, 'a-b-c', {})
183 cf.validate('a@bc.de', {})
184 #
185#
186run_test(TestEmailField)
187
188# TODO: FileField
189class TestFileField(unittest.TestCase):
190 def setUp(self):
191 self.klass = FileField
192 #
193#
194run_test(TestFileField)
195# TODO: FilePathField
196class TestFilePathField(unittest.TestCase):
197 def setUp(self):
198 self.klass = FilePathField
199 #
200#
201run_test(TestFilePathField)
202
203class TestFloatField(unittest.TestCase):
204 def setUp(self):
205 self.klass = FloatField
206 #
207 def test_conversion(self):
208 """ FloatField:: conversion """
209 cf = self.klass()
210 self.assertEqual(cf.to_python('1.0'), Decimal('1.0'))
211 #
212
213 def test_validate(self):
214 """ FloatField:: validate """
215 cf = self.klass(max_digits=2)
216 self.assertRaises(validators.ValidationError, cf.validate, Decimal('132.21'), {})
217 cf.validate(Decimal('12.1223'), {})
218 self.assertEqual(cf.to_python(''), None)
219 self.assertEqual(cf.to_python(None), None)
220 #
221#
222run_test(TestFloatField)
223# TODO: ImageField
224class TestImageField(unittest.TestCase):
225 def setUp(self):
226 self.klass = ImageField
227 #
228#
229run_test(TestImageField)
230
231class TestIntegerField(unittest.TestCase):
232 def setUp(self):
233 self.klass = IntegerField
234 #
235 def test_conversion(self):
236 """ IntegerField:: conversion """
237 cf = self.klass()
238 self.assertEqual(cf.to_python('22'), 22)
239 self.assertEqual(cf.to_python(''), None)
240 self.assertEqual(cf.to_python(None), None)
241 #
242#
243run_test(TestIntegerField)
244
245class TestIPAddressField(unittest.TestCase):
246 def setUp(self):
247 self.klass = IPAddressField
248 #
249 def test_validate(self):
250 """ IPAddressField:: validate """
251 cf = self.klass()
252 self.assertRaises(validators.ValidationError, cf.validate, '12.13.4.257', {})
253 cf.validate('1.2.3.4', {})
254 #
255
256 def test_conversion(self):
257 """ IPAddressField:: conversion """
258 cf = self.klass()
259 self.assertEqual(cf.to_python('1.2.3.4'), '1.2.3.4')
260 self.assertEqual(cf.to_python(''), '')
261 self.assertEqual(cf.to_python(None), None)
262 #
263#
264run_test(TestIPAddressField)
265# TODO: PhoneNumberField
266# XXX Maybe this is more for contrib than trunk?
267class TestPhoneNumberField(unittest.TestCase):
268 def setUp(self):
269 self.klass = PhoneNumberField
270 #
271#
272run_test(TestPhoneNumberField)
273
274class TestPositiveIntegerField(unittest.TestCase):
275 def setUp(self):
276 self.klass = PositiveIntegerField
277 #
278 def test_conversion(self):
279 """ PositiveIntegerField:: conversion """
280 cf = self.klass()
281 self.assertEqual(cf.to_python('22'), 22)
282 self.assertEqual(cf.to_python(''), None)
283 self.assertEqual(cf.to_python(None), None)
284 #
285
286 def test_validate(self):
287 """ PositiveIntegerField:: validate """
288 cf = self.klass()
289 self.assertRaises(validators.ValidationError, cf.validate, -1, {})
290 cf.validate(1, {})
291 #
292#
293run_test(TestPositiveIntegerField)
294
295class TestPositiveSmallIntegerField(unittest.TestCase):
296 def setUp(self):
297 self.klass = PositiveSmallIntegerField
298 #
299
300 def test_conversion(self):
301 """ PositiveSmallIntegerField:: conversion """
302 cf = self.klass()
303 self.assertEqual(cf.to_python('22'), 22)
304 self.assertEqual(cf.to_python(''), None)
305 self.assertEqual(cf.to_python(None), None)
306 #
307
308 def test_validate(self):
309 """ PositiveSmallIntegerField:: validate """
310 cf = self.klass()
311 self.assertRaises(validators.ValidationError, cf.validate, -1, {})
312 cf.validate(1, {})
313 #
314#
315run_test(TestPositiveSmallIntegerField)
316
317class TestSlugField(unittest.TestCase):
318 def setUp(self):
319 self.klass = SlugField
320 #
321 def test_validate(self):
322 """ SlugField:: validate """
323 cf = self.klass()
324 self.assertRaises(validators.ValidationError, cf.validate, 'asAD d-ad_ad', {})
325 self.assertRaises(validators.ValidationError, cf.validate, '1 bad-start', {})
326 self.assertRaises(validators.ValidationError, cf.validate, 'bad-end 2', {})
327 cf.validate('valid-slug_', {})
328 #
329
330 def test_conversion(self):
331 """ SlugField:: conversion """
332 cf = self.klass()
333 self.assertEqual(cf.to_python('valid-slug'), 'valid-slug')
334 self.assertEqual(cf.to_python(''), '')
335 self.assertEqual(cf.to_python(None), None)
336 #
337#
338run_test(TestSlugField)
339
340class TestSmallIntegerField(unittest.TestCase):
341 def setUp(self):
342 self.klass = SmallIntegerField
343 #
344
345 def test_conversion(self):
346 """ SmallIntegerField:: conversion """
347 cf = self.klass()
348 self.assertEqual(cf.to_python('22'), 22)
349 self.assertEqual(cf.to_python(''), None)
350 self.assertEqual(cf.to_python(None), None)
351 #
352#
353run_test(TestSmallIntegerField)
354
355class TestTextField(unittest.TestCase):
356 def setUp(self):
357 self.klass = TextField
358 #
359
360 def test_conversion(self):
361 """ TextField:: conversion """
362 cf = self.klass()
363 self.assertEqual(cf.to_python('12sa asd asd azxc'), '12sa asd asd azxc')
364 self.assertEqual(cf.to_python(''), '')
365 self.assertEqual(cf.to_python(None), None)
366 #
367#
368run_test(TestTextField)
369
370class TestTimeField(unittest.TestCase):
371 def setUp(self):
372 self.klass = TimeField
373 self.data = '13:45:16'
374 self.ret = time.strptime(self.data, '%H:%M:%S')
375 #
376
377 def test_conversion(self):
378 """ TimeField:: conversion """
379 cf = self.klass()
380 ret = cf.to_python(self.data)
381 self.assert_(isinstance(ret, datetime.time), '%s != %s' % (type(ret), datetime.time))
382 self.assertEqual(cf.to_python(''), None)
383 self.assertEqual(cf.to_python(None), None)
384 # TODO: test other time formats and TZ info
385 #
386#
387run_test(TestTimeField)
388
389class TestURLField(unittest.TestCase):
390 def setUp(self):
391 self.klass = URLField
392 #
393
394 def test_conversion(self):
395 """ URLField:: conversion """
396 cf = self.klass()
397 self.assertEqual(cf.to_python('www.djangoproject.com'), 'www.djangoproject.com')
398 self.assertEqual(cf.to_python(''), '')
399 self.assertEqual(cf.to_python(None), None)
400 #
401
402 def test_validate(self):
403 """ URLField:: validate (this can fail if you don't have internet connection)"""
404 cf = self.klass(verify_exists=False)
405 self.assertRaises(validators.ValidationError, cf.validate, 'this.not.exists', {})
406 cf.validate('http://www.djangoproject.com', {})
407 #
408
409 def test_validate_exists(self):
410 """ URLField:: validate """
411 cf = self.klass(verify_exists=True)
412 self.assertRaises(validators.ValidationError, cf.validate, 'http://this.not.exists', {})
413 cf.validate('http://www.djangoproject.com', {})
414 #
415#
416run_test(TestURLField)
417
418# TODO: USStateField
419# XXX Maybe this is more for contrib than trunk?
420class TestUSStateField(unittest.TestCase):
421 def setUp(self):
422 self.klass = USStateField
423 #
424#
425run_test(TestUSStateField)
426# TODO: XMLField
427class TestXMLField(unittest.TestCase):
428 def setUp(self):
429 self.klass = XMLField
430 #
431#
432run_test(TestXMLField)
433
434# THE BIG TODO
435# TODO: OrderingField
436# TODO: ForeignKey
437# TODO: OneToOneField
438# TODO: ManyToManyField
439
440if __name__ != '__main__':
441 # for django runtests.py
442 if errors or failures:
443 assert False, 'some tests are failed (E: %d, F:%d)' % (errors, failures)
444else:
445 unittest.main()
Back to Top