# Path: privagal/core/utils.py
# class PrivagalTestCase(TestCase):
#     client_class = PrivagalClient
#     login_template_name = None
# 
#     @classmethod
#     def setUpClass(cls):
#         super(PrivagalTestCase, cls).setUpClass()
#         if settings.PRIVAGAL_AUTH_TOKEN_REQUIRED:
#             cls.login_template_name = 'token_required.html'
#         else:
#             cls.login_template_name = 'password_required.html'
# 
#     @classmethod
#     def setUpTestData(cls):
#         cls.timeline = Page.objects.get(slug='timeline').specific
# 
#     def assertPageLogged(self, response):
#         self.assertTemplateNotUsed(
#             response, self.login_template_name, msg_prefix='Not page logged in')
# 
#     def assertNotPageLogged(self, response):
#         self.assertTemplateUsed(
#             response, self.login_template_name, msg_prefix='Page logged in')
# 
# Path: privagal/gallery/factories.py
# class GalleryFactory(PageFactory):
# 
#     class Meta:
#         model = Gallery
# 
#     date = fuzzy.FuzzyDate(datetime.date(2016, 1, 1))
#     body = factory.LazyAttribute(
#         lambda x: "<p>{}</p>".format(faker.paragraph()))
# 
#     @factory.post_generation
#     def images(self, created, extracted, **kwargs):
#         """
#         Usage example:
#         >>> GalleryFactory(images__images=[ImageFactory()])
#         """
#         images = kwargs.pop('images', [])
#         if not images:
#             images = [ImageFactory() for i in range(0, 3)]
# 
#         self.images = [ImageGalleryFactory.build(page=self, image=i) for i in images]
# 
# class ImageFactory(factory.DjangoModelFactory):
# 
#     class Meta:
#         model = get_image_model()
# 
#     title = factory.Faker('sentence', locale=locale)
#     file = factory.django.ImageField()
# 
# class ImageGalleryFactory(factory.DjangoModelFactory):
# 
#     class Meta:
#         model = ImageGallery
# 
#     description = factory.Faker('sentence', locale=locale)
# 
# Path: privagal/gallery/models.py
# class ImageGallery(Orderable):
#     page = ParentalKey(
#         Gallery,
#         related_name='images',
#         on_delete=models.CASCADE,
#     )
#     image = models.ForeignKey(
#         settings.WAGTAILIMAGES_IMAGE_MODEL,
#         related_name='images_gallery',
#         on_delete=models.CASCADE,
#     )
#     description = models.TextField(default='', blank=True)
# 
#     panels = [
#         ImageChooserPanel('image'),
#         FieldPanel('description'),
#     ]
# 
#     class Meta:
#         db_table = 'privagal_gallery_image'
#         verbose_name = _("Gallery")
# 
#     def delete(self, *args, **kwargs):
#         with transaction.atomic():
#             self.image.delete()
#             super(ImageGallery, self).delete(*args, **kwargs)


import unittest
    import mock
from wagtail.wagtailcore.models import Page
from wagtail.wagtailimages.models import get_image_model
from privagal.core.utils import PrivagalTestCase
from ..factories import GalleryFactory, ImageFactory, ImageGalleryFactory
from ..models import ImageGallery
    from unittest import mock  # pragma: no cover

    def test_delete_image_gallery_not_gallery(self):
        self.image_gallery.delete()
        self.gallery.refresh_from_db()
        self.assertFalse(self.gallery.images.all())

    def test_delete_image_gallery_cascade_image(self):
        self.image_gallery.delete()
        with self.assertRaises(Image.DoesNotExist):
            self.image.refresh_from_db()

    @mock.patch.object(ImageGallery, 'delete', side_effect=Exception('Boom'))
    def test_delete_image_gallery_fail(self, mock_delete):
        image_gallery_id = self.image_gallery.id
        with self.assertRaises(Exception):
            self.image_gallery.delete()
        self.image.refresh_from_db()
        self.assertTrue(ImageGallery.objects.get(id=image_gallery_id))

    def test_delete_image_not_gallery(self):
        self.image.delete()
        self.gallery.refresh_from_db()
        self.assertFalse(self.gallery.images.all())

    def test_delete_image_cascade_image_gallery(self):
        self.image.delete()
        with self.assertRaises(ImageGallery.DoesNotExist):
            self.image_gallery.refresh_from_db()



