Browse Source

First commit for EmComponent test

Written test environment init
Written test for newUid and __init__
Yann Weber 9 years ago
parent
commit
fdb7674e93
1 changed files with 174 additions and 4 deletions
  1. 174
    4
      EditorialModel/test/test_component.py

+ 174
- 4
EditorialModel/test/test_component.py View File

@@ -1,10 +1,180 @@
1
+import os
2
+import logging
3
+
1 4
 #from django.test import TestCase
5
+from django.conf import settings
6
+
2 7
 from unittest import TestCase
3
-from EditorialModel.component import EmComponent
8
+import unittest
9
+
10
+from EditorialModel.components import EmComponent, EmComponentNotExistError
11
+
12
+from Database.sqlsetup import SQLSetup
13
+from Database.sqlwrapper import SqlWrapper
14
+from Database import sqlutils
15
+import sqlalchemy as sqla
16
+
17
+os.environ.setdefault("DJANGO_SETTINGS_MODULE", "Lodel.settings")
18
+
19
+
20
+#A dummy EmComponent child class use to make tests
21
+class EmTestComp(EmComponent):
22
+    table = 'ttest'
23
+    def __init__(self, ion):
24
+        super(EmTestComp, self).__init__(ion)
4 25
 
5 26
 class ComponentTestCase(TestCase):
6 27
 
