def test_save_and_retrieve_cities(self): list_ = List() list_.save() first_city = City() first_city.name = 'First city' first_city.list = list_ first_city.save() second_city = City() second_city.name = 'Second city' second_city.list = list_ second_city.save() saved_list = List.objects.first() self.assertEqual(saved_list, list_) saved_cities = City.objects.all() self.assertEqual(saved_cities.count(), 2) first_saved_city = saved_cities[0] second_saved_city = saved_cities[1] self.assertEqual(first_saved_city.name, 'First city') self.assertEqual(first_saved_city.list, list_) self.assertEqual(second_saved_city.name, 'Second city') self.assertEqual(second_saved_city.list, list_)
def test_cannot_save_empty_city(self): list_ = List.objects.create() city = City() city.name = '' city.list = list_ with self.assertRaises(ValidationError): city.save() city.full_clean()