[Modeling-cvs] ProjectModeling/Modeling/tests test_PyModel.py,NONE,1.1.2.1
Status: Abandoned
Brought to you by:
sbigaret
From: <sbi...@us...> - 2003-05-15 17:45:01
|
Update of /cvsroot/modeling/ProjectModeling/Modeling/tests In directory sc8-pr-cvs1:/tmp/cvs-serv25871/tests Added Files: Tag: brch-0_9pre7-1-PyModel test_PyModel.py Log Message: Added PyModel.py and tests --- NEW FILE: test_PyModel.py --- #! /usr/bin/env python #----------------------------------------------------------------------------- # # Modeling Framework: an Object-Relational Bridge for python # (c) 2001, 2002, 2003 Sebastien Bigaret # # This file is part of the Modeling Framework. # # The Modeling Framework is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License as published # by the Free Software Foundation; either version 2 of the License, or (at # your option) any later version. # # The Modeling Framework is distributed in the hope that it will be # useful, but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # General Public License for more details. # # You should have received a copy of the GNU General Public License along # with the Modeling Framework; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA # #----------------------------------------------------------------------------- """ Tests for PyModel """ import unittest if __name__ == "__main__": import utils, sys utils.fixpath() from Modeling.PyModel import * class TestPyModel(unittest.TestCase): """ Tests for PyModel """ def checkEntitiesProperties(self, entities): """ For each entity in 'entities', check that its attributes and relationships entity() points to it. Also check that the relationships' destination entity is not None. See in particular the comment in updateComponent() for an explanation """ for e in entities: for a in e.attributes(): self.assertEqual(a.entity(), e) for r in e.relationships(): self.assertEqual(r.entity(), e) self.failUnless(r.destinationEntity(),'%s.%s.destinationEntity() is None'%(e.name(),r.name())) def setUp(self): self.model = Model('StoreEmployees',adaptorName='Postgresql') self.model.version='0.1' Entity.defaults['properties'] = [ APrimaryKey('id', isClassProperty=0, isRequired=1, doc='Default PK') ] AString.defaults['width'] = 20 def test_01_attr(self): "[PyModel] entity w/ attributes and default pk" model = self.model model.entities = [ Entity('Employee', properties=[ AString('lastName', isRequired=1, usedForLocking=1), AString('firstName', isRequired=1, width=50, usedForLocking=1), ] ), ] model.build() emp=model.component.entityNamed('Employee') self.checkEntitiesProperties((emp,)) self.assertEqual(emp.attributeNamed('lastName').width(), 20) self.assertEqual(emp.attributeNamed('firstName').width(), 50) self.assertEqual(emp.attributeNamed('firstName').isRequired(), 1) self.assertEqual(emp.primaryKeyAttributeNames(), ['id']) # default PK self.failIf(emp.primaryKeyAttributes()[0].isClassProperty()) self.assertEqual(emp.primaryKeyAttributes()[0].defaultValue(), None) def test_02_pk(self): "[PyModel] entity w/ pk" model = self.model model.entities = [ Entity('Employee', properties=[ APrimaryKey('ID', isClassProperty=1, isRequired=1, doc='SalesClerk PK') ] ), ] model.build() emp=model.component.entityNamed('Employee') self.checkEntitiesProperties((emp,)) self.assertEqual(emp.primaryKeyAttributeNames(), ['ID']) self.failUnless(emp.primaryKeyAttributes()[0].isClassProperty()) self.assertEqual(emp.primaryKeyAttributes()[0].defaultValue(), 0) def test_03_attrs_and_inheritance(self): "[PyModel] attrs_and_inheritance" model = self.model model.entities = [ Entity('Employee', properties=[ AString('lastName', isRequired=1, usedForLocking=1), AString('firstName', isRequired=1, width=50, usedForLocking=1), ] ), Entity('SalesClerk', parent='Employee', properties=[ APrimaryKey('id', isClassProperty=1, isRequired=1, # NB: avec ID & Rels c'est une erreur! doc='SalesClerk PK'), AString('firstName', isRequired=1, width=5, usedForLocking=1), ] ), Entity('Executive', parent='Employee', properties=[ AString('officeLocation', width=5), ] ), ] model.build() emp=model.component.entityNamed('Employee') sc=model.component.entityNamed('SalesClerk') ex=model.component.entityNamed('Executive') self.checkEntitiesProperties((emp,sc,ex)) self.assertEqual(emp.primaryKeyAttributeNames(), ['id']) self.assertEqual(emp.primaryKeyAttributeNames(), ['id']) # PK overriden self.assertEqual(emp.primaryKeyAttributeNames(), ['id']) self.failIf(emp.primaryKeyAttributes()[0].isClassProperty()) self.failIf(ex.primaryKeyAttributes()[0].isClassProperty()) self.failUnless(sc.primaryKeyAttributes()[0].isClassProperty()) # Propagation of attributes self.failIf([a for a in emp.attributesNames() if a not in sc.attributesNames()]) self.failIf([a for a in emp.attributesNames() if a not in ex.attributesNames()]) self.assertEqual(len(sc.attributes()),3) self.assertEqual(len(ex.attributes()),4) # self.assertEqual(emp.attributeNamed('firstName').width(), 50) self.assertEqual(sc.attributeNamed('firstName').width(), 5) self.assertEqual(emp.attributeNamed('id').entity(), emp) self.assertEqual(sc.attributeNamed('id').entity(), sc) self.assertEqual(ex.attributeNamed('id').entity(), ex) def test_04_fully_qualified_relationships_no_inverse(self): "[PyModel] fully_qualified_relationships" model = self.model model.entities = [ Entity('Employee', properties=[ AForeignKey('fkStore'), RToOne('toStore', 'Store',src='fkStore',dst='id'), RToMany('toMarks', 'Mark',src='id',dst='fkEmployee'), ] ), Entity('SalesClerk', parent='Employee'), Entity('Mark', properties=[ AForeignKey('fkEmployee') ]), Entity('Store'), ] model.build() emp=model.component.entityNamed('Employee') sc=model.component.entityNamed('SalesClerk') store=model.component.entityNamed('Store') mark=model.component.entityNamed('Mark') self.checkEntitiesProperties((emp,sc,store,mark)) self.assertEqual(len(sc.relationships()), 2) def check(self, toStore, toMarks, store, mark): self.failUnless(toStore.isToOne()) self.failUnless(toMarks.isToMany()) self.assertEqual(toStore.destinationEntity(), store) self.assertEqual(toMarks.destinationEntity(), mark) toStore=emp.propertyNamed('toStore') toMarks=emp.propertyNamed('toMarks') check(self, toStore, toMarks, store, mark) toStore=sc.propertyNamed('toStore') toMarks=sc.propertyNamed('toMarks') check(self, toStore, toMarks, store, mark) def test_05_unqualified_relationships_no_inverse(self): "[PyModel] unqualified_relationships_no_inverse" model = self.model model.entities = [ Entity('Employee', properties=[ RToMany('toMarks', 'Mark'), RToOne('toStore', 'Store'), ] ), Entity('SalesClerk', parent='Employee'), Entity('Mark'), Entity('Store'), ] model.build() emp=model.component.entityNamed('Employee') sc=model.component.entityNamed('SalesClerk') store=model.component.entityNamed('Store') mark=model.component.entityNamed('Mark') self.checkEntitiesProperties((emp,sc,store,mark)) self.assertEqual(len(sc.relationships()), 2) # Check that each entity has the expected number of attributes self.assertEqual(len(emp.attributes()), 2) # id + fk self.assertEqual(len(sc.attributes()), 2) # id + fk self.assertEqual(len(store.attributes()), 1) # id self.assertEqual(len(mark.attributes()), 2) # id + fk self.failUnless(emp.attributeNamed('fkStore')) self.failUnless(sc.attributeNamed('fkStore')) self.failUnless(mark.attributeNamed('fkEmployee')) def check(self, toStore, toMarks, store, mark): self.failUnless(toStore.isToOne()) self.failUnless(toMarks.isToMany()) self.assertEqual(toStore.destinationEntity(), store) self.assertEqual(toMarks.destinationEntity(), mark) # check the parent toStore=emp.propertyNamed('toStore') toMarks=emp.propertyNamed('toMarks') check(self, toStore, toMarks, store, mark) self.failIf(emp.propertyNamed('fkStore') not in toStore.sourceAttributes()) self.failIf(store.propertyNamed('id') not in toStore.destinationAttributes()) self.failIf(emp.propertyNamed('id') not in toMarks.sourceAttributes()) self.failIf(mark.propertyNamed('fkEmployee') not in toMarks.destinationAttributes()) # Also check the sub-entity toStore=sc.propertyNamed('toStore') toMarks=sc.propertyNamed('toMarks') check(self, toStore, toMarks, store, mark) self.failIf(sc.propertyNamed('fkStore') not in toStore.sourceAttributes()) self.failIf(store.propertyNamed('id') not in toStore.destinationAttributes()) self.failIf(sc.propertyNamed('id') not in toMarks.sourceAttributes()) self.failIf(mark.propertyNamed('fkEmployee') not in toMarks.destinationAttributes()) # test_06 qualified & unqualified # model = self.model # model.entities = [ # Entity('Employee', # properties=[ AForeignKey('fkStore'), # #RToOne('toStore', 'Store',src='fkStore',dst='id'), # RToMany('toMarks', 'Mark',src='id',dst='fkEmployee'), # ] ), # Entity('SalesClerk', parent='Employee'), # Entity('Mark', # properties=[ AForeignKey('fkEmployee'), # #RToOne('toEmployee','Employee',inverse='toMarks') # ] # ), # Entity('Store', # properties=[RToMany('toEmployees','Employee')#,src='id',dst='fkStore') # ]#,inverse='toStore')] # ), # # NB: quid si inverse='ghjgh'? --> TBD test! # ] def check_model(self, model): model.build() emp=model.component.entityNamed('Employee') sc=model.component.entityNamed('SalesClerk') store=model.component.entityNamed('Store') mark=model.component.entityNamed('Mark') self.checkEntitiesProperties((emp,sc,store,mark)) self.assertEqual(len(sc.relationships()), 2) def check(self, toStore, toMarks, store, mark): self.failUnless(toStore.isToOne()) self.failUnless(toMarks.isToMany()) self.assertEqual(toStore.destinationEntity(), store) self.assertEqual(toMarks.destinationEntity(), mark) toStore=emp.propertyNamed('toStore') toMarks=emp.propertyNamed('toMarks') check(self, toStore, toMarks, store, mark) toStore=sc.propertyNamed('toStore') toMarks=sc.propertyNamed('toMarks') check(self, toStore, toMarks, store, mark) #### JUSQU'ICI on est pareil que test_04 self.failUnless(mark.relationshipNamed('toEmployee').inverseRelationship()==emp.propertyNamed('toMarks')) self.failUnless(store.relationshipNamed('toEmployees').inverseRelationship()==emp.propertyNamed('toStore')) def test_07_relationships_with_inverse(self): "[PyModel] qualified_relationships_with_inverse" model = self.model model.entities = [ Entity('Employee', properties=[ AForeignKey('fkStore'), RToOne('toStore', 'Store',src='fkStore',dst='id'), RToMany('toMarks', 'Mark',src='id',dst='fkEmployee'), ] ), Entity('SalesClerk', parent='Employee'), Entity('Mark', properties=[ AForeignKey('fkEmployee'), RToOne('toEmployee','Employee',inverse='toMarks') ] ), Entity('Store', properties=[RToMany('toEmployees','Employee',inverse='toStore', )#src='id',dst='fkStore') ]#,inverse='toStore')] ), # NB: quid si inverse='ghjgh'? --> TBD test! ] self.check_model(self.model) # self.setUp() self.model.entities = [ Entity('Employee', properties=[ RToOne('toStore', 'Store'),#,src='fkStore',dst='id'), RToMany('toMarks', 'Mark'),#,src='id',dst='fkEmployee'), ] ), Entity('SalesClerk', parent='Employee'), Entity('Mark', properties=[ RToOne('toEmployee','Employee',inverse='toMarks') ] ), Entity('Store', properties=[RToMany('toEmployees','Employee',inverse='toStore',)] ), ] self.check_model(self.model) # Automatic creation of source + destination key self.setUp() self.model.entities = [ Entity('Employee', properties=[ RToOne('toStore', 'Store',src='fkStore',dst='id'), RToMany('toMarks', 'Mark',src='id',dst='fkEmployee'), ] ), Entity('SalesClerk', parent='Employee'), Entity('Mark', properties=[ RToOne('toEmployee','Employee',inverse='toMarks') ] ), Entity('Store', properties=[RToMany('toEmployees','Employee',inverse='toStore',)] ), ] #self.check_model(self.model) def test_08_qualified_relationships_with_inverse(self): "[PyModel] unqualified_relationships_with_inverse" model = self.model model.entities = [ Entity('Employee'), Entity('SalesClerk', parent='Employee'), Entity('Mark'), Entity('Store'), ] model.associations = [ Association('Employee', 'Store'), Association('Mark', 'Employee'), ] self.check_model(self.model) def test_suite(): suite = unittest.TestSuite() suite.addTest(unittest.makeSuite(TestPyModel, "test_08")) return suite if __name__ == "__main__": verbose='-V' in sys.argv and 'Y' or ('-v' in sys.argv and 1 or 0) errs = utils.run_suite(test_suite(), verbosity=verbose) sys.exit(errs and 1 or 0) ## TBD: test init/build sur elements, p.ex. RToOne + cardinality, etc. |