Skip to content
Permalink
Browse files

wip test

  • Loading branch information...
seb-odoo committed Mar 14, 2019
1 parent ec4ab8a commit 5f079ad74e5b0964fe15679ddc64b4e2e2eb5afa
Showing with 104 additions and 6 deletions.
  1. +104 −6 odoo/addons/base/tests/test_image.py
@@ -1,10 +1,12 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.

from PIL import Image
import base64
import binascii
import PIL

from odoo import tools
from odoo.tests.common import TransactionCase
from odoo.modules.module import get_resource_path


class TestImage(TransactionCase):
@@ -20,16 +22,112 @@ class TestImage(TransactionCase):
- image_resize_images
- is_image_size_above
"""
def setUp(self):
super(TestImage, self).setUp()
# This is the base64 of a 1px * 1px image saved as PNG.
self.base64_image_1x1 = b'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAIAAACQd1PeAAAADElEQVR4nGNgYGAAAAAEAAH2FzhVAAAAAElFTkSuQmCC'
self.base64_svg = base64.b64encode(b'<svg></svg>')
self.base64_image_1920_1080 = tools.image_to_base64(PIL.Image.new('RGB', (1920, 1080)), 'JPEG')

def test_00_base64_to_image(self):
get_resource_path('base', 'static', 'img', 'main_partner-image.png')
pass
"""Test that base64 is correctly opened as a PIL image."""

# CASE: base64 as bytes
image = tools.base64_to_image(self.base64_image_1x1)
self.assertEqual(type(image), PIL.PngImagePlugin.PngImageFile)
self.assertEqual(image.size, (1, 1))

# CASE: base64 as string
image = tools.base64_to_image(self.base64_image_1x1.decode('ASCII'))
self.assertEqual(type(image), PIL.PngImagePlugin.PngImageFile)
self.assertEqual(image.size, (1, 1))

# CASE: wrong base64: binascii.Error: Incorrect padding
with self.assertRaises(binascii.Error):
image = tools.base64_to_image(b'oazdazpodazdpok')

# CASE: wrong base64: OSError: cannot identify image file
with self.assertRaises(OSError):
image = tools.base64_to_image(b'oazdazpodazdpokd')

def test_01_image_to_base64(self):
pass
"""Test that a PIL image is correctly saved as base64."""
image = PIL.Image.new('RGB', (1, 1))
image_base64 = tools.image_to_base64(image, 'PNG')
self.assertEqual(image_base64, self.base64_image_1x1)

def test_10_image_resize_image(self):
pass
"""Test that image_resize_image is working as expected."""

# CASE: return False if base64_source is fasly
res = tools.image_resize_image(False)
self.assertEqual(res, False)

# CASE: return base64_source if size == (None, None)
res = tools.image_resize_image(self.base64_image_1x1, size=(None, None))
self.assertEqual(res, self.base64_image_1x1)

# CASE: return base64_source if format is SVG
res = tools.image_resize_image(self.base64_svg)
self.assertEqual(res, self.base64_svg)

# CASE: ok with default parameters
image = tools.base64_to_image(tools.image_resize_image(self.base64_image_1920_1080))
self.assertEqual(image.size, tools.IMAGE_BIG_SIZE)

# CASE: correct resize
image = tools.base64_to_image(tools.image_resize_image(self.base64_image_1920_1080, size=(800, 600)))
self.assertEqual(image.size, (800, 600))

# CASE: change filetype to PNG
image = tools.base64_to_image(tools.image_resize_image(self.base64_image_1920_1080, filetype='PNG'))
self.assertEqual(image.format, 'PNG')

# CASE: change filetype to JPEG (case insensitive)
image = tools.base64_to_image(tools.image_resize_image(self.base64_image_1x1, filetype='jpeg'))
self.assertEqual(image.format, 'JPEG')

# CASE: change filetype to BMP converted to PNG
image = tools.base64_to_image(tools.image_resize_image(self.base64_image_1x1, filetype='BMP'))
self.assertEqual(image.format, 'PNG')

# CASE: keep ratio if no height given
image = tools.base64_to_image(tools.image_resize_image(self.base64_image_1920_1080, size=(192, None)))
self.assertEqual(image.size, (192, 108))

# CASE: keep ratio if no width given
image = tools.base64_to_image(tools.image_resize_image(self.base64_image_1920_1080, size=(None, 108)))
self.assertEqual(image.size, (192, 108))

# CASE: resize above original size
image = tools.base64_to_image(tools.image_resize_image(self.base64_image_1920_1080, size=(3000, 2000)))
self.assertEqual(image.size, (3000, 2000))

# CASE: avoid_if_small=True and size above, return original
res = tools.image_resize_image(self.base64_image_1920_1080, size=(3000, 2000), avoid_if_small=True)
self.assertEqual(res, self.base64_image_1920_1080)

# CASE: same size, no sharpen
res = tools.image_resize_image(self.base64_image_1920_1080, size=(1920, 1080))
self.assertEqual(res, self.base64_image_1920_1080)

# CASE: upper_limit=True, no resize if above
image = tools.base64_to_image(tools.image_resize_image(self.base64_image_1920_1080, size=(3000, 2000), upper_limit=True))
self.assertEqual(image.size, (1920, 1080))

# CASE: upper_limit=True, keep ratio, no resize if above
image = tools.base64_to_image(tools.image_resize_image(self.base64_image_1920_1080, size=(3000, None), upper_limit=True))
self.assertEqual(image.size, (1920, 1080))

# CASE: upper_limit=True, keep ratio, no resize if above
image = tools.base64_to_image(tools.image_resize_image(self.base64_image_1920_1080, size=(None, 2000), upper_limit=True))
self.assertEqual(image.size, (1920, 1080))

# CASE: upper_limit=True, vertical image, correct resize if below
base64_image_1080_1920 = tools.image_to_base64(PIL.Image.new('RGB', (1080, 1920)), 'PNG')
image = tools.base64_to_image(tools.image_resize_image(base64_image_1080_1920, size=(3000, 192), upper_limit=True))
# TODO SEB fix 3px because of hack in sharpen...
self.assertEqual(image.size, (108, 192))

def test_11_image_resize_and_sharpen(self):
pass

0 comments on commit 5f079ad

Please sign in to comment.
You can’t perform that action at this time.