def test_retrieve_message_byfilter_or(self): '''This test case ensures filtering message using compound **or** works as expected.''' model_filter_gt = ModelFilter(ModelFacadeMessage.id, self.last_generated_pk, ModelFilter.GT) model_filter_ge = ModelFilter(ModelFacadeMessage.id, self.last_generated_pk + 1, ModelFilter.GT) model_filter_eq = ModelFilter(ModelFacadeMessage.id, 1, ModelFilter.EQ) model_filter_lt = ModelFilter(ModelFacadeMessage.id, -1, ModelFilter.LT) model_filter_le = ModelFilter(ModelFacadeMessage.id, -1, ModelFilter.LE) model_filter_like = ModelFilter(ModelFacadeMessage.message, "%%world 4%%", ModelFilter.LIKE) model_filter_or = ModelFilterOr(model_filter_gt, model_filter_ge, model_filter_eq, model_filter_like, model_filter_lt, model_filter_le) records = self.model_facade.get_records_paged( 0, 100, filter_expr=model_filter_or) self.assertEqual(1, len(records)) self.assertEqual(self.last_generated_pk, records[0].id) self.assertEqual(self.MESSAGES[-1], records[0].message)
def test_modelfilteror_ok(self): '''This test case ensures compound **or** filter correctly transform the filter into sql alchemy and_ statement.''' self._or_invoked = False model_filter = ModelFilterOr( ModelFilter(self._model.id, 1, ModelFilter.EQ), ModelFilter(self._model.id, 1, ModelFilter.EQ), ModelFilter(self._model.id, 1, ModelFilter.EQ)) query = Mock() def filter_fn(expr): self._or_invoked = True return Mock() self._model.id.table = Mock() query._primary_entity = query query.selectable = self._model.id.table query.filter = filter_fn query_new = model_filter.build(query) self.assertTrue(self._or_invoked) self.assertIsInstance(query_new, Mock)
def test_modelfiteror_unhandled_exception(self): '''This test case ensures unhandled exceptions raised from ModelFilter are gracefully handled by ModelFilterOr build.''' model_filter = ModelFilter(self._model.id, 1, ModelFilter.EQ) model_filter.get_expression = Mock( side_effect=Exception("Unhandled exception")) model_filter_or = ModelFilterOr(model_filter, model_filter, model_filter) with self.assertRaises(FantasticoError): model_filter_or.build(Mock())
def test_modelfilteror_notenoughargs(self): '''This test case ensures compound **or** filter can not be built with a single argument.''' with self.assertRaises(FantasticoNotSupportedError): ModelFilterOr(ModelFilter(self._model.id, 1, ModelFilter.EQ))
def test_modelfilteror_noargs(self): '''This test case ensures compound **or** filter can not be built without arguments.''' with self.assertRaises(FantasticoNotSupportedError): ModelFilterOr()