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_can_access_child_relations_of_superclass(self): fat_duck = Restaurant(name='The Fat Duck', serves_hot_dogs=False, reviews=[ Review(author='Michael Winner', body='Rubbish.') ]) self.assertEqual(1, fat_duck.reviews.count()) self.assertEqual(fat_duck.reviews.first().author, 'Michael Winner') self.assertEqual(fat_duck, fat_duck.reviews.all()[0].place) fat_duck.save() # ensure relations have been saved to the database fat_duck = Restaurant.objects.get(id=fat_duck.id) self.assertEqual(1, fat_duck.reviews.count()) self.assertEqual(fat_duck.reviews.first().author, 'Michael Winner')
def test_deserialize_with_multi_table_inheritance(self): fatduck = Restaurant.from_json('{"pk": 42, "name": "The Fat Duck", "serves_hot_dogs": false}') self.assertEqual(42, fatduck.id) data = fatduck.serializable_data() self.assertEqual(42, data['pk']) self.assertEqual("The Fat Duck", data['name'])
def test_deserialize_with_multi_table_inheritance(self): fat_duck = Restaurant.from_json( '{"pk": 42, "name": "The Fat Duck", "serves_hot_dogs": false, "reviews": [{"pk": null, "author": "Michael Winner", "body": "Rubbish."}]}' ) self.assertEqual(fat_duck.id, 42) self.assertEqual(fat_duck.name, "The Fat Duck") self.assertEqual(fat_duck.serves_hot_dogs, False) self.assertEqual(fat_duck.reviews.all()[0].author, "Michael Winner")
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_deserialize_with_multi_table_inheritance(self): fat_duck = Restaurant.from_json('{"pk": 42, "name": "The Fat Duck", "serves_hot_dogs": false, "reviews": [{"pk": null, "author": "Michael Winner", "body": "Rubbish."}]}') self.assertEqual(fat_duck.id, 42) self.assertEqual(fat_duck.name, "The Fat Duck") self.assertEqual(fat_duck.serves_hot_dogs, False) self.assertEqual(fat_duck.reviews.all()[0].author, "Michael Winner")