From 6f638779a123d6c600a3e400e740ac8145e8bdff Mon Sep 17 00:00:00 2001 From: James Date: Tue, 21 Sep 2021 15:09:15 +0100 Subject: [PATCH] bugfix: crash when statementID is not a string https://github.com/openownership/cove-bods/issues/67 --- CHANGELOG.md | 4 ++++ libcovebods/lib/common_checks.py | 2 +- tests/fixtures/0.2/bad_statement_id_type.json | 24 +++++++++++++++++++ tests/test_api_0_2.py | 19 +++++++++++++++ 4 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 tests/fixtures/0.2/bad_statement_id_type.json diff --git a/CHANGELOG.md b/CHANGELOG.md index a80b05d..3cad231 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ## [Unreleased] +### Fixed + +- A crash when statementID is not a string https://github.com/openownership/cove-bods/issues/67 + ## [0.10.0] - 2021-04-08 ### Changed diff --git a/libcovebods/lib/common_checks.py b/libcovebods/lib/common_checks.py index 3aaabc1..d673e3e 100644 --- a/libcovebods/lib/common_checks.py +++ b/libcovebods/lib/common_checks.py @@ -140,7 +140,7 @@ def run(self): count_replaces_statements_missing += 1 if replaces_statement_id in current_statement_ids: current_statement_ids.remove(replaces_statement_id) - if 'statementID' in statement: + if 'statementID' in statement and isinstance(statement['statementID'], str): statement_ids.add(statement['statementID']) # Return Results diff --git a/tests/fixtures/0.2/bad_statement_id_type.json b/tests/fixtures/0.2/bad_statement_id_type.json new file mode 100644 index 0000000..895bbb8 --- /dev/null +++ b/tests/fixtures/0.2/bad_statement_id_type.json @@ -0,0 +1,24 @@ +[ + { + "statementID": {}, + "statementType": "entityStatement", + "isComponent": false, + "statementDate": "2017-11-18", + "entityType": "registeredEntity", + "name": "CHRINON LTD", + "foundingDate": "2010-11-18", + "identifiers": [ + { + "scheme": "GB-COH", + "id": "07444723" + } + ], + "publicationDetails": { + "publicationDate": "2018-02-13", + "bodsVersion": "0.2", + "publisher": { + "name": "CHRINON LTD" + } + } + } +] diff --git a/tests/test_api_0_2.py b/tests/test_api_0_2.py index 20450c6..2611bae 100644 --- a/tests/test_api_0_2.py +++ b/tests/test_api_0_2.py @@ -1,3 +1,4 @@ +import json import tempfile import os from libcovebods.api import bods_json_output @@ -33,3 +34,21 @@ def test_iscompontent_os_03_dr_03(): assert results['validation_errors_count'] == 0 assert results['additional_fields_count'] == 0 assert results['additional_checks_count'] == 0 + + +def test_bad_statement_id_type(): + + cove_temp_folder = tempfile.mkdtemp(prefix='lib-cove-bods-tests-', dir=tempfile.gettempdir()) + json_filename = os.path.join(os.path.dirname( + os.path.realpath(__file__)), 'fixtures', '0.2', 'bad_statement_id_type.json' + ) + + results = bods_json_output(cove_temp_folder, json_filename) + + assert results['schema_version'] == '0.2' + assert results['validation_errors_count'] == 1 + validation_error = json.loads(results['validation_errors'][0][0]) + assert validation_error['message'].startswith("'statementID' should be a string.") + assert results['additional_fields_count'] == 0 + assert results['additional_checks_count'] == 0 +