00001 import unittest
00002 import win32com.client
00003 import win32ui
00004 import pythoncom
00005 import os
00006 import stat
00007 import utils.Builder
00008 bd = utils.Builder
00009
00010 from GPyUnit.util import DispatchEx
00011 from GPyUnit.util import dec_disable_early_binding
00012
00013 def _adjacent_file(file):
00014 import os.path
00015 return os.path.join(os.path.dirname(os.path.abspath(__file__)), file)
00016
00017 class TestCase6( unittest.TestCase ):
00018 def __init__(self, methodName='runTest'):
00019 unittest.TestCase.__init__(self, methodName)
00020 self.project = None
00021
00022 def setUp( self ):
00023 pass
00024
00025 def tearDown( self ):
00026 if self.project:
00027 self.project.Close( 0 )
00028
00029 def populate6A(self, p):
00030 f1 = bd.newFolder( p, p.RootFolder, 'Folder')
00031 f1.Name = 'NewFolder'
00032
00033 c1 = bd.newObjInFold( p, p.RootFolder, 'Compound')
00034 c1.Name = 'NewCompound'
00035
00036 p1 = bd.newObjInFold( p, f1, 'Primitive')
00037 p1.Name = 'NewPrimitive'
00038
00039 @dec_disable_early_binding
00040 def testA( self ):
00041
00042 """ testA
00043 test properties of IMgaObject such as
00044 Name
00045 ID
00046 AbsPath
00047 GetParent
00048 ObjType
00049 MetaBase
00050 Project
00051 Territory
00052
00053 methods such as
00054 ObjectByPath
00055 IsEqual
00056 """
00057
00058 mganame = _adjacent_file("_tc6_A_sf.mga")
00059 project = bd.creaP( mganame, "SF")
00060 if not project:
00061 self.fail("Create failed")
00062 try:
00063 self.populate6A( project)
00064 except:
00065 bd.saveP( project)
00066 raise
00067 bd.saveP( project)
00068
00069
00070
00071 self.project = project
00072 try:
00073 project.Open( "MGA=" + mganame )
00074 except:
00075 project.Close(0)
00076 raise
00077
00078 terr = project.CreateTerritory( None, None, None)
00079 trans = project.BeginTransaction( terr)
00080
00081 rf = project.RootFolder
00082
00083 fol = rf.ObjectByPath( "@NewFolder|kind=Folder")
00084 assert fol.Name == "NewFolder"
00085 assert fol.AbsPath == "/@NewFolder|kind=Folder|relpos=0"
00086
00087 fco = rf.ObjectByPath( "@NewFolder|kind=Folder/@NewPrimitive|kind=Primitive")
00088 assert fco.Name == "NewPrimitive"
00089 assert fco.AbsPath == "/@NewFolder|kind=Folder|relpos=0/@NewPrimitive|kind=Primitive|relpos=0"
00090 assert fco.GetParent() == ( fol, 6 )
00091
00092
00093 fcp = fol.ObjectByPath( "@NewPrimitive|kind=Primitive")
00094 assert fcp.Name == "NewPrimitive"
00095 assert fcp.Name == fco.Name
00096 assert fcp.AbsPath == fco.AbsPath
00097
00098
00099 assert fco == fcp
00100
00101 assert fco.IsEqual( fcp)
00102
00103 fc2 = rf.ObjectByPath( "@NewCompound|kind=Compound")
00104 assert fc2.Name == "NewCompound"
00105 assert fc2.AbsPath == "/@NewCompound|kind=Compound|relpos=0"
00106 assert fc2.GetParent() == ( rf, 6)
00107
00108 fco.Name = "OldPrimitive"
00109 fc2.Name = "OldCompound"
00110 fol.Name = "OldFolder"
00111
00112 t_fol = rf.ObjectByPath( "@OldFolder|kind=Folder")
00113 assert t_fol.Name == "OldFolder"
00114
00115 t_fco = rf.ObjectByPath( "@OldFolder|kind=Folder/@OldPrimitive|kind=Primitive")
00116 assert t_fco.Name == "OldPrimitive"
00117
00118 t_fc2 = rf.ObjectByPath( "@OldCompound|kind=Compound")
00119 assert t_fc2.Name == "OldCompound"
00120
00121
00122 assert ~t_fc2.IsEqual( t_fco)
00123 assert ~t_fc2.IsEqual( t_fol)
00124
00125 assert t_fol.IsEqual( fol)
00126 assert t_fol.ID == fol.ID
00127 assert t_fol.MetaBase == fol.MetaBase
00128 assert t_fol.MetaBase.Name == "Folder"
00129 assert t_fol.ObjType == fol.ObjType
00130 assert t_fol.ObjType == 6
00131
00132 assert t_fco.IsEqual( fco)
00133 assert t_fco.ID == fco.ID
00134 assert t_fco.MetaBase == fco.MetaBase
00135 assert t_fco.MetaBase.Name == "Primitive"
00136 assert t_fco.ObjType == fco.ObjType
00137 assert t_fco.ObjType == 1
00138
00139 assert t_fc2.IsEqual( fc2)
00140 assert t_fc2.ID == fc2.ID
00141 assert t_fc2.MetaBase == fc2.MetaBase
00142 assert t_fc2.MetaBase.Name == "Compound"
00143 assert t_fc2.ObjType == fc2.ObjType
00144 assert t_fc2.ObjType == 1
00145
00146 fco.Name = "NewPrimitive"
00147 fc2.Name = "NewCompound"
00148 fol.Name = "NewFolder"
00149
00150 assert fco.Project == project
00151 assert fco.Territory == terr
00152
00153 assert fc2.Project == project
00154 assert fc2.Territory == terr
00155
00156 assert fol.Project == project
00157 assert fol.Territory == terr
00158
00159
00160 project.CommitTransaction()
00161
00162
00163 ter2 = project.CreateTerritory( None, None, None)
00164 tran2 = project.BeginTransaction( ter2)
00165
00166 a_rf = project.RootFolder
00167
00168 assert a_rf.IsEqual( rf)
00169
00170 a_fco = a_rf.ObjectByPath( "@NewFolder|kind=Folder/@NewPrimitive|kind=Primitive")
00171 assert a_fco.Name == "NewPrimitive"
00172 assert a_fco != fco
00173 assert a_fco.IsEqual( fco)
00174
00175
00176 project.CommitTransaction()
00177
00178
00179 project.Save()
00180 project.Close(0)
00181
00182
00183 pass
00184
00185 def populate6C(self, p):
00186 c1 = bd.newObjInFold( p, p.RootFolder, 'Compound')
00187 c1.Name = 'NewCompound'
00188
00189 p1 = bd.new( p, c1, 'PrimitiveParts')
00190 p1.Name = 'NewPrimitiveParts'
00191
00192 @dec_disable_early_binding
00193 def testC( self ):
00194 """ testC
00195
00196 for a READ-ONLY project test methods like:
00197 IsLibObject
00198 IsWritable
00199 Status
00200
00201 then
00202
00203 create another project
00204 AttachLibrary attach the prev project as a library
00205 IsLibObject, IsWritable check on its objects
00206 """
00207
00208 mg1name = _adjacent_file("_tc6_C_readonly_sf.mga")
00209
00210
00211 for file in (mg1name, mg1name + "tmp"):
00212 if os.path.isfile(file):
00213
00214 os.chmod(file, stat.S_IWUSR)
00215
00216
00217
00218
00219
00220 project1 = bd.creaP( mg1name, "SF")
00221 if not project1:
00222 self.fail("Create failed")
00223 try:
00224 self.populate6C( project1)
00225 except:
00226 bd.saveP( project1)
00227 raise
00228 bd.saveP( project1)
00229
00230 self.project = project1
00231
00232
00233
00234 try:
00235
00236 os.chmod( mg1name, stat.S_IRUSR)
00237
00238 readonly = project1.Open( "MGA=" + mg1name )
00239
00240 except:
00241 project1.Close(0)
00242 raise
00243
00244 self.project = project1
00245
00246 terr1 = project1.CreateTerritory( None, None, None)
00247
00248 trans1 = project1.BeginTransaction( terr1, 1)
00249 rf = project1.RootFolder
00250
00251 obj = rf.ObjectByPath( "#1")
00252 assert obj.IsWritable
00253 assert not obj.IsLibObject
00254 assert obj.Status == 0
00255
00256
00257 project1.CommitTransaction()
00258
00259
00260 trans2 = project1.BeginTransaction( terr1)
00261
00262
00263 cmp1 = bd.newObjInFold( project1, rf, 'Compound')
00264 cmp1.Name = "ThisObjectWillNotSurvive"
00265
00266
00267 project1.CommitTransaction()
00268
00269
00270 try:
00271 project1.Save()
00272 assert False
00273 except:
00274 pass
00275
00276 project1.Close(True)
00277
00278 mg2name = _adjacent_file("_tc6_C_libuser_sf.mga")
00279
00280
00281 project2 = bd.creaP( mg2name, "SF")
00282 if not project2:
00283 self.fail("Create failed for " + mg2name + " project")
00284
00285 bd.saveP( project2)
00286
00287
00288 self.project = project2
00289 try:
00290 project2.Open( "MGA=" + mg2name )
00291 except:
00292 project2.Close(0)
00293 raise
00294
00295
00296 terr2 = project2.CreateTerritory( None, None, None)
00297 trans3 = project2.BeginTransaction( terr2)
00298 rf2 = project2.RootFolder
00299
00300
00301 try:
00302
00303 libroot = project2.RootFolder.AttachLibrary( "MGA=" + mg1name)
00304 pass
00305 except:
00306 print 'AttachLibrary failed'
00307 raise
00308
00309
00310
00311 flds = project2.RootFolder.ChildFolders
00312 assert flds.Count == 1
00313
00314
00315 obj = flds.Item(1).GetChildFCODisp( "NewCompound")
00316 assert obj.IsWritable
00317 assert obj.IsLibObject
00318 assert obj.Status == 0
00319
00320
00321 project2.CommitTransaction()
00322
00323 project2.Save()
00324 project2.Close(0)
00325
00326 def populate6D(self, p):
00327 f1 = bd.newFolder( p, p.RootFolder, 'Folder')
00328 f1.Name = 'NewFolder'
00329
00330 f2 = bd.newFolder( p, p.RootFolder, 'Folder')
00331 f2.Name = 'NewFolder'
00332
00333 c1 = bd.newObjInFold( p, p.RootFolder, 'Compound')
00334 c1.Name = 'NewCompound'
00335
00336 p1 = bd.newObjInFold( p, f1, 'Primitive')
00337 p1.Name = 'NewPrimitive'
00338
00339 ip = bd.new( p, p1, 'InputParameters')
00340 ip.Name = 'NewInputParameters'
00341
00342 is0 = bd.new( p, p1, 'InputSignals')
00343 is0.Name = 'NewInputSignals'
00344
00345 op = bd.new( p, p1, 'OutputParameters')
00346 op.Name = 'NewOutputParameters'
00347
00348 os = bd.new( p, p1, 'OutputSignals')
00349 os.Name = 'NewOutputSignals'
00350
00351 pp = bd.new( p, p1, 'Parameters')
00352 pp.Name = 'NewParameters'
00353
00354 p2 = bd.newObjInFold( p, f1, 'Primitive')
00355 p2.Name = 'NewPrimitive'
00356
00357 op = bd.new( p, p2, 'OutputParameters')
00358 op.Name = 'NewOutputParameters'
00359
00360 p2 = bd.newObjInFold( p, f2, 'Primitive')
00361 p2.Name = 'NewPrimitive'
00362
00363 op = bd.new( p, p2, 'OutputParameters')
00364 op.Name = 'NewOutputParameters'
00365
00366 cp1 = bd.new( p, c1, 'CompoundParts')
00367 cp1.Name = 'NewCompoundParts'
00368
00369 pp1 = bd.new( p, c1, 'PrimitiveParts')
00370 pp1.Name = 'NewPrimitiveParts'
00371
00372
00373 @dec_disable_early_binding
00374 def testD( self ):
00375
00376 """ testD
00377
00378 Check
00379 CheckTree
00380 ChildObjects
00381 ChildObjectByRelID
00382 """
00383
00384 mganame = _adjacent_file("_tc6_D_sf.mga")
00385
00386
00387 project = bd.creaP( mganame, "SF")
00388 if not project:
00389 self.fail("Create failed")
00390 try:
00391 self.populate6D( project)
00392 except:
00393 bd.saveP( project)
00394 raise
00395 bd.saveP( project)
00396
00397 self.project = project
00398
00399
00400 self.project = project
00401 try:
00402 project.Open( "MGA=" + mganame )
00403 except:
00404 project.Close(0)
00405 raise
00406
00407 terr = project.CreateTerritory( None, None, None)
00408 trans = project.BeginTransaction( terr)
00409
00410 rf = project.RootFolder
00411
00412
00413 objs = rf.ChildObjects
00414 names = [ "NewFolder", "NewCompound"]
00415 for obj in objs:
00416 obj.Check()
00417 obj.CheckTree()
00418 rid = obj.RelID
00419 one_obj = rf.ChildObjectByRelID( rid)
00420 assert one_obj
00421 assert one_obj.RelID == rid
00422 name_dummy = one_obj.Name
00423 assert name_dummy in names
00424
00425 one_fol = rf.ChildObjectByRelID( 1 )
00426 assert one_fol.Name == names[0]
00427
00428 one_pri = one_fol.ChildObjectByRelID( 2 )
00429 assert one_pri.Name == "NewPrimitive"
00430 one_pri = one_fol.ChildObjectByRelID( 1 )
00431 assert one_pri.Name == "NewPrimitive"
00432
00433 names = [ "NewInputParameters", "NewInputSignals", "NewOutputParameters", "NewOutputSignals", "NewParameters"]
00434 name2relid = { "NewInputParameters" : 1,
00435 "NewInputSignals" : 2,
00436 "NewOutputParameters" : 3,
00437 "NewOutputSignals" : 4,
00438 "NewParameters" : 5
00439 }
00440
00441 pri_objs = one_pri.ChildObjects
00442 for pri_obj in pri_objs:
00443 rid = pri_obj.RelID
00444 one_obj = one_pri.ChildObjectByRelID( rid)
00445 assert one_obj
00446 assert one_obj.RelID == rid
00447 s_name = one_obj.Name
00448 assert s_name in names
00449 assert name2relid[ s_name ] == rid
00450
00451
00452 project.CommitTransaction()
00453
00454 project.Save()
00455 project.Close(0)
00456
00457
00458 @dec_disable_early_binding
00459 def testE( self ):
00460
00461 """ testE
00462
00463 ObjectByPath
00464 NthObjectByPath
00465 """
00466
00467 mganame = _adjacent_file("_tc6_D_sf.mga")
00468
00469
00470 project = bd.creaP( mganame, "SF")
00471 if not project:
00472 self.fail("Create failed")
00473 try:
00474 self.populate6D( project)
00475 except:
00476 bd.saveP( project)
00477 raise
00478 bd.saveP( project)
00479
00480 self.project = project
00481
00482
00483 self.project = project
00484 try:
00485 project.Open( "MGA=" + mganame )
00486 except:
00487 project.Close(0)
00488 raise
00489
00490 terr = project.CreateTerritory( None, None, None)
00491
00492 trans = project.BeginTransaction( terr)
00493 rf = project.RootFolder
00494
00495 objs = list( rf.ChildObjects )
00496
00497 one_fol = rf.ChildObjectByRelID( 2 )
00498 assert one_fol.Name == "NewFolder"
00499 one_fol = rf.ChildObjectByRelID( 1 )
00500 assert one_fol.Name == "NewFolder"
00501
00502
00503 res = rf.ObjectByPath( "@NewFolder|kind=Folder")
00504 try:
00505 assert not res
00506 assert res == None
00507
00508 s_dummy = res.Name
00509 assert 0
00510 except AttributeError:
00511
00512 pass
00513 except:
00514 assert 0
00515
00516 res = rf.NthObjectByPath( 0, "@NewFolder|kind=Folder|relpos=0")
00517 assert res.Name == "NewFolder"
00518
00519
00520 one_pri = one_fol.ChildObjectByRelID( 2 )
00521 assert one_pri.Name == "NewPrimitive"
00522 one_pri = one_fol.ChildObjectByRelID( 1 )
00523 assert one_pri.Name == "NewPrimitive"
00524
00525
00526 res = one_fol.ObjectByPath( "@NewPrimitive|kind=Primitive")
00527 try:
00528
00529 s_dummy = res.Name
00530 assert 0
00531 except AttributeError:
00532
00533 pass
00534 except:
00535 assert 0
00536
00537 res = one_fol.NthObjectByPath( 0, "@NewPrimitive|kind=Primitive|relpos=0")
00538 assert res
00539 assert res.Name == "NewPrimitive"
00540 assert res == one_pri
00541
00542
00543 res = one_fol.ObjectByPath( "@NewPrimitive|kind=Primitive/@NewOutputParameters|kind=OutputParam")
00544 try:
00545
00546 s_dummy = res.Name
00547 assert 0
00548 except AttributeError:
00549
00550 pass
00551 except:
00552 assert 0
00553
00554 res = one_fol.NthObjectByPath( 0, "@NewPrimitive|kind=Primitive|relpos=0/@NewOutputParameters|kind=OutputParam|relpos=0")
00555 assert res.Name == "NewOutputParameters"
00556 assert res.RelID == 3
00557
00558 res = one_fol.NthObjectByPath( 0, "@NewPrimitive|kind=Primitive|relpos=1/@NewOutputParameters|kind=OutputParam|relpos=0")
00559 assert res.Name == "NewOutputParameters"
00560 assert res.RelID == 1
00561
00562
00563 res = one_pri.ObjectByPath( "@NewOutputParameters|kind=OutputParam")
00564 assert res.Name == "NewOutputParameters"
00565 assert res.RelID == 3
00566
00567 res = one_pri.NthObjectByPath( 0, "@NewOutputParameters|kind=OutputParam|relpos=888")
00568 assert res.Name == "NewOutputParameters"
00569 assert res.RelID == 3
00570
00571 f1 = rf.ObjectByPath( "#1/#1/#3")
00572 f2 = rf.NthObjectByPath( 0, "/@NewFolder|kind=Folder|relpos=0/@NewPrimitive|kind=Primitive|relpos=0/@NewOutputParameters|kind=OutputParam|relpos=0")
00573 assert f1 == f2
00574
00575 f1 = rf.ObjectByPath( "#1/#2/#1")
00576 f2 = rf.NthObjectByPath( 0, "/@NewFolder|kind=Folder|relpos=0/@NewPrimitive|kind=Primitive|relpos=1/@NewOutputParameters|kind=OutputParam|relpos=0")
00577 assert f1 == f2
00578
00579 f1 = rf.ObjectByPath( "#2/#1/#1")
00580 f2 = rf.NthObjectByPath( 0, "/@NewFolder|kind=Folder|relpos=1/@NewPrimitive|kind=Primitive|relpos=0/@NewOutputParameters|kind=OutputParam|relpos=0")
00581 assert f1 == f2
00582
00583
00584 project.CommitTransaction()
00585
00586 project.Save()
00587 project.Close(0)
00588
00589 """
00590 Let's produce the test suites
00591 """
00592
00593 def suites():
00594 suite = unittest.makeSuite(TestCase6,'test')
00595 return suite
00596
00597
00598 def suites2():
00599 tc = TestCase6()
00600
00601
00602
00603
00604 def suites3():
00605 suite = unittest.TestSuite()
00606 suite.addTest( TestCase6( "testA"))
00607
00608
00609 return suite
00610
00611
00612
00613
00614 if __name__ == "__main__":
00615 print "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
00616 print "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
00617 print "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
00618 print "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
00619 print "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
00620 print "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ TEST BEGINS ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
00621 print "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
00622 print "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
00623 print "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
00624 print "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
00625 print "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
00626
00627
00628 unittest.main()
00629
00630
00631
00632
00633
00634
00635
00636