Skip to content

Commit

Permalink
Add additional tests
Browse files Browse the repository at this point in the history
  • Loading branch information
FraLotito committed Jan 27, 2025
1 parent 8285460 commit 5ddc80f
Show file tree
Hide file tree
Showing 11 changed files with 92 additions and 31 deletions.
52 changes: 21 additions & 31 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ def validator():
def empty():
return json.load(open(f"{bad_json_dir}/empty.json", "r"))


# test top-level
@pytest.fixture
def bad_top_level_field():
Expand All @@ -41,6 +40,10 @@ def metadata_as_list():
def metadata_with_nested_attributes():
return json.load(open(f"{good_json_dir}/metadata_with_nested_attributes.json", "r"))

@pytest.fixture
def metadata_with_deeply_nested_attributes():
return json.load(open(f"{good_json_dir}/metadata_with_deeply_nested_attributes.json", "r"))


# test nodes
@pytest.fixture
Expand Down Expand Up @@ -83,6 +86,9 @@ def single_incidence_with_weight_as_string():
def empty_hypergraph():
return json.load(open(f"{good_json_dir}/empty_hypergraph.json", "r"))

@pytest.fixture
def empty_arrays():
return json.load(open(f"{good_json_dir}/empty_arrays.json", "r"))

# test nodes
@pytest.fixture
Expand Down Expand Up @@ -133,6 +139,12 @@ def missing_required_field_incidence():
def bad_node_float():
return json.load(open(f"{bad_json_dir}/bad_node_float.json", "r"))

### Tests for duplicated items

@pytest.fixture
def duplicated_nodes_edges():
return json.load(open(f"{good_json_dir}/duplicated_nodes_edges.json", "r"))


### Tests for directionality

Expand All @@ -141,53 +153,31 @@ def bad_node_float():
@pytest.fixture
def valid_direction_head():
"""Fixture for a valid incidence with direction 'head'."""
return {
"network-type": "directed",
"metadata": {},
"incidences": [{"edge": 1, "node": 2, "direction": "head"}]
}
return json.load(open(f"{good_json_dir}/valid_incidence_head.json", "r"))

@pytest.fixture
def valid_direction_tail():
"""Fixture for a valid incidence with direction 'tail'."""
return {
"network-type": "directed",
"metadata": {},
"incidences": [{"edge": 1, "node": 2, "direction": "tail"}]
}
return json.load(open(f"{good_json_dir}/valid_incidence_tail.json", "r"))

@pytest.fixture
def missing_direction():
"""Fixture for a valid incidence without the direction field."""
return {
"network-type": "directed",
"metadata": {},
"incidences": [{"edge": 1, "node": 2}]
}

# This should be a valid incidence unless the schema is updated to enforce the direction field
return json.load(open(f"{good_json_dir}/missing_direction.json", "r"))

@pytest.fixture
def invalid_direction_value():
"""Fixture for an invalid direction value."""
return {
"network-type": "directed",
"metadata": {},
"incidences": [{"edge": 1, "node": 2, "direction": "invalid_value"}]
}
return json.load(open(f"{bad_json_dir}/invalid_direction_value.json", "r"))

@pytest.fixture
def missing_required_fields_with_direction():
"""Fixture for incidences with direction but missing required fields."""
return {
"network-type": "directed",
"metadata": {},
"incidences": [{"direction": "head"}]
}
return json.load(open(f"{bad_json_dir}/missing_required_fields_with_direction.json", "r"))

@pytest.fixture
def extra_fields_with_direction():
"""Fixture for incidences with extra fields."""
return {
"network-type": "directed",
"metadata": {},
"incidences": [{"edge": 1, "node": 2, "direction": "head", "extra_field": "value"}]
}
return json.load(open(f"{bad_json_dir}/extra_fields_with_direction.json", "r"))
12 changes: 12 additions & 0 deletions tests/test_compliant_files_against_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,16 @@
def test_empty_hypergraph(validator, empty_hypergraph):
validator(empty_hypergraph)

def test_empty_arrays(validator, empty_arrays):
validator(empty_arrays)

# test metadata
def test_nested_attributes_validation(validator, metadata_with_nested_attributes):
validator(metadata_with_nested_attributes)

def metadata_with_deeply_nested_attributes(validator, deeply_nested_metadata):
validator(deeply_nested_metadata)


# test nodes
def test_single_node(validator, single_node):
Expand Down Expand Up @@ -41,6 +46,13 @@ def test_single_incidence_with_weights(validator, single_incidence_with_weights)
def test_single_incidence_with_attrs(validator, single_incidence_with_attrs):
validator(single_incidence_with_attrs)

# test duplicated
def test_duplicated_nodes_edges(validator, duplicated_nodes_edges):
"""Test with repeated nodes, edges, and incidences."""
validator(duplicated_nodes_edges) # Expect to pass unless uniqueness is enforced

# test direction


def test_valid_direction_head(validator, valid_direction_head):
"""Test a valid incidence with direction 'head'."""
Expand Down
7 changes: 7 additions & 0 deletions tests/test_files/HIF-compliant/duplicated_nodes_edges.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"network-type": "undirected",
"metadata": {},
"nodes": [{"node": "n1"}, {"node": "n1"}],
"edges": [{"edge": "e1"}, {"edge": "e1"}],
"incidences": [{"edge": "e1", "node": "n1"}, {"edge": "e1", "node": "n1"}]
}
7 changes: 7 additions & 0 deletions tests/test_files/HIF-compliant/empty_arrays.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"network-type": "undirected",
"metadata": {},
"incidences": [],
"nodes": [],
"edges": []
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"network-type": "asc",
"metadata": {
"level1": {
"level2": {
"level3": {
"key": "value"
}
}
}
},
"incidences": [{"edge": 1, "node": 2}],
"nodes": [{"node": "n1", "attrs": {"nested_attr": {"key1": "value1"}}}],
"edges": [{"edge": "e1", "attrs": {"nested_attr": {"key2": "value2"}}}]
}
5 changes: 5 additions & 0 deletions tests/test_files/HIF-compliant/missing_direction.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"network-type": "directed",
"metadata": {},
"incidences": [{"edge": 1, "node": 2}]
}
5 changes: 5 additions & 0 deletions tests/test_files/HIF-compliant/valid_incidence_head.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"network-type": "directed",
"metadata": {},
"incidences": [{"edge": 1, "node": 2, "direction": "head"}]
}
5 changes: 5 additions & 0 deletions tests/test_files/HIF-compliant/valid_incidence_tail.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"network-type": "directed",
"metadata": {},
"incidences": [{"edge": 1, "node": 2, "direction": "tail"}]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"network-type": "directed",
"metadata": {},
"incidences": [{"edge": 1, "node": 2, "direction": "head", "extra_field": "value"}]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"network-type": "directed",
"metadata": {},
"incidences": [{"edge": 1, "node": 2, "direction": "invalid_value"}]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"network-type": "directed",
"metadata": {},
"incidences": [{"direction": "head"}]
}

0 comments on commit 5ddc80f

Please sign in to comment.