check the dates in the Warranty forms
This commit is contained in:
parent
613fc79516
commit
a1e4f8fc5a
|
@ -43,6 +43,18 @@ class WarrantyCreateForm(forms.ModelForm):
|
|||
"""
|
||||
Basic form class to use crispies HTML5 forms.
|
||||
"""
|
||||
|
||||
def clean(self):
|
||||
cleaned_data = super().clean()
|
||||
valid_from = cleaned_data.get("valid_from")
|
||||
valid_until = cleaned_data.get("valid_until")
|
||||
|
||||
if valid_from and valid_until:
|
||||
if valid_from > valid_until:
|
||||
raise forms.ValidationError(
|
||||
"Valid from date must be before valid until date")
|
||||
return cleaned_data
|
||||
|
||||
class Meta:
|
||||
model = Warranty
|
||||
fields = '__all__'
|
||||
|
@ -52,6 +64,18 @@ class WarrantyUpdateForm(forms.ModelForm):
|
|||
"""
|
||||
Basic form class to use crispies HTML5 forms.
|
||||
"""
|
||||
|
||||
def clean(self):
|
||||
cleaned_data = super().clean()
|
||||
valid_from = cleaned_data.get("valid_from")
|
||||
valid_until = cleaned_data.get("valid_until")
|
||||
|
||||
if valid_from and valid_until:
|
||||
if valid_from > valid_until:
|
||||
raise forms.ValidationError(
|
||||
"Valid from date must be before valid until date")
|
||||
return cleaned_data
|
||||
|
||||
class Meta:
|
||||
model = Warranty
|
||||
fields = '__all__'
|
||||
|
|
|
@ -0,0 +1,45 @@
|
|||
import pytest
|
||||
from mixer.backend.django import mixer
|
||||
|
||||
from devices import forms
|
||||
|
||||
pytestmark = pytest.mark.django_db
|
||||
|
||||
|
||||
def test_warranty_create_form(create_admin_user):
|
||||
create_admin_user()
|
||||
form = forms.WarrantyCreateForm(data={})
|
||||
assert form.is_valid() is False, (
|
||||
"Should be false because no data was given")
|
||||
|
||||
device = mixer.blend("devices.Device")
|
||||
|
||||
data = {"device": device,
|
||||
"valid_from": "2010-01-01",
|
||||
"valid_until": "2020-01-01"}
|
||||
form = forms.WarrantyCreateForm(data=data)
|
||||
assert form.is_valid() is True, ("Should be valid with the given data.")
|
||||
|
||||
data = {"device": device,
|
||||
"valid_from": "2010-01-01",
|
||||
"valid_until": "2000-01-01"}
|
||||
form = forms.WarrantyCreateForm(data=data)
|
||||
assert form.is_valid() is False, (
|
||||
"Should be false because valid from is before valid until")
|
||||
assert "Valid from date must be before valid until date" == form.errors[
|
||||
"__all__"][0]
|
||||
|
||||
|
||||
def test_warranty_update_form(create_admin_user):
|
||||
create_admin_user()
|
||||
warranty = mixer.blend("devices.Warranty", valid_from="2010-01-01",
|
||||
valid_until="2020-01-01")
|
||||
data = {
|
||||
"warranty": warranty,
|
||||
"valid_from": "2010-01-01",
|
||||
"valid_until": "2000-01-01"
|
||||
}
|
||||
form = forms.WarrantyUpdateForm(data=data)
|
||||
assert form.is_valid() is False
|
||||
assert "Valid from date must be before valid until date" == form.errors[
|
||||
"__all__"][0]
|
Loading…
Reference in New Issue