Exemple #1
0
    def test_use_tm(self):
        # Resgister entry
        register_entry('integer_as_key_container', IntegerAsKeyContainer)

        root = get_root()
        container = root['integer_as_key_container']

        # create a base entry for direct node modification test
        request = self.layer.new_request()
        node = container['1'] = IntegerAsKeyNode()
        node.attrs['field'] = 'Value'
        node()

        # transaction manager used, calling nodes flushes session
        os.environ['CONE_SQL_USE_TM'] = '1'
        self.assertTrue(use_tm())

        # modify existing node
        node.attrs['field'] = 'New'
        node()

        # create new node
        container['2'] = IntegerAsKeyNode()
        container()

        session = get_session(request)
        res = session.query(IntegerAsPrimaryKeyRecord).all()
        self.assertEqual(len(res), 2)
        self.assertEqual(res[0].field, 'New')

        # rollback works, session was just flushed
        session.rollback()
        res = session.query(IntegerAsPrimaryKeyRecord).all()
        self.assertEqual(len(res), 1)
        self.assertEqual(res[0].field, 'Value')

        # no transaction manager used, calling nodes commits session
        os.environ['CONE_SQL_USE_TM'] = '0'
        self.assertFalse(use_tm())

        # modify existing node
        node.attrs['field'] = 'New'
        node()

        # create new node
        container['2'] = IntegerAsKeyNode()
        container()

        session = get_session(request)
        res = session.query(IntegerAsPrimaryKeyRecord).all()
        self.assertEqual(len(res), 2)
        self.assertEqual(res[0].field, 'New')

        # rollback has not effect, session was commited
        session.rollback()
        res = session.query(IntegerAsPrimaryKeyRecord).all()
        self.assertEqual(len(res), 2)
        self.assertEqual(res[0].field, 'New')
Exemple #2
0
 def __call__(self):
     session = self.session
     if self._new:
         session.add(self.record)
         self._new = False
     if use_tm():
         self.session.flush()
     else:
         self.session.commit()
Exemple #3
0
 def __call__(self):
     if use_tm():
         self.session.flush()
     else:
         self.session.commit()