Пример #1
0
 def test_attributes(self):
     #set & get
     b = Bag(BAG_DATA)
     b.setAttr('phone.home', private=False)
     assert not b.getAttr('phone.home', 'private')
     b.setAttr('phone.home', private=True, test='is a test')
     assert b['phone.home?test'] == 'is a test' #diverso dalla documentazione track
     assert b.getAttr('phone.#sim=vadophone', 'fbplayer') == 'gattuso'
     assert b['phone.#sim=vadophone?fbplayer'] == 'gattuso'
     b.delAttr('phone.home', 'test')
     assert not b['phone.home?test']
Пример #2
0
 def test_attributes(self):
     #set & get
     b = Bag(BAG_DATA)
     b.setAttr('phone.home', private=False)
     assert not b.getAttr('phone.home', 'private')
     b.setAttr('phone.home', private=True, test='is a test')
     assert b[
         'phone.home?test'] == 'is a test'  #diverso dalla documentazione track
     assert b.getAttr('phone.#sim=vadophone', 'fbplayer') == 'gattuso'
     assert b['phone.#sim=vadophone?fbplayer'] == 'gattuso'
     b.delAttr('phone.home', 'test')
     assert not b['phone.home?test']
Пример #3
0
class TestAttributeBag(unittest.TestCase):
    def setUp(self):
        self.fullbag = Bag(
            {
                "office": Bag({"John": Bag(), "Frank": Bag(), "Alexandra": Bag()}),
                "friends": Bag({"Henry": Bag(), "Fred": Bag()}),
                "relatives": Bag({"Karla": Bag(), "Albert": Bag()}),
            }
        )
        self.fullbag["office"].setItem("Xavier", Bag(), fromdate="9-15-2005", role="researcher")
        self.fullbag["office"].setAttr("Frank", fromdate="3-5-1998", role="developer")
        self.fullbag["office"].setAttr("John", fromdate="2-1-1990", role="boss", age=55)

    def testattributes(self):
        self.assertEqual(self.fullbag["office"].getAttr("Xavier", "role"), "researcher")
        self.assertEqual(self.fullbag["office.Frank?a:role"], "developer")
        l = [("Frank", "developer"), ("John", "boss"), ("Alexandra", None), ("Xavier", "researcher")]
        self.failUnless(self.fullbag["office"].digest("#k,#a.role") == self.fullbag["office.?d:#k,#a.role"] == l)
        self.failUnless(
            self.fullbag["office.Xavier"] == self.fullbag["office.#3"] == self.fullbag["office.#role=researcher"]
        )

    def testBagNodes(self):
        self.fullbag.delAttr("office.John", "age")
        n = self.fullbag.getNode("office.John")
        self.assertTrue(n.hasAttr("role"))
        self.assertFalse(n.hasAttr("age"))
        self.assertEqual(n, self.fullbag.getNodeByAttr("role", "boss"))

    def testgetNodeByAttr(self):
        researcher = self.fullbag.getNodeByAttr("role", "researcher")
        self.assertEqual(researcher, self.fullbag.getNode("office.Xavier"))

    def testfromToXml(self):
        self.fullbag["relatives.Karla.birthday"] = datetime.date(1952, 12, 10)
        self.fullbag["relatives.Karla.pierciengs"] = 0
        self.fullbag["relatives.Karla.dogname"] = ""
        self.fullbag.setAttr("relatives.Karla", age=54)
        self.fullbag.toXml("mybag.xml")
        x = self.fullbag["relatives"].toXml()
        b = Bag(x)
        self.assertEqual(b.asString(), self.fullbag["relatives"].asString())

    def testsfromSource(self):
        current = os.getcwd()
        fromlocal_std = Bag("%s/test_files/standardxml.xml" % current)
        self.assertTrue(isinstance(fromlocal_std, Bag))
        # non riesce a printarla per carattere non encodabile
        fromlocal_bag = Bag("%s/test_files/mybag.xml" % current)
        self.assertTrue(isinstance(fromlocal_bag, Bag))
        # uncomment the following test if you are online
        # fromurl=Bag('http://www.plone.org')
        # self.assertTrue(isinstance(fromurl, Bag))
        stringxml = '<?xml version="1.02" encoding="UTF-8"?><a><b name="fuffy"><d>dog</d></b><c/></a>'
        fromstringxml = Bag(stringxml)
        self.assertTrue(isinstance(fromstringxml, Bag))
        fromdirectory = Bag("%s/test_files" % current)
        self.assertTrue(isinstance(fromdirectory, Bag))

    def testmerge(self):
        newbag = Bag()
        newbag.setItem("Henry", self.fullbag["friends.Henry"], role="documentation and tests")
        newbag.setItem("Xavier", "Mr Xavier", role="documentation manager", age=26)
        allflagstrue = self.fullbag["office"].merge(newbag)  # all flags true
        allflagsfalse = self.fullbag["office"].merge(
            newbag, upd_values=False, add_values=False, upd_attr=False, add_attr=False
        )
        self.assertEqual(allflagsfalse.asString(), self.fullbag["office"].asString())
        notupdatevalues = self.fullbag["office"].merge(newbag, upd_values=False)
        self.assertTrue(isinstance(self.fullbag["office.Xavier"], Bag))
        notaddvalues = self.fullbag["office"].merge(newbag, add_values=False)
        self.assertEqual(len(self.fullbag["office"].items()), len(notaddvalues.items()))
        notupdateattrs = self.fullbag["office"].merge(newbag, upd_attr=False)
        self.assertEqual(self.fullbag["office.Xavier?a:role"], notupdateattrs["Xavier?a:role"])
        notaddattrs = self.fullbag["office"].merge(newbag, add_attr=False)
        self.assertFalse(notaddattrs.getAttr("Xavier", "age", default=False))

    def testsum(self):
        numbers = Bag()
        numbers.setItem("first", 20, height=22)
        numbers.setItem("second", 30, height=342)
        self.assertEqual(numbers.sum("#v,#a.height"), [50, 364])
