66@pytest .fixture
77def financial_doc_from_invoice_object ():
88 invoice_json_repsonse = json .load (open ("./tests/data/invoices/v2/invoice.json" ))
9- return FinancialDocument (invoice_json_repsonse ["document" ]["inference" ]["pages" ][0 ]["prediction" ])
9+ return FinancialDocument (
10+ invoice_json_repsonse ["document" ]["inference" ]["pages" ][0 ]["prediction" ]
11+ )
1012
1113
1214@pytest .fixture
1315def financial_doc_from_receipt_object ():
14- receipt_json_repsonse = json .load (open ("./tests/data/expense_receipts/v3/receipt.json" ))
15- return FinancialDocument (receipt_json_repsonse ["document" ]["inference" ]["pages" ][0 ]["prediction" ])
16+ receipt_json_repsonse = json .load (
17+ open ("./tests/data/expense_receipts/v3/receipt.json" )
18+ )
19+ return FinancialDocument (
20+ receipt_json_repsonse ["document" ]["inference" ]["pages" ][0 ]["prediction" ]
21+ )
1622
1723
1824@pytest .fixture
@@ -30,30 +36,40 @@ def financial_doc_object_from_scratch():
3036 company_number = "asdqsdae" ,
3137 orientation = 0 ,
3238 total_tax = 3 ,
33- time = "12:15"
39+ time = "12:15" ,
3440 )
3541
3642
3743@pytest .fixture
3844def financial_doc_from_receipt_object_all_na ():
39- json_repsonse = json .load (open ("./tests/data/expense_receipts/v3/receipt_all_na.json" ))
40- return FinancialDocument (json_repsonse ["document" ]["inference" ]["pages" ][0 ]["prediction" ])
45+ json_repsonse = json .load (
46+ open ("./tests/data/expense_receipts/v3/receipt_all_na.json" )
47+ )
48+ return FinancialDocument (
49+ json_repsonse ["document" ]["inference" ]["pages" ][0 ]["prediction" ]
50+ )
4151
4252
4353@pytest .fixture
4454def financial_doc_from_invoice_object_all_na ():
4555 json_repsonse = json .load (open ("./tests/data/invoices/v2/invoice_all_na.json" ))
46- return FinancialDocument (json_repsonse ["document" ]["inference" ]["pages" ][0 ]["prediction" ])
56+ return FinancialDocument (
57+ json_repsonse ["document" ]["inference" ]["pages" ][0 ]["prediction" ]
58+ )
4759
4860
4961@pytest .fixture
5062def receipt_pred ():
51- return json .load (open ("./tests/data/expense_receipts/v3/receipt_all_na.json" ))["document" ]["inference" ]["pages" ][0 ]["prediction" ]
63+ return json .load (open ("./tests/data/expense_receipts/v3/receipt_all_na.json" ))[
64+ "document"
65+ ]["inference" ]["pages" ][0 ]["prediction" ]
5266
5367
5468@pytest .fixture
5569def invoice_pred ():
56- return json .load (open ("./tests/data/invoices/v2/invoice_all_na.json" ))["document" ]["inference" ]["pages" ][0 ]["prediction" ]
70+ return json .load (open ("./tests/data/invoices/v2/invoice_all_na.json" ))["document" ][
71+ "inference"
72+ ]["pages" ][0 ]["prediction" ]
5773
5874
5975def test_constructor_1 (financial_doc_from_invoice_object ):
@@ -95,15 +111,15 @@ def test__str__receipt(financial_doc_from_receipt_object):
95111# Business tests from receipt
96112def test__receipt_reconstruct_total_excl_from_total_and_taxes_1 (receipt_pred ):
97113 # no incl implies no reconstruct for total excl
98- receipt_pred ["total_incl" ] = {"value" : "N/A" , "probability" : 0. }
114+ receipt_pred ["total_incl" ] = {"value" : "N/A" , "probability" : 0.0 }
99115 receipt_pred ["taxes" ] = [{"rate" : 20 , "value" : 9.5 , "probability" : 0.9 }]
100116 financial_doc = FinancialDocument (receipt_pred )
101117 assert financial_doc .total_excl .value is None
102118
103119
104120def test__receipt_reconstruct_total_excl_from_total_and_taxes_2 (receipt_pred ):
105121 # no taxes implies no reconstruct for total excl
106- receipt_pred ["total_incl" ] = {"value" : 12.54 , "probability" : 0. }
122+ receipt_pred ["total_incl" ] = {"value" : 12.54 , "probability" : 0.0 }
107123 receipt_pred ["taxes" ] = []
108124 financial_doc = FinancialDocument (receipt_pred )
109125 assert financial_doc .total_excl .value is None
@@ -112,8 +128,10 @@ def test__receipt_reconstruct_total_excl_from_total_and_taxes_2(receipt_pred):
112128def test__receipt_reconstruct_total_excl_from_total_and_taxes_3 (receipt_pred ):
113129 # working example
114130 receipt_pred ["total_incl" ] = {"value" : 12.54 , "probability" : 0.5 }
115- receipt_pred ["taxes" ] = [{"rate" : 20 , "value" : 0.5 , "probability" : 0.1 },
116- {"rate" : 10 , "value" : 4.25 , "probability" : 0.6 }]
131+ receipt_pred ["taxes" ] = [
132+ {"rate" : 20 , "value" : 0.5 , "probability" : 0.1 },
133+ {"rate" : 10 , "value" : 4.25 , "probability" : 0.6 },
134+ ]
117135 financial_doc = FinancialDocument (receipt_pred )
118136 assert financial_doc .total_excl .probability == 0.03
119137 assert financial_doc .total_excl .value == 7.79
@@ -128,8 +146,10 @@ def test__receipt_reconstruct_total_tax_1(receipt_pred):
128146
129147def test__receipt_reconstruct_total_tax_2 (receipt_pred ):
130148 # working example
131- receipt_pred ["taxes" ] = [{"rate" : 20 , "value" : 10.2 , "probability" : 0.5 },
132- {"rate" : 10 , "value" : 40.0 , "probability" : 0.1 }]
149+ receipt_pred ["taxes" ] = [
150+ {"rate" : 20 , "value" : 10.2 , "probability" : 0.5 },
151+ {"rate" : 10 , "value" : 40.0 , "probability" : 0.1 },
152+ ]
133153 financial_doc = FinancialDocument (receipt_pred )
134154 assert financial_doc .total_tax .value == 50.2
135155 assert financial_doc .total_tax .probability == 0.05
@@ -138,44 +158,48 @@ def test__receipt_reconstruct_total_tax_2(receipt_pred):
138158def test__receipt_taxes_match_total_incl_1 (receipt_pred ):
139159 # matching example
140160 receipt_pred ["total_incl" ] = {"value" : 507.25 , "probability" : 0.6 }
141- receipt_pred ["taxes" ] = [{"rate" : 20 , "value" : 10.99 , "probability" : 0.5 },
142- {"rate" : 10 , "value" : 40.12 , "probability" : 0.1 }]
161+ receipt_pred ["taxes" ] = [
162+ {"rate" : 20 , "value" : 10.99 , "probability" : 0.5 },
163+ {"rate" : 10 , "value" : 40.12 , "probability" : 0.1 },
164+ ]
143165 financial_doc = FinancialDocument (receipt_pred )
144166 assert financial_doc .checklist ["taxes_match_total_incl" ] is True
145- assert financial_doc .total_incl .probability == 1.
167+ assert financial_doc .total_incl .probability == 1.0
146168 for tax in financial_doc .taxes :
147- assert tax .probability == 1.
169+ assert tax .probability == 1.0
148170
149171
150172def test__receipt_taxes_match_total_incl_2 (receipt_pred ):
151173 # not matching example with close error
152174 receipt_pred ["total_incl" ] = {"value" : 507.25 , "probability" : 0.6 }
153- receipt_pred ["taxes" ] = [{"rate" : 20 , "value" : 10.9 , "probability" : 0.5 },
154- {"rate" : 10 , "value" : 40.12 , "probability" : 0.1 }]
175+ receipt_pred ["taxes" ] = [
176+ {"rate" : 20 , "value" : 10.9 , "probability" : 0.5 },
177+ {"rate" : 10 , "value" : 40.12 , "probability" : 0.1 },
178+ ]
155179 financial_doc = FinancialDocument (receipt_pred )
156180 assert financial_doc .checklist ["taxes_match_total_incl" ] is False
157181
158182
159183def test__receipt_taxes_match_total_incl_3 (receipt_pred ):
160184 # sanity check with null tax
161185 receipt_pred ["total_incl" ] = {"value" : 507.25 , "probability" : 0.6 }
162- receipt_pred ["taxes" ] = [{"rate" : 20 , "value" : 0. , "probability" : 0.5 }]
186+ receipt_pred ["taxes" ] = [{"rate" : 20 , "value" : 0.0 , "probability" : 0.5 }]
163187 financial_doc = FinancialDocument (receipt_pred )
164188 assert financial_doc .checklist ["taxes_match_total_incl" ] is False
165189
166190
167191# Business tests from invoice
168192def test__invoice_reconstruct_total_excl_from_total_and_taxes_1 (invoice_pred ):
169193 # no incl implies no reconstruct for total excl
170- invoice_pred ["total_incl" ] = {"amount" : "N/A" , "probability" : 0. }
194+ invoice_pred ["total_incl" ] = {"amount" : "N/A" , "probability" : 0.0 }
171195 invoice_pred ["taxes" ] = [{"rate" : 20 , "amount" : 9.5 , "probability" : 0.9 }]
172196 financial_doc = FinancialDocument (invoice_pred )
173197 assert financial_doc .total_excl .value is None
174198
175199
176200def test__invoice_reconstruct_total_excl_from_total_and_taxes_2 (invoice_pred ):
177201 # no taxes implies no reconstruct for total excl
178- invoice_pred ["total_incl" ] = {"amount" : 12.54 , "probability" : 0. }
202+ invoice_pred ["total_incl" ] = {"amount" : 12.54 , "probability" : 0.0 }
179203 invoice_pred ["taxes" ] = []
180204 financial_doc = FinancialDocument (invoice_pred )
181205 assert financial_doc .total_excl .value is None
@@ -184,8 +208,10 @@ def test__invoice_reconstruct_total_excl_from_total_and_taxes_2(invoice_pred):
184208def test__invoice_reconstruct_total_excl_from_total_and_taxes_3 (invoice_pred ):
185209 # working example
186210 invoice_pred ["total_incl" ] = {"value" : 12.54 , "probability" : 0.5 }
187- invoice_pred ["taxes" ] = [{"rate" : 20 , "value" : 0.5 , "probability" : 0.1 },
188- {"rate" : 10 , "value" : 4.25 , "probability" : 0.6 }]
211+ invoice_pred ["taxes" ] = [
212+ {"rate" : 20 , "value" : 0.5 , "probability" : 0.1 },
213+ {"rate" : 10 , "value" : 4.25 , "probability" : 0.6 },
214+ ]
189215 financial_doc = FinancialDocument (invoice_pred )
190216 assert financial_doc .total_excl .probability == 0.03
191217 assert financial_doc .total_excl .value == 7.79
@@ -200,8 +226,10 @@ def test__invoice_reconstruct_total_tax_1(invoice_pred):
200226
201227def test__invoice_reconstruct_total_tax_2 (invoice_pred ):
202228 # working example
203- invoice_pred ["taxes" ] = [{"rate" : 20 , "value" : 10.2 , "probability" : 0.5 },
204- {"rate" : 10 , "value" : 40.0 , "probability" : 0.1 }]
229+ invoice_pred ["taxes" ] = [
230+ {"rate" : 20 , "value" : 10.2 , "probability" : 0.5 },
231+ {"rate" : 10 , "value" : 40.0 , "probability" : 0.1 },
232+ ]
205233 financial_doc = FinancialDocument (invoice_pred )
206234 assert financial_doc .total_tax .value == 50.2
207235 assert financial_doc .total_tax .probability == 0.05
@@ -210,59 +238,68 @@ def test__invoice_reconstruct_total_tax_2(invoice_pred):
210238def test__invoice_taxes_match_total_incl_1 (invoice_pred ):
211239 # matching example
212240 invoice_pred ["total_incl" ] = {"value" : 507.25 , "probability" : 0.6 }
213- invoice_pred ["taxes" ] = [{"rate" : 20 , "value" : 10.99 , "probability" : 0.5 },
214- {"rate" : 10 , "value" : 40.12 , "probability" : 0.1 }]
241+ invoice_pred ["taxes" ] = [
242+ {"rate" : 20 , "value" : 10.99 , "probability" : 0.5 },
243+ {"rate" : 10 , "value" : 40.12 , "probability" : 0.1 },
244+ ]
215245 financial_doc = FinancialDocument (invoice_pred )
216246 assert financial_doc .checklist ["taxes_match_total_incl" ] is True
217- assert financial_doc .total_incl .probability == 1.
247+ assert financial_doc .total_incl .probability == 1.0
218248 for tax in financial_doc .taxes :
219- assert tax .probability == 1.
249+ assert tax .probability == 1.0
220250
221251
222252def test__invoice_taxes_match_total_incl_2 (invoice_pred ):
223253 # not matching example with close error
224254 invoice_pred ["total_incl" ] = {"value" : 507.25 , "probability" : 0.6 }
225- invoice_pred ["taxes" ] = [{"rate" : 20 , "value" : 10.9 , "probability" : 0.5 },
226- {"rate" : 10 , "value" : 40.12 , "probability" : 0.1 }]
255+ invoice_pred ["taxes" ] = [
256+ {"rate" : 20 , "value" : 10.9 , "probability" : 0.5 },
257+ {"rate" : 10 , "value" : 40.12 , "probability" : 0.1 },
258+ ]
227259 financial_doc = FinancialDocument (invoice_pred )
228260 assert financial_doc .checklist ["taxes_match_total_incl" ] is False
229261
230262
231263def test__invoice_taxes_match_total_incl_3 (invoice_pred ):
232264 # sanity check with null tax
233265 invoice_pred ["total_incl" ] = {"value" : 507.25 , "probability" : 0.6 }
234- invoice_pred ["taxes" ] = [{"rate" : 20 , "value" : 0. , "probability" : 0.5 }]
266+ invoice_pred ["taxes" ] = [{"rate" : 20 , "value" : 0.0 , "probability" : 0.5 }]
235267 financial_doc = FinancialDocument (invoice_pred )
236268 assert financial_doc .checklist ["taxes_match_total_incl" ] is False
237269
238270
239271def test__shouldnt_raise_when_tax_rate_none (invoice_pred ):
240272 # sanity check with null tax
241273 invoice_pred ["total_incl" ] = {"value" : 507.25 , "probability" : 0.6 }
242- invoice_pred ["taxes" ] = [{"rate" : "N/A" , "value" : 0. , "probability" : 0.5 }]
274+ invoice_pred ["taxes" ] = [{"rate" : "N/A" , "value" : 0.0 , "probability" : 0.5 }]
243275 financial_doc = FinancialDocument (invoice_pred )
244276 assert financial_doc .checklist ["taxes_match_total_incl" ] is False
245277
246278
247279def test_compare_1 (financial_doc_from_invoice_object ):
248280 # Compare same object must return all True
249- benchmark = FinancialDocument .compare (financial_doc_from_invoice_object , financial_doc_from_invoice_object )
281+ benchmark = FinancialDocument .compare (
282+ financial_doc_from_invoice_object , financial_doc_from_invoice_object
283+ )
250284 for value in benchmark .values ():
251285 assert value is True
252286
253287
254- def test_compare_2 (financial_doc_from_invoice_object , financial_doc_from_invoice_object_all_na ):
288+ def test_compare_2 (
289+ financial_doc_from_invoice_object , financial_doc_from_invoice_object_all_na
290+ ):
255291 # Compare full object and empty object
256- benchmark = FinancialDocument .compare (financial_doc_from_invoice_object , financial_doc_from_invoice_object_all_na )
292+ benchmark = FinancialDocument .compare (
293+ financial_doc_from_invoice_object , financial_doc_from_invoice_object_all_na
294+ )
257295 for key in set (benchmark .keys ()) - {"time" }:
258296 assert benchmark [key ] is False
259297
260298
261299def test_compare_3 (financial_doc_object_from_scratch ):
262300 # Compare financial doc from class
263301 benchmark = FinancialDocument .compare (
264- financial_doc_object_from_scratch ,
265- financial_doc_object_from_scratch
302+ financial_doc_object_from_scratch , financial_doc_object_from_scratch
266303 )
267304 for key in benchmark .keys ():
268305 if "__acc__" in key :
@@ -273,8 +310,7 @@ def test_compare_4(financial_doc_object_from_scratch):
273310 # Compare financial doc from class with empty taxes
274311 financial_doc_object_from_scratch .taxes = []
275312 benchmark = FinancialDocument .compare (
276- financial_doc_object_from_scratch ,
277- financial_doc_object_from_scratch
313+ financial_doc_object_from_scratch , financial_doc_object_from_scratch
278314 )
279315 for key in benchmark .keys ():
280316 if "__acc__" in key :
@@ -293,4 +329,6 @@ def test_invoice_or_receipt_get_same_field_types(receipt_pred, invoice_pred):
293329 financial_doc_from_invoice = FinancialDocument (invoice_pred )
294330 assert set (dir (financial_doc_from_invoice )) == set (dir (financial_doc_from_receipt ))
295331 for key in dir (financial_doc_from_receipt ):
296- assert type (getattr (financial_doc_from_receipt , key )) == type (getattr (financial_doc_from_invoice , key ))
332+ assert type (getattr (financial_doc_from_receipt , key )) == type (
333+ getattr (financial_doc_from_invoice , key )
334+ )
0 commit comments