def test_delete_qs_updates_ordering_middle_image(self): """Test that if RealtyImage QuerySet is deleted, ordering of images is updated. If image was not the first, order of previous images remains the same. """ test_realty = Realty.objects.get(slug='image-test') # create 3 RealtyImage objects test_image1 = create_valid_image('image1') test_realty_image1 = RealtyImage.objects.create(realty=test_realty, image=test_image1) test_image2 = create_valid_image('image2') test_realty_image2 = RealtyImage.objects.create(realty=test_realty, image=test_image2) test_image3 = create_valid_image('image3') test_realty_image3 = RealtyImage.objects.create(realty=test_realty, image=test_image3) # delete QuerySet (with the second image) RealtyImage.objects.filter(id=test_realty_image2.id).delete() # refresh remaining objects test_realty_image1.refresh_from_db() test_realty_image3.refresh_from_db() ordering = test_realty.images.values_list('order', flat=True) # first image has been deleted --> image ordering has been changed self.assertListEqual(list(ordering), [0, 1]) self.assertEqual(test_realty_image1.order, 0) self.assertEqual(test_realty_image3.order, 1)
def test_delete_object_updates_ordering_last_image(self): """Test that if RealtyImage object is deleted, ordering of images is updated. If image was the last, order of previous images remains the same. """ test_realty = Realty.objects.get(slug='image-test') # create 3 RealtyImage objects test_image1 = create_valid_image('image1') test_realty_image1 = RealtyImage.objects.create(realty=test_realty, image=test_image1) test_image2 = create_valid_image('image2') test_realty_image2 = RealtyImage.objects.create(realty=test_realty, image=test_image2) test_image3 = create_valid_image('image3') test_realty_image3 = RealtyImage.objects.create(realty=test_realty, image=test_image3) # delete the last RealtyImage test_realty_image3.delete() # refresh remaining objects test_realty_image1.refresh_from_db() test_realty_image2.refresh_from_db() ordering = test_realty.images.values_list('order', flat=True) # last image has been deleted --> image ordering hasn't changed self.assertListEqual(list(ordering), [0, 1]) self.assertEqual(test_realty_image1.order, 0) self.assertEqual(test_realty_image2.order, 1)
def setUp(self) -> None: test_user1 = CustomUser.objects.create_user( email='*****@*****.**', first_name='John', last_name='Doe', password='******', ) test_host1 = RealtyHost.objects.create(user=test_user1) test_location1 = Address.objects.create( country='Russia', city='Moscow', street='Arbat, 20', ) self.test_realty1 = Realty.objects.create( name='Realty 1', description='Desc 1', is_available=True, realty_type=RealtyTypeChoices.APARTMENTS, beds_count=1, max_guests_count=2, price_per_night=40, location=test_location1, host=test_host1, ) test_image_name1 = 'image1.png' test_image1 = create_valid_image(test_image_name1) RealtyImage.objects.create( image=test_image1, realty=self.test_realty1, )
def test_correct_order_if_multiple_items(self): """Test that each new item with the `order` field has a correct order.""" realty_image1: RealtyImage = RealtyImage.objects.first() # create new image test_image_name2 = 'image2.png' test_image2 = create_valid_image(test_image_name2) realty_image2: RealtyImage = RealtyImage.objects.create( image=test_image2, realty=self.test_realty1) realty_image1.refresh_from_db() realty_image2.refresh_from_db() # ordering is correct self.assertEqual(realty_image1.order, 0) self.assertEqual(realty_image2.order, 1)
def test_correct_order_if_value_passed_manually(self): """Test that `order` value can be passed manually.""" realty_image1: RealtyImage = RealtyImage.objects.first() # create new image test_image_name2 = 'image2.png' test_image2 = create_valid_image(test_image_name2) # pass `order` value as an argument realty_image2: RealtyImage = RealtyImage.objects.create( image=test_image2, realty=self.test_realty1, order=2) realty_image1.refresh_from_db() realty_image2.refresh_from_db() # ordering is correct self.assertEqual(realty_image1.order, 0) self.assertEqual(realty_image2.order, 2)
def test_post_image_success(self): """Test that user can upload a profile image.""" test_user = CustomUser.objects.get(email='*****@*****.**') test_image_name = 'image.png' test_image = create_valid_image(test_image_name) form_data = { 'profile_image': test_image, } self.client.login(email='*****@*****.**', password='******') response = self.client.post(reverse('accounts:edit_image'), data=form_data) self.assertRedirects( response, reverse('accounts:profile_show', kwargs={'user_pk': test_user.pk})) self.assertIsNotNone(test_user.profile.profile_image) self.assertEqual( test_user.profile.profile_image.name, f"upload/users/{test_user.email}/profile/{test_image_name}")