This repository has been archived by the owner on Sep 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27 from climatepolicyradar/feature/rnd-219-update…
…-block-types-and-parser-output-methods-in-data-access Azure Data Model Experimental Updates
- Loading branch information
Showing
5 changed files
with
212 additions
and
19 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import unittest | ||
|
||
import pydantic | ||
|
||
from cpr_data_access.parser_models import ( | ||
ParserOutput, | ||
CONTENT_TYPE_PDF, | ||
CONTENT_TYPE_HTML, | ||
) | ||
|
||
|
||
def test_parser_output_object(parser_output_json): | ||
""" | ||
Test that we correctly instantiate the parser output object. | ||
Also test the methods on the parser output object. | ||
""" | ||
|
||
# Instantiate the parser output object | ||
ParserOutput.parse_obj(parser_output_json) | ||
|
||
# Test the optional fields | ||
parser_output_empty_fields = parser_output_json.copy() | ||
parser_output_empty_fields["document_metadata"] = {} | ||
parser_output_empty_fields["document_cdn_object"] = None | ||
parser_output_empty_fields["document_md5_sum"] = None | ||
|
||
ParserOutput.parse_obj(parser_output_empty_fields) | ||
|
||
# Test the check html pdf metadata method | ||
parser_output_no_pdf_data = parser_output_json.copy() | ||
parser_output_no_pdf_data["pdf_data"] = None | ||
parser_output_no_pdf_data["document_content_type"] = CONTENT_TYPE_PDF | ||
|
||
with unittest.TestCase().assertRaises( | ||
pydantic.error_wrappers.ValidationError | ||
) as context: | ||
ParserOutput.parse_obj(parser_output_no_pdf_data) | ||
assert "pdf_data must be set for PDF documents" in str(context.exception) | ||
|
||
parser_output_no_html_data = parser_output_json.copy() | ||
parser_output_no_html_data["html_data"] = None | ||
parser_output_no_html_data["document_content_type"] = CONTENT_TYPE_HTML | ||
|
||
with unittest.TestCase().assertRaises( | ||
pydantic.error_wrappers.ValidationError | ||
) as context: | ||
ParserOutput.parse_obj(parser_output_no_html_data) | ||
assert "html_data must be set for HTML documents" in str(context.exception) | ||
|
||
parser_output_no_content_type = parser_output_json.copy() | ||
# PDF data is set as the default | ||
parser_output_no_content_type["document_content_type"] = None | ||
|
||
with unittest.TestCase().assertRaises( | ||
pydantic.error_wrappers.ValidationError | ||
) as context: | ||
ParserOutput.parse_obj(parser_output_no_content_type) | ||
assert ( | ||
"html_data and pdf_data must be null for documents with no content type." | ||
) in str(context.exception) | ||
|
||
parser_output_not_known_content_type = parser_output_json.copy() | ||
# PDF data is set as the default | ||
parser_output_not_known_content_type["document_content_type"] = "not_known" | ||
|
||
with unittest.TestCase().assertRaises( | ||
pydantic.error_wrappers.ValidationError | ||
) as context: | ||
ParserOutput.parse_obj(parser_output_not_known_content_type) | ||
assert ( | ||
"html_data and pdf_data must be null for documents with no content type." | ||
) in str(context.exception) | ||
|
||
# Test the text blocks property | ||
assert ParserOutput.parse_obj(parser_output_json).text_blocks != [] | ||
parser_output_no_data = parser_output_json.copy() | ||
parser_output_no_data["pdf_data"] = None | ||
parser_output_no_data["document_content_type"] = None | ||
assert ParserOutput.parse_obj(parser_output_no_data).text_blocks == [] | ||
|
||
# Test the to string method | ||
assert ParserOutput.parse_obj(parser_output_json).to_string() != "" | ||
assert ParserOutput.parse_obj(parser_output_no_data).to_string() == "" |