Skip to content

Commit

Permalink
Writing tests for export/json_report endpoint (#18)
Browse files Browse the repository at this point in the history
* Initial testing for report/json_report endpoint

* Finished testing for report/json_report endpoint

* Resolving merge conflict

* Commenting out unimplemented tests

* Changing the requirements for psycopg2-binary into 2.9.5

* Adding DoesNotExist import

* Finishing tests for export/json_report endpoint

* Deleted duplicate file - tests/report/json_report_test.py

---------

Co-authored-by: dzl-i <[email protected]>
  • Loading branch information
denzel-i and dzl-i authored Mar 12, 2023
1 parent 1a04d61 commit d0a3d1c
Show file tree
Hide file tree
Showing 3 changed files with 222 additions and 10 deletions.
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ weasyprint==58.1
beautifulsoup4==4.11.2
peewee==3.16.0
python-multipart==0.0.6
psycopg2-binary==2.8.6
psycopg2-binary==2.9.5
1 change: 1 addition & 0 deletions src/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from src.database import Users, Reports, Violations, Evaluations, db
from src.helpers import extract_text_from_invoice
from src.generation import generate_xslt_evaluation, generate_schema_evaluation, generate_wellformedness_evaluation
from peewee import DoesNotExist


def report_wellformedness_v1(invoice: Invoice) -> Evaluation:
Expand Down
229 changes: 220 additions & 9 deletions tests/export/json_report_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@

"""
=====================================
/report/json_report/v1 TESTS
/export/json_report/v1 TESTS
=====================================
"""

# Testing that the report was generated properly and matches input data
def test_json_valid_invoice():
invoice = Invoice(name="My Invoice", source="text", data=VALID_INVOICE_TEXT)
data = VALID_INVOICE_TEXT

invoice = Invoice(name="My Invoice", source="text", data=data)

report_id = invoice_upload_text_v1(invoice.name, invoice.data)["report_id"]
report = export_json_report_v1(report_id)
Expand All @@ -26,40 +28,249 @@ def test_json_valid_invoice():
# Checking for the name of the invoice
assert report.invoice_name == "My Invoice"

# Checking for invoice text
assert report.invoice_text == invoice.data

# Invoice hash must be a string
assert isinstance(report.invoice_hash, str)

# A valid invoice must be a valid report
assert report.is_valid

# A valid invoice should have 0 violations
assert report.total_warnings == 0
assert report.total_errors == 0

# Check for wellformedness
assert report.wellformedness_evaluation.is_valid == True
assert report.wellformedness_evaluation.num_rules_failed == 0
assert report.wellformedness_evaluation.num_errors == 0
assert report.wellformedness_evaluation.violations == []
assert report.wellformedness_evaluation.num_warnings == 0
assert len(report.wellformedness_evaluation.violations) == 0

# Check for schema
assert report.schema_evaluation.is_valid == True
assert report.schema_evaluation.num_rules_failed == 0
assert report.schema_evaluation.num_errors == 0
assert report.schema_evaluation.violations == []
assert report.schema_evaluation.num_warnings == 0
assert len(report.schema_evaluation.violations) == 0

# Check for syntax
assert report.syntax_evaluation.is_valid == True
assert report.syntax_evaluation.num_rules_failed == 0
assert report.syntax_evaluation.num_errors == 0
assert report.syntax_evaluation.violations == []
assert report.syntax_evaluation.num_warnings == 0
assert len(report.syntax_evaluation.violations) == 0

# Check for PEPPOL
assert report.peppol_evaluation.is_valid == True
assert report.peppol_evaluation.num_rules_failed == 0
assert report.peppol_evaluation.num_errors == 0
assert report.peppol_evaluation.violations == []
assert report.syntax_evaluation.num_warnings == 0
assert len(report.peppol_evaluation.violations) == 0

def test_json_unique_id():
data = VALID_INVOICE_TEXT

# Creating 2 invoices
invoice1 = Invoice(name="Invoice01", source="text", data=data)
invoice2 = Invoice(name="Invoice02", source="text", data=data)

# Creating 2 reports
report_id1 = invoice_upload_text_v1(invoice1.name, invoice1.data)["report_id"]
report1 = export_json_report_v1(report_id1)
report1 = Report(**report1)
report_id2 = invoice_upload_text_v1(invoice2.name, invoice2.data)["report_id"]
report2 = export_json_report_v1(report_id2)
report2 = Report(**report2)

# Check that the report_id is not the same
report1.report_id != report2.report_id

# Check names of the invoice
report1.invoice_name == "Invoice01"
report2.invoice_name == "Invoice02"

# Check that the hash is not the same
report1.invoice_hash != report2.invoice_hash

# Testing that a single rule fails when there is one error in the invoice
def test_json_single_violation():
data = VALID_INVOICE_TEXT

# Invalidating the currency code
data = invalidate_invoice(data, "attrib", "cbc:Amount", "currencyID", "TEST", 1)

invoice = Invoice(name="Invoice Test", source="text", data=data)

report_id = invoice_upload_text_v1(invoice.name, invoice.data)["report_id"]
report = export_json_report_v1(report_id)
report = Report(**report)

# Check the name of the invoice
assert report.invoice_name == "Invoice Test"

# Invalidating a currency code is fatal so report must not be valid
assert report.is_valid == False

# 1 error from syntax and 2 errors from peppol
assert report.total_warnings == 0
assert report.total_errors == 3

# Check for wellformedness
assert report.wellformedness_evaluation.is_valid == True
assert report.wellformedness_evaluation.num_rules_failed == 0
assert report.wellformedness_evaluation.num_errors == 0
assert report.wellformedness_evaluation.num_warnings == 0
assert len(report.wellformedness_evaluation.violations) == 0

# Check for schema
assert report.schema_evaluation.is_valid == True
assert report.schema_evaluation.num_rules_failed == 0
assert report.schema_evaluation.num_errors == 0
assert report.schema_evaluation.num_warnings == 0
assert len(report.schema_evaluation.violations) == 0

# Check for syntax
assert report.syntax_evaluation.is_valid == False
assert report.syntax_evaluation.num_rules_failed == 1
assert report.syntax_evaluation.num_errors == 1
assert report.syntax_evaluation.num_warnings == 0
assert len(report.syntax_evaluation.violations) == 1

# Check for PEPPOL
assert report.peppol_evaluation.is_valid == False
assert report.peppol_evaluation.num_rules_failed == 2
assert report.peppol_evaluation.num_errors == 2
assert report.syntax_evaluation.num_warnings == 0
assert len(report.peppol_evaluation.violations) == 2

# Testing that multiple violations are generated when there are multiple errors in the invoice
def test_json_multiple_violations_same_rule():
data = VALID_INVOICE_TEXT

# Invalidating the 2 ABNs
data = invalidate_invoice(data, "content", "cbc:EndpointID", "", "Not an ABN 1", 1)
data = invalidate_invoice(data, "content", "cbc:EndpointID", "", "Not an ABN 2", 2)

invoice = Invoice(name="Invoice Test", source="text", data=data)

report_id = invoice_upload_text_v1(invoice.name, invoice.data)["report_id"]
report = export_json_report_v1(report_id)
report = Report(**report)

# Invalidating an ABN is only a warning so report must still be valid
assert report.is_valid

# Invalidating 2 ABNs - 2 violation
assert report.total_warnings == 2
assert report.total_errors == 0

# Check for wellformedness
assert report.wellformedness_evaluation.is_valid == True
assert report.wellformedness_evaluation.num_rules_failed == 0
assert report.wellformedness_evaluation.num_errors == 0
assert report.wellformedness_evaluation.num_warnings == 0
assert len(report.wellformedness_evaluation.violations) == 0

# Check for schema
assert report.schema_evaluation.is_valid == True
assert report.schema_evaluation.num_rules_failed == 0
assert report.schema_evaluation.num_errors == 0
assert report.schema_evaluation.num_warnings == 0
assert len(report.schema_evaluation.violations) == 0

# Check for syntax
assert report.syntax_evaluation.is_valid == True
assert report.syntax_evaluation.num_rules_failed == 0
assert report.syntax_evaluation.num_errors == 0
assert report.syntax_evaluation.num_warnings == 0
assert len(report.syntax_evaluation.violations) == 0

# Check for PEPPOL
assert report.peppol_evaluation.is_valid == True
assert report.peppol_evaluation.num_rules_failed == 1
assert report.peppol_evaluation.num_errors == 0
assert report.peppol_evaluation.num_warnings == 2
assert len(report.peppol_evaluation.violations) == 2

def test_json_multiple_violations_different_rules():
data = VALID_INVOICE_TEXT

# Invalidating the 2 ABNs (PEPPOL)
data = invalidate_invoice(data, "content", "cbc:EndpointID", "", "Not an ABN 1", 1)
data = invalidate_invoice(data, "content", "cbc:EndpointID", "", "Not an ABN 2", 2)

# Invalidating the 2 Country/IdentificationCode (Syntax)
data = invalidate_invoice(data, 'content', 'cbc:IdentificationCode', '', 'TEST', 1)
data = invalidate_invoice(data, 'content', 'cbc:IdentificationCode', '', 'TEST', 2)

invoice = Invoice(name="Invoice Test", source="text", data=data)

report_id = invoice_upload_text_v1(invoice.name, invoice.data)["report_id"]
report = export_json_report_v1(report_id)
report = Report(**report)

# Invalidating an Country/IdentificationCode is fatal so report must be invalid
assert report.is_valid == False

# 2 Warnings and 2 Errors
assert report.total_warnings == 2
assert report.total_errors == 2

# Check for wellformedness
assert report.wellformedness_evaluation.is_valid == True
assert report.wellformedness_evaluation.num_rules_failed == 0
assert report.wellformedness_evaluation.num_errors == 0
assert report.wellformedness_evaluation.num_warnings == 0
assert len(report.wellformedness_evaluation.violations) == 0

# Check for schema
assert report.schema_evaluation.is_valid == True
assert report.schema_evaluation.num_rules_failed == 0
assert report.schema_evaluation.num_errors == 0
assert report.schema_evaluation.num_warnings == 0
assert len(report.schema_evaluation.violations) == 0

# Check for syntax
assert report.syntax_evaluation.is_valid == False
assert report.syntax_evaluation.num_rules_failed == 1
assert report.syntax_evaluation.num_errors == 2
assert report.syntax_evaluation.num_warnings == 0
assert len(report.syntax_evaluation.violations) == 2

# Check for PEPPOL
assert report.peppol_evaluation.is_valid == True
assert report.peppol_evaluation.num_rules_failed == 1
assert report.peppol_evaluation.num_errors == 0
assert report.peppol_evaluation.num_warnings == 2
assert len(report.peppol_evaluation.violations) == 2

# Testing invalid wellformedness
def test_json_invalid_wellformedness():
data = VALID_INVOICE_TEXT

# Removing a closing tag
data = remove_part_of_string(data, 11530, 11540)

invoice = Invoice(name="Invoice Test", source="text", data=data)

report_id = invoice_upload_text_v1(invoice.name, invoice.data)["report_id"]
report = export_json_report_v1(report_id)
report = Report(**report)

# Removing a closing tag is a fatal error
assert report.is_valid == False

# Violating 1 rule
assert report.total_warnings == 0
assert report.total_errors == 1

# Check for wellformedness
assert report.wellformedness_evaluation.is_valid == False
assert report.wellformedness_evaluation.num_rules_failed == 1
assert report.wellformedness_evaluation.num_errors == 1
assert report.wellformedness_evaluation.num_warnings == 0
assert len(report.wellformedness_evaluation.violations) == 1

# Invalid wellformedness means no schema, syntax and peppol evaluation
assert report.schema_evaluation == None
assert report.syntax_evaluation == None
assert report.peppol_evaluation == None

0 comments on commit d0a3d1c

Please sign in to comment.