def test_dangling_foreign_keys(self): heston_blumenthal = Chef.objects.create(name="Heston Blumenthal") snail_ice_cream = Dish.objects.create(name="Snail ice cream") chateauneuf = Wine.objects.create(name="Chateauneuf-du-Pape 1979") fat_duck = Restaurant(name="The Fat Duck", proprietor=heston_blumenthal, serves_hot_dogs=False, menu_items=[ MenuItem(dish=snail_ice_cream, price='20.00', recommended_wine=chateauneuf) ]) fat_duck_json = fat_duck.to_json() fat_duck = Restaurant.from_json(fat_duck_json) self.assertEqual("Heston Blumenthal", fat_duck.proprietor.name) self.assertEqual("Chateauneuf-du-Pape 1979", fat_duck.menu_items.all()[0].recommended_wine.name) heston_blumenthal.delete() fat_duck = Restaurant.from_json(fat_duck_json) # the deserialised record should recognise that the heston_blumenthal record is now missing self.assertEqual(None, fat_duck.proprietor) self.assertEqual("Chateauneuf-du-Pape 1979", fat_duck.menu_items.all()[0].recommended_wine.name) chateauneuf.delete() # oh dear, looks like we just drank the last bottle fat_duck = Restaurant.from_json(fat_duck_json) # the deserialised record should now have a null recommended_wine field self.assertEqual(None, fat_duck.menu_items.all()[0].recommended_wine) snail_ice_cream.delete() # NOM NOM NOM fat_duck = Restaurant.from_json(fat_duck_json) # the menu item should now be dropped entirely (because the foreign key to Dish has on_delete=CASCADE) self.assertEqual(0, fat_duck.menu_items.count())
def test_dangling_foreign_keys(self): heston_blumenthal = Chef.objects.create(name="Heston Blumenthal") snail_ice_cream = Dish.objects.create(name="Snail ice cream") chateauneuf = Wine.objects.create(name="Chateauneuf-du-Pape 1979") fat_duck = Restaurant(name="The Fat Duck", proprietor=heston_blumenthal, serves_hot_dogs=False, menu_items=[ MenuItem(dish=snail_ice_cream, price='20.00', recommended_wine=chateauneuf) ]) fat_duck_json = fat_duck.to_json() fat_duck = Restaurant.from_json(fat_duck_json) self.assertEqual("Heston Blumenthal", fat_duck.proprietor.name) self.assertEqual("Chateauneuf-du-Pape 1979", fat_duck.menu_items.all()[0].recommended_wine.name) heston_blumenthal.delete() fat_duck = Restaurant.from_json(fat_duck_json) # the deserialised record should recognise that the heston_blumenthal record is now missing self.assertEqual(None, fat_duck.proprietor) self.assertEqual("Chateauneuf-du-Pape 1979", fat_duck.menu_items.all()[0].recommended_wine.name) chateauneuf.delete() # oh dear, looks like we just drank the last bottle fat_duck = Restaurant.from_json(fat_duck_json) # the deserialised record should now have a null recommended_wine field self.assertEqual(None, fat_duck.menu_items.all()[0].recommended_wine) snail_ice_cream.delete() # NOM NOM NOM fat_duck = Restaurant.from_json(fat_duck_json) # the menu item should now be dropped entirely (because the foreign key to Dish has on_delete=CASCADE) self.assertEqual(0, fat_duck.menu_items.count())
def test_serialize_with_multi_table_inheritance(self): fat_duck = Restaurant(name='The Fat Duck', serves_hot_dogs=False, reviews=[ Review(author='Michael Winner', body='Rubbish.') ]) data = json.loads(fat_duck.to_json()) self.assertEqual(data['name'], 'The Fat Duck') self.assertEqual(data['serves_hot_dogs'], False) self.assertEqual(data['reviews'][0]['author'], 'Michael Winner')
def test_serialize_with_multi_table_inheritance(self): fat_duck = Restaurant(name='The Fat Duck', serves_hot_dogs=False, reviews=[ Review(author='Michael Winner', body='Rubbish.') ]) data = json.loads(fat_duck.to_json()) self.assertEqual(data['name'], 'The Fat Duck') self.assertEqual(data['serves_hot_dogs'], False) self.assertEqual(data['reviews'][0]['author'], 'Michael Winner')