1
1
# Copyright 2023 Giuseppe Borruso (gborruso@dinamicheaziendali.it)
2
2
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
3
3
4
- from datetime import date , timedelta
4
+ from datetime import timedelta
5
5
from unittest import mock
6
6
7
7
from dateutil .relativedelta import relativedelta
8
- from freezegun import freeze_time
9
8
10
- from odoo import api , fields
9
+ from odoo import fields
11
10
from odoo .tests import tagged
12
11
13
12
from odoo .addons .account .tests .common import AccountTestInvoicingCommon
@@ -31,6 +30,8 @@ def setUpClass(cls):
31
30
cls .eur_currency = cls .env .ref ("base.EUR" )
32
31
cls .usd_currency = cls .env .ref ("base.USD" )
33
32
cls .gbp_currency = cls .env .ref ("base.GBP" )
33
+ # all currencies but EUR and USD are archived by default
34
+ cls .gbp_currency .active = True
34
35
cls .company = cls .Company .create (
35
36
{"name" : "Test company" , "currency_id" : cls .eur_currency .id }
36
37
)
@@ -40,16 +41,17 @@ def setUpClass(cls):
40
41
{
41
42
"service" : "BOI" ,
42
43
"currency_ids" : [
43
- (4 , cls .usd_currency .id ),
44
- (4 , cls .gbp_currency .id ),
45
- (4 , cls .env .user .company_id .currency_id .id ),
44
+ fields .Command .set ([cls .usd_currency .id , cls .gbp_currency .id ])
46
45
],
47
46
}
48
47
)
49
48
cls .CurrencyRate .search ([]).unlink ()
49
+ # create fake return data from BOI
50
+ compute_date = cls ._get_no_weekend_date (cls .today - relativedelta (days = 1 ))
51
+ cls .mock_rates = {str (compute_date ): {"USD" : "1.1446" , "GBP" : "0.85880" }}
50
52
51
- @api . model
52
- def _get_no_weekend_date (self , compute_date ):
53
+ @classmethod
54
+ def _get_no_weekend_date (cls , compute_date ):
53
55
if compute_date .weekday () in [5 , 6 ]:
54
56
days_to_friday = 4 - compute_date .weekday ()
55
57
return compute_date + timedelta (days = days_to_friday )
@@ -59,101 +61,14 @@ def _get_no_weekend_date(self, compute_date):
59
61
def test_supported_currencies_BOI (self ):
60
62
self .ecb_provider ._get_supported_currencies ()
61
63
62
- def test_error_BOI (self ):
63
- with mock .patch (_BOI_provider_class + "._obtain_rates" , return_value = None ):
64
- self .ecb_provider ._update (self .today , self .today )
65
-
66
64
def test_update_BOI_yesterday (self ):
67
- compute_date = self ._get_no_weekend_date (self .today - relativedelta (days = 1 ))
68
- self .ecb_provider ._update (compute_date , self .today )
69
-
70
- rates = self .CurrencyRate .search (
71
- [("currency_id" , "=" , self .usd_currency .id )], limit = 1
72
- )
73
- self .assertTrue (rates )
74
-
75
- self .CurrencyRate .search ([("company_id" , "=" , self .company .id )]).unlink ()
76
-
77
- def test_update_BOI_month (self ):
78
- compute_date = self ._get_no_weekend_date (self .today - relativedelta (months = 1 ))
79
- self .ecb_provider ._update (compute_date , self .today )
80
-
81
- rates = self .CurrencyRate .search (
82
- [("currency_id" , "=" , self .usd_currency .id )], limit = 1
83
- )
84
- self .assertTrue (rates )
85
-
86
- self .CurrencyRate .search ([("company_id" , "=" , self .company .id )]).unlink ()
87
-
88
- def test_update_BOI_year (self ):
89
- compute_date = self ._get_no_weekend_date (self .today - relativedelta (years = 1 ))
90
- self .ecb_provider ._update (compute_date , self .today )
91
-
92
- rates = self .CurrencyRate .search (
93
- [("currency_id" , "=" , self .usd_currency .id )], limit = 1
94
- )
95
- self .assertTrue (rates )
96
-
97
- self .CurrencyRate .search ([("company_id" , "=" , self .company .id )]).unlink ()
98
-
99
- def test_update_BOI_scheduled (self ):
100
- self .ecb_provider .interval_type = "days"
101
- self .ecb_provider .interval_number = 14
102
- self .ecb_provider .next_run = self ._get_no_weekend_date (
103
- self .today - relativedelta (days = 1 )
104
- )
105
- self .ecb_provider ._scheduled_update ()
106
-
107
- rates = self .CurrencyRate .search (
108
- [("currency_id" , "=" , self .usd_currency .id )], limit = 1
109
- )
110
- self .assertTrue (rates )
111
-
112
- self .CurrencyRate .search ([("company_id" , "=" , self .company .id )]).unlink ()
113
-
114
- def test_update_BOI_sequence (self ):
115
- self .ecb_provider .interval_type = "days"
116
- self .ecb_provider .interval_number = 2
117
- self .ecb_provider .last_successful_run = None
118
- self .ecb_provider .next_run = date (2019 , 4 , 1 )
119
-
120
- self .ecb_provider ._scheduled_update ()
121
- self .assertEqual (self .ecb_provider .last_successful_run , date (2019 , 4 , 1 ))
122
- self .assertEqual (self .ecb_provider .next_run , date (2019 , 4 , 3 ))
123
- rates = self .CurrencyRate .search (
124
- [
125
- ("company_id" , "=" , self .company .id ),
126
- ("currency_id" , "=" , self .usd_currency .id ),
127
- ]
128
- )
129
- self .assertEqual (len (rates ), 1 )
130
-
131
- self .ecb_provider ._scheduled_update ()
132
- self .assertEqual (self .ecb_provider .last_successful_run , date (2019 , 4 , 3 ))
133
- self .assertEqual (self .ecb_provider .next_run , date (2019 , 4 , 5 ))
134
- rates = self .CurrencyRate .search (
135
- [
136
- ("company_id" , "=" , self .company .id ),
137
- ("currency_id" , "=" , self .usd_currency .id ),
138
- ]
139
- )
140
- self .assertEqual (len (rates ), 2 )
141
-
142
- self .CurrencyRate .search ([("company_id" , "=" , self .company .id )]).unlink ()
143
-
144
- @freeze_time ("2022-04-19 22:00" )
145
- def test_update_BOI_with_daily (self ):
146
- self .ecb_provider .interval_type = "days"
147
- self .ecb_provider .interval_number = 1
148
- self .ecb_provider .last_successful_run = date (2021 , 4 , 19 )
149
-
150
- self .ecb_provider ._scheduled_update ()
151
-
152
- self .assertEqual (self .ecb_provider .last_successful_run , date (2022 , 4 , 19 ))
153
-
154
- def test_foreign_base_currency (self ):
155
- self .test_update_BOI_yesterday ()
156
- self .test_update_BOI_month ()
157
- self .test_update_BOI_year ()
158
- self .test_update_BOI_scheduled ()
159
- self .test_update_BOI_sequence ()
65
+ with mock .patch (
66
+ _BOI_provider_class + "._obtain_rates" , return_value = self .mock_rates
67
+ ):
68
+ compute_date = self ._get_no_weekend_date (self .today - relativedelta (days = 1 ))
69
+ self .ecb_provider ._update (compute_date , self .today )
70
+
71
+ rates = self .CurrencyRate .search (
72
+ [("currency_id" , "=" , self .usd_currency .id )], limit = 1
73
+ )
74
+ self .assertEqual (rates .company_rate , 1.1446 )
0 commit comments