def test__get_models(self, is_condition): is_condition.return_value = True self.assertRaises(AttributeError, self.condition._get_models, Operand(self.condition)) is_condition.return_value = False models = self.condition._get_models(Operand(TestModel.test_field_one)) self.assertEqual(models, {TestModel})
class Condition(ConditionMixin): def __init__(self, left_operand, right_operand, operator): self._validate(left_operand, right_operand, operator) self._left_operand = Operand(left_operand) self._right_operand = Operand(right_operand) self._operator = operator def to_str(self): left_operand_str = self._left_operand.to_str() right_operand_str = self._right_operand.to_str() return CONDITION_TMPL % (left_operand_str, self._operator.string_repr, right_operand_str) def get_data(self, data=None): if data is None: data = [] self._add_operand_data(self._left_operand, data) self._add_operand_data(self._right_operand, data) return data @staticmethod def _add_operand_data(operand, data): if operand.is_data(): data.append(operand.instance) elif operand.is_condition(): operand.instance.get_data(data) def get_models(self): return self._get_models(Operand(self)) def _get_models(self, operand): if operand.is_condition(): return self._get_models(operand.instance.left_operand) | \ self._get_models(operand.instance.right_operand) return {operand.instance.model} if operand.is_field() else set() @staticmethod def _validate(left_operand, right_operand, operator): if operator in [operators.AND, operators.OR] \ and not (isinstance(left_operand, Condition) and isinstance(right_operand, Condition)): raise ConditionError( 'The condition can not be constructed. ' 'Are you sure that the condition is in parentheses?') @property def left_operand(self): return self._left_operand @property def right_operand(self): return self._right_operand
def test__add_operand_data(self, get_data, is_condition, is_data): get_data.return_value = None operand = Operand(self.condition) is_data.return_value = True data = [] self.condition._add_operand_data(operand, data) self.assertEqual(get_data.call_count, 0) self.assertEqual(operand.instance, data[0]) is_data.return_value = False is_condition.return_value = True data = [] self.condition._add_operand_data(operand, data) self.assertEqual(get_data.call_count, 1) self.assertEqual(data, []) is_data.return_value = False is_condition.return_value = False get_data.call_count = 0 self.condition._add_operand_data(operand, data) self.assertEqual(get_data.call_count, 0) self.assertEqual(data, [])
def get_models(self): return self._get_models(Operand(self))
def __init__(self, left_operand, right_operand, operator): self._validate(left_operand, right_operand, operator) self._left_operand = Operand(left_operand) self._right_operand = Operand(right_operand) self._operator = operator
def test_to_str(self, field_to_str, condition_to_str): condition_to_str.return_value = 'test_condition' field_to_str.return_value = 'test_field' operand = Operand(Condition(TestModel.test_field_one, TestModel.test_field_two, Operator('='))) self.assertEqual(operand.to_str(), 'test_condition') self.assertTrue(condition_to_str.called) operand = Operand(TestModel.test_field_one) self.assertEqual(operand.to_str(), 'test_field') self.assertTrue(field_to_str.called) operand = Operand(1) self.assertEqual(operand.to_str(), '%s') operand = Operand(None) self.assertEqual(operand.to_str(), '%s') operand = Operand(EmptyOperand()) self.assertEqual(operand.to_str(), '')
def test_is_data(self): operand = Operand(TestModel.test_field_one) self.assertFalse(operand.is_data()) operand = Operand(Condition(TestModel.test_field_one, TestModel.test_field_two, Operator('='))) self.assertFalse(operand.is_data()) operand = Operand(EmptyOperand()) self.assertFalse(operand.is_data()) operand = Operand(1) self.assertTrue(operand.is_data())