File: test_module.py

package info (click to toggle)
tryton-modules-customs 7.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 500 kB
  • sloc: python: 788; xml: 281; makefile: 11; sh: 3
file content (264 lines) | stat: -rw-r--r-- 9,525 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# This file is part of Tryton.  The COPYRIGHT file at the top level of
# this repository contains the full copyright notices and license terms.

from datetime import date
from decimal import Decimal

from trytond.pool import Pool
from trytond.tests.test_tryton import ModuleTestCase, with_transaction


class CustomsTestCase(ModuleTestCase):
    'Test Customs module'
    module = 'customs'

    @with_transaction()
    def test_tariff_code_match_date(self):
        "Test tariff code match date"
        pool = Pool()
        Tariff = pool.get('customs.tariff.code')
        Template = pool.get('product.template')
        Product_TariffCode = pool.get(
            'product-customs.tariff.code')
        Month = pool.get('ir.calendar.month')

        june, = Month.search([('index', '=', 6)])
        august, = Month.search([('index', '=', 8)])

        # Test start <= end
        tariff1 = Tariff(code='170390')
        tariff2 = Tariff(code='17039099',
                start_month=june, start_day=20,
                end_month=august, end_day=20)
        Tariff.save([tariff1, tariff2])
        template = Template(tariff_codes=[
                Product_TariffCode(tariff_code=tariff2),
                Product_TariffCode(tariff_code=tariff1),
                ], tariff_codes_category=False)

        for pattern, result in [
                ({'date': date(2015, 1, 1)}, tariff1),
                ({'date': date(2015, 7, 1)}, tariff2),
                ({'date': date(2016, 9, 1)}, tariff1),
                ]:
            self.assertEqual(template.get_tariff_code(pattern), result)

        # Test start > end
        tariff2.start_month = august
        tariff2.end_month = june
        tariff2.save()

        for pattern, result in [
                ({'date': date(2015, 1, 1)}, tariff2),
                ({'date': date(2015, 7, 1)}, tariff1),
                ({'date': date(2016, 9, 1)}, tariff2),
                ]:
            self.assertEqual(template.get_tariff_code(pattern), result)

    @with_transaction()
    def test_tariff_code_match_country(self):
        "Test tariff code match country"
        pool = Pool()
        Tariff = pool.get('customs.tariff.code')
        Country = pool.get('country.country')

        country1 = Country(name="Country 1")
        country1.save()
        country2 = Country(name="Country 2")
        country2.save()
        tariff1 = Tariff(code='170390')
        tariff1.save()
        tariff2 = Tariff(code='17039099', country=country1)
        tariff2.save()

        self.assertTrue(tariff1.match({}))
        self.assertTrue(tariff1.match({'country': None}))
        self.assertTrue(tariff1.match({'country': country1.id}))
        self.assertTrue(tariff2.match({}))
        self.assertTrue(tariff2.match({'country': None}))
        self.assertTrue(tariff2.match({'country': country1.id}))
        self.assertFalse(tariff2.match({'country': country2.id}))

    @with_transaction()
    def test_tariff_code_match_country_organization(self):
        "Test Tariff code match country with organization"
        pool = Pool()
        Tariff = pool.get('customs.tariff.code')
        Country = pool.get('country.country')
        Organization = pool.get('country.organization')

        country1 = Country(name="Country 1")
        country1.save()
        country2 = Country(name="Country 2")
        country2.save()
        organization = Organization(
            name="Organization", members=[{'country': country1.id}])
        organization.save()
        tariff1 = Tariff(code='170390')
        tariff1.save()
        tariff2 = Tariff(code='17039099', organization=organization)
        tariff2.save()

        self.assertTrue(tariff1.match({}))
        self.assertTrue(tariff1.match({'country': None}))
        self.assertTrue(tariff1.match({'country': country1.id}))
        self.assertTrue(tariff2.match({}))
        self.assertTrue(tariff2.match({'country': None}))
        self.assertTrue(tariff2.match({'country': country1.id}))
        self.assertFalse(tariff2.match({'country': country2.id}))

    @with_transaction()
    def test_get_tariff_code(self):
        'Test get_tariff_code'
        pool = Pool()
        Tariff = pool.get('customs.tariff.code')
        Template = pool.get('product.template')
        Category = pool.get('product.category')
        Product_TariffCode = pool.get(
            'product-customs.tariff.code')

        tariff1, tariff2, tariff3 = Tariff.create([
                {'code': '170390'},
                {'code': '17039099'},
                {'code': '1703909999'},
                ])

        category1 = Category(tariff_codes=[
                Product_TariffCode(tariff_code=tariff1),
                ], tariff_codes_parent=False, customs=True)
        category2 = Category(tariff_codes=[
                Product_TariffCode(tariff_code=tariff2),
                ], parent=category1, tariff_codes_parent=False, customs=True)
        template = Template(tariff_codes=[
                Product_TariffCode(tariff_code=tariff3),
                ], customs_category=category2, tariff_codes_category=False)

        self.assertEqual(template.get_tariff_code({}), tariff3)

        template.tariff_codes_category = True
        self.assertEqual(template.get_tariff_code({}), tariff2)

        category2.tariff_codes_parent = True
        self.assertEqual(template.get_tariff_code({}), tariff1)

    @with_transaction()
    def test_duty_rate_match(self):
        'Test duty rate match'
        pool = Pool()
        Tariff = pool.get('customs.tariff.code')
        Rate = pool.get('customs.duty.rate')
        Currency = pool.get('currency.currency')
        CurrencyRate = pool.get('currency.currency.rate')

        currency = Currency(name='cur', symbol='cur', code='XXX',
            rates=[CurrencyRate(rate=Decimal(1))])
        currency.save()

        tariff = Tariff(code='170390')
        tariff.save()

        rate1 = Rate(tariff_code=tariff,
            computation_type='amount',
            end_date=date(2015, 6, 30),
            amount=Decimal(10), currency=currency)
        rate2 = Rate(tariff_code=tariff,
            start_date=date(2015, 7, 1),
            end_date=date(2015, 12, 31),
            computation_type='amount',
            amount=Decimal(10), currency=currency)
        rate3 = Rate(tariff_code=tariff,
            start_date=date(2015, 12, 31),
            computation_type='amount',
            amount=Decimal(10), currency=currency)
        Rate.save([rate1, rate2, rate3])

        for pattern, result in [
                ({'date': date(2015, 1, 1)}, rate1),
                ({'date': date(2015, 8, 1)}, rate2),
                ({'date': date(2016, 9, 1)}, rate3),
                ]:
            self.assertEqual(tariff.get_duty_rate(pattern), result)

    @with_transaction()
    def test_duty_rate_compute(self):
        'Test duty rate compute'
        pool = Pool()
        Rate = pool.get('customs.duty.rate')
        Currency = pool.get('currency.currency')
        CurrencyRate = pool.get('currency.currency.rate')
        Uom = pool.get('product.uom')

        kg, g = Uom.search([('name', 'in', ['Kilogram', 'Gram'])],
            order=[('name', 'DESC')])
        currency1 = Currency(name='cur1', symbol='cur1', code='XX1',
            rates=[CurrencyRate(rate=Decimal(1))])
        currency2 = Currency(name='cur2', symbol='cur1', code='XX2',
            rates=[CurrencyRate(rate=Decimal('.5'))])
        Currency.save([currency1, currency2])

        rate = Rate(computation_type='amount',
            amount=Decimal(10), currency=currency1)
        self.assertEqual(rate.compute(currency2, 1, kg), Decimal(5))

        rate = Rate(computation_type='quantity',
            amount=Decimal(10), currency=currency1, uom=kg)
        self.assertEqual(rate.compute(currency2, 100, g), Decimal('0.5'))

    @with_transaction()
    def test_delete_category(self):
        'Test delete category'
        pool = Pool()
        Tariff = pool.get('customs.tariff.code')
        Category = pool.get('product.category')
        Product_TariffCode = pool.get(
            'product-customs.tariff.code')

        tariff = Tariff(code='170390')
        tariff.save()

        category = Category(name='Test', customs=True,
            tariff_codes=[
                Product_TariffCode(tariff_code=tariff),
                ])
        category.save()
        product_tariff_code, = category.tariff_codes

        Category.delete([category])

        self.assertEqual(
            Product_TariffCode.search([
                    ('id', '=', product_tariff_code.id),
                    ]), [])

    @with_transaction()
    def test_delete_template(self):
        'Test delete template'
        pool = Pool()
        Tariff = pool.get('customs.tariff.code')
        Template = pool.get('product.template')
        Uom = pool.get('product.uom')
        Product_TariffCode = pool.get(
            'product-customs.tariff.code')

        unit, = Uom.search([('name', '=', 'Unit')])

        tariff = Tariff(code='170390')
        tariff.save()

        template = Template(name='Test',
            default_uom=unit,
            tariff_codes=[
                Product_TariffCode(tariff_code=tariff),
                ])
        template.save()
        product_tariff_code, = template.tariff_codes

        Template.delete([template])

        self.assertEqual(
            Product_TariffCode.search([
                    ('id', '=', product_tariff_code.id),
                    ]), [])


del ModuleTestCase