[Fixed]-How to set dynamic many to many in factory boy with through table?

16๐Ÿ‘

โœ…

Iโ€™ve just recreated this setup and Iโ€™m struggling to see what the problem is. Here are several tests that demonstrate that all seems to be working well? Or am I misunderstanding the question?

# create recipe ingredient and recipe ingredient
recipe = RecipeFactory(name="recipe1")
ingredient = IngredientFactory(name="ingredient1")
recipe_ingredient = RecipeIngredientFactory(recipe=recipe, ingredient=ingredient)

# recipe created?       
r = Recipe.objects.all().first()
self.assertEqual(r, recipe)

# ingredient created?
i = Ingredient.objects.all().first()
self.assertEqual(i, ingredient)

# recipe ingredient created?
ri = RecipeIngredient.objects.all().first()
self.assertEqual(ri, recipe_ingredient)        

# test many to many
self.assertEqual(ri, r.recipeingredient_set.all()[0])
self.assertEqual(ri, i.recipeingredient_set.all()[0])

# add a new ingredient to recipe 1
ingredient2 = IngredientFactory(name='ingredient2')
recipe_ingredient2 = RecipeIngredientFactory(recipe=recipe, ingredient=ingredient2)

# test many to many
self.assertTrue(recipe_ingredient in r.recipeingredient_set.all())
self.assertTrue(recipe_ingredient2 in r.recipeingredient_set.all())

# create a pre-existing recipe and a set of ingredients
pizza_recipe = RecipeFactory(name='Pizza')
cheese_on_toast_recipe = RecipeFactory(name='Cheese on toast')

cheese_ingredient = IngredientFactory(name='Cheese')
tomato_ingredient = IngredientFactory(name='Tomato')
pizza_base_ingredient = IngredientFactory(name='Pizza base')
toast_ingredient = IngredientFactory(name='Toast')

# now put together
RecipeIngredientFactory(recipe=pizza_recipe, ingredient=cheese_ingredient)
RecipeIngredientFactory(recipe=pizza_recipe, ingredient=tomato_ingredient)
RecipeIngredientFactory(recipe=pizza_recipe, ingredient=pizza_base_ingredient)

RecipeIngredientFactory(recipe=cheese_on_toast_recipe, ingredient=cheese_ingredient)        
RecipeIngredientFactory(recipe=cheese_on_toast_recipe, ingredient=toast_ingredient)        

# test pizza recipe
pizza_ingredients = [cheese_ingredient, tomato_ingredient, pizza_base_ingredient]
pr = Recipe.objects.get(name='Pizza')

for recipe_ingredient in pr.recipeingredient_set.all():
    self.assertTrue(recipe_ingredient.ingredient in pizza_ingredients)

# test cheese on toast recipe
cheese_on_toast_ingredients = [cheese_ingredient, toast_ingredient]
cotr = Recipe.objects.get(name='Cheese on toast')

for recipe_ingredient in cotr.recipeingredient_set.all():
    self.assertTrue(recipe_ingredient.ingredient in cheese_on_toast_ingredients)

# test from ingredients side
cheese_recipes = [pizza_recipe, cheese_on_toast_recipe]
ci = Ingredient.objects.get(name='Cheese')

for recipe_ingredient in ci.recipeingredient_set.all():
    self.assertTrue(recipe_ingredient.recipe in cheese_recipes)
๐Ÿ‘คtdsymonds

Leave a comment