Best Python code snippet using yandex-tank
test_order.py
Source:test_order.py  
...128            'active_ids': [man_order.id],129        }))130        produce_form.product_qty = 1.0131        produce_wizard = produce_form.save()132        produce_wizard.do_produce()133        # man_order.button_mark_done()134        man_order.button_mark_done()135        self.assertEqual(man_order.state, 'done', "Production order should be in done state.")136    def test_explode_from_order(self):137        #138        # bom3 produces 2 Dozen of Doors (p6), aka 24139        # To produce 24 Units of Doors (p6)140        # - 2 Units of Tools (p5) -> need 4141        # - 8 Dozen of Sticks (p4) -> need 16142        # - 12 Units of Wood (p2) -> need 24143        # bom2 produces 1 Unit of Sticks (p4)144        # To produce 1 Unit of Sticks (p4)145        # - 2 Dozen of Sticks (p4) -> need 8146        # - 3 Dozen of Stones (p3) -> need 12147        # Update capacity, start time, stop time, and time efficiency.148        # ------------------------------------------------------------149        self.workcenter_1.write({'capacity': 1, 'time_start': 0, 'time_stop': 0, 'time_efficiency': 100})150        # Set manual time cycle 20 and 10.151        # --------------------------------152        self.operation_1.write({'time_cycle_manual': 20})153        (self.operation_2 | self.operation_3).write({'time_cycle_manual': 10})154        man_order = self.env['mrp.production'].create({155            'name': 'MO-Test',156            'product_id': self.product_6.id,157            'product_uom_id': self.product_6.uom_id.id,158            'product_qty': 48,159            'bom_id': self.bom_3.id,160        })161        # reset quantities162        self.product_1.type = "product"163        self.env['stock.change.product.qty'].create({164            'product_id': self.product_1.id,165            'new_quantity': 0.0,166            'location_id': self.warehouse_1.lot_stock_id.id,167        }).change_product_qty()168        (self.product_2 | self.product_4).write({169            'tracking': 'none',170        })171        # assign consume material172        man_order.action_assign()173        self.assertEqual(man_order.availability, 'waiting', "Production order should be in waiting state.")174        # check consume materials of manufacturing order175        self.assertEqual(len(man_order.move_raw_ids), 4, "Consume material lines are not generated proper.")176        product_2_consume_moves = man_order.move_raw_ids.filtered(lambda x: x.product_id == self.product_2)177        product_3_consume_moves = man_order.move_raw_ids.filtered(lambda x: x.product_id == self.product_3)178        product_4_consume_moves = man_order.move_raw_ids.filtered(lambda x: x.product_id == self.product_4)179        product_5_consume_moves = man_order.move_raw_ids.filtered(lambda x: x.product_id == self.product_5)180        consume_qty_2 = product_2_consume_moves.product_uom_qty181        self.assertEqual(consume_qty_2, 24.0, "Consume material quantity of Wood should be 24 instead of %s" % str(consume_qty_2))182        consume_qty_3 = product_3_consume_moves.product_uom_qty183        self.assertEqual(consume_qty_3, 12.0, "Consume material quantity of Stone should be 12 instead of %s" % str(consume_qty_3))184        self.assertEqual(len(product_4_consume_moves), 2, "Consume move are not generated proper.")185        for consume_moves in product_4_consume_moves:186            consume_qty_4 = consume_moves.product_uom_qty187            self.assertIn(consume_qty_4, [8.0, 16.0], "Consume material quantity of Stick should be 8 or 16 instead of %s" % str(consume_qty_4))188        self.assertFalse(product_5_consume_moves, "Move should not create for phantom bom")189        # create required lots190        lot_product_2 = self.env['stock.production.lot'].create({'product_id': self.product_2.id})191        lot_product_4 = self.env['stock.production.lot'].create({'product_id': self.product_4.id})192        # refuel stock193        inventory = self.env['stock.inventory'].create({194            'name': 'Inventory For Product C',195            'filter': 'partial',196            'line_ids': [(0, 0, {197                'product_id': self.product_2.id,198                'product_uom_id': self.product_2.uom_id.id,199                'product_qty': 30,200                'prod_lot_id': lot_product_2.id,201                'location_id': self.ref('stock.stock_location_14')202            }), (0, 0, {203                'product_id': self.product_3.id,204                'product_uom_id': self.product_3.uom_id.id,205                'product_qty': 60,206                'location_id': self.ref('stock.stock_location_14')207            }), (0, 0, {208                'product_id': self.product_4.id,209                'product_uom_id': self.product_4.uom_id.id,210                'product_qty': 60,211                'prod_lot_id': lot_product_4.id,212                'location_id': self.ref('stock.stock_location_14')213            })]214        })215        inventory.action_start()216        inventory.action_validate()217        # re-assign consume material218        man_order.action_assign()219        # Check production order status after assign.220        self.assertEqual(man_order.availability, 'assigned', "Production order should be in assigned state.")221        # Plan production order.222        man_order.button_plan()223        # check workorders224        # - main bom: Door: 2 operations225        #   operation 1: Cutting226        #   operation 2: Welding, waiting for the previous one227        # - kit bom: Stone Tool: 1 operation228        #   operation 1: Gift Wrapping229        workorders = man_order.workorder_ids230        kit_wo = man_order.workorder_ids.filtered(lambda wo: wo.operation_id == self.operation_1)231        door_wo_1 = man_order.workorder_ids.filtered(lambda wo: wo.operation_id == self.operation_2)232        door_wo_2 = man_order.workorder_ids.filtered(lambda wo: wo.operation_id == self.operation_3)233        for workorder in workorders:234            self.assertEqual(workorder.workcenter_id, self.workcenter_1, "Workcenter does not match.")235        self.assertEqual(kit_wo.state, 'ready', "Workorder should be in ready state.")236        self.assertEqual(door_wo_1.state, 'ready', "Workorder should be in ready state.")237        self.assertEqual(door_wo_2.state, 'pending', "Workorder should be in pending state.")238        self.assertEqual(kit_wo.duration_expected, 80, "Workorder duration should be 80 instead of %s." % str(kit_wo.duration_expected))239        self.assertEqual(door_wo_1.duration_expected, 20, "Workorder duration should be 20 instead of %s." % str(door_wo_1.duration_expected))240        self.assertEqual(door_wo_2.duration_expected, 20, "Workorder duration should be 20 instead of %s." % str(door_wo_2.duration_expected))241        # subbom: kit for stone tools242        kit_wo.button_start()243        finished_lot = self.env['stock.production.lot'].create({'product_id': man_order.product_id.id})244        kit_wo.write({245            'final_lot_id': finished_lot.id,246            'qty_producing': 48247        })248        kit_wo.record_production()249        self.assertEqual(kit_wo.state, 'done', "Workorder should be in done state.")250        # first operation of main bom251        finished_lot = self.env['stock.production.lot'].create({'product_id': man_order.product_id.id})252        door_wo_1.write({253            'final_lot_id': finished_lot.id,254            'qty_producing': 48255        })256        door_wo_1.record_production()257        self.assertEqual(door_wo_1.state, 'done', "Workorder should be in done state.")258        # second operation of main bom259        self.assertEqual(door_wo_2.state, 'ready', "Workorder should be in ready state.")260        door_wo_2.record_production()261        self.assertEqual(door_wo_2.state, 'done', "Workorder should be in done state.")262    def test_production_avialability(self):263        """264            Test availability of production order.265        """266        self.bom_3.bom_line_ids.filtered(lambda x: x.product_id == self.product_5).unlink()267        self.bom_3.bom_line_ids.filtered(lambda x: x.product_id == self.product_4).unlink()268        production_2 = self.env['mrp.production'].create({269            'name': 'MO-Test001',270            'product_id': self.product_6.id,271            'product_qty': 5.0,272            'bom_id': self.bom_3.id,273            'product_uom_id': self.product_6.uom_id.id,274        })275        production_2.action_assign()276        # check sub product availability state is waiting277        self.assertEqual(production_2.availability, 'waiting', 'Production order should be availability for waiting state')278        # Update Inventory279        inventory_wizard = self.env['stock.change.product.qty'].create({280            'product_id': self.product_2.id,281            'new_quantity': 2.0,282        })283        inventory_wizard.change_product_qty()284        production_2.action_assign()285        # check sub product availability state is partially available286        self.assertEqual(production_2.availability, 'partially_available', 'Production order should be availability for partially available state')287        # Update Inventory288        inventory_wizard = self.env['stock.change.product.qty'].create({289            'product_id': self.product_2.id,290            'new_quantity': 5.0,291        })292        inventory_wizard.change_product_qty()293        production_2.action_assign()294        # check sub product availability state is assigned295        self.assertEqual(production_2.availability, 'assigned', 'Production order should be availability for assigned state')296    def test_empty_routing(self):297        """ Check what happens when you work with an empty routing"""298        routing = self.env['mrp.routing'].create({'name': 'Routing without operations',299                                        'location_id': self.warehouse_1.wh_input_stock_loc_id.id,})300        self.bom_3.routing_id = routing.id301        production = self.env['mrp.production'].create({'name': 'MO test',302                                           'product_id': self.product_6.id,303                                           'product_qty': 3,304                                           'bom_id': self.bom_3.id,305                                           'product_uom_id': self.product_6.uom_id.id,})306        self.assertEqual(production.routing_id.id, False, 'The routing field should be empty on the mo')307        self.assertEqual(production.move_raw_ids[0].location_id.id, self.warehouse_1.wh_input_stock_loc_id.id, 'Raw moves start location should have altered.')308    def test_split_move_line(self):309        """ Consume more component quantity than the initial demand.310        It should create extra move and share the quantity between the two stock311        moves """312        mo, _, p_final, p1, p2 = self.generate_mo(qty_base_1=10, qty_final=1, qty_base_2=1)313        mo.action_assign()314        produce_form = Form(self.env['mrp.product.produce'].with_context({315            'active_id': mo.id,316            'active_ids': [mo.id],317        }))318        for i in range(len(produce_form.produce_line_ids)):319            with produce_form.produce_line_ids.edit(i) as line:320                line.qty_done += 1321        product_produce = produce_form.save()322        product_produce.do_produce()323        self.assertEqual(len(mo.move_raw_ids), 2)324        self.assertEqual(len(mo.move_raw_ids.mapped('move_line_ids')), 2)325        self.assertEqual(mo.move_raw_ids[0].move_line_ids.mapped('qty_done'), [2])326        self.assertEqual(mo.move_raw_ids[1].move_line_ids.mapped('qty_done'), [11])327        self.assertEqual(mo.move_raw_ids[0].quantity_done, 2)328        self.assertEqual(mo.move_raw_ids[1].quantity_done, 11)329        mo.button_mark_done()330        self.assertEqual(len(mo.move_raw_ids), 4)331        self.assertEqual(len(mo.move_raw_ids.mapped('move_line_ids')), 4)332        self.assertEqual(mo.move_raw_ids.mapped('quantity_done'), [1, 10, 1, 1])333        self.assertEqual(mo.move_raw_ids.mapped('move_line_ids.qty_done'), [1, 10, 1, 1])334    def test_multiple_post_inventory(self):335        """ Check the consumed quants of the produced quants when intermediate calls to `post_inventory` during a MO."""336        # create a bom for `custom_laptop` with components that aren't tracked337        unit = self.ref("uom.product_uom_unit")338        custom_laptop = self.env.ref("product.product_product_27")339        custom_laptop.tracking = 'none'340        product_charger = self.env['product.product'].create({341            'name': 'Charger',342            'type': 'product',343            'uom_id': unit,344            'uom_po_id': unit})345        product_keybord = self.env['product.product'].create({346            'name': 'Usb Keybord',347            'type': 'product',348            'uom_id': unit,349            'uom_po_id': unit})350        bom_custom_laptop = self.env['mrp.bom'].create({351            'product_tmpl_id': custom_laptop.product_tmpl_id.id,352            'product_qty': 1,353            'product_uom_id': unit,354            'bom_line_ids': [(0, 0, {355                'product_id': product_charger.id,356                'product_qty': 1,357                'product_uom_id': unit358            }), (0, 0, {359                'product_id': product_keybord.id,360                'product_qty': 1,361                'product_uom_id': unit362            })]363        })364        # put the needed products in stock365        source_location_id = self.ref('stock.stock_location_14')366        inventory = self.env['stock.inventory'].create({367            'name': 'Inventory Product Table',368            'filter': 'partial',369            'line_ids': [(0, 0, {370                'product_id': product_charger.id,371                'product_uom_id': product_charger.uom_id.id,372                'product_qty': 2,373                'location_id': source_location_id374            }), (0, 0, {375                'product_id': product_keybord.id,376                'product_uom_id': product_keybord.uom_id.id,377                'product_qty': 2,378                'location_id': source_location_id379            })]380        })381        inventory.action_validate()382        # create a mo for this bom383        mo_custom_laptop = self.env['mrp.production'].create({384            'product_id': custom_laptop.id,385            'product_qty': 2,386            'product_uom_id': unit,387            'bom_id': bom_custom_laptop.id388        })389        mo_custom_laptop.action_assign()390        self.assertEqual(mo_custom_laptop.availability, 'assigned')391        # produce one item, call `post_inventory`392        context = {"active_ids": [mo_custom_laptop.id], "active_id": mo_custom_laptop.id}393        produce_form = Form(self.env['mrp.product.produce'].with_context(context))394        produce_form.product_qty = 1.00395        custom_laptop_produce = produce_form.save()396        custom_laptop_produce.do_produce()397        mo_custom_laptop.post_inventory()398        # check the consumed quants of the produced quant399        first_move = mo_custom_laptop.move_finished_ids.filtered(lambda mo: mo.state == 'done')400        second_move = mo_custom_laptop.move_finished_ids.filtered(lambda mo: mo.state == 'confirmed')401        # produce the second item, call `post_inventory`402        context = {"active_ids": [mo_custom_laptop.id], "active_id": mo_custom_laptop.id}403        produce_form = Form(self.env['mrp.product.produce'].with_context(context))404        produce_form.product_qty = 1.00405        custom_laptop_produce = produce_form.save()406        custom_laptop_produce.do_produce()407        mo_custom_laptop.post_inventory()408    def test_update_quantity_3(self):409        """ Build 1 final products then update the Manufacturing410        order quantity. Check the remaining quantity to produce411        take care of the first quantity produced."""412        self.stock_location = self.env.ref('stock.stock_location_stock')413        mo, bom, p_final, p1, p2 = self.generate_mo(qty_final=2)414        self.assertEqual(len(mo), 1, 'MO should have been created')415        self.env['stock.quant']._update_available_quantity(p1, self.stock_location, 20)416        self.env['stock.quant']._update_available_quantity(p2, self.stock_location, 5)417        mo.action_assign()418        produce_form = Form(self.env['mrp.product.produce'].with_context({419            'active_id': mo.id,420            'active_ids': [mo.id],421        }))422        produce_form.product_qty = 1423        produce_wizard = produce_form.save()424        produce_wizard.do_produce()425        update_quantity_wizard = self.env['change.production.qty'].create({426            'mo_id': mo.id,427            'product_qty': 3,428        })429        update_quantity_wizard.change_prod_qty()430        produce_form = Form(self.env['mrp.product.produce'].with_context({431            'active_id': mo.id,432            'active_ids': [mo.id],433        }))434        produce_wizard = produce_form.save()435        produce_wizard.do_produce()436        mo.button_mark_done()437        self.assertEqual(sum(mo.move_raw_ids.filtered(lambda m: m.product_id == p1).mapped('quantity_done')), 12)438        self.assertEqual(sum(mo.move_finished_ids.mapped('quantity_done')), 3)439    def test_rounding(self):440        """ In previous versions we had rounding and efficiency fields.  We check if we can still do the same, but with only the rounding on the UoM"""441        self.product_6.uom_id.rounding = 1.0442        bom_eff = self.env['mrp.bom'].create({'product_id': self.product_6.id,443                                    'product_tmpl_id': self.product_6.product_tmpl_id.id,444                                    'product_qty': 1,445                                    'product_uom_id': self.product_6.uom_id.id,446                                    'type': 'normal',447                                    'bom_line_ids': [448                                        (0, 0, {'product_id': self.product_2.id, 'product_qty': 2.03}),449                                        (0, 0, {'product_id': self.product_8.id, 'product_qty': 4.16})450                                        ]})451        production = self.env['mrp.production'].create({'name': 'MO efficiency test',452                                           'product_id': self.product_6.id,453                                           'product_qty': 20,454                                           'bom_id': bom_eff.id,455                                           'product_uom_id': self.product_6.uom_id.id,})456        #Check the production order has the right quantities457        self.assertEqual(production.move_raw_ids[0].product_qty, 41, 'The quantity should be rounded up')458        self.assertEqual(production.move_raw_ids[1].product_qty, 84, 'The quantity should be rounded up')459        # produce product460        produce_form = Form(self.env['mrp.product.produce'].with_context({461            'active_id': production.id,462            'active_ids': [production.id],463        }))464        produce_form.product_qty = 8465        produce_wizard = produce_form.save()466        produce_wizard.do_produce()467        self.assertEqual(production.move_raw_ids[0].quantity_done, 16, 'Should use half-up rounding when producing')468        self.assertEqual(production.move_raw_ids[1].quantity_done, 34, 'Should use half-up rounding when producing')469    def test_product_produce_1(self):470        """ Check that no produce line are created when the consumed products are not tracked """471        self.stock_location = self.env.ref('stock.stock_location_stock')472        mo, bom, p_final, p1, p2 = self.generate_mo()473        self.assertEqual(len(mo), 1, 'MO should have been created')474        self.env['stock.quant']._update_available_quantity(p1, self.stock_location, 100)475        self.env['stock.quant']._update_available_quantity(p2, self.stock_location, 5)476        mo.action_assign()477        produce_form = Form(self.env['mrp.product.produce'].with_context({478            'active_id': mo.id,479            'active_ids': [mo.id],480        }))481        product_produce = produce_form.save()482        product_produce.do_produce()483        self.assertEqual(len(product_produce.produce_line_ids), 2, 'You should have produce lines even the consumed products are not tracked.')484    def test_product_produce_2(self):485        """ Check that line are created when the consumed products are486        tracked by serial and the lot proposed are correct. """487        self.stock_location = self.env.ref('stock.stock_location_stock')488        mo, bom, p_final, p1, p2 = self.generate_mo(tracking_base_1='serial', qty_base_1=1, qty_final=2)489        self.assertEqual(len(mo), 1, 'MO should have been created')490        lot_p1_1 = self.env['stock.production.lot'].create({491            'name': 'lot1',492            'product_id': p1.id,493        })494        lot_p1_2 = self.env['stock.production.lot'].create({495            'name': 'lot2',496            'product_id': p1.id,497        })498        self.env['stock.quant']._update_available_quantity(p1, self.stock_location, 1, lot_id=lot_p1_1)499        self.env['stock.quant']._update_available_quantity(p1, self.stock_location, 1, lot_id=lot_p1_2)500        self.env['stock.quant']._update_available_quantity(p2, self.stock_location, 5)501        mo.action_assign()502        produce_form = Form(self.env['mrp.product.produce'].with_context({503            'active_id': mo.id,504            'active_ids': [mo.id],505        }))506        product_produce = produce_form.save()507        self.assertEqual(len(product_produce.produce_line_ids), 3, 'You should have 3 produce lines. One for each serial to consume')508        product_produce.product_qty = 1509        produce_line_1 = product_produce.produce_line_ids[0]510        produce_line_1.qty_done = 1511        remaining_lot = (lot_p1_1 | lot_p1_2) - produce_line_1.lot_id512        product_produce.do_produce()513        produce_form = Form(self.env['mrp.product.produce'].with_context({514            'active_id': mo.id,515            'active_ids': [mo.id],516        }))517        product_produce = produce_form.save()518        self.assertEqual(len(product_produce.produce_line_ids), 2, 'You should have 2 produce lines since one has already be consumed.')519        for line in product_produce.produce_line_ids.filtered(lambda x: x.lot_id):520            self.assertEqual(line.lot_id, remaining_lot, 'Wrong lot proposed.')521    def test_product_produce_3(self):522        """ Check that line are created when the consumed products are523        tracked by serial and the lot proposed are correct. """524        self.stock_location = self.env.ref('stock.stock_location_stock')525        self.stock_shelf_1 = self.env.ref('stock.stock_location_components')526        self.stock_shelf_2 = self.env.ref('stock.stock_location_14')527        mo, _, p_final, p1, p2 = self.generate_mo(tracking_base_1='lot', qty_base_1=10, qty_final=1)528        self.assertEqual(len(mo), 1, 'MO should have been created')529        first_lot_for_p1 = self.env['stock.production.lot'].create({530            'name': 'lot1',531            'product_id': p1.id,532        })533        second_lot_for_p1 = self.env['stock.production.lot'].create({534            'name': 'lot2',535            'product_id': p1.id,536        })537        final_product_lot = self.env['stock.production.lot'].create({538            'name': 'lot1',539            'product_id': p_final.id,540        })541        self.env['stock.quant']._update_available_quantity(p1, self.stock_shelf_1, 3, lot_id=first_lot_for_p1)542        self.env['stock.quant']._update_available_quantity(p1, self.stock_shelf_2, 3, lot_id=first_lot_for_p1)543        self.env['stock.quant']._update_available_quantity(p1, self.stock_location, 8, lot_id=second_lot_for_p1)544        self.env['stock.quant']._update_available_quantity(p2, self.stock_location, 5)545        mo.action_assign()546        produce_form = Form(self.env['mrp.product.produce'].with_context({547            'active_id': mo.id,548            'active_ids': [mo.id],549        }))550        produce_form.product_qty = 1.0551        for i in range(len(produce_form.produce_line_ids)):552            with produce_form.produce_line_ids.edit(i) as line:553                line.qty_done += 1554        product_produce = produce_form.save()555        product_produce.lot_id = final_product_lot.id556        # product 1 lot 1 shelf1557        # product 1 lot 1 shelf2558        # product 1 lot 2559        self.assertEqual(len(product_produce.produce_line_ids), 4, 'You should have 4 produce lines. lot 1 shelf_1, lot 1 shelf_2, lot2 and for product which have tracking None')560        product_produce.do_produce()561        move_1 = mo.move_raw_ids.filtered(lambda m: m.product_id == p1)562        # qty_done/product_uom_qty lot563        # 3/3 lot 1 shelf 1564        # 1/1 lot 1 shelf 2565        # 2/2 lot 1 shelf 2566        # 2/0 lot 1 other567        # 5/4 lot 2568        ml_to_shelf_1 = move_1.move_line_ids.filtered(lambda ml: ml.lot_id == first_lot_for_p1 and ml.location_id == self.stock_shelf_1)569        ml_to_shelf_2 = move_1.move_line_ids.filtered(lambda ml: ml.lot_id == first_lot_for_p1 and ml.location_id == self.stock_shelf_2)570        self.assertEqual(sum(ml_to_shelf_1.mapped('qty_done')), 3.0, '3 units should be took from shelf1 as reserved.')571        self.assertEqual(sum(ml_to_shelf_2.mapped('qty_done')), 3.0, '3 units should be took from shelf2 as reserved.')572        self.assertEqual(move_1.quantity_done, 13, 'You should have used the tem units.')573        mo.button_mark_done()574        self.assertEqual(mo.state, 'done', "Production order should be in done state.")575    def test_product_produce_4(self):576        """ Possibility to produce with a given raw material in multiple locations. """577        self.stock_location = self.env.ref('stock.stock_location_stock')578        self.stock_shelf_1 = self.env.ref('stock.stock_location_components')579        self.stock_shelf_2 = self.env.ref('stock.stock_location_14')580        mo, _, p_final, p1, p2 = self.generate_mo(qty_final=1, qty_base_1=5)581        self.env['stock.quant']._update_available_quantity(p1, self.stock_shelf_1, 2)582        self.env['stock.quant']._update_available_quantity(p1, self.stock_shelf_2, 3)583        self.env['stock.quant']._update_available_quantity(p2, self.stock_location, 1)584        mo.action_assign()585        ml_p1 = mo.move_raw_ids.filtered(lambda x: x.product_id == p1).mapped('move_line_ids')586        ml_p2 = mo.move_raw_ids.filtered(lambda x: x.product_id == p2).mapped('move_line_ids')587        self.assertEqual(len(ml_p1), 2)588        self.assertEqual(len(ml_p2), 1)589        # Add some quantity already done to force an extra move line to be created590        ml_p1[0].qty_done = 1.0591        # Produce baby!592        product_produce = self.env['mrp.product.produce'].with_context({593            'active_id': mo.id,594            'active_ids': [mo.id],595        }).create({596            'product_qty': 1.0,597        })598        product_produce._onchange_product_qty()599        product_produce.do_produce()600        ml_p1 = mo.move_raw_ids.filtered(lambda x: x.product_id == p1).mapped('move_line_ids')601        self.assertEqual(len(ml_p1), 4)602        for ml in ml_p1:603            self.assertIn(ml.qty_done, [1.0, 2.0], 'Quantity done should be 1.0, 2.0 or 3.0')604        self.assertEqual(sum(ml_p1.mapped('qty_done')), 6.0, 'Total qty consumed should be 6.0')605        self.assertEqual(sum(ml_p1.mapped('product_uom_qty')), 5.0, 'Total qty reserved should be 5.0')606        mo.button_mark_done()607        self.assertEqual(mo.state, 'done', "Production order should be in done state.")608    def test_product_produce_5(self):609        """ Build 5 final products with different consumed lots,610        then edit the finished quantity and update the Manufacturing611        order quantity. Then check if the produced quantity do not612        change and it is possible to close the MO.613        """614        self.stock_location = self.env.ref('stock.stock_location_stock')615        mo, bom, p_final, p1, p2 = self.generate_mo(tracking_base_1='lot')616        self.assertEqual(len(mo), 1, 'MO should have been created')617        lot_1 = self.env['stock.production.lot'].create({618            'name': 'lot1',619            'product_id': p1.id,620        })621        lot_2 = self.env['stock.production.lot'].create({622            'name': 'lot2',623            'product_id': p1.id,624        })625        self.env['stock.quant']._update_available_quantity(p1, self.stock_location, 10, lot_id=lot_1)626        self.env['stock.quant']._update_available_quantity(p1, self.stock_location, 10, lot_id=lot_2)627        self.env['stock.quant']._update_available_quantity(p2, self.stock_location, 5)628        mo.action_assign()629        produce_wizard = self.env['mrp.product.produce'].with_context({630            'active_id': mo.id,631            'active_ids': [mo.id],632        }).create({633            'product_qty': 5.0,634        })635        produce_wizard._onchange_product_qty()636        for produce_line in produce_wizard.produce_line_ids:637            produce_line.qty_done = produce_line.qty_to_consume638        produce_wizard.do_produce()639        mo.move_finished_ids.move_line_ids.qty_done -= 1640        update_quantity_wizard = self.env['change.production.qty'].create({641            'mo_id': mo.id,642            'product_qty': 4,643        })644        update_quantity_wizard.change_prod_qty()645        self.assertEqual(mo.move_raw_ids.filtered(lambda m: m.product_id == p1).quantity_done, 20, 'Update the produce quantity should not impact already produced quantity.')646        mo.button_mark_done()647    def test_product_produce_6(self):648        """ Plan 5 finished products, reserve and produce 3. Post the current production.649        Simulate an unlock and edit and, on the opened moves, set the consumed quantity650        to 3. Now, try to update the quantity to produce to 3. It should fail since there651        are consumed quantities"""652        self.stock_location = self.env.ref('stock.stock_location_stock')653        mo, bom, p_final, p1, p2 = self.generate_mo()654        self.assertEqual(len(mo), 1, 'MO should have been created')655        self.env['stock.quant']._update_available_quantity(p1, self.stock_location, 20)656        self.env['stock.quant']._update_available_quantity(p2, self.stock_location, 5)657        mo.action_assign()658        produce_wizard = self.env['mrp.product.produce'].with_context({659            'active_id': mo.id,660            'active_ids': [mo.id],661        }).create({662            'product_qty': 3.0,663        })664        produce_wizard._onchange_product_qty()665        produce_wizard.do_produce()666        mo.post_inventory()667        self.assertEqual(len(mo.move_raw_ids), 4)668        mo.move_raw_ids.filtered(lambda m: m.state != 'done')[0].quantity_done = 3669        update_quantity_wizard = self.env['change.production.qty'].create({670            'mo_id': mo.id,671            'product_qty': 3,672        })673        with self.assertRaises(UserError):674            update_quantity_wizard.change_prod_qty()675    def test_product_produce_6bis(self):676        """ Plan 5 finished products, reserve and produce 3. Post the current production.677        update the quantity to produce to 3. It should unlink the extra reserved moves"""678        self.stock_location = self.env.ref('stock.stock_location_stock')679        mo, bom, p_final, p1, p2 = self.generate_mo()680        self.assertEqual(len(mo), 1, 'MO should have been created')681        self.env['stock.quant']._update_available_quantity(p1, self.stock_location, 20)682        self.env['stock.quant']._update_available_quantity(p2, self.stock_location, 5)683        mo.action_assign()684        produce_wizard = self.env['mrp.product.produce'].with_context({685            'active_id': mo.id,686            'active_ids': [mo.id],687        }).create({688            'product_qty': 3.0,689        })690        produce_wizard._onchange_product_qty()691        produce_wizard.do_produce()692        mo.post_inventory()693        self.assertEqual(len(mo.move_raw_ids), 4)694        update_quantity_wizard = self.env['change.production.qty'].create({695            'mo_id': mo.id,696            'product_qty': 3,697        })698        update_quantity_wizard.change_prod_qty()699        self.assertEqual(len(mo.move_raw_ids), 2)700        mo.button_mark_done()701        self.assertTrue(all(s == 'done' for s in mo.move_raw_ids.mapped('state')))702        self.assertEqual(sum(mo.move_raw_ids.mapped('move_line_ids.product_uom_qty')), 0)703    def test_product_produce_7(self):704        """ Plan 100 products to produce. Produce 50. Do a 'Post Inventory'.705        Update the quantity to produce to 200. Produce 50 again. Check that706        the components has been consumed correctly.707        """708        self.stock_location = self.env.ref('stock.stock_location_stock')709        mo, bom, p_final, p1, p2 = self.generate_mo(qty_base_1=2, qty_base_2=2, qty_final=100)710        self.assertEqual(len(mo), 1, 'MO should have been created')711        self.env['stock.quant']._update_available_quantity(p1, self.stock_location, 200)712        self.env['stock.quant']._update_available_quantity(p2, self.stock_location, 200)713        produce_form = Form(self.env['mrp.product.produce'].with_context({714            'active_id': mo.id,715            'active_ids': [mo.id],716        }))717        produce_form.product_qty = 50.0718        produce_wizard = produce_form.save()719        produce_wizard.do_produce()720        mo.post_inventory()721        update_quantity_wizard = self.env['change.production.qty'].create({722            'mo_id': mo.id,723            'product_qty': 200,724        })725        update_quantity_wizard.change_prod_qty()726        produce_form = Form(self.env['mrp.product.produce'].with_context({727            'active_id': mo.id,728            'active_ids': [mo.id],729        }))730        produce_form.product_qty = 50.0731        produce_wizard = produce_form.save()732        produce_wizard.do_produce()733        self.assertEqual(sum(mo.move_raw_ids.filtered(lambda m: m.product_id == p1).mapped('quantity_done')), 200)734        self.assertEqual(sum(mo.move_raw_ids.filtered(lambda m: m.product_id == p2).mapped('quantity_done')), 200)735        self.assertEqual(sum(mo.move_finished_ids.mapped('quantity_done')), 100)736    def test_product_produce_uom(self):737        plastic_laminate = self.env.ref('mrp.product_product_plastic_laminate')738        bom = self.env.ref('mrp.mrp_bom_plastic_laminate')739        dozen = self.env.ref('uom.product_uom_dozen')740        unit = self.env.ref('uom.product_uom_unit')741        plastic_laminate.tracking = 'serial'742        mo = self.env['mrp.production'].create({743            'name': 'Dozen Plastic Laminate',744            'product_id': plastic_laminate.id,745            'product_uom_id': dozen.id,746            'product_qty': 1,747            'bom_id': bom.id,748        })749        final_product_lot = self.env['stock.production.lot'].create({750            'name': 'lot1',751            'product_id': plastic_laminate.id,752        })753        mo.action_assign()754        self.assertEqual(mo.move_raw_ids.product_qty, 12, '12 units should be reserved.')755        # produce product756        produce_form = Form(self.env['mrp.product.produce'].with_context({757            'active_id': mo.id,758            'active_ids': [mo.id],759        }))760        produce_form.lot_id = final_product_lot761        product_produce = produce_form.save()762        self.assertEqual(product_produce.product_qty, 1)763        self.assertEqual(product_produce.product_uom_id, unit, 'Should be 1 unit since the tracking is serial.')764        product_produce.lot_id = final_product_lot.id765        product_produce.do_produce()766        move_line_raw = mo.move_raw_ids.mapped('move_line_ids').filtered(lambda m: m.qty_done)767        self.assertEqual(move_line_raw.qty_done, 1)768        self.assertEqual(move_line_raw.product_uom_id, unit, 'Should be 1 unit since the tracking is serial.')769        move_line_finished = mo.move_finished_ids.mapped('move_line_ids').filtered(lambda m: m.qty_done)770        self.assertEqual(move_line_finished.qty_done, 1)...produce.py
Source:produce.py  
1from __future__ import absolute_import2from kafka.protocol.api import Request, Response3from kafka.protocol.types import (4    Int16, Int32, Int64, String, Array, Schema, Bytes5)6class ProduceResponse_v0(Response):7    API_KEY = 08    API_VERSION = 09    SCHEMA = Schema(10        ('topics', Array(11            ('topic', String('utf-8')),12            ('partitions', Array(13                ('partition', Int32),14                ('error_code', Int16),15                ('offset', Int64)))))16    )17class ProduceResponse_v1(Response):18    API_KEY = 019    API_VERSION = 120    SCHEMA = Schema(21        ('topics', Array(22            ('topic', String('utf-8')),23            ('partitions', Array(24                ('partition', Int32),25                ('error_code', Int16),26                ('offset', Int64))))),27        ('throttle_time_ms', Int32)28    )29class ProduceResponse_v2(Response):30    API_KEY = 031    API_VERSION = 232    SCHEMA = Schema(33        ('topics', Array(34            ('topic', String('utf-8')),35            ('partitions', Array(36                ('partition', Int32),37                ('error_code', Int16),38                ('offset', Int64),39                ('timestamp', Int64))))),40        ('throttle_time_ms', Int32)41    )42class ProduceResponse_v3(Response):43    API_KEY = 044    API_VERSION = 345    SCHEMA = ProduceResponse_v2.SCHEMA46class ProduceResponse_v4(Response):47    """48    The version number is bumped up to indicate that the client supports49    KafkaStorageException.50    The KafkaStorageException will be translated to51    NotLeaderForPartitionException in the response if version <= 352    """53    API_KEY = 054    API_VERSION = 455    SCHEMA = ProduceResponse_v3.SCHEMA56class ProduceResponse_v5(Response):57    API_KEY = 058    API_VERSION = 559    SCHEMA = Schema(60        ('topics', Array(61            ('topic', String('utf-8')),62            ('partitions', Array(63                ('partition', Int32),64                ('error_code', Int16),65                ('offset', Int64),66                ('timestamp', Int64),67                ('log_start_offset', Int64))))),68        ('throttle_time_ms', Int32)69    )70class ProduceRequest(Request):71    API_KEY = 072    def expect_response(self):73        if self.required_acks == 0:  # pylint: disable=no-member74            return False75        return True76class ProduceRequest_v0(ProduceRequest):77    API_VERSION = 078    RESPONSE_TYPE = ProduceResponse_v079    SCHEMA = Schema(80        ('required_acks', Int16),81        ('timeout', Int32),82        ('topics', Array(83            ('topic', String('utf-8')),84            ('partitions', Array(85                ('partition', Int32),86                ('messages', Bytes)))))87    )88class ProduceRequest_v1(ProduceRequest):89    API_VERSION = 190    RESPONSE_TYPE = ProduceResponse_v191    SCHEMA = ProduceRequest_v0.SCHEMA92class ProduceRequest_v2(ProduceRequest):93    API_VERSION = 294    RESPONSE_TYPE = ProduceResponse_v295    SCHEMA = ProduceRequest_v1.SCHEMA96class ProduceRequest_v3(ProduceRequest):97    API_VERSION = 398    RESPONSE_TYPE = ProduceResponse_v399    SCHEMA = Schema(100        ('transactional_id', String('utf-8')),101        ('required_acks', Int16),102        ('timeout', Int32),103        ('topics', Array(104            ('topic', String('utf-8')),105            ('partitions', Array(106                ('partition', Int32),107                ('messages', Bytes)))))108    )109class ProduceRequest_v4(ProduceRequest):110    """111    The version number is bumped up to indicate that the client supports112    KafkaStorageException.113    The KafkaStorageException will be translated to114    NotLeaderForPartitionException in the response if version <= 3115    """116    API_VERSION = 4117    RESPONSE_TYPE = ProduceResponse_v4118    SCHEMA = ProduceRequest_v3.SCHEMA119class ProduceRequest_v5(ProduceRequest):120    """121    Same as v4. The version number is bumped since the v5 response includes an122    additional partition level field: the log_start_offset.123    """124    API_VERSION = 5125    RESPONSE_TYPE = ProduceResponse_v5126    SCHEMA = ProduceRequest_v4.SCHEMA127ProduceRequest = [128    ProduceRequest_v0, ProduceRequest_v1, ProduceRequest_v2,129    ProduceRequest_v3, ProduceRequest_v4, ProduceRequest_v5130]131ProduceResponse = [132    ProduceResponse_v0, ProduceResponse_v1, ProduceResponse_v2,133    ProduceResponse_v3, ProduceResponse_v4, ProduceResponse_v5...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
