Skip to content

Commit 4ad8b80

Browse files
ianardeeixunio
authored andcommitted
chg: ♻️ run tests and setup through black
1 parent 1b72757 commit 4ad8b80

17 files changed

+356
-275
lines changed

.github/workflows/linting.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,4 @@ jobs:
2222
pip install --requirement ./requirements.dev.txt
2323
- name: Analysing the code with Black
2424
run: |
25-
black --check ./mindee
25+
black --check .

setup.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ def make_requirements_list(file="requirements.txt", only_regular=True):
2424
lines = f.read().splitlines()
2525
if only_regular:
2626
regex = (
27-
"\/$|^#|^$$|^git\+"
28-
) # remove line with /, starting by # or space or empty
27+
"\/$|^#|^$$|^git\+" # remove line with /, starting by # or space or empty
28+
)
2929
return [line for line in lines if not re.findall(regex, line)]
3030
else:
3131
return lines
@@ -43,5 +43,5 @@ def make_requirements_list(file="requirements.txt", only_regular=True):
4343
author="Mindee",
4444
author_email="devrel@mindee.com",
4545
install_requires=make_requirements_list(),
46-
include_package_data=True
46+
include_package_data=True,
4747
)

tests/documents/test_document.py

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,38 +21,45 @@ def test_constructor():
2121

2222
# Plate tests
2323

24+
2425
def test_responseWrapper_plate(dummy_file_input):
25-
response = json.load(open('./tests/data/license_plates/v1/plate.json'))
26+
response = json.load(open("./tests/data/license_plates/v1/plate.json"))
2627
parsed_plate = Response.format_response(response, "license_plate", dummy_file_input)
2728
assert parsed_plate.license_plate.license_plates[0].value == "7EQE707"
2829

2930

3031
# Invoice tests
3132

33+
3234
def test_responseWrapper_invoice(dummy_file_input):
33-
response = json.load(open('./tests/data/invoices/v2/invoice.json'))
35+
response = json.load(open("./tests/data/invoices/v2/invoice.json"))
3436
parsed_invoice = Response.format_response(response, "invoice", dummy_file_input)
35-
assert parsed_invoice.invoice.invoice_date.value == '2018-09-25'
37+
assert parsed_invoice.invoice.invoice_date.value == "2018-09-25"
3638

3739

3840
# Receipt tests
3941

42+
4043
def test_responseWrapper_receipt(dummy_file_input):
41-
response = json.load(open('./tests/data/expense_receipts/v3/receipt.json'))
44+
response = json.load(open("./tests/data/expense_receipts/v3/receipt.json"))
4245
parsed_receipt = Response.format_response(response, "receipt", dummy_file_input)
4346
assert parsed_receipt.receipt.date.value == "2016-02-26"
4447

4548

4649
# Financial document tests
4750

51+
4852
def test_responseWrapper_financial_document_with_receipt(dummy_file_input):
49-
response = json.load(open('./tests/data/expense_receipts/v3/receipt.json'))
50-
parsed_financial_doc = Response.format_response(response, "financial_document", dummy_file_input)
53+
response = json.load(open("./tests/data/expense_receipts/v3/receipt.json"))
54+
parsed_financial_doc = Response.format_response(
55+
response, "financial_document", dummy_file_input
56+
)
5157
assert parsed_financial_doc.financial_document.date.value == "2016-02-26"
5258

5359

5460
def test_responseWrapper_financial_document_with_invoice(dummy_file_input):
55-
response = json.load(open('./tests/data/invoices/v2/invoice.json'))
56-
parsed_financial_doc = Response.format_response(response, "financial_document", dummy_file_input)
57-
assert parsed_financial_doc.financial_document.date.value == '2018-09-25'
58-
61+
response = json.load(open("./tests/data/invoices/v2/invoice.json"))
62+
parsed_financial_doc = Response.format_response(
63+
response, "financial_document", dummy_file_input
64+
)
65+
assert parsed_financial_doc.financial_document.date.value == "2018-09-25"

tests/documents/test_financial_doc.py

Lines changed: 82 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,19 @@
66
@pytest.fixture
77
def 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
1315
def 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
3844
def 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
4454
def 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
5062
def 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
5569
def 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

5975
def 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
96112
def 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

104120
def 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):
112128
def 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

129147
def 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):
138158
def 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

150172
def 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

159183
def 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
168192
def 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

176200
def 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):
184208
def 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

201227
def 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):
210238
def 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

222252
def 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

231263
def 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

239271
def 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

247279
def 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

261299
def 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

Comments
 (0)