예제 #1
0
 def setUpTestData(cls):
     image_model = get_image_model()
     cls.test_image_1 = image_model.objects.create(
         title="Test image 1",
         file=get_test_image_file(),
     )
     cls.test_image_2 = image_model.objects.create(
         title="Test image 2",
         file=get_test_image_file(),
     )
예제 #2
0
    def test_lazy_load_queryset_bulk(self):
        """
        Ensure that lazy loading StreamField works when gotten as part of a
        queryset list
        """
        file_obj = get_test_image_file()
        image_1 = Image.objects.create(title='Test image 1', file=file_obj)
        image_3 = Image.objects.create(title='Test image 3', file=file_obj)

        with_image = StreamModel.objects.create(body=json.dumps([
            {'type': 'image', 'value': image_1.pk},
            {'type': 'image', 'value': None},
            {'type': 'image', 'value': image_3.pk},
            {'type': 'text', 'value': 'foo'}]))

        with self.assertNumQueries(1):
            instance = StreamModel.objects.get(pk=with_image.pk)

        # Prefetch all image blocks
        with self.assertNumQueries(1):
            instance.body[0]

        # 1. Further image block access should not execute any db lookups
        # 2. The blank block '1' should be None.
        # 3. The values should be in the original order.
        with self.assertNumQueries(0):
            assert instance.body[0].value.title == 'Test image 1'
            assert instance.body[1].value is None
            assert instance.body[2].value.title == 'Test image 3'
예제 #3
0
 def test_invalid(self):
     fil = Filter(spec='width-400|format-foo')
     image = Image.objects.create(
         title="Test image",
         file=get_test_image_file(),
     )
     self.assertRaises(InvalidFilterSpecError, fil.run, image, BytesIO())
예제 #4
0
    def test_gif(self):
        fil = Filter(spec='width-400|format-gif')
        image = Image.objects.create(
            title="Test image",
            file=get_test_image_file(),
        )
        out = fil.run(image, BytesIO())

        self.assertEqual(out.format_name, 'gif')
예제 #5
0
    def setUp(self):
        self.login()

        img = Image.objects.create(
            title="LOTR cover",
            file=get_test_image_file(),
        )
        book = Book.objects.get(title="The Lord of the Rings")
        book.cover_image = img
        book.save()
예제 #6
0
 def setUp(self):
     self.image = Image.objects.create(
         title='Test image',
         file=get_test_image_file())
     self.with_image = StreamModel.objects.create(body=json.dumps([
         {'type': 'image', 'value': self.image.pk},
         {'type': 'text', 'value': 'foo'}]))
     self.no_image = StreamModel.objects.create(body=json.dumps([
         {'type': 'text', 'value': 'foo'}]))
     self.nonjson_body = StreamModel.objects.create(body="<h1>hello world</h1>")
예제 #7
0
    def test_custom_image_signal_handlers(self):
        #: Sadly signal receivers only get connected when starting django.
        #: We will re-attach them here to mimic the django startup behavior
        #: and get the signals connected to our custom model..
        signal_handlers.register_signal_handlers()

        image = get_image_model().objects.create(title="Test CustomImage", file=get_test_image_file())
        image_path = image.file.path
        image.delete()

        self.assertFalse(os.path.exists(image_path))
예제 #8
0
    def test_runs_operations(self):
        run_mock = Mock()

        def run(willow, image, env):
            run_mock(willow, image, env)

        self.operation_instance.run = run

        fil = Filter(spec='operation1|operation2')
        image = Image.objects.create(
            title="Test image",
            file=get_test_image_file(),
        )
        fil.run(image, BytesIO())

        self.assertEqual(run_mock.call_count, 2)
예제 #9
0
    def setUp(self):
        self.image = Image.objects.create(
            title='Test image',
            file=get_test_image_file())

        self.instance = StreamModel.objects.create(body=json.dumps([
            {'type': 'rich_text', 'value': '<p>Rich text</p>'},
            {'type': 'rich_text', 'value': '<p>Привет, Микола</p>'},
            {'type': 'image', 'value': self.image.pk},
            {'type': 'text', 'value': 'Hello, World!'}]))

        img_tag = self.image.get_rendition('original').img_tag()
        self.expected = ''.join([
            '<div class="block-rich_text"><div class="rich-text"><p>Rich text</p></div></div>',
            '<div class="block-rich_text"><div class="rich-text"><p>Привет, Микола</p></div></div>',
            '<div class="block-image">{}</div>'.format(img_tag),
            '<div class="block-text">Hello, World!</div>',
        ])
예제 #10
0
    def test_thumbnail(self):
        # Add a new image with source file
        image = get_image_model().objects.create(
            title="Test image",
            file=get_test_image_file(),
        )

        response = self.get_response(image.id)
        content = json.loads(response.content.decode('UTF-8'))

        self.assertIn('thumbnail', content)
        self.assertEqual(content['thumbnail']['width'], 165)
        self.assertEqual(content['thumbnail']['height'], 123)
        self.assertTrue(
            content['thumbnail']['url'].startswith('/media/images/test'))

        # Check that source_image_error didn't appear
        self.assertNotIn('source_image_error', content['meta'])
예제 #11
0
    def test_image_signal_handlers(self):
        image = get_image_model().objects.create(title="Test Image", file=get_test_image_file())
        image_path = image.file.path
        image.delete()

        self.assertFalse(os.path.exists(image_path))