Esempio n. 1
0
    def setUp(self):
        txnStart()
        self._pool = Server.getService("PSQLPool")
        self._conn = self._pool.getConnection()
        self._cursor = self._conn.cursor()

        self.wHome = Server.getHome("widgetHome")
        self.dHome = Server.getHome("doodleHome")

        self.d1 = self.dHome.create()
        self.w1 = self.wHome.create()

        self.w1._set_doodle(self.d1)
 def setUp(self):
     self.ps = Server.getService("PersistenceService")
     self.wh = Server.getHome("widgetHome")
     # self.ms = Server.getService("MappingService")
     # self.mapper = InMemoryDataMapper()
     # self.ms.registerMapper(Widget, self.mapper )
     Server.txn.begin()
Esempio n. 3
0
 def testRemove(self):
     w = Server.getHome('widgetHome').create()
     Server.txn.commit(0)
     Server.txn.begin()
     w1 = self.ps.load(Widget, w._get_primaryKey())
     self.ps.remove(w)
     Server.txn.commit(0)
     Server.txn.begin()
     self.assertRaises(KeyError, self.ps.load, Widget, w._get_primaryKey())
Esempio n. 4
0
 def testUpdate(self):
     w = Server.getHome('widgetHome').create()
     Server.txn.commit(0)
     Server.txn.begin()
     w1 = self.ps.load(Widget, w._get_primaryKey())
     assert w1._get_wedgie() == 0
     w1._set_wedgie(1)
     Server.txn.commit(0)
     Server.txn.begin()
     w2 = self.ps.load(Widget, w._get_primaryKey())
     assert w2._get_wedgie() == 1
Esempio n. 5
0
    def testLoadEntity(self):
        wHome = Server.getHome("widgetHome")
        widget = wHome.create()
        widget._set_wedgie(1)
        pKey = widget._get_primaryKey()
        Server.txn.commit(0)
        
        txnStart()
        widget = wHome.findByPrimaryKey(pKey)

        assert widget._get_primaryKey() == pKey
        assert widget._get_wedgie() == 1
Esempio n. 6
0
 def testSet(self):
     w = Server.getHome('moneyWidgetHome').create(self.money)
     Server.txn.commit(0)
     Server.txn.begin()
     w1 = self.ps.load(MoneyWidget, w._get_primaryKey())
     assert w1._get_money().getAmount() == 10.0
     money = Money(5.00, self.currency)
     w1._set_money(money)
     Server.txn.commit(0)
     Server.txn.begin()
     w2 = self.ps.load(MoneyWidget, w._get_primaryKey())
     assert w2._get_money().getAmount() == 5.0
Esempio n. 7
0
    def testRelateThree(self):

        """Create three objects, relate them, then see if the relation
        service returns the right values."""
        
        w1 = Server.getHome('widgetHome').create()
        w2 = Server.getHome('widgetHome').create()
        w3 = Server.getHome('widgetHome').create()
        w4 = Server.getHome('widgetHome').create()

        self.rs.relate(w1, w2, "many_test")
        self.rs.relate(w1, w3, "many_test")
        self.rs.relate(w1, w4, "many_test")

        relatives = self.rs.findPointingFrom(w1, "many_test")
        assert len(relatives) == 3, "Should have 3 relatives, found %s" % len(relatives)
        
        Server.txn.commit(0)
        Server.txn.begin()

        relatives = self.rs.findPointingFrom(w1, "many_test")
        assert len(relatives) == 3, "Should have 3 relatives, found %s" % len(relatives)
Esempio n. 8
0
    def testRelate(self):

        """Create two objects relate them, then see if the relation
        service returns the right values."""


        w1 = Server.getHome('widgetHome').create()
        w2 = Server.getHome('widgetHome').create()

        self.rs.relate(w1, w2, "relation")

        assert self.rs.isPointingTo(w1, w2, "relation")
                
        Server.txn.commit(0)
        Server.txn.begin()

        assert self.rs.isPointingTo(w1, w2, "relation")

        # Check primary keys, cause technically we shouldn't be
        # holding onto w1 and w2 after the commit.

        assert self.rs.findPointingFrom(w1, "relation")[0]._get_primaryKey() == w2._get_primaryKey()
        assert self.rs.findPointingTo(w2, "relation")[0]._get_primaryKey() == w1._get_primaryKey()
Esempio n. 9
0
    def testRemoveEntity(self):

        """Verifies that deleting an entity removes all the relations
        the entity was involved in."""

        w1 = Server.getHome('widgetHome').create()
        w2 = Server.getHome('widgetHome').create()
        
        self.rs.relate(w1, w2, "relation")
        assert self.rs.isPointingTo(w1, w2, "relation")
        
        Server.txn.commit(0)
        Server.txn.begin()

        w2 = Server.getHome('widgetHome').findByPrimaryKey(w2._get_primaryKey())
        assert self.rs.isPointingTo(w1, w2, "relation")
        
        Server.getHome('widgetHome').remove(w2)

        Server.txn.commit(0)
        Server.txn.begin()

        
        assert not self.rs.isPointingTo(w1, w2, "relation")
