def testList(self): val = MMListAttribute(value = [ "first" , "second" , "third" ] ) self.assertEquals(str(val[0] ) , "first") self.assertEquals(str(val[1] ) , "second") self.assertEquals(str(val[2] ) , "third") val.append("fourth") self.assertEquals(val.count(),4) self.assertEquals(str(val[3]) , "fourth") val.insert(2,"2 and half'th") self.assertEquals(str(val[0] ) , "first") self.assertEquals(str(val[1] ) , "second") self.assertEquals(str(val[2]) , "2 and half'th") self.assertEquals(str(val[3]) , "third") self.assertEquals(str(val[4]) , "fourth") self.assertEquals(val.count(),5) self.assertTrue("third" in val) self.assertEquals(len(list(iter(val))),5) self.assertEquals(len(val),5) val.insert(0,"zeroth") self.assertEquals(str(val[0] ) , "zeroth") self.assertEquals(str(val[1] ) , "first") self.assertEquals(str(val[2] ) , "second") self.assertEquals(str(val[3] ) , "2 and half'th") self.assertEquals(str(val[4] ) , "third") self.assertEquals(str(val[5] ) , "fourth") self.assertTrue("third" in val) self.assertTrue("zeroth" in val) self.assertEquals(val.count(),6) self.assertEquals(len(list(iter(val))),6) del val[4] self.assertEquals(str(val[0] ) , "zeroth") self.assertEquals(str(val[1] ) , "first") self.assertEquals(str(val[2] ) , "second") self.assertEquals(str(val[3]) , "2 and half'th") self.assertEquals(str(val[4]) , "fourth") self.assertFalse("third" in val) self.assertEquals(val.count(),5) val[3]="third" self.assertEquals(str(val[0] ) , "zeroth") self.assertEquals(str(val[1] ) , "first") self.assertEquals(str(val[2] ) , "second") self.assertEquals(str(val[3] ) , "third") self.assertEquals(str(val[4]) , "fourth") self.assertEquals(val.count(),5) self.assertEquals(len(list(iter(val))),5)
def testAttributeForward(self): val = MMListAttribute(value = [ "first" , "second" , "third" ] ) obj = ObjectProxy() #attr = MMAttribute("test",val,obj) obj["test"] = val attr = obj["test"] #Check values from initialisation self.assertEquals(attr.count() ,3 ) self.assertEquals(str(attr.__getitem__(1)) , "second" ) self.assertEquals(str(attr[1]) ,"second" ) self.assertEquals(str(attr["1"]) ,"second" ) self.assertTrue("second" in attr ) self.assertEquals(str(attr["-1"]) ,"third" ) #Delete and item. del attr["1"] #Check whats moved. # - array should be ["first", "third"]. self.assertEquals(attr.count() ,2 ) self.assertEquals(len(list(iter(attr))),2) self.assertEquals(len(attr),2) self.assertFalse("second" in attr ) self.assertEquals(str(attr["1"]) ,"third" ) #Check writeback occurred. #XXX #obj["test"]=attr val = attr.get_value() attr_id = attr.get_nodeid() #attr = None self.assertEquals(system.store.attrs[attr_id],(val.get_type(),val.get_parts())) #Test read from store!. #attr = obj["test"] valo = CreateAttributeValue("primary") attr["0"] = valo # - array should be ["primary", "third"]. self.assertEquals(attr.count() ,2 ) self.assertEquals(attr[0].get_value(),valo) #Test Stable key behaviour. skey = repr(attr[0]).split(":")[-1] self.assertNotEquals(skey,"0") self.assertNotEquals(skey,0) self.assertEquals(attr[0],attr["0"]) self.assertEquals(attr[skey],attr[0],msg=("attr[\"%s\"] != attr[0]" % skey)) skey = repr(attr["1"]).split(":")[-1] self.assertNotEquals(skey,"1") self.assertNotEquals(skey,1) self.assertEquals(attr[skey],attr[1]) # Check writeback again # - array should be ["primary", "third"]. val = copy.copy(obj["test"].get_value()) # - array should be ["primary", "second"]. attr[1]="second" val[1]="second" self.assertEquals(str(attr["1"]) ,"second" ) self.assertEquals(system.store.attrs[attr_id],(val.get_type(),val.get_parts())) #Check walk back thru and find a parser self.assertEquals(attr[1].GetFullExpansion(),"second") val = copy.copy( obj["test"].get_value()) # - array should be ["primary", "second" , "some fate"]. attr.append("some fate") val.append("some fate") self.assertEquals(system.store.attrs[attr_id],(val.get_type(),val.get_parts())) self.assertEquals(str(attr["2"]) ,"some fate" ) val = copy.copy( obj["test"].get_value()) attr.insert(2,"some date") val.insert(2,"some date") # - array should be ["primary", "second" ,"some date" ,"some fate"]. self.assertEquals(system.store.attrs[attr_id],(val.get_type(),val.get_parts())) self.assertEquals(str(attr["2"]) ,"some date" ) self.assertEquals(str(attr["3"]) ,"some fate" ) #Check trying insert an invalid class. class dummy (MMAttributeValue): typename = "dummy" contain_prefs = {} def test(a): a[2] = dummy(parts = {'f':"testing", 'g':"123"}) self.assertRaises(TypeError,test,attr) ###Test itertation.. # - note we get back the long term stable keys - not the # numeric indices - so our test needs be different # - note we also test return order s correct here, names = [ 0,1,2,3 ] fndNames= [] for i,(k,v) in izip_longest(names,attr.iteritems()): self.assertEquals(attr[i] , attr[k]) self.assertEquals(attr[i] , v) self.assertFalse(k in fndNames) fndNames += [ k ] self.assertTrue(isinstance( v, MMAttribute)) self.assertEquals(v,attr[k]) attrv = list(attr.__iter__()) self.assertEqual(len(attrv),4) attrk = list(attr.iterkeys()) self.assertEqual(len(attrk),4) for k,v in itertools.izip(attrk,attrv): self.assertEquals(attr[k],v)