1
0
Fork 0
mirror of https://github.com/yweber/lodel2.git synced 2025-11-21 05:09:16 +01:00

[Tests] Changed the tests for the EmField class using the new Model object

This commit is contained in:
Roland Haroutiounian 2015-07-30 12:17:34 +02:00
commit ba2ac6880c

View file

@ -1,40 +1,28 @@
import os import os
import logging
import datetime
from django.conf import settings
from unittest import TestCase from unittest import TestCase
import unittest
from EditorialModel.components import EmComponent, EmComponentNotExistError
from EditorialModel.fields import EmField from EditorialModel.fields import EmField
from EditorialModel.classes import EmClass from EditorialModel.model import Model
from EditorialModel.classtypes import EmClassType from EditorialModel.backend.json_backend import EmBackendJson
from EditorialModel.types import EmType
from EditorialModel.fieldgroups import EmFieldGroup
from EditorialModel.test.utils import *
from EditorialModel.fieldtypes import *
from Database import sqlutils EM_TEST = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'me.json')
EM_TEST_OBJECT = None
import sqlalchemy as sqla
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "Lodel.settings")
TEST_FIELD_DBNAME = 'lodel2_test_field_db.sqlite'
## SetUpModule ## SetUpModule
# #
# This function is called once for this module. # This function is called once for this module.
# It is designed to overwrite the database configurations, and prepare objects for test_case initialization # It is designed to overwrite the database configurations, and prepare objects for test_case initialization
def setUpModule(): def setUpModule():
initTestDb(TEST_FIELD_DBNAME) global EM_TEST_OBJECT
setDbConf(TEST_FIELD_DBNAME) EM_TEST_OBJECT = Model(EmBackendJson(EM_TEST))
logging.basicConfig(level=logging.CRITICAL) #initTestDb(TEST_FIELD_DBNAME)
pass #setDbConf(TEST_FIELD_DBNAME)
#logging.basicConfig(level=logging.CRITICAL)
def tearDownModule(): def tearDownModule():
cleanDb(TEST_FIELD_DBNAME) #cleanDb(TEST_FIELD_DBNAME)
pass pass
@ -46,66 +34,12 @@ class FieldTestCase(TestCase):
@classmethod @classmethod
def setUpClass(cls): def setUpClass(cls):
sqlsetup.init_db() pass
# Generation of the test data
testclass = EmClass.create("testclass1",EmClassType.entity)
EmField_integer()
EmFieldGroup.create('fieldgrp1',testclass)
EmType.create('testtype1',testclass)
saveDbState(TEST_FIELD_DBNAME)
def setUp(self): def setUp(self):
restoreDbState(TEST_FIELD_DBNAME) self.test_fieldtype = 'integer'
self.testClass = EmClass("testclass1") self.test_fieldgroup = EM_TEST_OBJECT.component(3)
self.testFieldType = EmField_integer()
self.testFieldgroup = EmFieldGroup('fieldgrp1')
self.testType = EmType('testtype1')
## Get_Field_Records (Function)
#
# Returns the list of fields corresponding to a given uid
#
# @param field EmField: EmField object
# @return Number of found records
def get_field_records(self,field):
return self._get_field_records_Db(field)
## _Get_Field_Records_Db (Function)
#
# Queries the database to get the list of fields for a given uid
#
# @param field EmField: EmField object
# @return Number of found records
def _get_field_records_Db(self,field):
dbe = field.db_engine
fieldtable = sqla.Table(EmField.table, sqlutils.meta(dbe))
conn = dbe.connect()
req = fieldtable.select().where(fieldtable.c.uid==field.uid).where(fieldtable.c.name==field.name)
res = conn.execute(req).fetchall()
return len(res)
## Get_table_columns (Function)
#
# Returns the columns list of a table
#
# @param table_name str: Name of the table
# @return list of columns
def get_table_columns(self,table_name):
return self._get_table_columns_Db(table_name)
## _Get_table_columns_Db (Function)
#
# Queries the database to get the list of columns of a table
#
# @param table_name str: Name of the table
# @return list of columns
def _get_table_columns_Db(self, table_name):
dbe = self.testClass.db_engine
table = sqla.Table(table_name, sqlutils.meta(dbe))
return table.c
## TestField (Class) ## TestField (Class)
# #
@ -115,46 +49,36 @@ class TestField(FieldTestCase):
## Test_create (Function) ## Test_create (Function)
# #
# tests the creation process of a field # tests the creation process of a field
def testCreate(self): def test_create(self):
""" Testing fields creation process """
'''
field_values = {
'name':'testfield1',
'fieldgroup_id' : self.testFieldgroup.uid,
'fieldtype' : self.testFieldType,
'rel_to_type_id': self.testType.uid
}
'''
field = EmField.create(name='testfield1', fieldgroup=self.testFieldgroup, fieldtype=self.testFieldType)
# We check that the field has been added in the em_field table field = EM_TEST_OBJECT.create_component(EmField.__name__, {'name': 'testfield1', 'fieldgroup_id': self.test_fieldgroup.uid, 'fieldtype': self.test_fieldtype})
field_records = self.get_field_records(field)
self.assertGreater(field_records,0)
# We check that the field has been added as a column in the corresponding table # We check that the field has been added
field_table_columns = self.get_table_columns(field.get_class_table()) field_records = EM_TEST_OBJECT.component(field.uid)
field_column_args = self.testFieldType.sqlalchemy_args() self.assertIsNot(field_records, False)
field_column_args['name']='testfield1'
field_column = sqla.Column(**field_column_args) # We check that the field has been added in the right list in the model object
self.assertIn(field_column.name, field_table_columns) field_components_records = EM_TEST_OBJECT.components(EmField)
pass self.assertIn(field, field_components_records)
## Test_Deletion
#
# tests the deletion process of a field
def test_deletion(self): def test_deletion(self):
""" Testing fields deletion process """ fields = []
field_names = ['field1', 'field2'] field_names = ['field1', 'field2']
# We create the two fields
for name in field_names: for name in field_names:
EmField.create(name=name, fieldgroup=self.testFieldgroup, fieldtype = self.testFieldType) fields.append(EM_TEST_OBJECT.create_component(EmField.__name__, {'name': name, 'fieldgroup_id': self.test_fieldgroup.uid, 'fieldtype': self.test_fieldtype}))
for i,name in enumerate(field_names): for field in fields:
test_field = EmField(name) # We check if the delete process was performed to the end
self.assertTrue(test_field.delete()) self.assertTrue(EM_TEST_OBJECT.delete_component(field.uid))
cols = self.get_table_columns(self.testClass.name) # We check that the field object is not in the editorial model anymore
for deleted_name in field_names[:i+1]: self.assertFalse(EM_TEST_OBJECT.component(field.uid))
self.assertNotIn(deleted_name, cols, "Column is not deleted")
for not_deleted_name in field_names[i+1:]: # We check that the field object is not in the EmField components list
self.assertIn(not_deleted_name, cols, "A bad column was deleted") field_components_records = EM_TEST_OBJECT.components(EmField)
self.assertNotIn(field, field_components_records)
with self.assertRaises(EmComponentNotExistError, msg="This field should be deleted"):
EmField(name)