Esempio n. 10
0
    def testOneToManyRelation(self):

        wHome = Server.getHome("widgetHome")
        parent = wHome.create()
        child1 = wHome.create()
        child2 = wHome.create()


        
        parentKey = parent._get_primaryKey()
        childKey1 = child1._get_primaryKey()
        childKey2 = child2._get_primaryKey()

        print parentKey, childKey1, childKey2

        parent._set_wedgie(1)

        child1._set_parent(parent)
        child2._set_parent(parent)
        
        Server.txn.commit(0)
        txnStart()

        parent = wHome.findByPrimaryKey(parentKey)

        children = parent._get_children()
        assert len(children) == 2, "Should have 2 children.  Has %s" % len(children)

        for child in children:
            assert child._get_primaryKey() in [childKey1, childKey2]

        Server.txn.commit(0)
        txnStart()

        removed = wHome.findByPrimaryKey(childKey2)
        wHome.remove(removed)

        Server.txn.commit(0)
        txnStart()

        parent = wHome.findByPrimaryKey(parentKey)

        children = parent._get_children()
        assert len(children) == 1, "Should have 1 child.  Has %s" % len(children)

        assert children[0]._get_primaryKey() == childKey1
Esempio n. 11
0
    def testOneToOneRelation(self):
        wHome = Server.getHome("widgetHome")
        widget1 = wHome.create()
        widget2 = wHome.create()

        widgetKey1 = widget1.primaryKey
        widgetKey2 = widget2.primaryKey

        widget1._set_other1(widget2)

        Server.txn.commit(0)
        txnStart()

        widget1 = wHome.findByPrimaryKey(widgetKey1)
        widget2 = wHome.findByPrimaryKey(widgetKey2)

        assert widget1._get_other1().primaryKey == widgetKey2
        assert widget2._get_other2().primaryKey == widgetKey1        
Esempio n. 12
0
    def testCreateEntity(self):
        wHome = Server.getHome("widgetHome")
        widget = wHome.create()
        pKey = widget._get_primaryKey()
        Server.txn.commit(0)
        
        txnStart()

        pool = Server.getService("PSQLPool")
        conn = pool.getConnection()
        cursor = conn.cursor()
        cursor.execute("SELECT primaryKey, wedgie FROM Widget WHERE primaryKey='%s'" % pKey)

        assert cursor.rowcount == 1, "Should have found 1 record matching widget.  Found: %s" % cursor.rowcount

        data = cursor.fetchone()
        assert data[0] == pKey
        assert data[1] == 0
Esempio n. 13
0
    def testRemoveEntity(self):

        wHome = Server.getHome("widgetHome")
        widget = wHome.create()
        pKey = widget._get_primaryKey()
        Server.txn.commit(0)

        txnStart()
        widget = wHome.findByPrimaryKey(pKey)
        wHome.remove(widget)
        
        Server.txn.commit(0)

        txnStart()
        try:
            wHome.findByPrimaryKey(pKey)
            raise AssertionError("Widget not deleted.")
        except FinderException:
            pass
Esempio n. 14
0
    def testTwoRelatesOneTransaction(self):
        """testTwoRelatesOneTransaction
        Assures that if you relate an object to two different objects over
        the course of a transaction that everything behaves properly as we
        have/had a problem with things freaking out.  The relations should
        all exist before the transaction starts.
        """
        wHome = Server.getHome('widgetHome')

        user1 = wHome.create()
        user2 = wHome.create()
        lead1 = wHome.create()
        lead2 = wHome.create()

        u1pk = user1._get_primaryKey()
        u2pk = user2._get_primaryKey()

        rs = self.rs

        rs.relate(user1, lead1, 'many_test')
        rs.relate(user2, lead2, 'many_test')

        Server.txn.commit(0)
        Server.txn.begin()
        
        lead3 = wHome.create()
        user1 = wHome.findByPrimaryKey(u1pk)
        user2 = wHome.findByPrimaryKey(u2pk)

        rs.relate(user1, lead3, 'many_test')
        #rs.relate(user2, lead3, 'many_test')

        try:
            Server.txn.commit(0) # This is usually the problem line
            Server.txn.begin()
        except:
            Server.txn.suspend()
            Server.txn.begin()
            assert 0, "Caught error during commit."

        assert 1
