Implement tests.

This commit refs #857
This commit is contained in:
Sergio Morillo 2015-10-26 20:39:11 +01:00
parent 0096a1de51
commit a5dd681902
4 changed files with 228 additions and 16 deletions

View File

@ -1,6 +1,7 @@
# The COPYRIGHT file at the top level of this repository contains the full
# copyright notices and license terms.
from trytond.pool import Pool
from .load import (LoadOrder, LoadOrderLine,
LoadUnitLoad, LoadUnitLoadOrder,
LoadUnitLoadData, Configuration)

54
load.py
View File

@ -27,7 +27,9 @@ class LoadOrder:
__name__ = 'carrier.load.order'
unit_loads = fields.Function(
fields.One2Many('stock.unit_load', None, 'Unit loads'),
fields.One2Many('stock.unit_load', None, 'Unit loads',
states={'readonly': Eval('state').in_(['cancel', 'done'])},
depends=['state']),
'get_unit_loads', setter='set_unit_loads', searcher='search_unit_loads')
ul_origin_restrict = fields.Boolean('Restrict UL origin',
states={'readonly': Eval('state') != 'draft'},
@ -88,19 +90,67 @@ class LoadOrder:
return res
def _get_shipment_moves(self, sale_line, grouped_items):
pool = Pool()
Move = pool.get('stock.move')
moves = []
other_moves = []
for item in grouped_items:
new_moves = item._get_new_moves({'from_location': sale_line.from_location.id,
'to_location': sale_line.to_location.id,
'planned_date': sale_line.delivery_date})
move, = [m for m in new_moves if m.product == item.product]
move.origin = sale_line
moves.extend(new_moves)
moves.append(move)
for new_move in new_moves:
if new_move.product.id == item.product.id:
continue
new_move.origin = item.load_line
other_moves.append(new_move)
if other_moves:
Move.save(other_moves)
return moves
def _get_items(self):
return self.unit_loads
@classmethod
def do(cls, records):
pool = Pool()
Move = pool.get('stock.move')
super(LoadOrder, cls).do(records)
moves = []
for record in records:
moves.extend(
[record._get_inventory_move(m) for m in record.outgoing_moves])
if moves:
Move.save(moves)
Move.do([m for r in records for l in r.lines for m in l.moves])
def _get_inventory_move(self, move):
pool = Pool()
Move = pool.get('stock.move')
location = move.unit_load.get_location(
[move.unit_load], product_id=move.product.id, type='storage')[move.unit_load.id]
return Move(
from_location=location,
to_location=move.from_location,
product=move.product,
uom=move.uom,
quantity=move.quantity,
planned_date=move.planned_date,
company=move.company,
currency=move.currency,
unit_price=move.unit_price,
unit_load=move.unit_load,
origin=move.origin,
lot=getattr(move, 'lot', None)
)
@classmethod
@ModelView.button_action('carrier_load_ul.wizard_load_ul')
def run_try(cls, records):

View File

@ -1,6 +1,6 @@
==============
Carrier load
==============
================
Carrier load UL
================
Imports::
@ -10,6 +10,10 @@ Imports::
>>> from trytond.modules.company.tests.tools import create_company, \
... get_company
>>> from trytond.modules.carrier_vehicle.tests.tools import create_carrier
>>> from trytond.modules.account.tests.tools import create_fiscalyear, \
... create_chart, get_accounts, create_tax
>>> from.trytond.modules.account_invoice.tests.tools import \
... set_fiscalyear_invoice_sequences, create_payment_term
>>> from trytond.modules.stock_unit_load.tests.tools import create_unit_load
>>> from proteus import config, Model, Wizard
>>> today = datetime.date.today()
@ -37,6 +41,72 @@ Reload the context::
>>> Group = Model.get('res.group')
>>> config._context = User.get_preferences(True, config.context)
Create sale user::
>>> sale_user = User()
>>> sale_user.name = 'Sale'
>>> sale_user.login = 'sale'
>>> sale_user.main_company = company
>>> sale_group, = Group.find([('name', '=', 'Sales')])
>>> sale_user.groups.append(sale_group)
>>> sale_user.save()
Create stock user::
>>> stock_user = User()
>>> stock_user.name = 'Stock'
>>> stock_user.login = 'stock'
>>> stock_user.main_company = company
>>> stock_group, = Group.find([('name', '=', 'Stock')])
>>> stock_user.groups.append(stock_group)
>>> stock_user.save()
Create account user::
>>> account_user = User()
>>> account_user.name = 'Account'
>>> account_user.login = 'account'
>>> account_user.main_company = company
>>> account_group, = Group.find([('name', '=', 'Account')])
>>> account_user.groups.append(account_group)
>>> account_user.save()
Create fiscal year::
>>> fiscalyear = set_fiscalyear_invoice_sequences(
... create_fiscalyear(company))
>>> fiscalyear.click('create_period')
Create chart of accounts::
>>> _ = create_chart(company)
>>> accounts = get_accounts(company)
>>> revenue = accounts['revenue']
>>> expense = accounts['expense']
>>> cash = accounts['cash']
>>> Journal = Model.get('account.journal')
>>> cash_journal, = Journal.find([('type', '=', 'cash')])
>>> cash_journal.credit_account = cash
>>> cash_journal.debit_account = cash
>>> cash_journal.save()
Create tax::
>>> tax = create_tax(Decimal('.10'))
>>> tax.save()
Create parties::
>>> Party = Model.get('party.party')
>>> customer = Party(name='Customer')
>>> customer.save()
Create payment term::
>>> payment_term = create_payment_term()
>>> payment_term.save()
Create carrier::
>>> carrier = create_carrier(config)
@ -81,6 +151,7 @@ Create load order::
'draft'
>>> order.ul_origin_restrict
True
>>> order.party = customer
>>> line = order.lines.new()
>>> line.ul_quantity = Decimal(1)
>>> order.save()
@ -96,6 +167,44 @@ Create unit load::
>>> Unitload = Model.get('stock.unit_load')
>>> ul = create_unit_load(config=config, do_state=False)
>>> template = ul.product.template
>>> template.salable = True
>>> template.account_expense = expense
>>> template.account_revenue = revenue
>>> template.save()
Add other products to unit load::
>>> ProductUom = Model.get('product.uom')
>>> unit, = ProductUom.find([('name', '=', 'Unit')])
>>> ProductTemplate = Model.get('product.template')
>>> Product = Model.get('product.product')
>>> ProductCategory = Model.get('product.category')
>>> category = ProductCategory(name='Category')
>>> category.save()
>>> product = Product()
>>> template = ProductTemplate()
>>> template.name = 'Plastic Case 30x30'
>>> template.category = category
>>> template.default_uom = unit
>>> template.type = 'goods'
>>> template.salable = True
>>> template.list_price = Decimal('10')
>>> template.cost_price = Decimal('5')
>>> template.account_expense = expense
>>> template.account_revenue = revenue
>>> template.customer_taxes.append(tax)
>>> template.save()
>>> product.template = template
>>> product.save()
>>> move = ul.moves.new()
>>> move.planned_date = today
>>> move.product = product
>>> move.quantity = Decimal(2)
>>> move.from_location = ul.moves[0].from_location
>>> move.to_location = ul.moves[0].to_location
>>> move.currency = move.company.currency
>>> ul.save()
Starting load wizard::
@ -109,7 +218,7 @@ Starting load wizard::
>>> start_load.form.loaded_uls
0
>>> start_load.form.ul_code = 'X'
>>> start_load.execute('add_') # doctest:
>>> start_load.execute('load_') # doctest:
Traceback (most recent call last):
...
UserError: ('UserError', ('Cannot find Unit load "X".', ''))
@ -117,24 +226,24 @@ Starting load wizard::
Check UL loading restrictions::
>>> start_load.form.ul_code = ul.code
>>> start_load.execute('add_') # doctest:
>>> start_load.execute('load_') # doctest:
Traceback (most recent call last):
...
UserError: ('UserError', (u'UL "1" must be in Done state.', ''))
>>> ul.click('do')
>>> start_load.execute('add_') # doctest:
>>> start_load.execute('load_') # doctest:
Traceback (most recent call last):
...
UserError: ('UserError', (u'UL "1" does not belong to any origin of this load order.', ''))
>>> order.ul_origin_restrict = False
>>> order.save()
>>> start_load.execute('add_') # doctest:
>>> start_load.execute('load_') # doctest:
Traceback (most recent call last):
...
UserWarning: ('UserWarning', ('loading_ul_origin_1', u'UL "1" does not belong to any origin of this load order.', ''))
>>> Model.get('res.user.warning')(user=config.user,
... name='loading_ul_origin_1', always=True).save()
>>> start_load.execute('add_')
>>> start_load.execute('load_')
>>> start_load.form.loaded_uls
1
>>> order.reload()
@ -145,7 +254,7 @@ Check UL loading restrictions::
>>> len(order.unit_loads)
1
>>> start_load.form.ul_code = ul.code
>>> start_load.execute('add_') # doctest:
>>> start_load.execute('load_') # doctest:
Traceback (most recent call last):
...
UserError: ('UserError', (u'UL "1" is already loaded.', ''))
@ -160,7 +269,7 @@ Add an invalid UL::
>>> ul2.save()
>>> ul2.click('do')
>>> start_load.form.ul_code = ul2.code
>>> start_load.execute('add_') # doctest:
>>> start_load.execute('load_') # doctest:
Traceback (most recent call last):
...
UserError: ('UserError', (u'UL "2" must be in a storage location.', ''))
@ -171,7 +280,7 @@ Add an invalid UL::
>>> ul2.click('do')
>>> Model.get('res.user.warning')(user=config.user,
... name='loading_ul_origin_2', always=True).save()
>>> start_load.execute('add_') # doctest:
>>> start_load.execute('load_') # doctest:
Traceback (most recent call last):
...
UserError: ('UserError', (u'All valid lines of load order "1" are complete. Cannot load more ULs.', ''))
@ -180,7 +289,22 @@ Unload UL::
>>> order.lines[0].ul_quantity += 1
>>> order.save()
>>> start_load.execute('add_')
>>> start_load.execute('load_')
>>> start_load.form.loaded_uls
2
>>> start_load.form.uls_loaded.append(ul2)
>>> start_load.execute('unload_')
>>> start_load.form.loaded_uls
1
>>> ul2.load_line == None
True
>>> start_load.execute('end')
>>> order.reload()
>>> len(order.unit_loads)
1
>>> start_load = Wizard('carrier.load_uls', [order])
>>> start_load.form.ul_code = ul2.code
>>> start_load.execute('load_')
>>> start_load.execute('end')
>>> order.reload()
>>> len(order.unit_loads)
@ -189,8 +313,6 @@ Unload UL::
>>> order.reload()
>>> len(order.unit_loads)
1
>>> ul2.load_line == None
True
Finish loading::
@ -201,3 +323,38 @@ Finish loading::
1
>>> order.state
u'done'
Check sale::
>>> len(order.sales)
1
>>> order.sale != None
True
>>> order.sale.state
u'quotation'
>>> len(order.sale.lines)
1
>>> order.sale.lines[0].product.id == ul.product.id
True
>>> len(order.sale.shipments)
1
>>> len(order.sale.shipments[0].outgoing_moves)
1
>>> order.sale.shipments[0].outgoing_moves[0].unit_load.id == ul.id
True
>>> len(order.sale.shipments[0].inventory_moves)
1
>>> order.sale.shipments[0].inventory_moves[0].unit_load.id == ul.id
True
>>> len(order.inventory_moves)
1
>>> order.inventory_moves[0].product.rec_name
u'Plastic Case 30x30'
>>> order.inventory_moves[0].quantity
2.0
>>> len(order.outgoing_moves)
1
>>> order.outgoing_moves[0].product.rec_name
u'Plastic Case 30x30'
>>> order.outgoing_moves[0].quantity
2.0

View File

@ -11,6 +11,10 @@ this repository contains the full copyright notices and license terms. -->
<field name="unit_loads" colspan="4"
view_ids="carrier_load_ul.unit_load_view_tree,stock_unit_load.unit_load_view_form"/>
</page>
<page id="moves" string="Moves">
<field name="inventory_moves" colspan="4"/>
<field name="outgoing_moves" colspan="4"/>
</page>
</xpath>
<xpath expr="/form/group[@id='state_buttons']/group[@id='buttons']/button[@name='do']"
position="before">