File: test_module.py

package info (click to toggle)
tryton-modules-product-price-list-dates 7.0.0-3
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 380 kB
  • sloc: python: 456; xml: 114; makefile: 11; sh: 3
file content (153 lines) | stat: -rw-r--r-- 5,239 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# This file is part of Tryton.  The COPYRIGHT file at the top level of
# this repository contains the full copyright notices and license terms.

import datetime
from decimal import Decimal

from trytond.modules.company.tests import (
    CompanyTestMixin, create_company, set_company)
from trytond.pool import Pool
from trytond.tests.test_tryton import ModuleTestCase, with_transaction
from trytond.transaction import Transaction


class ProductPriceListDatesTestCase(CompanyTestMixin, ModuleTestCase):
    'Test Product Price List Dates module'
    module = 'product_price_list_dates'
    extras = [
        'product_price_list_cache', 'sale_price_list', 'purchase_price_list']

    def create_price_list(self, field, date):
        pool = Pool()
        PriceList = pool.get('product.price_list')

        price_list, = PriceList.create([{
                    'name': "Price List",
                    'price': 'list_price',
                    'lines': [('create', [{
                                    field: date,
                                    'formula': 'unit_price * 0.9',
                                    }, {
                                    'formula': 'unit_price',
                                    }])],
                    }])
        return price_list

    def create_product(self, list_price=Decimal(10)):
        pool = Pool()
        Product = pool.get('product.product')
        Template = pool.get('product.template')
        Uom = pool.get('product.uom')

        unit, = Uom.search([('name', '=', "Unit")])
        template = Template(
            name="Template", list_price=list_price, default_uom=unit)
        template.save()
        product = Product(template=template)
        product.save()
        return product

    @with_transaction()
    def test_price_list_start_date(self):
        "Test price list with start date"
        pool = Pool()
        Date = pool.get('ir.date')

        today = Date.today()
        tomorrow = today + datetime.timedelta(days=1)

        company = create_company()
        with set_company(company):
            product = self.create_product()
            uom = product.default_uom
            price_list = self.create_price_list('start_date', tomorrow)

            self.assertEqual(
                price_list.compute(
                    product, 1, uom, pattern={'date': today}),
                Decimal(10))
            self.assertEqual(
                price_list.compute(
                    product, 1, uom, pattern={'date': tomorrow}),
                Decimal(9))

    @with_transaction()
    def test_price_list_end_date(self):
        "Test price list with end date"
        pool = Pool()
        Date = pool.get('ir.date')

        today = Date.today()
        yesterday = today - datetime.timedelta(days=1)

        company = create_company()
        with set_company(company):
            product = self.create_product()
            uom = product.default_uom
            price_list = self.create_price_list('end_date', yesterday)

            self.assertEqual(
                price_list.compute(
                    product, 1, uom, pattern={'date': today}),
                Decimal(10))
            self.assertEqual(
                price_list.compute(
                    product, 1, uom, pattern={'date': yesterday}),
                Decimal(9))

    @with_transaction()
    def test_price_list_with_context_date(self):
        "Test price list with context date"
        pool = Pool()
        Date = pool.get('ir.date')

        today = Date.today()
        tomorrow = today + datetime.timedelta(days=1)

        company = create_company()
        with set_company(company):
            product = self.create_product()
            uom = product.default_uom
            price_list = self.create_price_list('start_date', tomorrow)

            with Transaction().set_context(date=today):
                self.assertEqual(
                    price_list.compute(product, 1, uom),
                    Decimal(10))
            with Transaction().set_context(date=tomorrow):
                self.assertEqual(
                    price_list.compute(product, 1, uom),
                    Decimal(9))

    @with_transaction()
    def test_price_list_cache(self):
        "Test price list cache"
        pool = Pool()
        Cache = pool.get('product.price_list.cache')
        Date = pool.get('ir.date')

        today = Date.today()
        tomorrow = today + datetime.timedelta(days=1)

        company = create_company()
        with set_company(company):
            product = self.create_product()
            uom = product.default_uom
            price_list = self.create_price_list('start_date', tomorrow)

            price_list.fill_cache()

            caches = Cache.search([])
            self.assertEqual(len(caches), 2)

            for date, result in [
                    (today, Decimal(10)),
                    (tomorrow, Decimal(9)),
                    ]:
                with self.subTest(date=date):
                    with Transaction().set_context(date=date):
                        cache = Cache.get(price_list, product)
                        self.assertEqual(cache.get_unit_price(1, uom), result)


del ModuleTestCase