Esempio n. 15
0
    def testRelationConflict(self):
        """Test 2 users trying to modify the same relation"""
        wHome = Server.getHome('widgetHome')

        w1a = wHome.create()
        w1b = wHome.create()
        w1c = wHome.create()
        w2 = wHome.create()

        # Keys for loading it later
        w1aKey = w1a._get_primaryKey()
        w1bKey = w1b._get_primaryKey()
        w1cKey = w1c._get_primaryKey()
        w2Key = w2._get_primaryKey()

        # w2 is assocated with w1a.  2 transactions each try to
        # move ti, one to w1b and one to w1c.  At the end, the last
        # committed transaction should win with no invalid references
        # laying around.
        self.rs.relate(w1a, w2, 'many_test')
        Server.txn.commit(0)

        # Load the objects in each transaction
        Server.txn.begin()
        w1b = wHome.findByPrimaryKey(w1bKey)
        w2a = wHome.findByPrimaryKey(w2Key)
        context1 = Server.txn.suspend()

        Server.txn.begin()
        w1c = wHome.findByPrimaryKey(w1cKey)
        w2b = wHome.findByPrimaryKey(w2Key)
        context2 = Server.txn.suspend()

        Server.txn.resume(context1)
        self.rs.relate(w1b, w2a, 'many_test')
        context1 = Server.txn.suspend()

        Server.txn.resume(context2)
        self.rs.relate(w1c, w2b, 'many_test')
        context2 = Server.txn.suspend()

        Server.txn.resume(context1)
        Server.txn.commit(0)

        Server.txn.resume(context2)
        Server.txn.commit(0)

        Server.txn.begin()
        # Reload all the objects for this transaction
        w1a = wHome.findByPrimaryKey(w1aKey)
        w1b = wHome.findByPrimaryKey(w1bKey)
        w1c = wHome.findByPrimaryKey(w1cKey)
        w2 = wHome.findByPrimaryKey(w2Key)
        
        # Validate the final object
        relatives = self.rs.findPointingFrom(w1c, 'many_test')
        assert len(relatives) == 1, "Should have 1 relative, found %s" % len(relatives)
        assert relatives[0]._get_primaryKey() == w2._get_primaryKey()

        # Validate the object we moved
        relatives = self.rs.findPointingTo(w2, 'many_test')
        assert len(relatives) == 1, "Should have 1 relative, found %s" % len(relatives)
        assert relatives[0]._get_primaryKey() == w1c._get_primaryKey()

        # Assure both the other objects have no relations
        relatives = self.rs.findPointingFrom(w1a, 'many_test')
        assert len(relatives) == 0, "Should have 0 relatives, found %s" % len(relatives)
        relatives = self.rs.findPointingFrom(w1b, 'many_test')
        assert len(relatives) == 0, "Should have 0 relatives, found %s" % len(relatives)
Esempio n. 16
0
 def _get_home():
     return Server.getHome("widgetHome")
Esempio n. 17
0
 def testLoad(self):
     w = Server.getHome('widgetHome').create()
     Server.txn.commit(0)
     Server.txn.begin()
     w1 = self.ps.load(Widget, w._get_primaryKey())
     assert w1._get_primaryKey() == w._get_primaryKey()
Esempio n. 18
0
 def testCreate(self):
     # Just test to make sure we don't get any errors.
     w = Server.getHome('widgetHome').create()
     Server.txn.commit(0)
     Server.txn.begin()
    def testGetMetadata(self):
        w1 = Server.getHome('widgetHome').create()
        w2 = Server.getHome('widgetHome').create()

        w1._set_other1(w2)
        w1._set_name('foobar')
        w1._set_wedgie(99)

        w2._set_name('barfoo')
        w2._set_wedgie(69)

        pk1 = w1._get_primaryKey()
        pk2 = w2._get_primaryKey()

        Server.txn.commit(1)
        txnStart()

        results = self.qs.query({'type':'widget',
                                 'primaryKey':pk1,
                                 'query_select':
                                 ['wedgie', 'name', 'parent', 'doodle']})

        results = results.fetchMany('query', 100)
        assert len(results) == 1

        result = results[0].value()

        fields = self._getFields(self.eds.getFields('widget'))

        assert len(result.keys()) == len(fields)

        assert result['name'] == 'foobar'
        assert result['parent'] is None
        assert result['doodle'] is None
        #assert result['other1'].primaryKey == pk2
        #assert result['other2'] is None
        assert result['wedgie'] == 99
        assert result['primaryKey'] == pk1

        # Test getting 2 objects back
        results = self.qs.query_metadata('type=widget&primaryKey=%s&primaryKey=%s' % (pk1, pk2))
        results = results.fetchMany('query', 100)
        assert len(results) == 2

        result = results[0].value()

        fields = self._getFields(self.eds.getFields('widget'))

        assert len(result.keys()) == len(fields)

        assert result['name'] == 'foobar'
        assert result['parent'] is None
        assert result['doodle'] is None
        assert result['other1'].primaryKey == pk2
        assert result['other2'] is None
        assert result['wedgie'] == 99
        assert result['primaryKey'] == pk1

        result = results[1].value()

        fields = self._getFields(self.eds.getFields('widget'))

        assert len(result.keys()) == len(fields)

        assert result['name'] == 'barfoo'
        assert result['parent'] is None
        assert result['doodle'] is None
        assert result['other1'] is None
        assert result['other2'].primaryKey == pk1
        assert result['wedgie'] == 69
        assert result['primaryKey'] == pk2
Esempio n. 20
0
 def testGet(self):
     w = Server.getHome('moneyWidgetHome').create(self.money)
     Server.txn.commit(0)
     Server.txn.begin()
     w1 = self.ps.load(MoneyWidget, w._get_primaryKey())
     assert w1._get_money().getAmount() == 10.0
Esempio n. 21
0
 def _get_home():
     return Server.getHome("moneyWidgetHome")
Esempio n. 22
0
 def _get_home():
     return Server.getHome("doodleHome")