1 |
|
---|
2 | from django import forms
|
---|
3 | from django.test import TestCase
|
---|
4 |
|
---|
5 | class FormFieldAttrstTestCase(TestCase):
|
---|
6 |
|
---|
7 | def test_char_field_attrs_are_present(self):
|
---|
8 | f = forms.CharField(min_length=10, max_length=20)
|
---|
9 | self.assertEquals(f.min_length, 10)
|
---|
10 | self.assertEquals(f.max_length, 20)
|
---|
11 |
|
---|
12 | def test_integer_field_attrs_are_present(self):
|
---|
13 | f = forms.IntegerField(min_value=10, max_value=20)
|
---|
14 | self.assertEquals(f.min_value, 10)
|
---|
15 | self.assertEquals(f.max_value, 20)
|
---|
16 |
|
---|
17 | def test_float_field_attrs_are_present(self):
|
---|
18 | f = forms.FloatField(min_value=10, max_value=20)
|
---|
19 | self.assertEquals(f.min_value, 10)
|
---|
20 | self.assertEquals(f.max_value, 20)
|
---|
21 |
|
---|
22 | def test_decimal_field_attrs_are_present(self):
|
---|
23 | f = forms.DecimalField(min_value=10, max_value=20, max_digits=8, decimal_places=4)
|
---|
24 | self.assertEquals(f.max_digits, 8)
|
---|
25 | self.assertEquals(f.decimal_places, 4)
|
---|
26 | self.assertEquals(f.min_value, 10)
|
---|
27 | self.assertEquals(f.max_value, 20)
|
---|
28 |
|
---|
29 | def test_char_field_min_length_and_max_length_can_be_set(self):
|
---|
30 | f = forms.CharField()
|
---|
31 | f.min_length = 2
|
---|
32 | f.max_length = 2
|
---|
33 | self.assertRaises(forms.ValidationError, f.clean, "x")
|
---|
34 | self.assertRaises(forms.ValidationError, f.clean, "xxx")
|
---|
35 |
|
---|
36 | def test_integer_field_min_value_and_max_value_can_be_set(self):
|
---|
37 | f = forms.IntegerField()
|
---|
38 | f.min_value = 2
|
---|
39 | f.max_value = 2
|
---|
40 | self.assertRaises(forms.ValidationError, f.clean, "1")
|
---|
41 | self.assertRaises(forms.ValidationError, f.clean, "3")
|
---|
42 |
|
---|
43 | def test_float_field_min_value_and_max_value_can_be_set(self):
|
---|
44 | f = forms.FloatField()
|
---|
45 | f.min_value = 2
|
---|
46 | f.max_value = 2
|
---|
47 | self.assertRaises(forms.ValidationError, f.clean, "1")
|
---|
48 | self.assertRaises(forms.ValidationError, f.clean, "3")
|
---|
49 |
|
---|
50 | def test_decimal_field_min_value_and_max_value_can_be_set(self):
|
---|
51 | f = forms.DecimalField()
|
---|
52 | f.min_value = 2
|
---|
53 | f.max_value = 2
|
---|
54 | self.assertRaises(forms.ValidationError, f.clean, "1")
|
---|
55 | self.assertRaises(forms.ValidationError, f.clean, "3")
|
---|
56 |
|
---|
57 | def test_decimal_field_max_digits_and_decimal_places_can_be_set(self):
|
---|
58 | f = forms.DecimalField()
|
---|
59 | f.max_digits = 2
|
---|
60 | f.decimal_places = 1
|
---|
61 | self.assertRaises(forms.ValidationError, f.clean, "111")
|
---|
62 | self.assertRaises(forms.ValidationError, f.clean, "0.11")
|
---|
63 |
|
---|