'Testing consume quants and produced quants with workorder'

def test_00_workorder_process(self):

    dining_table = self.env.ref('mrp.product_product_computer_desk')
    product_table_sheet = self.env.ref('mrp.product_product_computer_desk_head')
    product_table_leg = self.env.ref('mrp.product_product_computer_desk_leg')
    product_bolt = self.env.ref('mrp.product_product_computer_desk_bolt')
    production_table = self.env['mrp.production'].create({'product_id': dining_table.id, 'product_qty': 1.0, 'product_uom_id': dining_table.uom_id.id, 'bom_id': self.ref('mrp.mrp_bom_desk')})
    dining_table.tracking = 'lot'
    product_table_sheet.tracking = 'lot'
    product_table_leg.tracking = 'lot'
    product_bolt.tracking = 'lot'
    lot_sheet = self.env['stock.production.lot'].create({'product_id': product_table_sheet.id})
    lot_leg = self.env['stock.production.lot'].create({'product_id': product_table_leg.id})
    lot_bolt = self.env['stock.production.lot'].create({'product_id': product_bolt.id})
    inventory = self.env['stock.inventory'].create({'name': 'Inventory    Product    Table', 'filter': 'partial', 'line_ids': [(0, 0, {'product_id': product_table_sheet.id, 'product_uom_id': product_table_sheet.uom_id.id, 'product_qty': 20, 'prod_lot_id': lot_sheet.id, 'location_id': self.source_location_id}), (0, 0, {'product_id': product_table_leg.id, 'product_uom_id': product_table_leg.uom_id.id, 'product_qty': 20, 'prod_lot_id': lot_leg.id, 'location_id': self.source_location_id}), (0, 0, {'product_id': product_bolt.id, 'product_uom_id': product_bolt.uom_id.id, 'product_qty': 20, 'prod_lot_id': lot_bolt.id, 'location_id': self.source_location_id})]})
    inventory.action_done()
    production_table.button_plan()
    self.assertEqual(len(production_table.workorder_ids), 3)
    workorders = production_table.workorder_ids
    self.assertEqual(workorders[0].state, 'ready', 'First    workorder    state    should    be    ready.')
    self.assertEqual(workorders[1].state, 'pending')
    self.assertEqual(workorders[2].state, 'pending')
    finished_lot = self.env['stock.production.lot'].create({'product_id': production_table.product_id.id})
    workorders[0].write({'final_lot_id': finished_lot.id})
    workorders[0].button_start()
    workorders[0].active_move_lot_ids[0].write({'lot_id': lot_sheet.id, 'quantity_done': 1})
    self.assertEqual(workorders[0].state, 'progress')
    workorders[0].record_production()
    self.assertEqual(workorders[0].state, 'done')
    move_table_sheet = production_table.move_raw_ids.filtered((lambda x: (x.product_id == product_table_sheet)))
    self.assertEqual(move_table_sheet.quantity_done, 1)
    workorders[1].button_start()
    workorders[1].active_move_lot_ids[0].write({'lot_id': lot_leg.id, 'quantity_done': 4})
    workorders[1].record_production()
    move_leg = production_table.move_raw_ids.filtered((lambda x: (x.product_id == product_table_leg)))
    self.assertEqual(workorders[1].state, 'done')
    self.assertEqual(move_leg.quantity_done, 4)
    finish_move = production_table.move_finished_ids.filtered((lambda x: (x.product_id.id == dining_table.id)))
    workorders[2].button_start()
    move_lot = workorders[2].active_move_lot_ids[0]
    move_lot.write({'lot_id': lot_bolt.id, 'quantity_done': 4})
    move_table_bolt = production_table.move_raw_ids.filtered((lambda x: (x.product_id.id == product_bolt.id)))
    workorders[2].record_production()
    self.assertEqual(workorders[2].state, 'done')
    self.assertEqual(move_table_bolt.quantity_done, 4)
    production_table.button_mark_done()
    self.assertEqual(sum(move_table_sheet.quant_ids.mapped('qty')), 1, ('Wrong    quantity    of    consumed    product    %s' % move_table_sheet.product_id.name))
    self.assertEqual(sum(move_leg.quant_ids.mapped('qty')), 4, ('Wrong    quantity    of    consumed    product    %s' % move_leg.product_id.name))
    self.assertEqual(sum(move_table_bolt.quant_ids.mapped('qty')), 4, ('Wrong    quantity    of    consumed    product    %s' % move_table_bolt.product_id.name))
    consume_quants = ((move_table_sheet.quant_ids + move_leg.quant_ids) + move_table_bolt.quant_ids)
    finish_move = production_table.move_finished_ids.filtered((lambda x: (x.product_id.id == dining_table.id)))
    finished_quant = finish_move.quant_ids[0]
    for quant in consume_quants:
        self.assertEqual(len(quant.produced_quant_ids), 1)
        self.assertEqual(quant.produced_quant_ids[0].lot_id.id, finished_lot.id)
        self.assertEqual(quant.produced_quant_ids[0].id, finished_quant.id)
    self.assertEqual(finished_quant.consumed_quant_ids, consume_quants)