Пример #4
0
class TestAttributeBag(unittest.TestCase):
    def setUp(self):
        self.fullbag = Bag({
            'office':
            Bag({
                'John': Bag(),
                'Frank': Bag(),
                'Alexandra': Bag()
            }),
            'friends':
            Bag({
                'Henry': Bag(),
                'Fred': Bag()
            }),
            'relatives':
            Bag({
                'Karla': Bag(),
                'Albert': Bag()
            })
        })
        self.fullbag['office'].setItem('Xavier',
                                       Bag(),
                                       fromdate='9-15-2005',
                                       role='researcher')
        self.fullbag['office'].setAttr('Frank',
                                       fromdate='3-5-1998',
                                       role='developer')
        self.fullbag['office'].setAttr('John',
                                       fromdate='2-1-1990',
                                       role='boss',
                                       age=55)

    def testattributes(self):
        self.assertEqual(self.fullbag['office'].getAttr('Xavier', 'role'),
                         'researcher')
        self.assertEqual(self.fullbag['office.Frank?a:role'], 'developer')
        l = [('Frank', 'developer'), ('John', 'boss'), ('Alexandra', None),
             ('Xavier', 'researcher')]
        self.failUnless(self.fullbag['office'].digest('#k,#a.role') ==
                        self.fullbag['office.?d:#k,#a.role'] == l)
        self.failUnless(
            self.fullbag['office.Xavier'] == self.fullbag['office.#3'] ==
            self.fullbag['office.#role=researcher'])

    def testBagNodes(self):
        self.fullbag.delAttr('office.John', 'age')
        n = self.fullbag.getNode('office.John')
        self.assertTrue(n.hasAttr('role'))
        self.assertFalse(n.hasAttr('age'))
        self.assertEqual(n, self.fullbag.getNodeByAttr('role', 'boss'))

    def testgetNodeByAttr(self):
        researcher = self.fullbag.getNodeByAttr('role', 'researcher')
        self.assertEqual(researcher, self.fullbag.getNode('office.Xavier'))

    def testfromToXml(self):
        self.fullbag['relatives.Karla.birthday'] = datetime.date(1952, 12, 10)
        self.fullbag['relatives.Karla.pierciengs'] = 0
        self.fullbag['relatives.Karla.dogname'] = ''
        self.fullbag.setAttr('relatives.Karla', age=54)
        self.fullbag.toXml('mybag.xml')
        x = self.fullbag['relatives'].toXml()
        b = Bag(x)
        self.assertEqual(b.asString(), self.fullbag['relatives'].asString())

    def testsfromSource(self):
        current = os.getcwd()
        fromlocal_std = Bag('%s/test_files/standardxml.xml' % current)
        self.assertTrue(isinstance(fromlocal_std, Bag))
        #non riesce a printarla per carattere non encodabile
        fromlocal_bag = Bag('%s/test_files/mybag.xml' % current)
        self.assertTrue(isinstance(fromlocal_bag, Bag))
        #uncomment the following test if you are online
        #fromurl=Bag('http://www.plone.org')
        #self.assertTrue(isinstance(fromurl, Bag))
        stringxml = '<?xml version="1.02" encoding="UTF-8"?><a><b name="fuffy"><d>dog</d></b><c/></a>'
        fromstringxml = Bag(stringxml)
        self.assertTrue(isinstance(fromstringxml, Bag))
        fromdirectory = Bag('%s/test_files' % current)
        self.assertTrue(isinstance(fromdirectory, Bag))

    def testmerge(self):
        newbag = Bag()
        newbag.setItem('Henry',
                       self.fullbag['friends.Henry'],
                       role='documentation and tests')
        newbag.setItem('Xavier',
                       'Mr Xavier',
                       role='documentation manager',
                       age=26)
        allflagstrue = self.fullbag['office'].merge(newbag)  #all flags true
        allflagsfalse = self.fullbag['office'].merge(newbag,
                                                     upd_values=False,
                                                     add_values=False,
                                                     upd_attr=False,
                                                     add_attr=False)
        self.assertEqual(allflagsfalse.asString(),
                         self.fullbag['office'].asString())
        notupdatevalues = self.fullbag['office'].merge(newbag,
                                                       upd_values=False)
        self.assertTrue(isinstance(self.fullbag['office.Xavier'], Bag))
        notaddvalues = self.fullbag['office'].merge(newbag, add_values=False)
        self.assertEqual(len(self.fullbag['office'].items()),
                         len(notaddvalues.items()))
        notupdateattrs = self.fullbag['office'].merge(newbag, upd_attr=False)
        self.assertEqual(self.fullbag['office.Xavier?a:role'],
                         notupdateattrs['Xavier?a:role'])
        notaddattrs = self.fullbag['office'].merge(newbag, add_attr=False)
        self.assertFalse(notaddattrs.getAttr('Xavier', 'age', default=False))

    def testsum(self):
        numbers = Bag()
        numbers.setItem('first', 20, height=22)
        numbers.setItem('second', 30, height=342)
        self.assertEqual(numbers.sum('#v,#a.height'), [50, 364])