|
@@ -1,40 +1,28 @@
|
1
|
1
|
import os
|
2
|
|
-import logging
|
3
|
|
-import datetime
|
4
|
2
|
|
5
|
|
-from django.conf import settings
|
6
|
3
|
from unittest import TestCase
|
7
|
|
-import unittest
|
8
|
|
-
|
9
|
|
-from EditorialModel.components import EmComponent, EmComponentNotExistError
|
10
|
4
|
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.test.utils import *
|
16
|
|
-from EditorialModel.fieldtypes import *
|
17
|
|
-
|
18
|
|
-from Database import sqlutils
|
|
5
|
+from EditorialModel.model import Model
|
|
6
|
+from EditorialModel.backend.json_backend import EmBackendJson
|
19
|
7
|
|
20
|
|
-import sqlalchemy as sqla
|
|
8
|
+EM_TEST = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'me.json')
|
|
9
|
+EM_TEST_OBJECT = None
|
21
|
10
|
|
22
|
|
-os.environ.setdefault("DJANGO_SETTINGS_MODULE", "Lodel.settings")
|
23
|
|
-
|
24
|
|
-TEST_FIELD_DBNAME = 'lodel2_test_field_db.sqlite'
|
25
|
11
|
|
26
|
12
|
## SetUpModule
|
27
|
13
|
#
|
28
|
14
|
# This function is called once for this module.
|
29
|
15
|
# It is designed to overwrite the database configurations, and prepare objects for test_case initialization
|
30
|
16
|
def setUpModule():
|
31
|
|
- initTestDb(TEST_FIELD_DBNAME)
|
32
|
|
- setDbConf(TEST_FIELD_DBNAME)
|
33
|
|
- logging.basicConfig(level=logging.CRITICAL)
|
34
|
|
- pass
|
|
17
|
+ global EM_TEST_OBJECT
|
|
18
|
+ EM_TEST_OBJECT = Model(EmBackendJson(EM_TEST))
|
|
19
|
+ #initTestDb(TEST_FIELD_DBNAME)
|
|
20
|
+ #setDbConf(TEST_FIELD_DBNAME)
|
|
21
|
+ #logging.basicConfig(level=logging.CRITICAL)
|
|
22
|
+
|
35
|
23
|
|
36
|
24
|
def tearDownModule():
|
37
|
|
- cleanDb(TEST_FIELD_DBNAME)
|
|
25
|
+ #cleanDb(TEST_FIELD_DBNAME)
|
38
|
26
|
pass
|
39
|
27
|
|
40
|
28
|
|
|
@@ -46,66 +34,12 @@ class FieldTestCase(TestCase):
|
46
|
34
|
|
47
|
35
|
@classmethod
|
48
|
36
|
def setUpClass(cls):
|
49
|
|
- sqlsetup.init_db()
|
50
|
|
-
|
51
|
|
- # Generation of the test data
|
52
|
|
- testclass = EmClass.create("testclass1",EmClassType.entity)
|
53
|
|
- EmField_integer()
|
54
|
|
- EmFieldGroup.create('fieldgrp1',testclass)
|
55
|
|
- EmType.create('testtype1',testclass)
|
56
|
|
- saveDbState(TEST_FIELD_DBNAME)
|
57
|
|
-
|
|
37
|
+ pass
|
58
|
38
|
|
59
|
39
|
def setUp(self):
|
60
|
|
- restoreDbState(TEST_FIELD_DBNAME)
|
61
|
|
- self.testClass = EmClass("testclass1")
|
62
|
|
- self.testFieldType = EmField_integer()
|
63
|
|
- self.testFieldgroup = EmFieldGroup('fieldgrp1')
|
64
|
|
- self.testType = EmType('testtype1')
|
65
|
|
-
|
66
|
|
- ## Get_Field_Records (Function)
|
67
|
|
- #
|
68
|
|
- # Returns the list of fields corresponding to a given uid
|
69
|
|
- #
|
70
|
|
- # @param field EmField: EmField object
|
71
|
|
- # @return Number of found records
|
72
|
|
- def get_field_records(self,field):
|
73
|
|
- return self._get_field_records_Db(field)
|
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 field EmField: EmField object
|
80
|
|
- # @return Number of found records
|
81
|
|
- def _get_field_records_Db(self,field):
|
82
|
|
- dbe = field.db_engine
|
83
|
|
- fieldtable = sqla.Table(EmField.table, sqlutils.meta(dbe))
|
84
|
|
- conn = dbe.connect()
|
85
|
|
- req = fieldtable.select().where(fieldtable.c.uid==field.uid).where(fieldtable.c.name==field.name)
|
86
|
|
- res = conn.execute(req).fetchall()
|
87
|
|
-
|
88
|
|
- return len(res)
|
89
|
|
-
|
90
|
|
- ## Get_table_columns (Function)
|
91
|
|
- #
|
92
|
|
- # Returns the columns list of a table
|
93
|
|
- #
|
94
|
|
- # @param table_name str: Name of the table
|
95
|
|
- # @return list of columns
|
96
|
|
- def get_table_columns(self,table_name):
|
97
|
|
- return self._get_table_columns_Db(table_name)
|
|
40
|
+ self.test_fieldtype = 'integer'
|
|
41
|
+ self.test_fieldgroup = EM_TEST_OBJECT.component(3)
|
98
|
42
|
|
99
|
|
- ## _Get_table_columns_Db (Function)
|
100
|
|
- #
|
101
|
|
- # Queries the database to get the list of columns of a table
|
102
|
|
- #
|
103
|
|
- # @param table_name str: Name of the table
|
104
|
|
- # @return list of columns
|
105
|
|
- def _get_table_columns_Db(self, table_name):
|
106
|
|
- dbe = self.testClass.db_engine
|
107
|
|
- table = sqla.Table(table_name, sqlutils.meta(dbe))
|
108
|
|
- return table.c
|
109
|
43
|
|
110
|
44
|
## TestField (Class)
|
111
|
45
|
#
|
|
@@ -115,46 +49,36 @@ class TestField(FieldTestCase):
|
115
|
49
|
## Test_create (Function)
|
116
|
50
|
#
|
117
|
51
|
# tests the creation process of a field
|
118
|
|
- def testCreate(self):
|
119
|
|
- """ Testing fields creation process """
|
120
|
|
- '''
|
121
|
|
- field_values = {
|
122
|
|
- 'name':'testfield1',
|
123
|
|
- 'fieldgroup_id' : self.testFieldgroup.uid,
|
124
|
|
- 'fieldtype' : self.testFieldType,
|
125
|
|
- 'rel_to_type_id': self.testType.uid
|
126
|
|
- }
|
127
|
|
- '''
|
128
|
|
- field = EmField.create(name='testfield1', fieldgroup=self.testFieldgroup, fieldtype=self.testFieldType)
|
129
|
|
-
|
130
|
|
- # We check that the field has been added in the em_field table
|
131
|
|
- field_records = self.get_field_records(field)
|
132
|
|
- self.assertGreater(field_records,0)
|
133
|
|
-
|
134
|
|
- # We check that the field has been added as a column in the corresponding table
|
135
|
|
- field_table_columns = self.get_table_columns(field.get_class_table())
|
136
|
|
- field_column_args = self.testFieldType.sqlalchemy_args()
|
137
|
|
- field_column_args['name']='testfield1'
|
138
|
|
- field_column = sqla.Column(**field_column_args)
|
139
|
|
- self.assertIn(field_column.name, field_table_columns)
|
140
|
|
- pass
|
141
|
|
-
|
|
52
|
+ def test_create(self):
|
|
53
|
+
|
|
54
|
+ field = EM_TEST_OBJECT.create_component(EmField.__name__, {'name': 'testfield1', 'fieldgroup_id': self.test_fieldgroup.uid, 'fieldtype': self.test_fieldtype})
|
|
55
|
+
|
|
56
|
+ # We check that the field has been added
|
|
57
|
+ field_records = EM_TEST_OBJECT.component(field.uid)
|
|
58
|
+ self.assertIsNot(field_records, False)
|
|
59
|
+
|
|
60
|
+ # We check that the field has been added in the right list in the model object
|
|
61
|
+ field_components_records = EM_TEST_OBJECT.components(EmField)
|
|
62
|
+ self.assertIn(field, field_components_records)
|
|
63
|
+
|
|
64
|
+ ## Test_Deletion
|
|
65
|
+ #
|
|
66
|
+ # tests the deletion process of a field
|
142
|
67
|
def test_deletion(self):
|
143
|
|
- """ Testing fields deletion process """
|
|
68
|
+ fields = []
|
144
|
69
|
field_names = ['field1', 'field2']
|
|
70
|
+
|
|
71
|
+ # We create the two fields
|
145
|
72
|
for name in field_names:
|
146
|
|
- EmField.create(name=name, fieldgroup=self.testFieldgroup, fieldtype = self.testFieldType)
|
147
|
|
-
|
148
|
|
- for i,name in enumerate(field_names):
|
149
|
|
- test_field = EmField(name)
|
150
|
|
- self.assertTrue(test_field.delete())
|
151
|
|
-
|
152
|
|
- cols = self.get_table_columns(self.testClass.name)
|
153
|
|
- for deleted_name in field_names[:i+1]:
|
154
|
|
- self.assertNotIn(deleted_name, cols, "Column is not deleted")
|
155
|
|
- for not_deleted_name in field_names[i+1:]:
|
156
|
|
- self.assertIn(not_deleted_name, cols, "A bad column was deleted")
|
157
|
|
-
|
158
|
|
- with self.assertRaises(EmComponentNotExistError, msg="This field should be deleted"):
|
159
|
|
- EmField(name)
|
160
|
|
-
|
|
73
|
+ fields.append(EM_TEST_OBJECT.create_component(EmField.__name__, {'name': name, 'fieldgroup_id': self.test_fieldgroup.uid, 'fieldtype': self.test_fieldtype}))
|
|
74
|
+
|
|
75
|
+ for field in fields:
|
|
76
|
+ # We check if the delete process was performed to the end
|
|
77
|
+ self.assertTrue(EM_TEST_OBJECT.delete_component(field.uid))
|
|
78
|
+
|
|
79
|
+ # We check that the field object is not in the editorial model anymore
|
|
80
|
+ self.assertFalse(EM_TEST_OBJECT.component(field.uid))
|
|
81
|
+
|
|
82
|
+ # We check that the field object is not in the EmField components list
|
|
83
|
+ field_components_records = EM_TEST_OBJECT.components(EmField)
|
|
84
|
+ self.assertNotIn(field, field_components_records)
|