Files
odoo_source/odoo/addons/base/tests/test_avatar_mixin.py
T
Fabien Pinckaers 6b87526048 [IMP] Speed Imp: remove unnecessary base64 encode & decode
Avoid to base64 encode, then decode to process assets and images for a ~25% speed improvement.
Change image processing tool to work on images, rather than base64 encoded strings.

Performance is ~25% faster on assets & images:

  /web/assets/...frontend.min.css:    13ms to 7ms,  base64 enc/dec: 2 -> 0
  /web/image/XML_ID:                  10ms to 8ms,  base64 enc/dec: 3 -> 0
  /web/image/res.users/2/avatar_128:  40ms to 20ms, base64 enc/dec: 6 -> 2

closes odoo/odoo#82851

Related: odoo/enterprise#23537
Signed-off-by: Fabien Pinckaers <fp@odoo.com>
2022-01-22 11:51:42 +00:00

61 lines
2.7 KiB
Python

# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from base64 import b64decode
from odoo.tests.common import TransactionCase
class TestAvatarMixin(TransactionCase):
""" tests the avatar mixin """
def setUp(self):
super().setUp()
self.user_without_image = self.env['res.users'].create({
'name': 'Marc Demo',
'email': 'mark.brown23@example.com',
'image_1920': False,
'create_date': '2015-11-12 00:00:00',
'login': 'demo_1',
'password': 'demo_1'
})
self.user_without_image.partner_id.create_date = '2015-11-12 00:00:00'
self.user_without_name = self.env['res.users'].create({
'name': '',
'email': 'marc.grey25@example.com',
'image_1920': False,
'login': 'marc_1',
'password': 'marc_1',
})
self.external_partner = self.env['res.partner'].create({
'name': 'Josh Demo',
'email': 'josh.brown23@example.com',
'image_1920': False
})
def test_partner_has_avatar_even_if_it_has_no_image(self):
self.assertTrue(self.user_without_image.partner_id.avatar_128)
self.assertTrue(self.user_without_image.partner_id.avatar_256)
self.assertTrue(self.user_without_image.partner_id.avatar_512)
self.assertTrue(self.user_without_image.partner_id.avatar_1024)
self.assertTrue(self.user_without_image.partner_id.avatar_1920)
def test_content_of_generated_partner_avatar(self):
expectedAvatar = (
"<?xml version='1.0' encoding='UTF-8' ?>"
"<svg height='180' width='180' xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink'>"
"<rect fill='hsl(184, 40%, 45%)' height='180' width='180'/>"
"<text fill='#ffffff' font-size='96' text-anchor='middle' x='90' y='125' font-family='sans-serif'>M</text>"
"</svg>"
)
self.assertEqual(expectedAvatar, b64decode(self.user_without_image.partner_id.avatar_1920).decode('utf-8'))
def test_partner_without_name_has_default_placeholder_image_as_avatar(self):
self.assertEqual(self.user_without_name.partner_id._avatar_get_placeholder(), b64decode(self.user_without_name.partner_id.avatar_1920))
def test_external_partner_has_default_placeholder_image_as_avatar(self):
self.assertEqual(self.external_partner._avatar_get_placeholder(), b64decode(self.external_partner.avatar_1920))
def test_partner_and_user_have_the_same_avatar(self):
self.assertEqual(self.user_without_image.partner_id.avatar_1920, self.user_without_image.avatar_1920)