#    Author: Leonardo Pistone
#    Copyright 2015 Camptocamp SA
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as
#    published by the Free Software Foundation, either version 3 of the
#    License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
from openerp.tests.common import TransactionCase


class TestPickingToPackOps(TransactionCase):

    def test_one_move_takes_owner_from_move(self):
        result = self.picking._prepare_pack_ops(self.picking, [],
                                                {self.product: 10})
        self.assertEqual(1, len(result))
        self.assertEqual(self.partner2.id, result[0]['owner_id'])

    def setUp(self):
        super(TestPickingToPackOps, self).setUp()
        self.product = self.env.ref('product.product_product_36')
        self.partner1 = self.env.ref('base.res_partner_1')
        self.partner2 = self.env.ref('base.res_partner_2')

        self.picking = self.env['stock.picking'].new({
            'picking_type_id': self.env.ref('stock.picking_type_in').id,
            'owner_id': self.partner1.id,  # we do not want to use this one
            'move_lines': [(0, 0, {
                'product_id': self.product.id,
                'product_qty': 10,
                'restrict_partner_id': self.partner2.id,  # we want this!
            })],
        })

@@ -904,22 +904,20 @@
     
-result
+pick
  = self.
 pick
@@ -916,99 +916,230 @@
 elf.
+_
 picking
-._prepare_pack_ops(self.picking, %5B%5D,%0A                                               
+_factory(owner=self.partner1)%0A        pick.move_lines = self._move_factory(product=self.product1,%0A                                             owner=self.partner2)%0A%0A        result = pick._prepare_pack_ops(pick, %5B%5D,
  %7Bse
@@ -1152,15 +1152,18 @@
 duct
+1
 : 
-1
+5.
 0%7D)%0A
+%0A
     
@@ -1278,310 +1278,498 @@
 def 
-setUp(self):%0A        super(TestPickingToPackOps, self).setUp()%0A        self.product = self.env.ref('product.product_product_36')%0A        self.partner1 = self.env.ref('base.res_partner_1')%0A        self.partner2 = self.env.ref('base.res_partner_2')%0A%0A        self.picking = self.env%5B'stock.picking'%5D.new(%7B
+_picking_factory(self, owner):%0A        return self.env%5B'stock.picking'%5D.create(%7B%0A            'picking_type_id': self.env.ref('stock.picking_type_in').id,%0A            'owner_id': owner.id,%0A        %7D)%0A%0A    def _move_factory(self, product, owner, qty=5.0):%0A        return self.env%5B'stock.move'%5D.create(%7B%0A            'name': '/',%0A            'restrict_partner_id': owner.id,%0A            'product_id': product.id,%0A            'product_uom_qty': qty,%0A            'product_uom': product.uom_id.id,
 %0A   
@@ -1774,36 +1774,32 @@
            '
-picking_type
+location
 _id': self.e
@@ -1816,158 +1816,209 @@
 ock.
-picking_type_in').id,%0A            'owner_id': self.partner1.id,  # we do not want to use this one%0A            'move_lines': %5B(0, 0, %7B%0A        
+stock_location_suppliers').id,%0A            'location_dest_id': self.env.ref('stock.stock_location_stock').id,%0A        %7D)%0A%0A    def setUp(self):%0A        super(TestPickingToPackOps, self).setUp()%0A
         
 'pro
@@ -2017,172 +2017,248 @@
     
-'
+self.
 product
-_id':
+1 =
  self.
-product.id,%0A                'product_qty': 10,%0A                'restrict_partner_id': self.partner2.id,  # we want this!%0A            %7D)%5D,%0A        %7D
+env.ref('product.product_product_33')%0A        self.product2 = self.env.ref('product.product_product_36')%0A        self.partner1 = self.env.ref('base.res_partner_1')%0A        self.partner2 = self.env.ref('base.res_partner_2'
 )%0A

refactor test with factories