Skip to content

Commit

Permalink
fix: Put back in common.py the function read_config(). Extend the u…
Browse files Browse the repository at this point in the history
…nit tests. (#36)

Signed-off-by: Nikos Livathinos <[email protected]>
  • Loading branch information
nikos-livathinos authored Oct 1, 2024
1 parent 3d88489 commit d0bdb22
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
10 changes: 10 additions & 0 deletions docling_ibm_models/tableformer/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,16 @@ def validate_config(config):
return True


def read_config(config_filename):
with open(config_filename, "r") as fd:
config = json.load(fd)

# Validate the config file
validate_config(config)

return config


def safe_get_parameter(input_dict, index_path, default=None, required=False):
r"""
Safe get parameter from a nested dictionary.
Expand Down
17 changes: 17 additions & 0 deletions tests/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@
# Copyright IBM Corp. 2024 - 2024
# SPDX-License-Identifier: MIT
#
import json
import tempfile

import docling_ibm_models.tableformer.common as c


test_config_a = {
"base_dir": "./tests/test_data/",
"curr_dir": "./tests/test_data/test_common/",
Expand Down Expand Up @@ -70,3 +74,16 @@ def test_config_validation():
assert val, "Valid configuration didn't pass the validation test"
except AssertionError:
assert i == 2, "Configuration validation error"

def test_read_config():
r"""
Testing the read_config() function
"""
with tempfile.NamedTemporaryFile(mode="w", suffix=".json", delete=False) as fp:
# Write a tmp file
json.dump(test_config_a, fp)
fp.close()

# Read the tmp file and extract the config
config = c.read_config(fp.name)
assert isinstance(config, dict)

0 comments on commit d0bdb22

Please sign in to comment.