def testValidation(self): Entity._ally_listeners = {} validateAutoId(Entity.Id) validateRequired(Entity.Required) validateMaxLength(Entity.WithLength, 5) validateManaged(Entity.Managed) dummyService = DummyServiceEntity() proxySrvNonValid = proxyWrapFor(dummyService) proxySrv = proxyWrapFor(dummyService) bindValidations(proxySrv) assert isinstance(proxySrv, IServiceEntity) e = Entity() self.assertRaisesRegex(InputError, "(Entity.Required='Expected a value')", proxySrv.insert, e) self.assertEqual(proxySrvNonValid.insert(e), 'inserted') self.assertRaisesRegex(InputError, "(Entity.Id='Expected a value')", proxySrv.update, e) self.assertEqual(proxySrvNonValid.update(e), 'updated') e.Id = 'custom id' self.assertRaisesRegex(InputError, "(Entity.Id='No value expected')", proxySrv.insert, e) self.assertTrue(proxySrv.update(e) == 'updated') e = Entity() e.Required = 'Provided a value' self.assertTrue(proxySrv.insert(e) == 'inserted') e.Id = 'id' self.assertTrue(proxySrv.update(e) == 'updated') e = Entity() e.Required = 'required' e.WithLength = 'This is a longer text then 5' self.assertRaisesRegex(InputError, "(Entity.WithLength='Maximum length allowed is 5)", proxySrv.insert, e) e.WithLength = 'hello' self.assertTrue(proxySrv.insert(e) == 'inserted') e.WithLength = 'This is a longer text then 5' e.Id = 'id' self.assertRaisesRegex(InputError, "(Entity.WithLength='Maximum length allowed is 5)", proxySrv.update, e) e.WithLength = 'hello' self.assertTrue(proxySrv.update(e) == 'updated') e = Entity() e.Required = 'required' e.Managed = 'should not have value' self.assertRaisesRegex(InputError, "(Entity.Managed='No value expected')", proxySrv.insert, e) e.Id = 'id' self.assertRaisesRegex(InputError, "(Entity.Managed='No value expected')", proxySrv.update, e) self.assertRaises(AttributeError, getattr, proxySrv, '_hidden')
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])
def bindSecurityValidations(proxy): bindValidations(proxy, mappingsOf(meta))
def bindExampleValidations(proxy): bindValidations(proxy, mappingsOf(meta))
def bindInternationalizationValidations(proxy): bindValidations(proxy, mappingsOf(meta))