From 4375c7bc2f556dc998329e51c9dd566a1b45ed20 Mon Sep 17 00:00:00 2001 From: Graeme Gellatly Date: Sun, 8 Oct 2017 19:41:57 +1300 Subject: [PATCH 1/5] [REF] Refactor the way print options are defined and sent. --- .../models/ir_actions_report.py | 90 +++++++++---------- .../models/printing_printer.py | 69 +++++++------- 2 files changed, 79 insertions(+), 80 deletions(-) diff --git a/base_report_to_printer/models/ir_actions_report.py b/base_report_to_printer/models/ir_actions_report.py index e9108eb..0caed4a 100644 --- a/base_report_to_printer/models/ir_actions_report.py +++ b/base_report_to_printer/models/ir_actions_report.py @@ -48,7 +48,7 @@ class IrActionsReport(models.Model): report = self._get_report_from_name(report_name) if not report: return {} - result = report.behaviour()[report] + result = report.behaviour() serializable_result = { 'action': result['action'], 'printer_name': result['printer'].name, @@ -56,64 +56,63 @@ class IrActionsReport(models.Model): return serializable_result @api.multi - def behaviour(self): - result = {} + def _get_user_default_print_behaviour(self): printer_obj = self.env['printing.printer'] - printing_act_obj = self.env['printing.report.xml.action'] - # Set hardcoded default action - default_action = 'client' - # Retrieve system wide printer - default_printer = printer_obj.get_default() - - # Retrieve user default values user = self.env.user - if user.printing_action: - default_action = user.printing_action - if user.printing_printer_id: - default_printer = user.printing_printer_id + return dict( + action=user.printing_action or 'client', + printer=user.printing_printer_id or printer_obj.get_default(), + tray=str(user.printer_tray_id.system_name) if + user.printer_tray_id else False + ) - for report in self: - action = default_action - printer = default_printer + @api.multi + def _get_report_default_print_behaviour(self): + result = {} + report_action = self.property_printing_action_id + if report_action and report_action.action_type != 'user_default': + result['action'] = report_action.action_type + if self.printing_printer_id: + result['printer'] = self.printing_printer_id + if self.printer_tray_id: + result['tray'] = self.printer_tray_id.system_name + return result - # Retrieve report default values - report_action = report.property_printing_action_id - if report_action and report_action.action_type != 'user_default': - action = report_action.action_type - if report.printing_printer_id: - printer = report.printing_printer_id + @api.multi + def behaviour(self): + self.ensure_one() + printing_act_obj = self.env['printing.report.xml.action'] - # Retrieve report-user specific values - print_action = printing_act_obj.search([ - ('report_id', '=', report.id), - ('user_id', '=', self.env.uid), - ('action', '!=', 'user_default'), - ], limit=1) - if print_action: - user_action = print_action.behaviour() - action = user_action['action'] - if user_action['printer']: - printer = user_action['printer'] + result = self._get_user_default_print_behaviour() + result.update(self._get_report_default_print_behaviour()) - result[report] = { - 'action': action, - 'printer': printer, - } + # Retrieve report-user specific values + print_action = printing_act_obj.search([ + ('report_id', '=', self.id), + ('user_id', '=', self.env.uid), + ('action', '!=', 'user_default'), + ], limit=1) + if print_action: + result.update(print_action.behaviour()) return result @api.multi def print_document(self, record_ids, data=None): """ Print a document, do not return the document file """ - document = self.with_context( + document, doc_format = self.with_context( must_skip_send_to_printer=True).render_qweb_pdf( record_ids, data=data) - behaviour = self.behaviour()[self] - printer = behaviour['printer'] + behaviour = self.behaviour() + printer = behaviour.pop('printer', None) + if not printer: raise exceptions.Warning( _('No printer configured to print this report.') ) - return printer.print_document(self, document, self.report_type) + # TODO should we use doc_format instead of report_type + return printer.print_document(self, document, + doc_format=self.report_type, + **behaviour) @api.multi def _can_print_report(self, behaviour, printer, document): @@ -138,11 +137,12 @@ class IrActionsReport(models.Model): document, doc_format = super(IrActionsReport, self).render_qweb_pdf( docids, data=data) - behaviour = self.behaviour()[self] - printer = behaviour['printer'] + behaviour = self.behaviour() + printer = behaviour.pop('printer', None) can_print_report = self._can_print_report(behaviour, printer, document) if can_print_report: - printer.print_document(self, document, self.report_type) + printer.print_document(self, document, doc_format=self.report_type, + **behaviour) return document, doc_format diff --git a/base_report_to_printer/models/printing_printer.py b/base_report_to_printer/models/printing_printer.py index 396c456..fb599d0 100644 --- a/base_report_to_printer/models/printing_printer.py +++ b/base_report_to_printer/models/printing_printer.py @@ -118,36 +118,7 @@ class PrintingPrinter(models.Model): return vals @api.multi - def print_options(self, report=None, format=None, copies=1): - """ Hook to set print options """ - options = {} - if format == 'raw': - options['raw'] = 'True' - if copies > 1: - options['copies'] = str(copies) - if report is not None: - printing_act_obj = self.env['printing.report.xml.action'] - if report.printer_tray_id: - tray = report.printer_tray_id - else: - # Retrieve user default values - tray = self.env.user.printer_tray_id - - # Retrieve report-user specific values - action = printing_act_obj.search([ - ('report_id', '=', report.id), - ('user_id', '=', self.env.uid), - ('action', '!=', 'user_default'), - ], limit=1) - if action.printer_tray_id: - tray = action.printer_tray_id - - if tray: - options['InputSlot'] = str(tray.system_name) - return options - - @api.multi - def print_document(self, report, content, format, copies=1): + def print_document(self, report, content, **print_opts): """ Print a file Format could be pdf, qweb-pdf, raw, ... @@ -161,16 +132,44 @@ class PrintingPrinter(models.Model): os.close(fd) return self.print_file( - file_name, report=report, copies=copies, format=format) + file_name, report=report, **print_opts) + + @staticmethod + def _set_option_doc_format(report, value): + return {'raw': 'True'} if value == 'raw' else {} + + # Backwards compatibility of builtin used as kwarg + _set_option_format = _set_option_doc_format @api.multi - def print_file(self, file_name, report=None, copies=1, format=None): + def _set_option_tray(self, report, value): + """Note we use self here as some older PPD use tray + rather than InputSlot so we may need to query printer in override""" + return {'InputSlot': str(value)} + + @staticmethod + def _set_option_noop(report, value): + return {} + + _set_option_action = _set_option_noop + _set_option_printer = _set_option_noop + + @api.multi + def print_options(self, report=None, **print_opts): + options = {} + for option, value in print_opts.items(): + try: + getattr(self, '_set_option_%s' % option)(report, value) + except AttributeError: + options[option] = str(value) + return options + + @api.multi + def print_file(self, file_name, report=None, **print_opts): """ Print a file """ self.ensure_one() - connection = self.server_id._open_connection(raise_on_error=True) - options = self.print_options( - report=report, format=format, copies=copies) + options = self.print_options(report=report, **print_opts) _logger.debug( 'Sending job to CUPS printer %s on %s' From 142cdfbb209f83b5ed50cab1e8fcf097a00a193c Mon Sep 17 00:00:00 2001 From: Graeme Gellatly Date: Sun, 8 Oct 2017 19:46:52 +1300 Subject: [PATCH 2/5] [FIX] Update tests to new return signature of behaviour --- .../models/ir_actions_report.py | 2 +- .../models/printing_printer.py | 2 +- .../tests/test_ir_actions_report.py | 92 +++++++++---------- .../tests/test_printing_printer.py | 25 ++--- .../tests/test_printing_printer_tray.py | 10 +- 5 files changed, 66 insertions(+), 65 deletions(-) diff --git a/base_report_to_printer/models/ir_actions_report.py b/base_report_to_printer/models/ir_actions_report.py index 0caed4a..104fae0 100644 --- a/base_report_to_printer/models/ir_actions_report.py +++ b/base_report_to_printer/models/ir_actions_report.py @@ -63,7 +63,7 @@ class IrActionsReport(models.Model): action=user.printing_action or 'client', printer=user.printing_printer_id or printer_obj.get_default(), tray=str(user.printer_tray_id.system_name) if - user.printer_tray_id else False + user.printer_tray_id else False ) @api.multi diff --git a/base_report_to_printer/models/printing_printer.py b/base_report_to_printer/models/printing_printer.py index fb599d0..a8ae6d4 100644 --- a/base_report_to_printer/models/printing_printer.py +++ b/base_report_to_printer/models/printing_printer.py @@ -145,7 +145,7 @@ class PrintingPrinter(models.Model): def _set_option_tray(self, report, value): """Note we use self here as some older PPD use tray rather than InputSlot so we may need to query printer in override""" - return {'InputSlot': str(value)} + return {'InputSlot': str(value)} if value else {} @staticmethod def _set_option_noop(report, value): diff --git a/base_report_to_printer/tests/test_ir_actions_report.py b/base_report_to_printer/tests/test_ir_actions_report.py index 172b804..0b98ebd 100644 --- a/base_report_to_printer/tests/test_ir_actions_report.py +++ b/base_report_to_printer/tests/test_ir_actions_report.py @@ -67,7 +67,7 @@ class TestIrActionsReportXml(TransactionCase): """ It should return correct serializable result for behaviour """ with mock.patch.object(self.Model, '_get_report_from_name') as mk: res = self.Model.print_action_for_report_name('test') - behaviour = mk().behaviour()[mk()] + behaviour = mk().behaviour() expect = { 'action': behaviour['action'], 'printer_name': behaviour['printer'].name, @@ -85,11 +85,11 @@ class TestIrActionsReportXml(TransactionCase): report.property_printing_action_id = False report.printing_printer_id = False self.assertEqual(report.behaviour(), { - report: { - 'action': 'client', - 'printer': self.env['printing.printer'], - }, - }) + 'action': 'client', + 'printer': self.env['printing.printer'], + 'tray': False, + }, + ) def test_behaviour_user_values(self): """ It should return the action and printer from user """ @@ -97,11 +97,11 @@ class TestIrActionsReportXml(TransactionCase): self.env.user.printing_action = 'client' self.env.user.printing_printer_id = self.new_printer() self.assertEqual(report.behaviour(), { - report: { - 'action': 'client', - 'printer': self.env.user.printing_printer_id, - }, - }) + 'action': 'client', + 'printer': self.env.user.printing_printer_id, + 'tray': False, + }, + ) def test_behaviour_report_values(self): """ It should return the action and printer from report """ @@ -110,11 +110,11 @@ class TestIrActionsReportXml(TransactionCase): report.property_printing_action_id = self.new_action() report.printing_printer_id = self.new_printer() self.assertEqual(report.behaviour(), { - report: { - 'action': report.property_printing_action_id.action_type, - 'printer': report.printing_printer_id, - }, - }) + 'action': report.property_printing_action_id.action_type, + 'printer': report.printing_printer_id, + 'tray': False, + }, + ) def test_behaviour_user_action(self): """ It should return the action and printer from user action""" @@ -122,11 +122,11 @@ class TestIrActionsReportXml(TransactionCase): self.env.user.printing_action = 'client' report.property_printing_action_id.action_type = 'user_default' self.assertEqual(report.behaviour(), { - report: { - 'action': 'client', - 'printer': report.printing_printer_id, - }, - }) + 'action': 'client', + 'printer': report.printing_printer_id, + 'tray': False, + }, + ) def test_behaviour_printing_action_on_wrong_user(self): """ It should return the action and printer ignoring printing action @@ -138,11 +138,11 @@ class TestIrActionsReportXml(TransactionCase): ('id', '!=', self.env.user.id), ], limit=1) self.assertEqual(report.behaviour(), { - report: { - 'action': 'client', - 'printer': report.printing_printer_id, - }, - }) + 'action': 'client', + 'printer': report.printing_printer_id, + 'tray': False, + }, + ) def test_behaviour_printing_action_on_wrong_report(self): """ It should return the action and printer ignoring printing action @@ -155,11 +155,11 @@ class TestIrActionsReportXml(TransactionCase): ('id', '!=', report.id), ], limit=1) self.assertEqual(report.behaviour(), { - report: { - 'action': 'client', - 'printer': report.printing_printer_id, - }, - }) + 'action': 'client', + 'printer': report.printing_printer_id, + 'tray': False, + }, + ) def test_behaviour_printing_action_with_no_printer(self): """ It should return the action from printing action and printer from other @@ -170,11 +170,11 @@ class TestIrActionsReportXml(TransactionCase): printing_action.user_id = self.env.user printing_action.report_id = report self.assertEqual(report.behaviour(), { - report: { - 'action': printing_action.action, - 'printer': report.printing_printer_id, - }, - }) + 'action': printing_action.action, + 'printer': report.printing_printer_id, + 'tray': False, + }, + ) def test_behaviour_printing_action_with_printer(self): """ It should return the action and printer from printing action """ @@ -184,11 +184,11 @@ class TestIrActionsReportXml(TransactionCase): printing_action.user_id = self.env.user printing_action.printer_id = self.new_printer() self.assertEqual(report.behaviour(), { - report: { - 'action': printing_action.action, - 'printer': printing_action.printer_id, - }, - }) + 'action': printing_action.action, + 'printer': printing_action.printer_id, + 'tray': False, + }, + ) def test_behaviour_printing_action_user_defaults(self): """ It should return the action and printer from user with printing action @@ -199,11 +199,11 @@ class TestIrActionsReportXml(TransactionCase): printing_action.user_id = self.env.user printing_action.action = 'user_default' self.assertEqual(report.behaviour(), { - report: { - 'action': 'client', - 'printer': report.printing_printer_id, - }, - }) + 'action': 'client', + 'printer': report.printing_printer_id, + 'tray': False, + }, + ) def test_onchange_printer_tray_id_empty(self): action = self.env['ir.actions.report'].new( diff --git a/base_report_to_printer/tests/test_printing_printer.py b/base_report_to_printer/tests/test_printing_printer.py index 91da7c3..8216ad7 100644 --- a/base_report_to_printer/tests/test_printing_printer.py +++ b/base_report_to_printer/tests/test_printing_printer.py @@ -40,16 +40,16 @@ class TestPrintingPrinter(TransactionCase): """ It should generate the right options dictionnary """ # TODO: None here used as report - tests here should be merged # with tests in test_printing_printer_tray from when modules merged - self.assertEqual(self.Model.print_options(None, 'raw'), { - 'raw': 'True', - }) - self.assertEqual(self.Model.print_options(None, 'pdf', 2), { - 'copies': '2', - }) - self.assertEqual(self.Model.print_options(None, 'raw', 2), { - 'raw': 'True', - 'copies': '2', - }) + self.assertEqual(self.Model.print_options( + None, doc_format='raw'), {'raw': 'True',} + ) + self.assertEqual(self.Model.print_options( + None, doc_format='pdf', copies=2), {'copies': '2',} + ) + self.assertEqual(self.Model.print_options( + None, doc_format='raw', copies=2), + {'raw': 'True', 'copies': '2',} + ) @mock.patch('%s.cups' % server_model) def test_print_report(self, cups): @@ -58,7 +58,8 @@ class TestPrintingPrinter(TransactionCase): with mock.patch('%s.mkstemp' % model) as mkstemp: mkstemp.return_value = fd, file_name printer = self.new_record() - printer.print_document(self.report, b'content to print', 'pdf') + printer.print_document(self.report, b'content to print', + doc_format='pdf') cups.Connection().printFile.assert_called_once_with( printer.system_name, file_name, @@ -75,7 +76,7 @@ class TestPrintingPrinter(TransactionCase): printer = self.new_record() with self.assertRaises(UserError): printer.print_document( - self.report, b'content to print', 'pdf') + self.report, b'content to print', doc_format='pdf') @mock.patch('%s.cups' % server_model) def test_print_file(self, cups): diff --git a/base_report_to_printer/tests/test_printing_printer_tray.py b/base_report_to_printer/tests/test_printing_printer_tray.py index 7fe7dcb..badde3e 100644 --- a/base_report_to_printer/tests/test_printing_printer_tray.py +++ b/base_report_to_printer/tests/test_printing_printer_tray.py @@ -129,14 +129,14 @@ class TestPrintingPrinter(TransactionCase): self.env.user.printer_tray_id = False report.printer_tray_id = False action.printer_tray_id = False - options = self.Model.print_options(report, 'pdf') + options = self.Model.print_options(report, doc_format='pdf') self.assertFalse('InputSlot' in options) # Only user tray is defined self.env.user.printer_tray_id = user_tray report.printer_tray_id = False action.printer_tray_id = False - options = self.Model.print_options(report, 'pdf') + options = self.Model.print_options(report, doc_format='pdf') self.assertEquals(options, { 'InputSlot': 'User tray', }) @@ -145,7 +145,7 @@ class TestPrintingPrinter(TransactionCase): self.env.user.printer_tray_id = False report.printer_tray_id = report_tray action.printer_tray_id = False - options = self.Model.print_options(report, 'pdf') + options = self.Model.print_options(report, doc_format='pdf') self.assertEquals(options, { 'InputSlot': 'Report tray', }) @@ -154,7 +154,7 @@ class TestPrintingPrinter(TransactionCase): self.env.user.printer_tray_id = False report.printer_tray_id = False action.printer_tray_id = action_tray - options = self.Model.print_options(report, 'pdf') + options = self.Model.print_options(report, doc_format='pdf') self.assertEquals(options, { 'InputSlot': 'Action tray', }) @@ -163,7 +163,7 @@ class TestPrintingPrinter(TransactionCase): self.env.user.printer_tray_id = user_tray report.printer_tray_id = report_tray action.printer_tray_id = action_tray - options = self.Model.print_options(report, 'pdf') + options = self.Model.print_options(report, doc_format='pdf') self.assertEquals(options, { 'InputSlot': 'Action tray', }) From 57bd6a112e61d50b8980f308bad9f1c779ba3b40 Mon Sep 17 00:00:00 2001 From: Graeme Gellatly Date: Sun, 8 Oct 2017 20:36:35 +1300 Subject: [PATCH 3/5] [To FIX] Hackish fix to test, needs to be done properly as behaviour is now part of the call to report. --- base_report_to_printer/tests/test_printing_printer.py | 6 +++--- base_report_to_printer/tests/test_report.py | 3 ++- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/base_report_to_printer/tests/test_printing_printer.py b/base_report_to_printer/tests/test_printing_printer.py index 8216ad7..b961d7c 100644 --- a/base_report_to_printer/tests/test_printing_printer.py +++ b/base_report_to_printer/tests/test_printing_printer.py @@ -41,14 +41,14 @@ class TestPrintingPrinter(TransactionCase): # TODO: None here used as report - tests here should be merged # with tests in test_printing_printer_tray from when modules merged self.assertEqual(self.Model.print_options( - None, doc_format='raw'), {'raw': 'True',} + None, doc_format='raw'), {'raw': 'True'} ) self.assertEqual(self.Model.print_options( - None, doc_format='pdf', copies=2), {'copies': '2',} + None, doc_format='pdf', copies=2), {'copies': '2'} ) self.assertEqual(self.Model.print_options( None, doc_format='raw', copies=2), - {'raw': 'True', 'copies': '2',} + {'raw': 'True', 'copies': '2'} ) @mock.patch('%s.cups' % server_model) diff --git a/base_report_to_printer/tests/test_report.py b/base_report_to_printer/tests/test_report.py index 8a1f5fe..c9b878b 100644 --- a/base_report_to_printer/tests/test_report.py +++ b/base_report_to_printer/tests/test_report.py @@ -90,7 +90,8 @@ class TestReport(HttpCase): records = self.env[report.model].search([], limit=5) document, doc_format = report.render_qweb_pdf(records.ids) print_document.assert_called_once_with( - report, document, report.report_type) + report, document, + action='server', doc_format='qweb-pdf', tray=False) def test_print_document_not_printable(self): """ It should print the report, regardless of the defined behaviour """ From 0d0acbc09620d6422ece8697baa3d9ae29b57c6c Mon Sep 17 00:00:00 2001 From: Graeme Gellatly Date: Sun, 8 Oct 2017 21:02:25 +1300 Subject: [PATCH 4/5] [FIX] Set back old tray behaviour to take user or report default if tray not set on action - update tests for printer tray to new determinant location --- .../models/ir_actions_report.py | 5 +- .../models/printing_printer.py | 6 +- .../tests/test_ir_actions_report.py | 70 +++++++++++++++++++ .../tests/test_printing_printer.py | 43 ++++++++++-- .../tests/test_printing_printer_tray.py | 68 ------------------ 5 files changed, 118 insertions(+), 74 deletions(-) diff --git a/base_report_to_printer/models/ir_actions_report.py b/base_report_to_printer/models/ir_actions_report.py index 104fae0..e6d078c 100644 --- a/base_report_to_printer/models/ir_actions_report.py +++ b/base_report_to_printer/models/ir_actions_report.py @@ -93,7 +93,10 @@ class IrActionsReport(models.Model): ('action', '!=', 'user_default'), ], limit=1) if print_action: - result.update(print_action.behaviour()) + # For some reason design takes report defaults over + # False action entries so we must allow for that here + result.update({k: v for k, v in + print_action.behaviour().items() if v}) return result @api.multi diff --git a/base_report_to_printer/models/printing_printer.py b/base_report_to_printer/models/printing_printer.py index a8ae6d4..057f874 100644 --- a/base_report_to_printer/models/printing_printer.py +++ b/base_report_to_printer/models/printing_printer.py @@ -157,9 +157,13 @@ class PrintingPrinter(models.Model): @api.multi def print_options(self, report=None, **print_opts): options = {} + if not report: + return options + for option, value in print_opts.items(): try: - getattr(self, '_set_option_%s' % option)(report, value) + options.update(getattr( + self, '_set_option_%s' % option)(report, value)) except AttributeError: options[option] = str(value) return options diff --git a/base_report_to_printer/tests/test_ir_actions_report.py b/base_report_to_printer/tests/test_ir_actions_report.py index 0b98ebd..683ba04 100644 --- a/base_report_to_printer/tests/test_ir_actions_report.py +++ b/base_report_to_printer/tests/test_ir_actions_report.py @@ -44,6 +44,12 @@ class TestIrActionsReportXml(TransactionCase): 'uri': 'URI', }) + def new_tray(self, vals=None, defaults=None): + values = dict(defaults) + if vals is not None: + values.update(vals) + return self.env['printing.tray'].create(values) + def test_print_action_for_report_name_gets_report(self): """ It should get report by name """ with mock.patch.object(self.Model, '_get_report_from_name') as mk: @@ -205,6 +211,70 @@ class TestIrActionsReportXml(TransactionCase): }, ) + def test_print_tray_behaviour(self): + """ + It should return the correct tray + """ + report = self.env['ir.actions.report'].search([], limit=1) + action = self.env['printing.report.xml.action'].create({ + 'user_id': self.env.user.id, + 'report_id': report.id, + 'action': 'server', + }) + printer = self.new_printer() + tray_vals = { + 'name': 'Tray', + 'system_name': 'Tray', + 'printer_id': printer.id, + } + user_tray = self.new_tray({'system_name':'User tray'}, tray_vals) + report_tray = self.new_tray({'system_name': 'Report tray'}, tray_vals) + action_tray = self.new_tray({'system_name': 'Action tray'}, tray_vals) + + + # No report passed + self.env.user.printer_tray_id = False + options = self.printer.print_options() + self.assertFalse('InputSlot' in options) + + # No tray defined + self.env.user.printer_tray_id = False + report.printer_tray_id = False + action.printer_tray_id = False + options = report.behaviour() + self.assertTrue('tray' in options) + + # Only user tray is defined + self.env.user.printer_tray_id = user_tray + report.printer_tray_id = False + action.printer_tray_id = False + options = report.behaviour() + self.assertIn({'tray': 'User tray'}, options) + + # Only report tray is defined + self.env.user.printer_tray_id = False + report.printer_tray_id = report_tray + action.printer_tray_id = False + self.assertIn({'tray': 'Report tray'}, report.behaviour()) + + # Only action tray is defined + self.env.user.printer_tray_id = False + report.printer_tray_id = False + action.printer_tray_id = action_tray + self.assertIn({'tray': 'Action tray'}, report.behaviour()) + + # User and report tray defined + self.env.user.printer_tray_id = False + report.printer_tray_id = False + action.printer_tray_id = action_tray + self.assertIn({'tray': 'User tray'}, report.behaviour()) + + # All trays are defined + self.env.user.printer_tray_id = user_tray + report.printer_tray_id = report_tray + action.printer_tray_id = action_tray + self.assertIn({'tray': 'Action tray'}, report.behaviour()) + def test_onchange_printer_tray_id_empty(self): action = self.env['ir.actions.report'].new( {'printer_tray_id': False}) diff --git a/base_report_to_printer/tests/test_printing_printer.py b/base_report_to_printer/tests/test_printing_printer.py index b961d7c..0fec88a 100644 --- a/base_report_to_printer/tests/test_printing_printer.py +++ b/base_report_to_printer/tests/test_printing_printer.py @@ -36,20 +36,55 @@ class TestPrintingPrinter(TransactionCase): def new_record(self): return self.Model.create(self.printer_vals) - def test_printing_options(self): + def test_option_tray(self): + """ + It should put the value in InputSlot + """ + self.assertEqual(self.Model._set_option_tray(None, 'Test Tray'), + {'InputSlot': 'Test Tray'}) + self.assertEqual(self.Model._set_option_tray(None, False), + {}) + + def test_option_noops(self): + """ + Noops should return an empty dict + """ + self.assertEqual(self.Model._set_option_action(None, 'printer'), {}) + self.assertEqual(self.Model._set_option_printer(None, self.printer), + {}) + + def test_option_doc_format(self): + """ + Raw documents should set raw boolean. + """ + self.assertEqual(self.Model._set_option_doc_format(None, 'raw'), + {'raw': 'True'}) + # Deprecate _set_option_format in v12. + self.assertEqual(self.Model._set_option_format(None, 'raw'), + {'raw': 'True'}) + + self.assertEqual(self.Model._set_option_doc_format(None, 'pdf'), {}) + # Deprecate _set_option_format in v12. + self.assertEqual(self.Model._set_option_format(None, 'pdf'), {}) + + def test_print_options(self): """ It should generate the right options dictionnary """ # TODO: None here used as report - tests here should be merged # with tests in test_printing_printer_tray from when modules merged + report = self.env['ir.actions.report'].search([], limit=1) self.assertEqual(self.Model.print_options( - None, doc_format='raw'), {'raw': 'True'} + report, doc_format='raw'), {'raw': 'True'} ) self.assertEqual(self.Model.print_options( - None, doc_format='pdf', copies=2), {'copies': '2'} + report, doc_format='pdf', copies=2), {'copies': '2'} ) self.assertEqual(self.Model.print_options( - None, doc_format='raw', copies=2), + report, doc_format='raw', copies=2), {'raw': 'True', 'copies': '2'} ) + self.assertIn('InputSlot', + self.Model.print_options(report, tray='Test').keys() + ) @mock.patch('%s.cups' % server_model) def test_print_report(self, cups): diff --git a/base_report_to_printer/tests/test_printing_printer_tray.py b/base_report_to_printer/tests/test_printing_printer_tray.py index badde3e..1be8da6 100644 --- a/base_report_to_printer/tests/test_printing_printer_tray.py +++ b/base_report_to_printer/tests/test_printing_printer_tray.py @@ -100,74 +100,6 @@ class TestPrintingPrinter(TransactionCase): }, } - def test_print_options(self): - """ - It should generate the right options dictionnary - """ - report = self.env['ir.actions.report'].search([], limit=1) - action = self.env['printing.report.xml.action'].create({ - 'user_id': self.env.user.id, - 'report_id': report.id, - 'action': 'server', - }) - user_tray = self.new_tray({ - 'system_name': 'User tray', - }) - report_tray = self.new_tray({ - 'system_name': 'Report tray', - }) - action_tray = self.new_tray({ - 'system_name': 'Action tray', - }) - - # No report passed - self.env.user.printer_tray_id = False - options = self.Model.print_options() - self.assertFalse('InputSlot' in options) - - # No tray defined - self.env.user.printer_tray_id = False - report.printer_tray_id = False - action.printer_tray_id = False - options = self.Model.print_options(report, doc_format='pdf') - self.assertFalse('InputSlot' in options) - - # Only user tray is defined - self.env.user.printer_tray_id = user_tray - report.printer_tray_id = False - action.printer_tray_id = False - options = self.Model.print_options(report, doc_format='pdf') - self.assertEquals(options, { - 'InputSlot': 'User tray', - }) - - # Only report tray is defined - self.env.user.printer_tray_id = False - report.printer_tray_id = report_tray - action.printer_tray_id = False - options = self.Model.print_options(report, doc_format='pdf') - self.assertEquals(options, { - 'InputSlot': 'Report tray', - }) - - # Only action tray is defined - self.env.user.printer_tray_id = False - report.printer_tray_id = False - action.printer_tray_id = action_tray - options = self.Model.print_options(report, doc_format='pdf') - self.assertEquals(options, { - 'InputSlot': 'Action tray', - }) - - # All trays are defined - self.env.user.printer_tray_id = user_tray - report.printer_tray_id = report_tray - action.printer_tray_id = action_tray - options = self.Model.print_options(report, doc_format='pdf') - self.assertEquals(options, { - 'InputSlot': 'Action tray', - }) - @mock.patch('%s.cups' % server_model) def test_update_printers(self, cups): """ From b63eef0453cbcfc1194bedc701dd32131bd05db7 Mon Sep 17 00:00:00 2001 From: Graeme Gellatly Date: Mon, 9 Oct 2017 02:10:42 +1300 Subject: [PATCH 5/5] [PORT] Finalize move of printer_tray to base_report_to_printer, fix tests as report param is not a string but recordset --- .../tests/test_ir_actions_report.py | 22 +++++++++---------- .../tests/test_printing_printer.py | 8 +++---- 2 files changed, 13 insertions(+), 17 deletions(-) diff --git a/base_report_to_printer/tests/test_ir_actions_report.py b/base_report_to_printer/tests/test_ir_actions_report.py index 683ba04..22c5c04 100644 --- a/base_report_to_printer/tests/test_ir_actions_report.py +++ b/base_report_to_printer/tests/test_ir_actions_report.py @@ -227,14 +227,13 @@ class TestIrActionsReportXml(TransactionCase): 'system_name': 'Tray', 'printer_id': printer.id, } - user_tray = self.new_tray({'system_name':'User tray'}, tray_vals) + user_tray = self.new_tray({'system_name': 'User tray'}, tray_vals) report_tray = self.new_tray({'system_name': 'Report tray'}, tray_vals) action_tray = self.new_tray({'system_name': 'Action tray'}, tray_vals) - # No report passed self.env.user.printer_tray_id = False - options = self.printer.print_options() + options = printer.print_options() self.assertFalse('InputSlot' in options) # No tray defined @@ -248,32 +247,31 @@ class TestIrActionsReportXml(TransactionCase): self.env.user.printer_tray_id = user_tray report.printer_tray_id = False action.printer_tray_id = False - options = report.behaviour() - self.assertIn({'tray': 'User tray'}, options) + self.assertEqual('User tray', report.behaviour()['tray']) # Only report tray is defined self.env.user.printer_tray_id = False report.printer_tray_id = report_tray action.printer_tray_id = False - self.assertIn({'tray': 'Report tray'}, report.behaviour()) + self.assertEqual('Report tray', report.behaviour()['tray']) # Only action tray is defined self.env.user.printer_tray_id = False report.printer_tray_id = False action.printer_tray_id = action_tray - self.assertIn({'tray': 'Action tray'}, report.behaviour()) + self.assertEqual('Action tray', report.behaviour()['tray']) # User and report tray defined - self.env.user.printer_tray_id = False - report.printer_tray_id = False - action.printer_tray_id = action_tray - self.assertIn({'tray': 'User tray'}, report.behaviour()) + self.env.user.printer_tray_id = user_tray + report.printer_tray_id = report_tray + action.printer_tray_id = False + self.assertEqual('Report tray', report.behaviour()['tray']) # All trays are defined self.env.user.printer_tray_id = user_tray report.printer_tray_id = report_tray action.printer_tray_id = action_tray - self.assertIn({'tray': 'Action tray'}, report.behaviour()) + self.assertEqual('Action tray', report.behaviour()['tray']) def test_onchange_printer_tray_id_empty(self): action = self.env['ir.actions.report'].new( diff --git a/base_report_to_printer/tests/test_printing_printer.py b/base_report_to_printer/tests/test_printing_printer.py index 0fec88a..bcec2be 100644 --- a/base_report_to_printer/tests/test_printing_printer.py +++ b/base_report_to_printer/tests/test_printing_printer.py @@ -50,8 +50,7 @@ class TestPrintingPrinter(TransactionCase): Noops should return an empty dict """ self.assertEqual(self.Model._set_option_action(None, 'printer'), {}) - self.assertEqual(self.Model._set_option_printer(None, self.printer), - {}) + self.assertEqual(self.Model._set_option_printer(None, self.Model), {}) def test_option_doc_format(self): """ @@ -82,9 +81,8 @@ class TestPrintingPrinter(TransactionCase): report, doc_format='raw', copies=2), {'raw': 'True', 'copies': '2'} ) - self.assertIn('InputSlot', - self.Model.print_options(report, tray='Test').keys() - ) + self.assertTrue('InputSlot' in self.Model.print_options(report, + tray='Test')) @mock.patch('%s.cups' % server_model) def test_print_report(self, cups):