class TestBasicBag(unittest.TestCase): def setUp(self): self.bag = Bag() self.fullbag = Bag( { "office": Bag({"John": Bag(), "Frank": Bag(), "Alexandra": Bag()}), "friends": Bag({"Henry": Bag(), "Fred": Bag()}), "relatives": Bag({"Karla": Bag(), "Albert": Bag()}), } ) def testinit(self): """ this test case check different kinds of bag's instantiation: - from a list of tuples - from a dictionary - empty bag """ firstbag = Bag({"a": 1}) # bag from a dict secondbag = Bag([("a", 1)]) # bag from list of tuple thirdbag = Bag() thirdbag.setItem("a", 1) # empty bag filled bagstring = "0 - (int) a: 1 " # bag as string self.failUnless(firstbag.asString() == secondbag.asString() == thirdbag.asString() == bagstring) def testsetgetItem(self): """ this test case checks the methods setItem and getItem """ self.bag.setItem("a.b.c", 5) self.failUnless(self.bag.getItem("a.b.c") == self.bag["a.b.c"] == 5) self.bag["a.b.c"] = 6 self.failUnless(self.bag.getItem("a.b.c") == self.bag["a.b.c"] == 6) def testindexposition(self): """ this test case checks the method index and the argument "_position" """ self.bag["a.b.c"] = 6 self.bag.addItem("a.b.d", 15) self.failUnless(self.bag.getItem("a.b.#1") == self.bag["a.b.d"] == 15) self.assertEqual(self.bag["a.b"].index("d"), 1) self.bag.setItem("a.b.middle", 10, _position="<d") self.assertEqual(self.bag["a.b"].index("middle"), 1) self.assertEqual(self.bag["a.b"].index("d"), 2) def testgetIndexList(self): il = self.fullbag.getIndexList() self.assertEqual(len(il), 10) def testkeysitemsvalues(self): """ this test case checks the methods -keys -items -values """ self.failUnless(self.fullbag["office"].keys() == self.fullbag["office?"] == ["Frank", "John", "Alexandra"]) self.failUnless( len(self.fullbag["office"].keys()) == len(self.fullbag["office"].values()) == len(self.fullbag["office"].items()) == 3 ) self.assertTrue(self.fullbag["office"].has_key("John")) def testpopin(self): """ this test case checks the method pop and the clause "in" """ self.assertTrue("John" in self.fullbag["office"]) self.fullbag["office.Desmond"] = Bag() d = self.fullbag["office.Desmond"] removed = self.fullbag["office"].pop("#3") self.assertEqual(d, removed) self.assertEqual(self.fullbag["office"].keys(), ["Frank", "John", "Alexandra"]) def testasDict(self): """ this test case checks the method asDict """ d = self.fullbag["office"].asDict() self.assertTrue(isinstance(d, dict)) self.assertEqual(d.keys(), self.fullbag["office?"])
class TestBasicBag(unittest.TestCase): def setUp(self): self.bag = Bag() self.fullbag = Bag({ 'office': Bag({ 'John': Bag(), 'Frank': Bag(), 'Alexandra': Bag() }), 'friends': Bag({ 'Henry': Bag(), 'Fred': Bag() }), 'relatives': Bag({ 'Karla': Bag(), 'Albert': Bag() }) }) def testinit(self): """ this test case check different kinds of bag's instantiation: - from a list of tuples - from a dictionary - empty bag """ firstbag = Bag({'a': 1}) #bag from a dict secondbag = Bag([('a', 1)]) #bag from list of tuple thirdbag = Bag() thirdbag.setItem('a', 1) #empty bag filled bagstring = '0 - (int) a: 1 ' #bag as string self.failUnless(firstbag.asString() == secondbag.asString() == thirdbag.asString() == bagstring) def testsetgetItem(self): """ this test case checks the methods setItem and getItem """ self.bag.setItem('a.b.c', 5) self.failUnless(self.bag.getItem('a.b.c') == self.bag['a.b.c'] == 5) self.bag['a.b.c'] = 6 self.failUnless(self.bag.getItem('a.b.c') == self.bag['a.b.c'] == 6) def testindexposition(self): """ this test case checks the method index and the argument "_position" """ self.bag['a.b.c'] = 6 self.bag.addItem('a.b.d', 15) self.failUnless(self.bag.getItem('a.b.#1') == self.bag['a.b.d'] == 15) self.assertEqual(self.bag['a.b'].index('d'), 1) self.bag.setItem('a.b.middle', 10, _position='<d') self.assertEqual(self.bag['a.b'].index('middle'), 1) self.assertEqual(self.bag['a.b'].index('d'), 2) def testgetIndexList(self): il = self.fullbag.getIndexList() self.assertEqual(len(il), 10) def testkeysitemsvalues(self): """ this test case checks the methods -keys -items -values """ self.failUnless(self.fullbag['office'].keys() == self. fullbag['office?'] == ['Frank', 'John', 'Alexandra']) self.failUnless( len(self.fullbag['office'].keys()) == len( self.fullbag['office'].values()) == len( self.fullbag['office'].items()) == 3) self.assertTrue(self.fullbag['office'].has_key('John')) def testpopin(self): """ this test case checks the method pop and the clause "in" """ self.assertTrue('John' in self.fullbag['office']) self.fullbag['office.Desmond'] = Bag() d = self.fullbag['office.Desmond'] removed = self.fullbag['office'].pop('#3') self.assertEqual(d, removed) self.assertEqual(self.fullbag['office'].keys(), ['Frank', 'John', 'Alexandra']) def testasDict(self): """ this test case checks the method asDict """ d = self.fullbag['office'].asDict() self.assertTrue(isinstance(d, dict)) self.assertEqual(d.keys(), self.fullbag['office?'])