# Path: privagal/core/utils.py
# class PrivagalTestCase(TestCase):
#     client_class = PrivagalClient
#     login_template_name = None
# 
#     @classmethod
#     def setUpClass(cls):
#         super(PrivagalTestCase, cls).setUpClass()
#         if settings.PRIVAGAL_AUTH_TOKEN_REQUIRED:
#             cls.login_template_name = 'token_required.html'
#         else:
#             cls.login_template_name = 'password_required.html'
# 
#     @classmethod
#     def setUpTestData(cls):
#         cls.timeline = Page.objects.get(slug='timeline').specific
# 
#     def assertPageLogged(self, response):
#         self.assertTemplateNotUsed(
#             response, self.login_template_name, msg_prefix='Not page logged in')
# 
#     def assertNotPageLogged(self, response):
#         self.assertTemplateUsed(
#             response, self.login_template_name, msg_prefix='Page logged in')
# 
# Path: privagal/gallery/factories.py
# class GalleryFactory(PageFactory):
# 
#     class Meta:
#         model = Gallery
# 
#     date = fuzzy.FuzzyDate(datetime.date(2016, 1, 1))
#     body = factory.LazyAttribute(
#         lambda x: "<p>{}</p>".format(faker.paragraph()))
# 
#     @factory.post_generation
#     def images(self, created, extracted, **kwargs):
#         """
#         Usage example:
#         >>> GalleryFactory(images__images=[ImageFactory()])
#         """
#         images = kwargs.pop('images', [])
#         if not images:
#             images = [ImageFactory() for i in range(0, 3)]
# 
#         self.images = [ImageGalleryFactory.build(page=self, image=i) for i in images]
# 
# class ImageFactory(factory.DjangoModelFactory):
# 
#     class Meta:
#         model = get_image_model()
# 
#     title = factory.Faker('sentence', locale=locale)
#     file = factory.django.ImageField()
# 
# class ImageGalleryFactory(factory.DjangoModelFactory):
# 
#     class Meta:
#         model = ImageGallery
# 
#     description = factory.Faker('sentence', locale=locale)
# 
# Path: privagal/gallery/models.py
# class ImageGallery(Orderable):
#     page = ParentalKey(
#         Gallery,
#         related_name='images',
#         on_delete=models.CASCADE,
#     )
#     image = models.ForeignKey(
#         settings.WAGTAILIMAGES_IMAGE_MODEL,
#         related_name='images_gallery',
#         on_delete=models.CASCADE,
#     )
#     description = models.TextField(default='', blank=True)
# 
#     panels = [
#         ImageChooserPanel('image'),
#         FieldPanel('description'),
#     ]
# 
#     class Meta:
#         db_table = 'privagal_gallery_image'
#         verbose_name = _("Gallery")
# 
#     def delete(self, *args, **kwargs):
#         with transaction.atomic():
#             self.image.delete()
#             super(ImageGallery, self).delete(*args, **kwargs)


# Path: privagal/gallery/tests/test_models.py
import unittest
    import mock
from wagtail.wagtailcore.models import Page
from wagtail.wagtailimages.models import get_image_model
from privagal.core.utils import PrivagalTestCase
from ..factories import GalleryFactory, ImageFactory, ImageGalleryFactory
from ..models import ImageGallery
    from unittest import mock  # pragma: no cover


    def test_delete_image_gallery_not_gallery(self):
        self.image_gallery.delete()
        self.gallery.refresh_from_db()
        self.assertFalse(self.gallery.images.all())

    def test_delete_image_gallery_cascade_image(self):
        self.image_gallery.delete()
        with self.assertRaises(Image.DoesNotExist):
            self.image.refresh_from_db()

    @mock.patch.object(ImageGallery, 'delete', side_effect=Exception('Boom'))
    def test_delete_image_gallery_fail(self, mock_delete):
        image_gallery_id = self.image_gallery.id
        with self.assertRaises(Exception):
            self.image_gallery.delete()
        self.image.refresh_from_db()
        self.assertTrue(ImageGallery.objects.get(id=image_gallery_id))

    def test_delete_image_not_gallery(self):
        self.image.delete()
        self.gallery.refresh_from_db()
        self.assertFalse(self.gallery.images.all())

    def test_delete_image_cascade_image_gallery(self):
        self.image.delete()
        with self.assertRaises(ImageGallery.DoesNotExist):
            self.image_gallery.refresh_from_db()



class ImageGalleryQuerysetRelationshipTestCase(PrivagalTestCase):