Skip to content

Commit

Permalink
Make extra field check optional and improve logic
Browse files Browse the repository at this point in the history
  • Loading branch information
amc-corey-cox committed Nov 11, 2024
1 parent 02da771 commit 9b376a2
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 9 deletions.
8 changes: 4 additions & 4 deletions src/koza/io/writer/writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def __init__(
node_properties: List[str] = None,
edge_properties: List[str] = None,
sssom_config: SSSOMConfig = None,
skip_checks: bool = False,
check_fields: bool = False,
kwargs: dict = None,
):
"""Do not override this method; implement `init` instead."""
Expand All @@ -28,7 +28,7 @@ def __init__(
self.node_properties = node_properties
self.edge_properties = edge_properties
self.sssom_config = sssom_config
self.skip_checks = skip_checks
self.check_fields = check_fields
self.converter = KGXConverter()

kwargs = kwargs or {}
Expand All @@ -39,15 +39,15 @@ def write(self, entities: Iterable):

if nodes:
for node in nodes:
if not self.skip_checks:
if self.check_fields:
self.check_extra_fields(tuple(node.keys()), tuple(self.node_properties))
self.write_node(node)

if edges:
for edge in edges:
if self.sssom_config:
edge = self.sssom_config.apply_mapping(edge)
if not self.skip_checks:
if self.check_fields:
self.check_extra_fields(tuple(edge.keys()), tuple(self.edge_properties))
self.write_edge(edge)

Expand Down
2 changes: 1 addition & 1 deletion tests/unit/test_tsvwriter_node_and_edge.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def test_tsv_writer():
outdir = "output/tests"
outfile = "tsvwriter-node-and-edge"

t = TSVWriter(outdir, outfile, node_properties, edge_properties)
t = TSVWriter(outdir, outfile, node_properties, edge_properties, check_fields=True)
t.write(ent)
t.finalize()

Expand Down
4 changes: 2 additions & 2 deletions tests/unit/test_tsvwriter_node_and_edge_extra_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def test_tsv_writer_extra_node_params():
outdir = "output/tests"
outfile = "tsvwriter-node-and-edge"

t = TSVWriter(outdir, outfile, node_properties, edge_properties)
t = TSVWriter(outdir, outfile, node_properties, edge_properties, check_fields=True)
expected_message = "Extra fields found in row: ['deprecated', 'has_attribute', 'name']"
with pytest.raises(ValueError, match=re.escape(expected_message)):
t.write(ent)
Expand Down Expand Up @@ -184,7 +184,7 @@ def test_tsv_writer_extra_edge_params():
outdir = "output/tests"
outfile = "tsvwriter-node-and-edge"

t = TSVWriter(outdir, outfile, node_properties, edge_properties)
t = TSVWriter(outdir, outfile, node_properties, edge_properties, check_fields=True)
expected_message = "Extra fields found in row: ['qualifier', 'retrieval_source_ids', 'subject_aspect_qualifier']"
with pytest.raises(ValueError, match=re.escape(expected_message)):
t.write(ent)
2 changes: 1 addition & 1 deletion tests/unit/test_tsvwriter_node_only.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def test_tsv_writer():
outdir = "output/tests"
outfile = "tsvwriter-node-only"

t = TSVWriter(outdir, outfile, node_properties)
t = TSVWriter(outdir, outfile, node_properties, check_fields=True)
t.write(ent)
t.finalize()

Expand Down
2 changes: 1 addition & 1 deletion tests/unit/test_tsvwriter_node_only_extra_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def test_tsv_writer():

t = TSVWriter(outdir, outfile, node_properties)

t = TSVWriter(outdir, outfile, node_properties)
t = TSVWriter(outdir, outfile, node_properties, check_fields=True)
expected_message = "Extra fields found in row: ['has_attribute', 'name']"
with pytest.raises(ValueError, match=re.escape(expected_message)):
t.write(ent)

0 comments on commit 9b376a2

Please # to comment.