# -*- coding: utf-8 -*-
##############################################################################
# For copyright and license notices, see __openerp__.py file in root directory
##############################################################################
import openerp.tests.common as common
from openerp import workflow, exceptions


class TestMrpOperationsExtension(common.TransactionCase):

    def setUp(self):
        super(TestMrpOperationsExtension, self).setUp()
        self.production_model = self.env['mrp.production']
        self.work_order_produce_model = self.env['mrp.work.order.produce']
        self.produce_line_model = self.env['mrp.product.produce.line']
        self.production = self.production_model.browse(
            self.env.ref('mrp_operations_extension.mrp_production_opeext').id)

    def test_confirm_production_operation_extension_case1(self):
        workflow.trg_validate(self.uid, 'mrp.production', self.production.id,
                              'button_confirm', self.cr)
        self.production.force_production()
        for line in self.production.workcenter_lines:
            workflow.trg_validate(self.uid, 'mrp.production.workcenter.line',
                                  line.id, 'button_start_working', self.cr)
            self.assertEqual(
                line.state, 'startworking',
                'Error work center line not in start working state')
            consume = self.work_order_produce_model.with_context(
                active_ids=[line.id], active_id=line.id).create({})
            result = consume.with_context(
                active_ids=[line.id], active_id=line.id).on_change_qty(
                consume.product_qty, [])
            if 'value' in result:
                if ('consume_lines' in result['value'] and
                        result['value']['consume_lines']):
                    for cl in result['value']['consume_lines']:
                        consu_vals = cl[2]
                        consu_vals['work_produce_id'] = consume.id
                        self.produce_line_model.create(consu_vals)
            if not consume.final_product:
                consume.with_context(active_id=line.id).do_consume()
            else:
                consume.with_context(active_id=line.id).do_consume_produce()
            workflow.trg_validate(
                self.uid, 'mrp.production.workcenter.line', line.id,
                'button_done', self.cr)
            self.assertEqual(
                line.state, 'done',
                'Error work center line not in done state')
        self.assertEqual(
            self.production.state, 'done',
            'Error MRP production not in done state')

    def test_confirm_production_operation_extension_case2(self):
        workflow.trg_validate(self.uid, 'mrp.production', self.production.id,
                              'button_confirm', self.cr)
        self.production.force_production()
        with self.assertRaises(exceptions.Warning):
            workflow.trg_validate(
                self.uid, 'mrp.production.workcenter.line',
                self.production.workcenter_lines[1].id, 'button_start_working',
                self.cr)

@@ -810,16 +810,126 @@
 xt').id)
+%0A        self.production_case1 = self.production.copy()%0A        self.production_case2 = self.production.copy()
 %0A%0A    de
@@ -1009,32 +1009,45 @@
 ow.trg_validate(
+%0A            
 self.uid, 'mrp.p
@@ -1073,32 +1073,27 @@
 oduction
-.id,%0A           
+_case1.id,%0A
         
@@ -1088,39 +1088,32 @@
 id,%0A            
-       
 'button_confirm'
@@ -1138,32 +1138,38 @@
  self.production
+_case1
 .force_productio
@@ -1199,32 +1199,38 @@
  self.production
+_case1
 .workcenter_line
@@ -2741,16 +2741,22 @@
 oduction
+_case1
 .state, 
@@ -2755,32 +2755,32 @@
 .state, 'done',%0A
-
             'Err
@@ -2905,32 +2905,45 @@
 ow.trg_validate(
+%0A            
 self.uid, 'mrp.p
@@ -2973,31 +2973,19 @@
 tion
-.id,%0A                  
+_case2.id,%0A
     
@@ -3042,16 +3042,22 @@
 oduction
+_case2
 .force_p
@@ -3238,32 +3238,38 @@
  self.production
+_case2
 .workcenter_line
@@ -3272,24 +3272,40 @@
 lines%5B1%5D.id,
+%0A               
  'button_sta
@@ -3316,32 +3316,16 @@
 orking',
-%0A               
  self.cr

Fix travis error.