Files
tryton/modules/stock_forecast/tests/test_module.py
T

352 lines
14 KiB
Python
Raw Normal View History

2014-12-02 12:56:06 +01:00
# This file is part of Tryton. The COPYRIGHT file at the top level of
# this repository contains the full copyright notices and license terms.
2021-12-11 17:58:44 +01:00
import datetime
from decimal import Decimal
2021-12-11 17:58:44 +01:00
from dateutil.relativedelta import relativedelta
2021-12-11 17:58:44 +01:00
from trytond.modules.company.tests import (
CompanyTestMixin, create_company, set_company)
from trytond.pool import Pool
2016-02-06 19:13:18 +01:00
from trytond.tests.test_tryton import ModuleTestCase, with_transaction
from trytond.transaction import Transaction, inactive_records
2009-08-10 13:10:25 +02:00
class StockForecastTestCase(CompanyTestMixin, ModuleTestCase):
2014-01-12 20:00:13 +01:00
'Test StockForecast module'
2015-01-27 18:14:10 +01:00
module = 'stock_forecast'
2009-08-10 13:10:25 +02:00
@with_transaction()
def test_forecast_rec_name(self):
pool = Pool()
Forecast = pool.get('stock.forecast')
Location = pool.get('stock.location')
customer, = Location.search([('code', '=', 'CUS')])
warehouse, = Location.search([('code', '=', 'WH')])
storage, = Location.search([('code', '=', 'STO')])
company = create_company()
with set_company(company):
forecast = Forecast(
warehouse=warehouse,
destination=customer,
from_date=datetime.date(2023, 1, 1),
to_date=datetime.date(2023, 3, 31),
)
forecast.save()
with inactive_records():
forecasts = Forecast.search([('rec_name', '=', "Warehouse")])
self.assertEqual(
forecast.rec_name,
"Warehouse → Customer @ [01/01/2023 - 03/31/2023]")
self.assertEqual(forecasts, [forecast])
2016-02-06 19:13:18 +01:00
@with_transaction()
def test_distribute(self):
2014-01-12 20:00:13 +01:00
'Test distribute'
2016-02-06 19:13:18 +01:00
pool = Pool()
Line = pool.get('stock.forecast.line')
line = Line()
for values, result in (
((1, 5), {0: 5}),
((4, 8), {0: 2, 1: 2, 2: 2, 3: 2}),
((2, 5), {0: 2, 1: 3}),
((10, 4), {0: 0, 1: 1, 2: 0, 3: 1, 4: 0,
5: 0, 6: 1, 7: 0, 8: 1, 9: 0}),
):
self.assertEqual(line.distribute(*values), result)
2016-02-06 19:13:18 +01:00
@with_transaction()
def test_create_moves_before(self):
"Test create moves before start date"
2016-02-06 19:13:18 +01:00
pool = Pool()
Uom = pool.get('product.uom')
Template = pool.get('product.template')
Product = pool.get('product.product')
Location = pool.get('stock.location')
Forecast = pool.get('stock.forecast')
Move = pool.get('stock.move')
2016-02-06 19:13:18 +01:00
unit, = Uom.search([('name', '=', 'Unit')])
template, = Template.create([{
'name': 'Test create_moves',
'type': 'goods',
'default_uom': unit.id,
}])
product, = Product.create([{
'template': template.id,
}])
customer, = Location.search([('code', '=', 'CUS')])
warehouse, = Location.search([('code', '=', 'WH')])
storage, = Location.search([('code', '=', 'STO')])
company = create_company()
with set_company(company):
today = datetime.date.today()
2016-02-06 19:13:18 +01:00
forecast, = Forecast.create([{
2013-01-06 22:18:13 +01:00
'warehouse': warehouse.id,
'destination': customer.id,
'from_date': today + relativedelta(months=1, day=1),
'to_date': today + relativedelta(months=1, day=20),
'company': company.id,
'lines': [
('create', [{
'product': product.id,
'quantity': 10,
'uom': unit.id,
'minimal_quantity': 2,
}],
),
],
}])
2016-02-06 19:13:18 +01:00
Forecast.confirm([forecast])
2016-02-06 19:13:18 +01:00
Forecast.create_moves([forecast])
line, = forecast.lines
self.assertEqual(line.quantity_executed, 0)
self.assertEqual(len(line.moves), 5)
self.assertEqual(sum(move.quantity for move in line.moves), 10)
self.assertGreaterEqual(
min(m.planned_date for m in line.moves), forecast.from_date)
2016-02-06 19:13:18 +01:00
Forecast.delete_moves([forecast])
line, = forecast.lines
self.assertEqual(len(line.moves), 0)
2016-02-06 19:13:18 +01:00
Move.create([{
2013-01-06 22:18:13 +01:00
'from_location': storage.id,
'to_location': customer.id,
'product': product.id,
2023-08-22 13:18:33 +02:00
'unit': unit.id,
2013-01-06 22:18:13 +01:00
'quantity': 2,
'planned_date': today + relativedelta(months=1, day=5),
'company': company.id,
'currency': company.currency.id,
'unit_price': Decimal('1'),
}])
line, = forecast.lines
self.assertEqual(line.quantity_executed, 2)
2016-02-06 19:13:18 +01:00
Forecast.create_moves([forecast])
line, = forecast.lines
self.assertEqual(line.quantity_executed, 2)
self.assertEqual(len(line.moves), 4)
self.assertEqual(sum(move.quantity for move in line.moves), 8)
self.assertGreaterEqual(
min(m.planned_date for m in line.moves), forecast.from_date)
@with_transaction()
def test_create_moves_during(self):
"Test create moves during the period"
pool = Pool()
Uom = pool.get('product.uom')
Template = pool.get('product.template')
Product = pool.get('product.product')
Location = pool.get('stock.location')
Forecast = pool.get('stock.forecast')
Move = pool.get('stock.move')
unit, = Uom.search([('name', '=', 'Unit')])
template, = Template.create([{
'name': 'Test create_moves',
'type': 'goods',
'default_uom': unit.id,
}])
product, = Product.create([{
'template': template.id,
}])
customer, = Location.search([('code', '=', 'CUS')])
warehouse, = Location.search([('code', '=', 'WH')])
storage, = Location.search([('code', '=', 'STO')])
company = create_company()
with set_company(company):
today = datetime.date.today()
forecast, = Forecast.create([{
'warehouse': warehouse.id,
'destination': customer.id,
'from_date': today - relativedelta(days=20),
'to_date': today + relativedelta(days=10),
'company': company.id,
'lines': [
('create', [{
'product': product.id,
'quantity': 20,
'uom': unit.id,
'minimal_quantity': 2,
}],
),
],
}])
Forecast.confirm([forecast])
Forecast.create_moves([forecast])
line, = forecast.lines
self.assertEqual(line.quantity_executed, 0)
self.assertEqual(len(line.moves), 10)
self.assertEqual(sum(move.quantity for move in line.moves), 20)
self.assertGreaterEqual(
min(m.planned_date for m in line.moves), today)
Forecast.delete_moves([forecast])
line, = forecast.lines
self.assertEqual(len(line.moves), 0)
Move.create([{
'from_location': storage.id,
'to_location': customer.id,
'product': product.id,
2023-08-22 13:18:33 +02:00
'unit': unit.id,
'quantity': 10,
'planned_date': today - relativedelta(days=5),
'company': company.id,
'currency': company.currency.id,
'unit_price': Decimal('1'),
}])
line, = forecast.lines
self.assertEqual(line.quantity_executed, 10)
Forecast.create_moves([forecast])
line, = forecast.lines
self.assertEqual(line.quantity_executed, 10)
self.assertEqual(len(line.moves), 5)
self.assertEqual(sum(move.quantity for move in line.moves), 10)
self.assertGreaterEqual(
min(m.planned_date for m in line.moves), today)
@with_transaction()
def test_create_moves_after(self):
"Test create not moves after end date"
pool = Pool()
Uom = pool.get('product.uom')
Template = pool.get('product.template')
Product = pool.get('product.product')
Location = pool.get('stock.location')
Forecast = pool.get('stock.forecast')
unit, = Uom.search([('name', '=', 'Unit')])
template, = Template.create([{
'name': 'Test create_moves',
'type': 'goods',
'default_uom': unit.id,
}])
product, = Product.create([{
'template': template.id,
}])
customer, = Location.search([('code', '=', 'CUS')])
warehouse, = Location.search([('code', '=', 'WH')])
storage, = Location.search([('code', '=', 'STO')])
company = create_company()
with set_company(company):
today = datetime.date.today()
forecast, = Forecast.create([{
'warehouse': warehouse.id,
'destination': customer.id,
'from_date': today - relativedelta(days=20),
'to_date': today - relativedelta(days=10),
'company': company.id,
'lines': [
('create', [{
'product': product.id,
'quantity': 20,
'uom': unit.id,
'minimal_quantity': 2,
}],
),
],
}])
Forecast.confirm([forecast])
Forecast.create_moves([forecast])
line, = forecast.lines
self.assertEqual(len(line.moves), 0)
2016-02-06 19:13:18 +01:00
@with_transaction()
def test_complete(self):
'Test complete'
pool = Pool()
Uom = pool.get('product.uom')
Template = pool.get('product.template')
Product = pool.get('product.product')
Location = pool.get('stock.location')
Forecast = pool.get('stock.forecast')
Move = pool.get('stock.move')
ForecastComplete = pool.get('stock.forecast.complete', type='wizard')
2014-05-06 11:58:06 +02:00
2016-02-06 19:13:18 +01:00
unit, = Uom.search([('name', '=', 'Unit')])
template, = Template.create([{
'name': 'Test complete',
'type': 'goods',
'default_uom': unit.id,
}])
product, = Product.create([{
'template': template.id,
}])
customer, = Location.search([('code', '=', 'CUS')])
supplier, = Location.search([('code', '=', 'SUP')])
warehouse, = Location.search([('code', '=', 'WH')])
storage, = Location.search([('code', '=', 'STO')])
company = create_company()
with set_company(company):
2014-05-06 11:58:06 +02:00
today = datetime.date.today()
2016-02-06 19:13:18 +01:00
moves = Move.create([{
2014-05-06 11:58:06 +02:00
'from_location': supplier.id,
'to_location': storage.id,
'product': product.id,
2023-08-22 13:18:33 +02:00
'unit': unit.id,
2014-05-06 11:58:06 +02:00
'quantity': 10,
'effective_date': (today
+ relativedelta(months=-1, day=1)),
'company': company.id,
'currency': company.currency.id,
'unit_price': Decimal('1'),
}, {
'from_location': storage.id,
'to_location': customer.id,
'product': product.id,
2023-08-22 13:18:33 +02:00
'unit': unit.id,
2014-05-06 11:58:06 +02:00
'quantity': 5,
'effective_date': (today
+ relativedelta(months=-1, day=15)),
'company': company.id,
'currency': company.currency.id,
'unit_price': Decimal('1'),
}])
2016-02-06 19:13:18 +01:00
Move.do(moves)
2014-05-06 11:58:06 +02:00
2016-02-06 19:13:18 +01:00
forecast, = Forecast.create([{
2014-05-06 11:58:06 +02:00
'warehouse': warehouse.id,
'destination': customer.id,
'from_date': today + relativedelta(months=1, day=1),
'to_date': today + relativedelta(months=1, day=20),
'company': company.id,
}])
with Transaction().set_context(
active_model=Forecast.__name__, active_id=forecast.id):
2016-02-06 19:13:18 +01:00
session_id, _, _ = ForecastComplete.create()
forecast_complete = ForecastComplete(session_id)
2023-02-11 12:40:41 +01:00
forecast_complete.ask.company = company
forecast_complete.ask.warehouse = warehouse
forecast_complete.ask.destination = customer
2016-02-06 19:13:18 +01:00
forecast_complete.ask.from_date = (
today + relativedelta(months=-1, day=1))
forecast_complete.ask.to_date = (
today + relativedelta(months=-1, day=20))
2023-02-11 12:40:41 +01:00
forecast_complete.ask.products = [product]
2014-05-06 11:58:06 +02:00
forecast_complete.transition_complete()
self.assertEqual(len(forecast.lines), 1)
forecast_line, = forecast.lines
self.assertEqual(forecast_line.product, product)
self.assertEqual(forecast_line.uom, unit)
self.assertEqual(forecast_line.quantity, 5)
self.assertEqual(forecast_line.minimal_quantity, 1)
2012-04-03 20:07:44 +02:00
del ModuleTestCase