def test_manufacturing_years_false(self): """Проверяем, что если ввести год начала пр-ва старше года окончания пр-ва то валидация не проходит""" data = self.form_data.copy() data["first_year"], data["last_year"] = 1960, 1959 form = BoatForm(data=data) self.assertFalse(form.is_valid()) self.assertTrue(form.errors.get("last_year") == ['Last year has to be superior then' ' first year']) form.errors.clear()
def test_url_sailboatdata(self): """Проверка возникновения ошибки при неправильном урле""" msg3 = "This URL does not work!" msg4 = "Please provide url exactly to 'sailboatdata.com' " msg5 = "Connection error" data = self.form_data.copy() # проверка при неправильном урле data["boat_sailboatdata_link"] = "https://sailboatdata.com/sailboat/bavaria-cruiser-xxx" form = BoatForm(data=data) request = requests.head(data.get("boat_sailboatdata_link")) self.assertFalse(form.is_valid()) self.assertFalse(request.status_code // 100 == 2) self.assertTrue(form.errors.get("boat_sailboatdata_link") == [msg3]) form.errors.clear() # урл правильный, но не на "sailboatdata" data = self.form_data.copy() data["boat_sailboatdata_link"] = \ "https://docs.djangoproject.com/en/2.2/ref/forms/validation/" form = BoatForm(data=data) request = requests.head(data.get("boat_sailboatdata_link")) self.assertTrue(request.status_code // 100 == 2) self.assertFalse(form.is_valid()) self.assertTrue(form.errors.get("boat_sailboatdata_link") == [msg4]) form.errors.clear() # проверка на ошибку связи data = self.form_data.copy() data["boat_sailboatdata_link"] = "https://devefgbghbghnghng.com" form = BoatForm(data=data) self.assertFalse(form.is_valid()) self.assertTrue(form.errors.get("boat_sailboatdata_link") == [msg5]) form.errors.clear()
def test_UniqueNameValidator(self): """Тестируем кастомный валидатор UniqueNameValidator:""" Heading.objects.create(name="heading") SubHeading.objects.create(name="boat", foreignkey_id=1) self.assertTrue(SubHeading.objects.filter(name="boat").exists()) data = self.form_data.copy() form = BoatForm(data=data) message = "Subheading with this name is already exists. Please choose an alternative " \ "name or slightly correct current one" self.assertFalse(form.is_valid()) self.assertEqual([message], form.errors.get("boat_name"))
def test_HiddenInput_currency(self): """Проверяем, чтобы в форме редактирования лодки поле "currency" было скрытым и оно не требовалось к занесению""" data = self.form_data.copy form = BoatForm(data=data, pk=1) self.assertEqual("<class 'django.forms.widgets.HiddenInput'>", str(type(form.fields[ "currency"].widget))) self.assertFalse(form.fields["currency"].required) self.assertIsInstance(form.fields["currency"].widget, forms.HiddenInput)
def test_years_choices(self): """Проверяем работу генератора годов""" data = self.form_data.copy() form = BoatForm(data=data) self.assertEqual(list(form.fields["first_year"].choices), list(form.fields[ "last_year"].choices)) # Проверяем , чтобы choices не были пустыми self.assertNotEqual(list(form.fields["first_year"].choices), []) self.assertNotEqual(list(form.fields["last_year"].choices), [])
def test_UniqueSailboatLinkValidator(self): """Тестируем работу кастомного валидатора UniqueSailboatLinkValidator""" self.assertFalse(settings.DEBUG) BoatModel.objects.bulk_create([BoatModel(boat_name="boat", boat_length=30, boat_mast_type="YA", boat_keel_type="modified", boat_price=10000, boat_country_of_origin="AX", boat_sailboatdata_link="https://sailboatdata.com/sailboat" "/freedom-35-pedrick", boat_description="xxx", first_year=1959, last_year=1960, ), ]) data = self.form_data.copy() data.update({ "boat_sailboatdata_link": "https://sailboatdata.com/sailboat/freedom-35-pedrick"}) form = BoatForm(data=data) self.assertTrue(BoatModel.objects.filter( boat_sailboatdata_link="https://sailboatdata.com/sailboat/freedom-35-pedrick" "").exists()) self.assertEquals("https://sailboatdata.com/sailboat/freedom-35-pedrick", data["boat_sailboatdata_link"]) message = 'same link to the "Sailboatdata" is already exist in database!' self.assertFalse(form.is_valid()) self.assertEqual([message], form.errors.get("boat_sailboatdata_link"))
def test_price_level(self): """Проверям, чтобы цена не была менее 5000 euro. Работа валидатора формы""" # Для создаваемой лодки msg = "Are you sure? It is almost free!" data = self.form_data.copy() data["currency"] = "SEK" data["boat_price"] = 10000 form = BoatForm(data=data) self.assertFalse(form.is_valid()) self.assertTrue(form.errors.get("boat_price")) form.errors.clear() # для редактируемой лодки data["currency"] = "EUR" data["boat_price"] = 4000 form = BoatForm(data=data, pk=1) self.assertFalse(form.is_valid()) self.assertTrue(form.errors.get("boat_price") == [msg]) form.errors.clear()
def test_manufacturing_years_true(self): """Проверка валидации корректно заполненной формы""" data = self.form_data.copy() form = BoatForm(data=data) self.assertTrue(form.is_valid())
def test_currency_choices(self): """Проверяем работу генератора валют""" data = self.form_data.copy form = BoatForm(data=data) self.assertNotEqual(list(form.fields["currency"].choices), [])