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,
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
):
28 Test converting DataVolumeField to Python types using the to_python()
33 self
.f
= DataVolumeField(required
=True, min_value
=0.)
35 def test_trivial(self
):
37 Check that setUp() is sane.
42 def test_clean_none(self
):
44 Check that a ValidationError is raised when None is passed in.
47 self
.assertRaises(ValidationError
, self
.f
.clean
, None)
49 def test_validationerror(self
):
51 Make sure that ValidationError is raised when appropriate.
54 self
.assertRaises(ValidationError
, self
.f
.clean
, 'gdrcigeudr7d')
55 self
.assertRaises(ValidationError
, self
.f
.clean
, ' ')
56 self
.assertRaises(ValidationError
, self
.f
.clean
, '')
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
):
65 Make sure that empty fields clean() to None when a value isn't
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
):
75 Make sure that correct values are generated for valid data.
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)