Browse Source

[#11] Ajout des premiers tests sur EmField

Roland Haroutiounian 10 years ago
parent
commit
e6f6720a57
1 changed files with 145 additions and 0 deletions
  1. 145
    0
      EditorialModel/test/test_field.py

+ 145
- 0
EditorialModel/test/test_field.py View File

@@ -0,0 +1,145 @@
1
+import os
2
+import logging
3
+import datetime
4
+
5
+from django.conf import settings
6
+from unittest import TestCase
7
+import unittest
8
+
9
+from EditorialModel.components import EmComponent, EmComponentNotExistError
10
+from EditorialModel.fields import EmField
11
+from EditorialModel.classes import EmClass
12
+from EditorialModel.classtypes import EmClassType
13
+from EditorialModel.types import EmType
14
+from EditorialModel.fieldgroups import EmFieldGroup
15
+from EditorialModel.fieldtypes import *
16
+
17
+from Database.sqlsetup import SQLSetup
18
+from Database.sqlwrapper import SqlWrapper
19
+from Database.sqlquerybuilder import SqlQueryBuilder
20
+from Database import sqlutils
21
+
22
+import sqlalchemy as sqla
23
+
24
+os.environ.setdefault("DJANGO_SETTINGS_MODULE", "Lodel.settings")
25
+
26
+## SetUpModule
27
+#
28
+# This function is called once for this module.
29
+# It is designed to overwrite the database configurations, and prepare objects for test_case initialization
30
+def setUpModule():
31
+    # Overwriting the database parameters to make tests
32
+    settings.LODEL2SQLWRAPPER['db'] = {
33
+        'default':{
34
+            'ENGINE': 'sqlite',
35
+            'NAME': '/tmp/roland.testdb.sqlite'
36
+        }
37
+    }
38
+
39
+    logging.basicConfig(level=logging.CRITICAL)
40
+
41
+## FieldTestCase (Class)
42
+#
43
+# The parent class of all other test cases for the fields module.
44
+# It defines a SetUp function and some utility functions for EmField tests.
45
+class FieldTestCase(TestCase):
46
+
47
+    def setUp(self):
48
+        sqls = SQLSetup()
49
+        sqls.initDb()
50
+
51
+        # Generation of the test data
52
+        self.testClass = EmClass.create("testclass1",EmClassType.entity)
53
+        self.testClassUid = self.testClass.uid
54
+
55
+        self.testType = EmType.create('testtype1',self.testClass)
56
+        self.testTypeUid = self.testType.uid
57
+
58
+        self.testFieldType = EmFieldType('testfieldtype1')
59
+        self.testFieldTypeUid = self.testFieldType.uid
60
+
61
+        self.testFieldgroup = EmFieldGroup.create('fieldgrp1',self.testClass)
62
+        self.testFieldgroupUid = self.testFieldgroup.uid
63
+
64
+        pass
65
+
66
+    ## Get_Field_Records (Function)
67
+    #
68
+    # Returns the list of fields corresponding to a given uid
69
+    #
70
+    # @param uid int: Global identifier of the field
71
+    # @return list of found fields
72
+    def get_field_records(self,uid):
73
+        return self._get_field_records_Db(uid)
74
+
75
+    ## _Get_Field_Records_Db (Function)
76
+    #
77
+    # Queries the database to get the list of fields for a given uid
78
+    #
79
+    # @param uid int: Global identifier of the field
80
+    # @return list of found fields
81
+    def _get_field_records_Db(self,uid):
82
+        sql_wrapper = SqlWrapper(read_db='default', write_db='default', alchemy_logs=False)
83
+        sql_builder = SqlQueryBuilder(sql_wrapper, 'em_field')
84
+        sql_builder.Select(('uid'))
85
+        sql_builder.From('em_field')
86
+        sql_builder.Where('em_field.uid=%s' % uid)
87
+        records = sql_builder.Execute().fetchall()
88
+        field_records = []
89
+        for record in records:
90
+            field_records.append(dict(zip(record.keys(), record)))
91
+
92
+        return field_records
93
+
94
+    ## Get_table_columns (Function)
95
+    #
96
+    # Returns the columns list of a table
97
+    #
98
+    # @param table_name str: Name of the table
99
+    # @return list of columns
100
+    def get_table_columns(self,table_name):
101
+        return self._get_table_columns_Db(table_name)
102
+
103
+    ## _Get_table_columns_Db (Function)
104
+    #
105
+    # Queries the database to get the list of columns of a table
106
+    #
107
+    # @param table_name str: Name of the table
108
+    # @return list of columns
109
+    def _get_table_columns_Db(self, table_name):
110
+        table = sqla.Table(table_name, self.dber)
111
+        return table.c
112
+
113
+## TestField (Class)
114
+#
115
+# The test class for the fields module
116
+class TestField(FieldTestCase):
117
+
118
+    ## TestFieldName (Function)
119
+    #
120
+    # The field's name is correctly populated
121
+    def testFieldName(self):
122
+        emField = EmField('testfield')
123
+        self.assertEqual(emField.name,'testfield')
124
+
125
+    ## Test_create (Function)
126
+    #
127
+    # tests the creation process of a field
128
+    def testCreate(self):
129
+
130
+        field = EmField.create('testfield1', self.testFieldgroup, self.testFieldtype)
131
+        fieldUid = field.uid
132
+        # We check that the field has been added in the em_field table
133
+        field_records = self.get_field_records(fieldUid)
134
+        self.assertEqual(len(field_records),1)
135
+        self.assertEqual(fieldUid,field_record[0]['uid'])
136
+        self.assertEqual(field.name,field_record[0]['name'])
137
+
138
+        # We check that the field has been added as a column in the corresponding table
139
+        field_table_columns = self.get_table_columns(field.get_class_table())
140
+        field_column_args = EmField_boolean.sqlalchemy_args()
141
+        field_column_args['name']='testfield1'
142
+        field_column = sqla.Column(**field_column_args)
143
+        self.assertIn(field_column,field_table_columns)
144
+        pass
145
+

Loading…
Cancel
Save