'django pytest / update delete view testing
I'm working on pytest testing for my 1st django app, kind of cookery book.
I have problems with edit/delete view tests.
For example, i have a test for testing add recipe view.
Here is recipe model:
class Recipe(models.Model):
"""Instructions how to prepare a single dish."""
title = models.CharField(max_length=50)
cooking_time = models.IntegerField(help_text='in minutes', validators=[MinValueValidator(1), MaxValueValidator(5000)])
difficulty_level = models.IntegerField(choices=DIFFICULTY_LEVELS, default=1)
description = models.TextField()
created = models.DateTimeField(auto_now_add=True)
cuisine = models.ForeignKey('Cuisine', on_delete=models.CASCADE, null=True)
ingredient = models.ManyToManyField(Ingredient, through='IngredientRecipe')
meal_plan = models.ManyToManyField('MealPlan', through='RecipeMealPlan')
here is it's fixture:
@pytest.fixture()
def example_recipe():
rec = Recipe.objects.create(title='rec', cooking_time=10, difficulty_level=2, description='bla, bla, bla')
return rec
And tests which works fine:
@pytest.mark.django_db
def test_add_recipe(client, example_recipe):
dct = {
'title': 'rec',
'cooking_time': 10,
'difficulty_level': 2,
'description': 'bla, bla, bla'
}
url = reverse('add-recipe')
response = client.post(url, dct)
assert response.status_code == 302
assert Recipe.objects.get(**dct)
@pytest.mark.django_db
def test_add_recipe2(client):
url = reverse('add-recipe')
response = client.get(url)
assert response.status_code == 302
Now I'm trying to write test for recipe update/delete views.
My recipe update test currently looks like this:
@pytest.mark.django_db
def test_ingredient_update_view(client, example_ingredient):
url = reverse('update-ingredient', kwargs={'id': example_ingredient.id})
response = client.get(url)
assert Ingredient.objects.save(
name='cos',
nutrient=2,
glycemic_index=2
)
I know its wrong but i face problems to make it work. Same with delete view.
Could someone take a look on above and give me some advice?
Thanks in advance for any help.
Solution 1:[1]
@pytest.mark.django_db
def test_ingredient_update_view(client, example_ingredient):
url = reverse('update-ingredient', kwargs={'pk': example_ingredient.pk})
response = client.get(url)
obj = Ingredient.objects.get(example_ingredient.pk)
obj.name = 'else'
obj.save()
assert response.status_code == 302
current error is:
TypeError: 'int' object is not iterable
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | python_student |
