Browse Source

Written some tests for PluginVersion

Yann Weber 8 years ago
parent
commit
278d10bf43
1 changed files with 89 additions and 0 deletions
  1. 89
    0
      tests/plugin/test_pluginversion.py

+ 89
- 0
tests/plugin/test_pluginversion.py View File

@@ -0,0 +1,89 @@
1
+import unittest
2
+
3
+from lodel.plugin.plugins import PluginVersion
4
+from lodel.plugin.exceptions import PluginVersionError
5
+
6
+class PluginVersionTestCase(unittest.TestCase):
7
+
8
+    def test_init_str(self):
9
+        """Tests PluginVersion instanciation from string"""
10
+        test_datas = [(1,2,3), (0,0,0), (54,0,1)]
11
+
12
+        for test_data in test_datas:
13
+            str_init = '.'.join([str(v) for v in test_data])
14
+            v = PluginVersion(str_init)
15
+            self.assertEqual(str(v), str_init)
16
+            for i, field in enumerate(['major', 'minor', 'revision']):
17
+                self.assertEqual(test_data[i], getattr(v, field))
18
+    
19
+    def test_init_short_str(self):
20
+        """ Tests PluginVersion instanciation from string with less than
21
+            3 numbers"""
22
+        #Tuples with shortstr, expected result
23
+        test_datas = [
24
+            ("1", "1.0.0"),
25
+            ("42", "42.0.0"),
26
+            ("0.1", "0.1.0"),
27
+            ("1.0", "1.0.0"),
28
+            ("0", "0.0.0")]
29
+        for test_str, expt_res in test_datas:
30
+            v = PluginVersion(test_str)
31
+            self.assertEqual(expt_res, str(v),
32
+                "When instanctiated with '%s' expected result was '%s', but \
33
+got : '%s'" % (test_str, expt_res, str(v)))
34
+
35
+    def test_init_invalid_str(self):
36
+        """Tests PluginVersion instanciation from invalid string"""
37
+        test_datas = {
38
+            'bad count': '1.2.3.4.5',
39
+            'bad values': 'a.1.2',
40
+            'bad separators': '1;2;3',
41
+            'bad value': 'foobar' }
42
+
43
+        for fail_reason, value in test_datas.items():
44
+            with self.assertRaises(PluginVersionError,msg="Instanciation \
45
+should fail when '%s' is given as argument because it is a %s" % (
46
+    value, fail_reason)):
47
+                v = PluginVersion(value)
48
+
49
+    def test_comparison(self):
50
+        """ Tests comparison operators on PluginVersion """
51
+        cmp_funs = [
52
+            PluginVersion.__lt__,
53
+            PluginVersion.__le__,
54
+            PluginVersion.__eq__,
55
+            PluginVersion.__ne__,
56
+            PluginVersion.__gt__,
57
+            PluginVersion.__ge__]
58
+
59
+        cmp_values_res = [
60
+            (   ("0.0.0", "0.0.0"),
61
+                (False, True, True, False, False, True)),
62
+            (   ("1.0.0", "0.0.0"),
63
+                (False, False, False, True, True, True)),
64
+            (   ("0.0.0", "1.0.0"),
65
+                (True, True, False, True, False, False)),
66
+            (   ("3.2.1", "2.2.1"),
67
+                (False, False, False, True, True, True)),
68
+            (   ("3.2.1", "3.2.2"),
69
+                (True, True, False, True, False, False)),
70
+            (   ("3.2.1", "3.3.0"),
71
+                (True, True, False, True, False, False)),
72
+            (   ("3.2.2018", "3.2.1"),
73
+                (False, False, False, True, True, True)),
74
+            (   ("3.42.0", "3.24.520000"),
75
+                (False, False, False, True, True, True))
76
+        ]
77
+
78
+        for cmp_cnt, (cmp_str_values, expt_res) in enumerate(cmp_values_res):
79
+            cmp_values = tuple([PluginVersion(v) for v in cmp_str_values])
80
+            for i, cmp_fun in enumerate(cmp_funs):
81
+                if expt_res[i]:
82
+                    self.assertTrue(cmp_fun(*cmp_values),
83
+                        msg="Expected comparison %d %s%s to be True, but False \
84
+returned" % (cmp_cnt, cmp_fun, cmp_values))
85
+                else:
86
+                    self.assertFalse(cmp_fun(*cmp_values),
87
+                        msg="Expected comparison %d %s%s to be False, but True \
88
+returned" % (cmp_cnt, cmp_fun, cmp_values))
89
+                

Loading…
Cancel
Save