def testSet(self): """Test adding values via struct.name=val """ s = taskBase.Struct() for name, val in self.valDict.items(): setattr(s, name, val) self.assertEqual(self.valDict, s.getDict())
def testInit(self): """Test Struct.__init__ """ s = taskBase.Struct(**self.valDict) self.assertEqual(self.valDict, s.getDict()) for name, val in self.valDict.items(): self.assertEqual(getattr(s, name), val)
def testMergeItems(self): """Test mergeItems """ s = taskBase.Struct(**self.valDict) newS = taskBase.Struct() newS.mergeItems(s) # with no names listed, should merge nothing self.assertEqual(len(newS), 0) self.assertNotEqual(s, newS) newS.mergeItems(s, "foo", "bar") self.assertEqual(len(newS), 2) self.assertNotEqual(s, newS) newS.mergeItems(s, "baz", "alist") self.assertEqual(len(newS), 4) for name, val in newS.getDict().items(): self.assertEqual(val, self.valDict[name]) with self.assertRaises(RuntimeError): newS.mergeItems(s, name)
def testCopy(self): """Test copy, which returns a shallow copy """ s = taskBase.Struct(**self.valDict) sc = s.copy() self.assertEqual(s.getDict(), sc.getDict()) # shallow copy, so changing a list should propagate (not necessarily a # feature) sc.alist[0] = 97 self.assertEqual(s, sc) sc.foo += 1 self.assertNotEqual(s, sc)
def run(self, val): with self.timer("context"): addRet = self.add.run(val) multRet = self.mult.run(addRet.val) self.metadata.add("addmult", multRet.val) return taskBase.Struct(val=multRet.val, )
def run(self, val): self.metadata.add("mult", self.config.multiplicand) return taskBase.Struct(val=val * self.config.multiplicand, )
def run(self, val): self.metadata.add("add", self.config.addend) return taskBase.Struct(val=val + self.config.addend, )
def run(self, val): addend = self.config.addend return taskBase.Struct(val=val + (2 * addend))
def testInitException(self): """Test that struct key names cannot start with double underscores. """ with self.assertRaises(RuntimeError): taskBase.Struct(__foo=13)