def process(self, chain, response:Response, **keyargs):
        '''
        @see: HandlerProcessor.process
        
        Wraps the invoking and all processors after invoking in a transaction.
        '''
        assert isinstance(chain, Chain), 'Invalid processors chain %s' % chain
        assert isinstance(response, Response), 'Invalid response %s' % response

        setKeepAlive(True)
        
        def onFinalize():
            '''
            Handle the finalization
            '''
            if Response.isSuccess in response:
                if response.isSuccess is True: endSessions(commit)
                else: endSessions(rollback)
            else: endSessions(commit) # Commit if there is no success flag

        def onError():
            '''
            Handle the error.
            '''
            endSessions(rollback)
        
        chain.callBack(onFinalize)
        chain.callBackError(onError)
    def process(self, chain, response: Response, **keyargs):
        '''
        @see: HandlerProcessor.process
        
        Wraps the invoking and all processors after invoking in a transaction.
        '''
        assert isinstance(chain, Chain), 'Invalid processors chain %s' % chain
        assert isinstance(response, Response), 'Invalid response %s' % response

        setKeepAlive(True)

        def onFinalize():
            '''
            Handle the finalization
            '''
            if Response.isSuccess in response:
                if response.isSuccess is True: endSessions(commit)
                else: endSessions(rollback)
            else: endSessions(commit)  # Commit if there is no success flag

        def onError():
            '''
            Handle the error.
            '''
            endSessions(rollback)

        chain.callBack(onFinalize)
        chain.callBackError(onError)
Exemple #3
0
    def test(self):
        articleTypeService = createProxy(IArticleTypeService)(ProxyWrapper(ArticleTypeServiceAlchemy()))
        assert isinstance(articleTypeService, IArticleTypeService)

        bindSession(articleTypeService, self.sessionCreate)
        bindValidations(articleTypeService, mappingsOf(meta))

        setKeepAlive(True)

        at = ArticleType()
        at.Name = 'Test Type 1'
        articleTypeService.insert(at)
        self.assertEqual(at.Id, 1)

        at.Id = 20
        # Validate auto id.
        self.assertRaisesRegex(InputError, "(ArticleType.Id='No value expected')", articleTypeService.insert, at)

        del at.Id
        at.Name = 'Test Type 2'
        articleTypeService.insert(at)
        self.assertEqual(at.Id, 2)

        endSessions(commit)

        at = ArticleType()
        at.Name = 'Test Type 1'
        # Validate unique id.
        self.assertRaisesRegex(InputError, "(ArticleType.Name='Already an entry with this value')",
                               articleTypeService.insert, at)

        at = ArticleType()
        at.Name = 'Test Type 3'
        articleTypeService.insert(at)
        self.assertEqual(at.Id, 3)

        endSessions(commit)

        at = articleTypeService.getById(2)
        at.Name = 'Invalid'
        endSessions(commit)
        at = articleTypeService.getById(2)
        self.assertEqual(at.Name, 'Test Type 2')

        endSessions(commit)

        at = articleTypeService.getById(2)
        at.Name = 'Invalid'
        articleTypeService.update(at)
        endSessions(commit)
        at = articleTypeService.getById(2)
        self.assertEqual(at.Name, 'Invalid')

        articleTypeService.delete(at.Id)
        self.assertRaisesRegex(InputError, "(ArticleType.Id='Unknown id')", articleTypeService.getById, at.Id)

        articleService = createProxy(IArticleService)(ProxyWrapper(ArticleServiceAlchemy()))
        assert isinstance(articleService, IArticleService)

        bindSession(articleService, self.sessionCreate)
        bindValidations(articleService, mappingsOf(meta))

        a = Article()
        a.Name = 'Article 1'
        a.Type = 1
        articleService.insert(a)
        self.assertEqual(a.Id, 1)

        a = Article()
        a.Name = 'Article 2'
        a.Type = 12
        # Invalid foreign key
        self.assertRaisesRegex(InputError, "(Article.Type='Unknown foreign id')", articleService.insert, a)

        endSessions(commit)

        a = articleService.getById(1)
        self.assertEqual(a.Id, 1)
        self.assertEqual(a.Name, 'Article 1')
        self.assertEqual(a.Type, 1)
        at = articleTypeService.getById(a.Type)
        self.assertEqual(at.Id, 1)
        self.assertEqual(at.Name, 'Test Type 1')

        endSessions(commit)

        q = QArticleType()
        q.name.orderDesc()
        self.assertEqual([e.Id for e in articleTypeService.getAll(q=q)], [3, 1])
        q = QArticleType()
        q.name.orderAsc()
        self.assertEqual([e.Id for e in articleTypeService.getAll(q=q)], [1, 3])
        q = QArticleType(name='%1')
        self.assertEqual([e.Id for e in articleTypeService.getAll(q=q)], [1])