def test_new_menuform_valid(self): form = MenuForm( data={ 'season': 'winter', 'items': Item.objects.all(), 'expiration_date': '05/05/2015' }) self.assertTrue(form.is_valid())
def test_new_menuform_invalid_date_format(self): form = MenuForm( data={ 'season': 'winter', 'items': Item.objects.all(), 'expiration_date': '2015-05-08' }) self.assertFalse(form.is_valid())
def test_menu_form_no_exp_date_set(self): """Form with no expiration date set.""" form_data = ({ 'season': 'Spring/Summer', 'items': [1, 3], 'expiration_date': '' }) form = MenuForm(data=form_data, choices=ALL_ITEMS) self.assertFalse(form.is_valid())
def test_menu_form_invalid_no_items(self): """Form with no items selcetd.""" form_data = ({ 'season': 'Spring/Summer', 'items': [], 'expiration_date': PLUS_TWO_WEEKS }) form = MenuForm(data=form_data, choices=ALL_ITEMS) self.assertFalse(form.is_valid())
def test_menu_form_invalid_season_contains_brandname(self): """Form with no season > chars.""" form_data = ({ 'season': 'Spring with Coca-Cola', 'items': [1, 3], 'expiration_date': PLUS_TWO_WEEKS }) form = MenuForm(data=form_data, choices=ALL_ITEMS) self.assertFalse(form.is_valid())
def test_menu_form_exp_date_in_past(self): """Form with expiration date set in the past.""" form_data = ({ 'season': 'Spring/Summer', 'items': [1, 3], 'expiration_date': MINUS_TWO_WEEKS }) form = MenuForm(data=form_data, choices=ALL_ITEMS) self.assertFalse(form.is_valid())
def test_menu_form_valid(self): """Form with valid data input""" form_data = ({ 'season': 'Spring/Summer', 'items': [1, 3], 'expiration_date': PLUS_TWO_WEEKS }) form = MenuForm(data=form_data, choices=ALL_ITEMS) self.assertTrue(form.is_valid())
def test_invalid_season_fails_validation(self): form = MenuForm( data={ 'season': 'a', 'items': self.items, 'expiration_date': self.valid_menu.expiration_date }) self.assertFalse(form.is_valid())
def test_menu_form_invalid_season_missing(self): """Form with no season input""" form_data = ({ 'season': '', 'items': [1, 3], 'expiration_date': PLUS_TWO_WEEKS }) form = MenuForm(data=form_data, choices=ALL_ITEMS) self.assertFalse(form.is_valid())
def addmenu(request): if request.method=='POST': form=MenuForm(request.POST) if form.is_valid(): menuname=form.cleaned_data['menuname'] url=form.cleaned_data['url'] else: form=MenuForm() return render(request, 'dist/menu.html', dict(displayMenu='block', mainmenu=u'菜单管理', submenu=u'添加菜单',form=form,menulist=menulist,submenulist=submenulist))
def test_menu_form_invalid_items(self): form = MenuForm( data={ 'season': self.menu.season, 'items': ['1'], 'expiration_date': self.menu.expiration_date }) self.assertFalse(form.is_valid())
def test_menu_form_invalid_season_too_long(self): """Form with no season > chars.""" form_data = ({ 'season': 'Spring/Summer Spring/Summer Spring/Summer Spring/Summer Spring/Summer', 'items': [1, 3], 'expiration_date': PLUS_TWO_WEEKS }) form = MenuForm(data=form_data, choices=ALL_ITEMS) self.assertFalse(form.is_valid())
def test_menu_form_valid_data(self): form = MenuForm( data={ 'season': self.menu.season, 'items': ['1', '2'], 'expiration_date': self.menu.expiration_date }) self.assertTrue(form.is_valid())
def test_valid_formdata_passes_validation(self): # a valid season has at least 2 characters # a valid expiration date is later than now form = MenuForm( data={ 'season': self.valid_menu.season, 'items': self.items, 'expiration_date': self.valid_menu.expiration_date }) self.assertTrue(form.is_valid())
def test_menu_form(self): test_item = Item.objects.create(name='test_item', description='Filler', chef=self.test_user) form_data = { 'season': 'something specific', 'expiration_date': '01/01/2020', 'items': [test_item.id] } form = MenuForm(data=form_data) self.assertTrue(form.is_valid())
def test_invalid_expiration_date_fails_validation(self): now = timezone.now() timedelta = datetime.timedelta(seconds=1) invalid_expiration_date = now - timedelta form = MenuForm( data={ 'season': self.valid_menu.season, 'items': self.items, 'expiration_date': invalid_expiration_date }) self.assertFalse(form.is_valid())
def test_menu_form_valid(self): """Form with valid data input""" form = MenuForm(data=MenuBaseTest.form_valid_data) self.assertTrue(form.is_valid())
def test_menu_form_no_data(self): form = MenuForm(data={}) self.assertFalse(form.is_valid()) self.assertEquals(len(form.errors), 3)
def test_menu_form_invalid_expiration_date(self): """Form with invalid expiration date""" form = MenuForm(data=MenuBaseTest.form_with_invalid_expiration_date) self.assertFalse(form.is_valid())
def test_menu_form_invalid_number_of_selected_items(self): """Form with invalid number of selected items, below minimum""" form = MenuForm( data=MenuBaseTest.form_with_invalid_number_of_selected_items) self.assertFalse(form.is_valid())
def test_menu_form_invalid_string_length(self): """Form with invalid string length (over the maximum)""" form = MenuForm(data=MenuBaseTest.form_with_invalid_string_length) self.assertFalse(form.is_valid())
def test_menu_form_invalid(self): form = MenuForm(data={'publishedDateInput': ''}) self.assertFalse(form.is_valid())
class Tests(TestCase): def setUp(self): self.user = User.objects.create_user(username='******', password='******') self.client.login(username='******', password='******') self.ingredient = Ingredient.objects.create(name="chocolate") self.ingredient.save() self.item = Item.objects.create( name="Chocolate soda", description="Chocolate soda with a cherry on top", chef=self.user, ) self.item.save() self.item.ingredients.add(self.ingredient) self.menu = Menu.objects.create( season="Fall", expiration_date="2020-05-31" ) self.menu.save() self.menu.items.add(self.item) self.menu.save() self.form = MenuForm( data={'season': self.menu.season, 'items': [self.item], 'expiration_date': self.menu.expiration_date}) def test_ingredient_string_representation(self): self.assertEqual(str(self.ingredient), self.ingredient.name) def test_item_string_representation(self): self.assertEqual(str(self.item), self.item.name) def test_menu_string_representation(self): self.assertEqual(str(self.menu), self.menu.season) def test_MenuForm_valid(self): self.assertTrue(self.form.is_valid()) def test_menu_list_view(self): resp = self.client.get(reverse('menu_list')) self.assertEqual(resp.status_code, 200) self.assertIn(self.menu, resp.context['menus']) def test_menu_detail_view(self): resp = self.client.get(reverse('menu_detail', kwargs={'pk': self.menu.pk})) self.assertEqual(resp.status_code, 200) self.assertEqual(self.menu, resp.context['menu']) def test_item_detail_view(self): resp = self.client.get(reverse('item_detail', kwargs={'pk': self.item.pk})) self.assertEqual(resp.status_code, 200) self.assertEqual(self.item, resp.context['item']) resp = self.client.get(reverse('item_detail', kwargs={'pk': 400})) self.assertEqual(resp.status_code, 404) def test_create_new_menu_view_POST(self): form_data = { 'season': self.menu.season, 'items': [self.item.pk], 'expiration_date': self.menu.expiration_date, } resp = self.client.post(reverse('menu_new'), form_data) # get the lastest menu saved menu = Menu.objects.latest('id') self.assertRedirects(resp, reverse('menu_detail', kwargs={'pk': menu.pk})) def test_create_new_menu_view_GET(self): form_data = { 'season': self.menu.season, 'items': [self.item.pk], 'expiration_date': self.menu.expiration_date, } resp = self.client.get(reverse('menu_new')) self.assertEqual(resp.status_code, 200)
def test_menu_form_valid(self): form = MenuForm(data={'publishedDateInput': '2020-31-12'}) self.assertTrue(form.is_valid())
def test_menu_form_with_existing_season_name(self): """Form with existing season name""" form = MenuForm(data=MenuBaseTest.form_with_existing_season_name_data) self.assertFalse(form.is_valid())