def test_clean_validation_custom_exists(self):
        """
        If a custom benefit exists, and the data for range, type and value is supplied, the form should validate.
        Clean should return the cleaned data.
        """

        create_benefit(CustomBenefitModel)

        form = forms.BenefitForm(
            data={
                'range': self.range.id,
                'type': Benefit.FIXED,
                'value': 5,
                'custom_benefit': ''
            })

        self.assertTrue(form.is_valid())
        self.assertEqual(
            {
                'range': self.range,
                'type': Benefit.FIXED,
                'value': D('5'),
                'custom_benefit': '',
                'max_affected_items': None
            }, form.clean())
 def test_init_with_custom_benefit(self):
     """
     If a custom benefit exists, the type field should not be required.
     """
     create_benefit(CustomBenefitModel)
     form = forms.BenefitForm()
     self.assertFalse(form.fields['type'].required)
     self.assertEqual(form.fields['custom_benefit'].initial, None)
    def test_clean_only_range_custom_exists(self):
        """
        If a custom benefit exists, the type field is not required. Still, the clean method should throw a
        ValidationError, if only the range is supplied.
        """
        create_benefit(CustomBenefitModel)

        form = forms.BenefitForm(data={
            'range': self.range,
            'type': '',
            'value': '',
            'custom_benefit': ''
        })

        self.assertFalse(form.is_valid())
        self.assertRaises(ValidationError, form.clean)
 def test_init_with_custom_benefit_with_instance(self):
     """
     If a custom benefit exists and the kwargs instance is passed to init, the initial value for the custom_benefit
     should be the instance.
     """
     benefit = create_benefit(CustomBenefitModel)
     form = forms.BenefitForm(instance=benefit)
     self.assertFalse(form.fields['type'].required)
     self.assertEqual(form.fields['custom_benefit'].initial, benefit.id)
Example #5
0
def create_offer():
    range = models.Range.objects.create(
        name="All products", includes_all_products=True)
    condition = models.CountCondition.objects.create(
        range=range,
        type=models.Condition.COUNT,
        value=1)
    benefit = custom.create_benefit(CustomAction)
    return models.ConditionalOffer.objects.create(
        condition=condition,
        benefit=benefit,
        offer_type=models.ConditionalOffer.SITE)
Example #6
0
    def test_custom_benefits_and_conditions_not_deleted(self):
        condition = custom.create_condition(BasketOwnerCalledBarry)
        condition_id = condition.id

        benefit = custom.create_benefit(CustomBenefitModel)
        benefit_id = benefit.id

        offer = factories.create_offer(benefit=benefit, condition=condition)
        offer.delete()

        self.assertTrue(
            models.Condition.objects.filter(id=condition_id).exists())
        self.assertTrue(models.Benefit.objects.filter(id=benefit_id).exists())
    def test_clean_validation_with_custom_benefit(self):
        """
        If a custom benefit is selected, the form should be valid.
        """
        benefit = create_benefit(CustomBenefitModel)

        form = forms.BenefitForm(data={
            'range': '',
            'type': '',
            'value': '',
            'custom_benefit': benefit.id
        })

        self.assertTrue(form.is_valid())
        self.assertEqual(
            {
                'range': None,
                'type': '',
                'value': None,
                'custom_benefit': str(benefit.id),
                'max_affected_items': None
            }, form.clean())
Example #8
0
 def setUp(self):
     self.custom_benefits = [
         custom.create_benefit(CustomBenefitModel),
         custom.create_benefit(CustomBenefitWithoutName)
     ]