7
-    def test_component_instanciate_with_numeric_id(self):
8
-        testComp = EmComponent(2)
9
-        self.assertEqual(testComp.id, 2)
28
+    # ############# # 
29
+    #  TESTS SETUP  #
30
+    # ############# #
31
+    @classmethod
32
+    def setUpClass(cls):
33
+        #Overwritting db confs to make tests
34
+        settings.LODEL2SQLWRAPPER = {
35
+            'default': {
36
+                'ENGINE': 'sqlite',
37
+                'NAME': '/tmp/testdb.sqlite'
38
+            }
39
+        }
40
+        
41
+        #Disable logging but CRITICAL
42
+        logging.basicConfig(level=logging.CRITICAL)
43
+
44
+        #testDB setup
45
+        #   TODO May be slow
46
+        sqls = SQLSetup()
47
+        tables = sqls.get_schema()
48
+        ttest = {   'name':'ttest',
49
+                    'columns':  [
50
+                        {"name":"uid",          "type":"INTEGER", "extra":{"foreignkey":"uids.uid", "nullable":False, "primarykey":True}},
51
+                        {"name":"name",         "type":"VARCHAR(50)", "extra":{"nullable":False, "unique":True}},
52
+                        {"name":"string",       "type":"TEXT"},
53
+                        {"name":"help",         "type":"TEXT"},
54
+                        {"name":"rank",         "type":"INTEGER"},
55
+                        {"name":"date_update",  "type":"DATE"},
56
+                        {"name":"date_create",  "type":"DATE"}
57
+                    ]
58
+                }
59
+        tables.append(ttest)
60
+
61
+        
62
+        cls.db = SqlWrapper(read_db='default', write_db = 'default')
63
+        cls.tables = tables
64
+
65
+    @property
66
+    def db(self):
67
+        return self.__class__.db
68
+    @property
69
+    def tables(self):
70
+        return self.__class__.tables
71
+
72
+    def setUp(self):
73
+        # Db RAZ
74
+        self.db.dropAll()
75
+        # Db schema creation
76
+        self.db.createAllFromConf(self.tables);
77
+        self.dber = self.db.r_engine
78
+        self.dbew = self.db.w_engine
79
+        
80
+        # Test Em creation
81
+        conn = self.dber.connect()
82
+        test_table = sqla.Table(EmTestComp.table, sqlutils.meta(self.dber))
83
+        uids_table = sqla.Table('uids', sqlutils.meta(self.dber))
84
+        tuid = [ 1, 2, 3 , 42, 84 , 1025 ]
85
+
86
+        #Creating uid for the EmTestComp
87
+        for uid in tuid:
88
+            req = uids_table.insert(values={'uid':uid, 'table': EmTestComp.table })
89
+            conn.execute(req)
90
+
91
+        self.test_values = [
92
+            { 'uid': tuid[0], 'name': 'test', 'string': 'testcomp', 'help': 'help test', 'rank': 0},
93
+            { 'uid': tuid[1], 'name': 'test-em_comp', 'string': 'Super test comp', 'help': '', 'rank': 1},
94
+            { 'uid': tuid[2], 'name': 'test2', 'string': '', 'help': '', 'rank': 3},
95
+            { 'uid': tuid[3], 'name': 'foo', 'string': 'bar', 'help': 'foobar', 'rank': 1337},
96
+            { 'uid': tuid[4], 'name': '123', 'string': '456', 'help': '4242', 'rank': 42},
97
+            { 'uid': tuid[5], 'name': 'name', 'string': 'string', 'help': 'help', 'rank': 12},
98
+        ]
99
+
100
+        req = test_table.insert(values=self.test_values)
101
+        conn.execute(req)
102
+        conn.close()
103
+        pass
104
+    
105
+    # ############# #
106
+    #  TESTS BEGIN  #
107
+    # ############# #
108
+
109
+    #
110
+    #   EmComponent.newUid
111
+    #
112
+    def test_newuid(self):
113
+        """ Test valid calls for newUid method """
114
+        for _ in range(10):
115
+            nuid = EmTestComp.newUid()
116
+        
117
+            conn = self.dber.connect()
118
+            tuid = sqla.Table('uids', sqlutils.meta(self.dber))
119
+            req = sqla.select([tuid]).where(tuid.c.uid == nuid)
120
+            rep = conn.execute(req)
121
+            res = rep.fetchall()
122
+        
123
+            self.assertEqual(len(res), 1)
124
+            res = res[0]
125
+            self.assertEqual(res.uid, nuid)
126
+            self.assertEqual(res.table, EmTestComp.table)
127
+        pass
128
+
129
+    @unittest.skip("Enable me!") #TODO stop skipping it
130
+    def test_newuid_abstract(self):
131
+        """ Test not valit call for newUid method """
132
+        with self.assertRaises(NotImplementedError):
133
+            EmComponent.newUid()
134
+        pass
135
+    #
136
+    #   EmComponent.__init__
137
+    #
138
+    @unittest.skip("Enable me!") #TODO stop skipping it
139
+    def test_component_abstract_init(self):
140
+        """ Test not valid call (from EmComponent) of __init__ """
141
+        with self.assertRaises(NotImplementedError):
142
+            test_comp = EmComponent(2)
143
+        with self.assertRaises(NotImplementedError):
144
+            test_comp = EmComponent('name')
145
+        pass
146
+
147
+
148
+    def test_component_init_not_exist(self):
149
+        """ Test __init__ with non existing objects """
150
+        with self.assertRaises(EmComponentNotExistError):
151
+            test_comp = EmTestComp('not_exist')
152
+
153
+        # TODO this assertion depends of the EmComponent behavior when instanciate with an ID
154
+        #with self.assertRaises(EmComponentNotExistError):
155
+        #    test_comp = EmTestComp(4096)
156
+
157
+        pass
158
+
159
+    def test_component_init_uid(self):
160
+        """ Test __init__ with numerical ID """
161
+        for val in self.test_values:
162
+            test_comp = EmTestComp(val['uid'])
163
+            self.assertIsInstance(test_comp, EmTestComp)
164
+            self.assertEqual(test_comp.id, val['uid'])
165
+        pass
10 166
 
167
+    def test_component_init_name(self):
168
+        """ Test __init__ with names """
169
+        for val in self.test_values:
170
+            test_comp = EmTestComp(val['name'])
171
+            self.assertIsInstance(test_comp, EmTestComp)
172
+            for vname in val:
173
+                if vname == 'uid':
174
+                    prop = 'id'
175
+                else:
176
+                    prop = vname
177
+                self.assertEqual(test_comp.__get_attr__(prop), val[vname])
178
+        pass
179
+        
180
+        

Loading…
Cancel
Save