|
| 1 | +# Copyright 2024 ForgeFlow S.L. (https://www.forgeflow.com) |
| 2 | +# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl). |
| 3 | +import base64 |
| 4 | + |
| 5 | +import chardet |
| 6 | + |
| 7 | +from .common import EDIBackendCommonComponentRegistryTestCase |
| 8 | +from .fake_components import FakeOutputGenerator |
| 9 | + |
| 10 | + |
| 11 | +class EDIBackendTestOutputCase(EDIBackendCommonComponentRegistryTestCase): |
| 12 | + @classmethod |
| 13 | + def setUpClass(cls): |
| 14 | + super().setUpClass() |
| 15 | + cls._build_components( |
| 16 | + cls, |
| 17 | + FakeOutputGenerator, |
| 18 | + ) |
| 19 | + vals = { |
| 20 | + "model": cls.partner._name, |
| 21 | + "res_id": cls.partner.id, |
| 22 | + } |
| 23 | + cls.record = cls.backend.create_record("test_csv_output", vals) |
| 24 | + |
| 25 | + def setUp(self): |
| 26 | + super().setUp() |
| 27 | + FakeOutputGenerator.reset_faked() |
| 28 | + |
| 29 | + def test_encoding_default(self): |
| 30 | + """ |
| 31 | + Test default output/input encoding (UTF-8). Use string with special |
| 32 | + character to test the encoding applied. |
| 33 | + """ |
| 34 | + self.backend.with_context(fake_output="Palmotićeva").exchange_generate( |
| 35 | + self.record |
| 36 | + ) |
| 37 | + # Test decoding is applied correctly |
| 38 | + self.assertEqual(self.record._get_file_content(), "Palmotićeva") |
| 39 | + # Test encoding used |
| 40 | + content = base64.b64decode(self.record.exchange_file) |
| 41 | + encoding = chardet.detect(content)["encoding"].lower() |
| 42 | + self.assertEqual(encoding, "utf-8") |
| 43 | + |
| 44 | + def test_encoding(self): |
| 45 | + """ |
| 46 | + Test specific output/input encoding. Use string with special |
| 47 | + character to test the encoding applied. |
| 48 | + """ |
| 49 | + self.exchange_type_out.write({"encoding": "UTF-16"}) |
| 50 | + self.backend.with_context(fake_output="Palmotićeva").exchange_generate( |
| 51 | + self.record |
| 52 | + ) |
| 53 | + # Test decoding is applied correctly |
| 54 | + self.assertEqual(self.record._get_file_content(), "Palmotićeva") |
| 55 | + # Test encoding used |
| 56 | + content = base64.b64decode(self.record.exchange_file) |
| 57 | + encoding = chardet.detect(content)["encoding"].lower() |
| 58 | + self.assertEqual(encoding, "utf-16") |
| 59 | + |
| 60 | + def test_encoding_error_handler(self): |
| 61 | + self.exchange_type_out.write({"encoding": "ascii"}) |
| 62 | + # By default, error handling raises error |
| 63 | + with self.assertRaises(UnicodeEncodeError): |
| 64 | + self.backend.with_context(fake_output="Palmotićeva").exchange_generate( |
| 65 | + self.record |
| 66 | + ) |
| 67 | + self.exchange_type_out.write({"encoding_out_error_handler": "ignore"}) |
| 68 | + self.backend.with_context(fake_output="Palmotićeva").exchange_generate( |
| 69 | + self.record |
| 70 | + ) |
| 71 | + self.assertEqual(self.record._get_file_content(), "Palmotieva") |
| 72 | + |
| 73 | + def test_decoding_error_handler(self): |
| 74 | + self.backend.with_context(fake_output="Palmotićeva").exchange_generate( |
| 75 | + self.record |
| 76 | + ) |
| 77 | + # Change encoding to ascii to check the decoding |
| 78 | + self.exchange_type_out.write({"encoding": "ascii"}) |
| 79 | + # By default, error handling raises error |
| 80 | + with self.assertRaises(UnicodeDecodeError): |
| 81 | + content = self.record._get_file_content() |
| 82 | + self.exchange_type_out.write({"encoding_in_error_handler": "ignore"}) |
| 83 | + content = self.record._get_file_content() |
| 84 | + self.assertEqual(content, "Palmotieva") |
0 commit comments