def test_limit_on_image_size(self): ''' Test that when adding a category, with an image of dimensions 225 by 225: - The page will not redirect as the image cannot be added - An error message is raised Test that when adding a category, with an image of dimensions 200 by 200: - The page will redirect as the image has been added ''' mock_image = image('test_image_1.jpeg') form = { "category_image": mock_image, "phone_category": "CategoryTest1" } response = self.elena.post('/admin/front/phonecategory/add/', form) self.assertIn(str.encode(category_image_error.format(959, 1280)), response.content) self.assertEqual(response.status_code, 200) mock_image_2 = image('test_image_6.png') form_2 = { "category_image": mock_image_2, "phone_category": "CategoryTest1" } response = self.elena.post('/admin/front/phonecategory/add/', form_2) self.assertRedirects(response, "/admin/front/phonecategory/", 302)
def phone_form(self, phone_model, phone_size, quantity): """Generate mock data for a phone.""" mock_image = image('test_image_5.png') form = { "phone_model": phone_model, "color": self.color_one.pk, "size_sku": phone_size or self.size_iphone, "price": 25000, "quantity": quantity, "in_stock": True, "currency": self.currency_v.pk, "main_image": mock_image, "phone_information-TOTAL_FORMS": 1, "phone_information-INITIAL_FORMS": 0, "phone_information-MIN_NUM_FORMS": 0, "phone_information-MAX_NUM_FORMS": 1000, "phone_images-TOTAL_FORMS": 1, "phone_images-INITIAL_FORMS": 0, "phone_images-MIN_NUM_FORMS": 0, "phone_images-MAX_NUM_FORMS": 1000, "phone_features-TOTAL_FORMS": 1, "phone_features-INITIAL_FORMS": 0, "phone_features-MIN_NUM_FORMS": 0, "phone_features-MAX_NUM_FORMS": 1000 } return form
def test_non_hot_deals_rendering(self): ''' Test that if a Hot Deal is not in stock or its quantity is zero: - That it is not rendered on the landing page - That any hot deal with the opposite is rendered on the landing page ''' PhoneModel.objects.create(category=self.android, brand=self.samsung_brand, brand_model="Samsung Note 8", average_review=5.0) self.samsung_note_8 = PhoneModel.objects.get( brand_model="Samsung Note 8") PhoneModelList.objects.create(phone_model=self.samsung_note_8, currency=self.currency_v, price=25000, size_sku=self.size_android, main_image=image("test_image_5.png"), color=self.color_one, quantity=4, is_in_stock=True) self.samsung_note_8_rose_gold = PhoneModelList.objects.get( phone_model=self.samsung_note_8, color=self.color_one) self.elena.post('/admin/front/hotdeal/add/', {"item": self.samsung_note_5_rose_gold.pk}) self.elena.post('/admin/front/hotdeal/add/', {"item": self.samsung_note_7_rose_gold.pk}) get_response = self.client.get("/") self.assertContains(get_response, "Samsung Note 7") self.assertNotContains(get_response, "Samsung Note 8") self.assertContains(get_response, "Samsung Note 5")
def test_phone_category_edit(self): ''' Test that when a phone category is edited: - The page redirects successfully ''' mock_image = image('test_image_6.png') form = {"phone_category": "Iphone", "category_image": mock_image} response = self.elena.post("/admin/front/phonecategory/{}/change/". format(self.iphone.pk), form) self.assertRedirects(response, "/admin/front/phonecategory/", 302)
def setUp(self): User.objects.create(email="*****@*****.**", first_name="Example", last_name="User", is_staff=False, is_active=True, is_change_allowed=False, phone_number=72200000, photo=image("test_image_5.png")) self.user = User.objects.get(first_name="Example") super(PhoneProfileTemplate, self).setUp()
def test_category_image_rendering_on_view(self): """ Test that when adding a category with the right image size: - That it renders on the view """ mock_image_2 = image('test_image_6.png') form_2 = { "category_image": mock_image_2, "phone_category": "CategoryTest1" } self.elena.post('/admin/front/phonecategory/add/', form_2) response = self.client.get("/") self.assertContains(response, "src=\"/media/phone_categories/test_image_6")
def test_cache_cleared_on_add(self): ''' Test that the cache for landing page images is deleted when a landing page image is added. Ideally the test should operate in a scenario where the cache already exists. ''' self.phone_category_cache_exists() category_add_url = "/admin/front/phonecategory/add/" category_form = { "phone_category": "Phone1", "category_image": image('test_image_6.png') } response = self.elena.post(category_add_url, category_form) self.assertEqual(response.status_code, 302) category_cache_after_add = cache.get('phone_categories') self.assertEqual(category_cache_after_add, None)
def test_category_edition(self): ''' Test that when a category is edited the cache for the category is deleted. ''' self.client.get('/phone_category/{}/'.format(self.iphone.pk)) cache_after_access = cache.get('category_{}'.format(self.iphone.pk)) self.assertNotEqual(cache_after_access, None) iphone_id = self.iphone.pk form = { "phone_category": "New Category", "category_image": image('test_image_6.png') } category_edit_url = "/admin/front/phonecategory/{}/change/" response = self.elena.post(category_edit_url.format(iphone_id), form) self.assertEqual(response.status_code, 302) edited_category = PhoneCategory.objects.get(pk=self.iphone.pk) self.assertEqual(str(edited_category), "New Category") cache_after_delete = cache.get('category_{}'.format(self.iphone.pk)) self.assertEqual(cache_after_delete, None)
def setUp(self): super(PhoneCategoryViewsTestCase, self).setUp() self.category_image = image('test_image_6.png') self.add_url = "/admin/front/phonecategory/add/"