def setUp(self): """create an instance in the database""" self.ingredient = Ingredient.objects.create(name="Pepperoni") self.item = Item( name="Pizza", description="Round and delicious", chef=user, ) self.item.save() self.item.ingredients.add(self.ingredient) self.menu = Menu(season="YearLong", expiration_date=timezone.now() + timezone.timedelta(days=1)) self.menu.save() self.menu.items.add(self.item)
def setUp(self): """create an instance in the database""" self.ingredient = Ingredient.objects.create(name="lskdjflkjfd") self.item = Item( name="LKJklsjd", description="LKSJdlfkjsldfjsd", chef=user, ) self.item.save() self.item.ingredients.add(self.ingredient) self.menu = Menu(season="asdfasdfadsfadsf", expiration_date=timezone.now() + timezone.timedelta(days=1)) self.menu.save() self.menu.items.add(self.item)
def create_valid_item(self, chef, ingredients, **kwargs): """chef is a User object, ingredients is a queryset of Ingredient objects or a list of ingredient PKs """ valid_data = {**VALID_ITEM_DATA, 'chef': chef} item = Item(**valid_data) for key, value in kwargs.items(): setattr(item, key, value) item.save() # can't add MTM relations to an object that isn't in DB item.ingredients = ingredients item.save() return item
def add(self, product): try: item = Item.objects.get( menu=self.menu, product=product, ) except Item.DoesNotExist: item = Item() item.menu = self.menu item.product = product item.save() else: #ItemAlreadyExists item.save()
class NewVisitorTest(LiveServerTestCase): """basically runs through the website using the browser to see if its working""" def setUp(self): """establishes a model instance in the database""" self.browser = webdriver.Firefox() self.ingredient = Ingredient.objects.create(name="lskdjflkjfd") self.item = Item( name="LKJklsjd", description="LKSJdlfkjsldfjsd", chef=user, ) self.item.save() self.item.ingredients.add(self.ingredient) self.menu = Menu(season="asdfasdfadsfadsf", expiration_date=timezone.now() + timezone.timedelta(days=1)) self.menu.save() self.menu.items.add(self.item) def tearDown(self): self.browser.quit() # notice how this one doesn't start with 'test', # that's because it is a helper method to keep our code DRY. def solve_loading_interference(self, specific_text, specific_text_not="hippo"): """is a method instead of doing 'time.sleep()'""" #we set this up so not only do we not have to use a 'time.sleep()' #to let the browser load, we catch the browser when it loads #within a half second which cuts down on wait time to finish the testing. #you have to do a time.wait type of maneuver after whenever you do #input.send_keys.(Keys.ENTER) or click() start_time = time.time() while True: try: page_text = self.browser.find_element_by_tag_name('body').text self.assertIn(specific_text, page_text) self.assertNotIn(specific_text_not, page_text) return except (AssertionError, WebDriverException) as e: if time.time() - start_time > MAX_WAIT: raise e time.sleep(0.5) def test_can_start_a_list_and_retrieve_it_later(self): """runs through the browser to make sure site is working""" self.browser.get(self.live_server_url) body_text = self.browser.find_element_by_tag_name('body').text self.assertIn("asdfasdfadsfadsf", body_text) anchors = self.browser.find_elements_by_tag_name('a') for anchor in anchors: if anchor.text == "asdfasdfadsfadsf": anchorA = anchor break anchorA.click() self.solve_loading_interference("On the menu this season:") edit_menu = self.browser.find_element_by_class_name('btn') edit_menu.click() self.solve_loading_interference("Change menu") edit_menu = self.browser.find_element_by_class_name('btn') edit_menu.click() self.solve_loading_interference("On the menu this season:") anchors = self.browser.find_elements_by_tag_name('a') for anchor in anchors: if anchor.text == "LKJklsjd": anchorA = anchor break anchorA.click() self.solve_loading_interference("lskdjflkjfd") anchors = self.browser.find_elements_by_tag_name('a') for anchor in anchors: if anchor.text == "Soda Fountain": anchorA = anchor break anchorA.click() self.solve_loading_interference("asdfasdfadsfadsf") new_menu = self.browser.find_element_by_class_name('top-menu') new_menu.click() self.solve_loading_interference("Create new menu") inputbox = self.browser.find_element_by_id('id_season') inputbox.send_keys('best menu ever') checkbox = self.browser.find_element_by_id('id_items_0') checkbox.click() el = self.browser.find_element_by_id('id_expiration_date_year') for option in el.find_elements_by_tag_name('option'): if option.text == '2019': option.click() # select() in earlier versions of webdriver break submit_new_menu = self.browser.find_element_by_class_name('save') submit_new_menu.click() self.solve_loading_interference("On the menu this season")
class MyTests(TestCase): """all of the unittests""" def setUp(self): """create an instance in the database""" self.ingredient = Ingredient.objects.create(name="Pepperoni") self.item = Item( name="Pizza", description="Round and delicious", chef=user, ) self.item.save() self.item.ingredients.add(self.ingredient) self.menu = Menu(season="YearLong", expiration_date=timezone.now() + timezone.timedelta(days=1)) self.menu.save() self.menu.items.add(self.item) def test_models_creation(self): """Test creation of the model with sample data above""" now = timezone.now() self.assertLess(self.item.created_date, now) self.assertLess(self.menu.created_date, now) self.assertIn(self.ingredient, self.item.ingredients.all()) self.assertIn(self.item, self.menu.items.all()) self.assertEqual(user, self.item.chef) def test_menu_list_view(self): """Test the rendering of the menu list template""" resp = self.client.get(reverse('menu_list')) self.assertEqual(resp.status_code, 200) self.assertIn(self.menu, resp.context['menus']) self.assertTemplateUsed(resp, 'menu/menu_list.html') self.assertContains(resp, self.menu.season) def test_menu_detail_view(self): """Test the rendering of the menu detail view""" resp = self.client.get( reverse('menu_detail', kwargs={'pk': self.menu.id})) self.assertEqual(resp.status_code, 200) self.assertEqual(self.menu, resp.context['menu']) self.assertTemplateUsed(resp, 'menu/menu_detail.html') self.assertContains(resp, self.menu.season) def test_item_detail_view(self): """Test the rendering of the item detail view""" resp = self.client.get( reverse('item_detail', kwargs={'pk': self.item.id})) self.assertEqual(resp.status_code, 200) self.assertEqual(self.item, resp.context['item']) self.assertTemplateUsed(resp, 'menu/item_detail.html') self.assertContains(resp, self.item.name) def test_create_new_menu_view_get(self): """Test the rendering of the create new menu view""" resp = self.client.get(reverse('menu_new')) self.assertEqual(resp.status_code, 200) self.assertTemplateUsed(resp, 'menu/create_new_menu.html') self.assertContains(resp, "Add New Menu") def test_create_new_menu_view_post(self): """Test the posting of data from the create new menu view""" expiration_date = timezone.now() + timezone.timedelta(days=2) self.client.post('/menu/new/', data={ 'expiration_date': expiration_date.strftime("%Y-%m-%d"), 'season': 'Spring 2018', 'created_date': timezone.now().strftime("%Y-%m-%d"), 'items': ['1'] }) self.assertEqual(Menu.objects.count(), 2) def test_edit_menu_view_get(self): """Test the rendering of the edit menu view""" resp = self.client.get( reverse('menu_edit', kwargs={'pk': self.menu.id})) self.assertEqual(resp.status_code, 200) self.assertTemplateUsed(resp, 'menu/edit_menu.html') self.assertContains(resp, "Edit Menu") def test_edit_menu_view_post(self): """Test the posting of data from the edit menu view""" expiration_date = timezone.now() + timezone.timedelta(days=3) self.client.post( reverse('menu_edit', args=[self.menu.id]), { 'expiration_date': expiration_date.strftime("%Y-%m-%d"), 'season': 'Spring 2019', 'created_date': timezone.now().strftime("%Y-%m-%d"), 'items': ['1'] }) menu = Menu.objects.get(id=1) self.assertEqual(menu.season, 'Spring 2019') def test_edit_item_view_get(self): """Test the rendering of data from the item edit view""" resp = self.client.get( reverse('item_edit', kwargs={'pk': self.item.id})) self.assertEqual(resp.status_code, 200) self.assertTemplateUsed(resp, 'menu/item_edit.html') self.assertContains(resp, "Edit Item")
class MyTests(TestCase): """all of the unittests""" def setUp(self): """create an instance in the database""" self.ingredient = Ingredient.objects.create(name="lskdjflkjfd") self.item = Item( name="LKJklsjd", description="LKSJdlfkjsldfjsd", chef=user, ) self.item.save() self.item.ingredients.add(self.ingredient) self.menu = Menu(season="asdfasdfadsfadsf", expiration_date=timezone.now() + timezone.timedelta(days=1)) self.menu.save() self.menu.items.add(self.item) def test_models_creation(self): now = timezone.now() self.assertLess(self.item.created_date, now) self.assertLess(self.menu.created_date, now) self.assertIn(self.ingredient, self.item.ingredients.all()) self.assertIn(self.item, self.menu.items.all()) self.assertEqual(user, self.item.chef) def test_menu_list_view(self): resp = self.client.get(reverse('mysite:menu_list')) self.assertEqual(resp.status_code, 200) self.assertIn(self.menu, resp.context['menus']) self.assertTemplateUsed(resp, 'menu/list_all_current_menus.html') self.assertContains(resp, self.menu.season) def test_menu_detail_view(self): resp = self.client.get( reverse('mysite:menu_detail', kwargs={'pk': self.menu.id})) self.assertEqual(resp.status_code, 200) self.assertEqual(self.menu, resp.context['menu']) self.assertTemplateUsed(resp, 'menu/menu_detail.html') self.assertContains(resp, self.menu.season) def test_item_detail_view(self): resp = self.client.get( reverse('mysite:item_detail', kwargs={'pk': self.item.id})) self.assertEqual(resp.status_code, 200) self.assertEqual(self.item, resp.context['item']) self.assertTemplateUsed(resp, 'menu/detail_item.html') self.assertContains(resp, self.item.name) def test_create_new_menu_view_get(self): resp = self.client.get(reverse('mysite:menu_new')) self.assertEqual(resp.status_code, 200) self.assertTemplateUsed(resp, 'menu/new_menu.html') self.assertContains(resp, "Create new menu") def test_create_new_menu_view_post(self): expiration_date = timezone.now() + timezone.timedelta(days=2) self.client.post('/menu/new/', data={ 'expiration_date': expiration_date.strftime("%Y-%m-%d"), 'season': 'Spring 2018', 'created_date': timezone.now().strftime("%Y-%m-%d"), 'items': ['1'] }) self.assertEqual(Menu.objects.count(), 2) def test_edit_menu_view_get(self): resp = self.client.get( reverse('mysite:menu_edit', kwargs={'pk': self.menu.id})) self.assertEqual(resp.status_code, 200) self.assertTemplateUsed(resp, 'menu/edit_menu.html') self.assertContains(resp, "Change menu") def test_edit_menu_view_post(self): expiration_date = timezone.now() + timezone.timedelta(days=3) self.client.post( reverse('mysite:menu_edit', args=[self.menu.id]), { 'expiration_date': expiration_date.strftime("%Y-%m-%d"), 'season': 'Spring 2019', 'created_date': timezone.now().strftime("%Y-%m-%d"), 'items': ['1'] }) menu = Menu.objects.get(id=1) self.assertEqual(menu.season, 'Spring 2019') def test_edit_item_view_get(self): resp = self.client.get( reverse('mysite:item_edit', kwargs={'pk': self.item.id})) self.assertEqual(resp.status_code, 200) self.assertTemplateUsed(resp, 'menu/item_edit.html') self.assertContains(resp, "Change item")
def setUp(self): """Create menus and menu items.""" self.factory = RequestFactory() menu1 = Menu.objects.create(name='main', depth=5) menu2 = Menu.objects.create(name='second') Item.objects.bulk_create([ Item(id=1, menu=menu1, name='Index', url='index'), Item(id=2, menu=menu1, name='I2', url='/i2', parent_id=1), Item(id=22, menu=menu1, name='I22', url='/i22', parent_id=1), Item(id=3, menu=menu1, name='I3', url='/i3', parent_id=2), Item(id=4, menu=menu1, name='I4', url='/i4', parent_id=3), Item(id=41, menu=menu1, name='I41', url='i41', parent_id=3), Item(id=32, menu=menu1, name='I32', url='/i32', parent_id=2), Item(id=42, menu=menu1, name='I42', url='/i42', parent_id=32), Item(id=5, menu=menu1, name='I5', url='/i5', parent_id=4), Item(id=52, menu=menu1, name='I52', url='/i52', parent_id=4), Item(id=6, menu=menu1, name='Another Root Item', url='/another_root'), Item(id=7, menu=menu1, name='Another Root Item2', url='/another_root_2'), Item(id=62, menu=menu1, name='I62', url='/i62', parent_id=6), Item(id=8, menu=menu2, name='I8', url='/i8'), ])