def test_update_advert_images_reorder(self): """Test upadting an advert with reordering of images""" advert, image_ids = sample_ad_with_images(user=self.user) payload = { 'images': [image_ids[2], image_ids[1], image_ids[0]] } existing_links = AdvertImageLink.objects \ .filter(advert=advert) \ .order_by('slot') for index, link in enumerate(existing_links): self.assertEqual(link.image.id, image_ids[index]) res = self.client.patch(advert_detail_url(advert.id), payload) advert.refresh_from_db() self.assertEqual(res.status_code, status.HTTP_200_OK) new_links = AdvertImageLink.objects \ .filter(advert=advert) \ .order_by('slot') self.assertEqual(len(new_links), 3) for index, link in enumerate(new_links): self.assertEqual(link.image.id, image_ids[2 - index])
def test_update_advert_with_images(self): """Test updating an advert with images""" advert, _ = sample_ad_with_images(user=self.user) new_image_1 = sample_image() new_image_2 = sample_image() payload = { 'title': 'New Title', 'images': [new_image_1.id, new_image_2.id] } res = self.client.patch(advert_detail_url(advert.id), payload) old_title = advert.title advert.refresh_from_db() self.assertEqual(res.status_code, status.HTTP_200_OK) self.assertNotEqual(advert.title, old_title) new_links = AdvertImageLink.objects \ .filter(advert=advert) \ .order_by('slot') self.assertEqual(len(new_links), 2) self.assertEqual(new_links[0].image, new_image_1) self.assertEqual(new_links[1].image, new_image_2)
def test_retrieve_specific_ad_with_images(self): """Test retrieving ad with images""" advert, ad_image_ids = sample_ad_with_images(title='Test1') res = self.client.get(advert_detail_url(advert.id)) self.assertEqual(res.status_code, status.HTTP_200_OK) self.assertEqual(res.data["id"], advert.id) for index, img_id in enumerate(ad_image_ids): self.assertEqual(res.data["images"][index]["id"], img_id)
def test_list_all_ads_thumbnails(self): """Test that listed ads contain thumbnail url""" _, ad_1_image_ids = sample_ad_with_images(title='Test1') _, ad_2_image_ids = sample_ad_with_images(title='Test2') _, ad_3_image_ids = sample_ad_with_images(title='Test3') res = self.client.get(ADVERTS_URL) self.assertEqual(res.status_code, status.HTTP_200_OK) self.assertEqual(len(res.data), 3) main_thumbnail_ids = [ ad_1_image_ids[0], ad_2_image_ids[0], ad_3_image_ids[0] ] thumbnails = AdvertImage.objects.filter(id__in=main_thumbnail_ids) thumbnails_urls = [ f'http://testserver{img.thumbnail.url}' for img in thumbnails ] for entry in res.data: self.assertIn(entry['thumbnail'], thumbnails_urls)
def setUp(self): self.client = APIClient() self.category_1 = sample_category(name='Test_Category_1') self.category_2 = sample_category(name='Test_Category_2') self.category_3 = sample_category( name='Test_Category_3', parent=self.category_2 ) self.category_4 = sample_category( name='Test_Category_4', parent=self.category_3 ) sample_ad_with_images(title='Test_1', category=self.category_1) sample_ad_with_images(title='Test_2', category=self.category_2) sample_ad_with_images(title='Test_3', category=self.category_3) sample_ad_with_images(title='Test_3', category=self.category_4)
def setUp(self): self.client = APIClient() self.user = sample_user(email='*****@*****.**') self.user_2 = sample_user(email='*****@*****.**') sample_ad_with_images(user=self.user, title='Test_1') sample_ad_with_images(user=self.user_2, title='Test_2') sample_ad_with_images(user=self.user, title='Test_3')
def setUp(self): self.client = APIClient() self.location_1 = sample_location(name='New York') self.location_2 = sample_location(name='Chicago') self.location_3 = sample_location(name='New Orlean') sample_ad_with_images(title='Test_1', location=self.location_1) sample_ad_with_images(title='Test_2', location=self.location_2) sample_ad_with_images(title='Test_3', location=self.location_3)
def setUp(self): self.client = APIClient() sample_ad_with_images(title='Test_1', price=1.50) sample_ad_with_images(title='Test_2', price=15.00) sample_ad_with_images(title='Test_3', price=0.50) sample_ad_with_images(title='Test_4', price=5.00) sample_ad_with_images(title='Test_5', price=9.99) sample_ad_with_images(title='Test_6', price=1.99)
def setUp(self): self.client = APIClient() sample_ad_with_images(title='Test_1', price=3.50) sample_ad_with_images(title='Test_2', price=5.50) sample_ad_with_images(title='Test_3', price=2.50)
def setUp(self): self.client = APIClient() self.ad_1, _ = sample_ad_with_images(title='Test_1') self.ad_2, _ = sample_ad_with_images(title='Test_2') self.ad_3, _ = sample_ad_with_images(title='Test_3')