def testValueGetterDictionaryAccess(self): col = ColumnDefn("title", valueGetter="aspectToGet") col2 = ColumnDefn("title", valueGetter="otherAspectToGet") col3 = ColumnDefn("title", valueGetter="missingAspect") data = {"aspectToGet": "valueToGet", "otherAspectToGet": 2} self.assertEqual(col.GetValue(data), "valueToGet") self.assertEqual(col2.GetValue(data), 2) self.assertEqual(col3.GetValue(data), None)
def testValueGetterDictionaryModifying(self): data = {"aspectToGet": "firstValue", "otherAspectToGet": 2} col = ColumnDefn("title", valueGetter="aspectToGet") self.assertEqual(col.GetValue(data), "firstValue") col.SetValue(data, "newValue") self.assertEqual(data["aspectToGet"], "newValue") col = ColumnDefn("title", valueGetter="otherAspectToGet") self.assertEqual(col.GetValue(data), 2) col.SetValue(data, 3) self.assertEqual(data["otherAspectToGet"], 3)
def testValueGetterListModifying(self): data = ["zero", "first", 2, "third"] col = ColumnDefn("title", valueGetter=1) self.assertEqual(col.GetValue(data), "first") col.SetValue(data, "newValue") self.assertEqual(data[1], "newValue")
def testValueGetterMethod(self): col = ColumnDefn("title", valueGetter="aspectToGet") col2 = ColumnDefn("title", valueGetter="otherAspectToGet") col3 = ColumnDefn("title", valueGetter="missingAspect") class DataObject: def aspectToGet(self): return "valueToGet" def otherAspectToGet(self): return 2 data = DataObject() self.assertEqual(col.GetValue(data), "valueToGet") self.assertEqual(col2.GetValue(data), 2) self.assertEqual(col3.GetValue(data), None)
def testValueGetterFunction(self): def getterFunction1(modelObject): return "constant" def getterFunction2(modelObject): return modelObject.otherAspectToGet * 2 class DataObject: def __init__(self, value1, value2): self.aspectToGet = value1 self.otherAspectToGet = value2 col1 = ColumnDefn(valueGetter=getterFunction1) col2 = ColumnDefn(valueGetter=getterFunction2) data = DataObject("valueToGet", 2) self.assertEqual(col1.GetValue(data), "constant") self.assertEqual(col2.GetValue(data), 4)
def testValueGetterAttribute(self): def getterFunction(modelObject): return modelObject.otherAspectToGet * 2 class DataObject: def __init__(self, value1, value2): self.aspectToGet = value1 self.otherAspectToGet = value2 col = ColumnDefn(valueGetter="aspectToGet") col2 = ColumnDefn(valueGetter="otherAspectToGet") col3 = ColumnDefn(valueGetter="missingAspect") col4 = ColumnDefn(valueGetter=getterFunction) data = DataObject("valueToGet", 2) self.assertEqual(col.GetValue(data), "valueToGet") self.assertEqual(col2.GetValue(data), 2) self.assertEqual(col3.GetValue(data), None) self.assertEqual(col4.GetValue(data), 4)
def testValueGetterListAccess(self): col = ColumnDefn("title", valueGetter=1) col2 = ColumnDefn("title", valueGetter=2) col3 = ColumnDefn("title", valueGetter=99) data = ["zero", "first", 2, "third"] self.assertEqual(col.GetValue(data), "first") self.assertEqual(col2.GetValue(data), 2) self.assertEqual(col3.GetValue(data), None) data = ("zero1", "first1", 21, "third1") self.assertEqual(col.GetValue(data), "first1") self.assertEqual(col2.GetValue(data), 21) self.assertEqual(col3.GetValue(data), None)
def testValueGetterListMiss(self): data = ["zero", "first", 2, "third"] col = ColumnDefn("title", valueGetter=99) self.assertEqual(col.GetValue(data), None) col.SetValue(data, 3) self.assertEqual(data, ["zero", "first", 2, "third"])