def testGetAttr(self): attrNames = "apple", "banana", "pears", "orange", "peach", "nectarine", "grape", "watermelon" attrValues = range(len(attrNames)) obj = SObject() for attr, val in zip(attrNames, attrValues): setattr(obj, attr, val) assert obj.getAttrs() == attrNames for attr, value in zip(attrNames, attrValues): assert value == getattr(obj, attr) # test simple case equality assert obj == SObject(*zip(attrNames, attrValues)) obj2 = SObject(**dict(zip(attrNames, attrValues))) assert sorted(attrNames) == sorted(obj2.getAttrs())
def testConversionFromDict(self): nestedDicts = dict( something=12, otherthing='blah blah blah', someList=[1, 2, 3], listOfDicts=[dict(), dict(anInt=1, aStr='yes, me is a str')], nestedDict=dict(bleargh=111)) nestedDicts['cyclic_ref'] = nestedDicts convertedFromDict = SObject.FromDict(nestedDicts) convertedDict = convertedFromDict.toDict() assert convertedDict['cyclic_ref'] is convertedDict #assert convertedDict == nestedDicts #python doesn't like comparing cyclically nested dicts... nestedDicts.pop('cyclic_ref') asStr = str(convertedFromDict) convertedDict = SObject.FromDict(nestedDicts).toDict() assert convertedDict == nestedDicts
def testSerialization(self): sobject = self.constructData() filepath = self.getTestFilepath() #test writing to file stream with open(filepath, 'w') as fStream: sobject.serialize(fStream) #test reading from file stream with open(filepath) as fStream: SObject.UnserializeStream(fStream) filepath.delete() assert not filepath.exists() #test writing and reading from disk sobject.write(filepath) assert filepath.exists() unserialized = SObject.Load(filepath) assert unserialized == sobject print sobject
def testGetAttr(self): attrNames = 'apple', 'banana', 'pears', 'orange', 'peach', 'nectarine', 'grape', 'watermelon' attrValues = range(len(attrNames)) obj = SObject() for attr, val in zip(attrNames, attrValues): setattr(obj, attr, val) assert obj.getAttrs() == attrNames for attr, value in zip(attrNames, attrValues): assert value == getattr(obj, attr) #test simple case equality assert obj == SObject(*zip(attrNames, attrValues)) obj2 = SObject(**dict(zip(attrNames, attrValues))) assert sorted(attrNames) == sorted(obj2.getAttrs())
def constructData(self): root = SObject() root.aString = "other thing" root.anotherStr = "bananas" root.anInt = 12 root.none = None root.aBool = False root.big_str = _BIG_STR root.s2 = SObject( ("some_float", 200.0), ("someBool", True), ("unicode_test", u'some unicode value "with crap in it"\nand a newline!'), ) root.s2.crazy_list = [ 1, 2.5, "3", True, False, ["nested", "lists", "are", "fun", (7, 8, 9, {"dict": 1, "inside": 2, "tuple": (3)})], ] root.s2.emptyTuple = () root.s2.similarKeyDict = {123: "key as int", "123": "key as str"} root.s3 = SObject( ("duplicate_ref_to_s2", root.s2), ("a_stupidly_long_attribute_name_to_test_whether_it_works_ok_with_serialization", 0x902), ) root.s4 = SObject(("recursive_ref_to_root", root), ("anotherTokenAttribute", -100)) # test SObjects in lists - mixed with other stuff root.s5 = SObject() root.s5.objList = [root.s2, root.s3, root.s5, 1, 3.5, True, False, None] root.s5.largeList = range(1000) # test tuples root.s6 = SObject(("aTuple", (1, 2, 3)), ("objTuple", (SObject(), SObject()))) assert root.s4.recursive_ref_to_root is root return root
def constructData(self): root = SObject() root.aString = "other thing" root.anotherStr = "bananas" root.anInt = 12 root.none = None root.aBool = False root.big_str = _BIG_STR root.s2 = SObject( ('some_float', 200.0), ('someBool', True), ('unicode_test', u'some unicode value "with crap in it"\nand a newline!')) root.s2.crazy_list = [ 1, 2.5, '3', True, False, [ 'nested', 'lists', 'are', 'fun', (7, 8, 9, { 'dict': 1, 'inside': 2, 'tuple': (3) }) ] ] root.s2.emptyTuple = () root.s2.similarKeyDict = {123: 'key as int', '123': 'key as str'} root.s3 = SObject(('duplicate_ref_to_s2', root.s2), ( 'a_stupidly_long_attribute_name_to_test_whether_it_works_ok_with_serialization', 0x902)) root.s4 = SObject(('recursive_ref_to_root', root), ('anotherTokenAttribute', -100)) #test SObjects in lists - mixed with other stuff root.s5 = SObject() root.s5.objList = [ root.s2, root.s3, root.s5, 1, 3.5, True, False, None ] root.s5.largeList = range(1000) #test tuples root.s6 = SObject(('aTuple', (1, 2, 3)), ('objTuple', (SObject(), SObject()))) assert root.s4.recursive_ref_to_root is root return root