Merge branch 'develop' into feature/search_autocomplete_haste
[ganeti_webmgr.git] / ganeti / tests / fields.py
blob7efbcb1eaae56bf670cc02e6e73ffb9dcba5b69f
1 # Copyright (C) 2010 Oregon State University et al.
3 # This program is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU General Public License
5 # as published by the Free Software Foundation; either version 2
6 # of the License, or (at your option) any later version.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software
15 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
16 # USA.
18 from django.test import TestCase
20 from ganeti.fields import DataVolumeField
21 from django.core.exceptions import ValidationError
23 __all__ = ('TestDataVolumeFieldToPython',)
26 class TestDataVolumeFieldToPython(TestCase):
27 """
28 Test converting DataVolumeField to Python types using the to_python()
29 method.
30 """
32 def setUp(self):
33 self.f = DataVolumeField(required=True, min_value=0.)
35 def test_trivial(self):
36 """
37 Check that setUp() is sane.
38 """
40 pass
42 def test_clean_none(self):
43 """
44 Check that a ValidationError is raised when None is passed in.
45 """
47 self.assertRaises(ValidationError, self.f.clean, None)
49 def test_validationerror(self):
50 """
51 Make sure that ValidationError is raised when appropriate.
52 """
54 self.assertRaises(ValidationError, self.f.clean, 'gdrcigeudr7d')
55 self.assertRaises(ValidationError, self.f.clean, ' ')
56 self.assertRaises(ValidationError, self.f.clean, '')
58 # Wrong units?
59 self.assertRaises(ValidationError, self.f.clean, '100.0 GMB')
60 self.assertRaises(ValidationError, self.f.clean, '250 B')
61 self.assertRaises(ValidationError, self.f.clean, '50 yogdiecidu')
63 def test_empty_not_required(self):
64 """
65 Make sure that empty fields clean() to None when a value isn't
66 required.
67 """
69 self.f.required = False
70 self.assertEquals(self.f.clean(''), None)
71 self.assertEquals(self.f.clean(' '), None)
73 def test_correct_values(self):
74 """
75 Make sure that correct values are generated for valid data.
76 """
78 self.assertEquals(self.f.clean('9001 GB'), 9217024)
79 self.assertEquals(self.f.clean('9001.000 GB'), 9217024)
80 self.assertEquals(self.f.clean('9001G'), 9217024)
81 self.assertEquals(self.f.clean('0.5G'), 512)
82 self.assertEquals(self.f.clean('100.0 MB'), 100)
83 self.assertEquals(self.f.clean('100.00MB'), 100)
84 self.assertEquals(self.f.clean('100.000 M'), 100)
85 self.assertEquals(self.f.clean('100M'), 100)