Procházet zdrojové kódy

Add tests for LeType

Yann Weber před 9 roky
rodič
revize
35c749c0f5
2 změnil soubory, kde provedl 120 přidání a 1 odebrání
  1. 1
    1
      leobject/test/test_leobject.py
  2. 119
    0
      leobject/test/test_letype.py

+ 1
- 1
leobject/test/test_leobject.py Zobrazit soubor

@@ -1,5 +1,5 @@
1 1
 """
2
-    Tests for _LeObject
2
+    Tests for _LeObject and LeObject
3 3
 """
4 4
 
5 5
 import unittest

+ 119
- 0
leobject/test/test_letype.py Zobrazit soubor

@@ -0,0 +1,119 @@
1
+"""
2
+    Test for LeType and childs
3
+"""
4
+
5
+import unittest
6
+from unittest import TestCase
7
+from unittest.mock import patch
8
+
9
+import EditorialModel
10
+import leobject
11
+import leobject.test.utils
12
+
13
+class LeTypeTestCase(TestCase):
14
+    
15
+    @classmethod
16
+    def setUpClass(cls):
17
+        """ Write the generated code in a temporary directory and import it """
18
+        cls.tmpdir = leobject.test.utils.tmp_load_factory_code()
19
+    @classmethod
20
+    def tearDownClass(cls):
21
+        """ Remove the temporary directory created at class setup """
22
+        leobject.test.utils.cleanup(cls.tmpdir)
23
+ 
24
+    def test_init(self):
25
+        """ testing the constructor """
26
+        from dyncode import Publication, Numero, LeObject
27
+        
28
+        with self.assertRaises(NotImplementedError):
29
+            leobject.letype.LeType(42)
30
+
31
+        badargs = [
32
+            {'class_id':Numero._class_id + 1},
33
+            {'type_id':Numero._type_id + 1},
34
+        ]
35
+        for badarg in badargs:
36
+            with self.assertRaises(RuntimeError):
37
+                Numero(42, **badarg)
38
+
39
+        badargs = [
40
+            ({'lodel_id':'foobar'}, TypeError),
41
+            ({'lodel_id': 42, 'titre_mais_qui_existe_pas':'hello'}, AttributeError),
42
+        ]
43
+        for badarg, expect_e in badargs:
44
+            with self.assertRaises(expect_e, msg="Invalid argument given %s"%badarg):
45
+                Numero(**badarg)
46
+    
47
+    ## @todo when we will have a field in a type that has a check_function try to make check_datas_or_raise raise with invalid value
48
+    def test_check_datas(self):
49
+        """ testing the check_datas* methods """
50
+        from dyncode import Publication, Numero, LeObject
51
+
52
+        datas = { 'titre':'foobar' }
53
+        Numero.check_datas(datas, False)
54
+        Numero.check_datas(datas, True)
55
+        with self.assertRaises(leobject.leobject.LeObjectError):
56
+            Numero.check_datas_or_raise({}, True)
57
+
58
+    @patch('leobject.letype.LeType.populate')
59
+    def test_datas(self, dsmock):
60
+        """ Testing the datas @property method """
61
+        from dyncode import Publication, Numero, LeObject
62
+        num = Numero(42, titre = 'foofoo')
63
+        self.assertEqual({'lodel_id' : 42, 'titre': 'foofoo'}, num.datas)
64
+        num.all_datas
65
+        dsmock.assert_called_once()
66
+
67
+class LeTypeMockDsTestCase(TestCase):
68
+    """ Tests that need to mock the datasource """
69
+
70
+    @classmethod
71
+    def setUpClass(cls):
72
+        """ Write the generated code in a temporary directory and import it """
73
+        cls.tmpdir = leobject.test.utils.tmp_load_factory_code()
74
+    @classmethod
75
+    def tearDownClass(cls):
76
+        """ Remove the temporary directory created at class setup """
77
+        leobject.test.utils.cleanup(cls.tmpdir)
78
+
79
+    @patch('leobject.datasources.dummy.DummyDatasource.get')
80
+    def test_populate(self, dsmock):
81
+        from dyncode import Publication, Numero, LeObject
82
+
83
+        num = Numero(1, type_id = Numero._type_id)
84
+        missing_fields = [f for f in Numero._fields if not (f in ['lodel_id', 'type_id'])]
85
+        num.populate()
86
+        dsmock.assert_called_once_with(Publication, Numero, missing_fields, [('lodel_id','=','1')],[])
87
+
88
+    @patch('leobject.datasources.dummy.DummyDatasource.update')
89
+    def test_update(self, dsmock):
90
+        from dyncode import Publication, Numero, LeObject
91
+        
92
+        datas = { 'titre' : 'foobar', 'string': 'wow' }
93
+
94
+        Numero.update(['lodel_id = 1'], datas)
95
+        dsmock.assert_called_once_with(Numero, Publication, [('lodel_id','=','1')], [], datas)
96
+
97
+    @patch('leobject.datasources.dummy.DummyDatasource.delete')
98
+    def test_delete(self, dsmock):
99
+        from dyncode import Publication, Numero, LeObject
100
+        
101
+        Numero.delete(['lodel_id = 1'])
102
+        dsmock.assert_called_once_with(Numero, Publication, [('lodel_id','=','1')], [])
103
+
104
+    @patch('leobject.datasources.dummy.DummyDatasource.update')
105
+    def test_db_update(self, dsmock):
106
+        from dyncode import Publication, Numero, LeObject
107
+        
108
+        num = Numero(1, type_id = Numero._type_id, class_id = Numero._class_id, titre = 'Hello world !')
109
+        num.db_update()
110
+        dsmock.assert_called_once_with(Numero, Publication, [('lodel_id','=','1')], [], num.datas)
111
+
112
+    @patch('leobject.datasources.dummy.DummyDatasource.delete')
113
+    def test_db_delete(self, dsmock):
114
+        from dyncode import Publication, Numero, LeObject
115
+
116
+        num = Numero(1, type_id = Numero._type_id, class_id = Numero._class_id, titre = 'Hello world !')
117
+        num.db_delete()
118
+        dsmock.assert_called_once_with(Numero, Publication, [('lodel_id','=','1')], [])
119
+

Loading…
Zrušit
Uložit