Mercurial > public > weighmail
comparison weighmail/tests/test_utils.py @ 9:a3ef032b6878
Added tests for the functions in utils. Fixed bug with GB.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Sun, 20 May 2012 12:13:33 -0500 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
8:472b0072a7f9 | 9:a3ef032b6878 |
---|---|
1 import unittest | |
2 | |
3 from weighmail.utils import get_limit, make_label | |
4 | |
5 | |
6 TEST_DATA = [ | |
7 ('0', 0), | |
8 ('42', 42), | |
9 ('500623', 500623), | |
10 ('1KB', 1024), | |
11 ('2KB', 2048), | |
12 ('1MB', 1024 * 1024), | |
13 ('5MB', 5 * 1024 * 1024), | |
14 ('1GB', 1024 * 1024 * 1024), | |
15 ('10GB', 10 * 1024 * 1024 * 1024), | |
16 ('0KB', 0), | |
17 ('0MB', 0), | |
18 ('0GB', 0), | |
19 ] | |
20 | |
21 INVALID_TEST_DATA = "foo -13 120JB $*)".split() | |
22 | |
23 | |
24 class GetLimitTestCase(unittest.TestCase): | |
25 | |
26 def test_get_limit(self): | |
27 | |
28 for x in TEST_DATA: | |
29 self.assertEqual(get_limit(x[0]), x[1]) | |
30 | |
31 def test_get_limit_lower(self): | |
32 | |
33 for x in TEST_DATA: | |
34 self.assertEqual(get_limit(x[0].lower()), x[1]) | |
35 | |
36 def test_get_limit_mixed(self): | |
37 | |
38 for x in TEST_DATA: | |
39 if x[0].endswith('B'): | |
40 x1 = x[0][:-2] + x[0][-2].lower() + x[0][-1] | |
41 x2 = x[0][:-2] + x[0][-2] + x[0][-1].lower() | |
42 self.assertEqual(get_limit(x1), x[1]) | |
43 self.assertEqual(get_limit(x2), x[1]) | |
44 | |
45 def test_invalid(self): | |
46 | |
47 for x in INVALID_TEST_DATA: | |
48 self.assertRaises(ValueError, get_limit, x) | |
49 | |
50 def test_none(self): | |
51 | |
52 self.assertIsNone(get_limit('')) | |
53 self.assertIsNone(get_limit(None)) | |
54 | |
55 | |
56 class MakeLabelTestCase(unittest.TestCase): | |
57 | |
58 def test_simple(self): | |
59 | |
60 label = make_label('red', '50', '100') | |
61 self.assertEqual(label.name, 'red') | |
62 self.assertEqual(label.min, 50) | |
63 self.assertEqual(label.max, 100) | |
64 | |
65 def test_no_min(self): | |
66 | |
67 for v in ['', None]: | |
68 label = make_label('red', v, '100') | |
69 self.assertEqual(label.name, 'red') | |
70 self.assertIsNone(label.min) | |
71 self.assertEqual(label.max, 100) | |
72 | |
73 def test_no_max(self): | |
74 | |
75 for v in ['', None]: | |
76 label = make_label('red', '210', v) | |
77 self.assertEqual(label.name, 'red') | |
78 self.assertEqual(label.min, 210) | |
79 self.assertIsNone(label.max) | |
80 | |
81 def test_invalid(self): | |
82 | |
83 self.assertRaises(ValueError, make_label, 'red', None, None) | |
84 self.assertRaises(ValueError, make_label, 'red', '', None) | |
85 self.assertRaises(ValueError, make_label, 'red', None, '') | |
86 self.assertRaises(ValueError, make_label, 'red', '', '') | |
87 self.assertRaises(ValueError, make_label, 'red', '100', '90') |