Skip to content

Commit

Permalink
Pull request #140: Fix tests
Browse files Browse the repository at this point in the history
Merge in HYP/hypernetx from fix-tests to develop

* commit '1556756e368052c8e72e89fbf584dc4113ddfa05':
  Fix tests
  • Loading branch information
bonicim committed Feb 23, 2024
2 parents 6db4ac3 + 1556756 commit 6910e69
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 63 deletions.
13 changes: 9 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,25 @@ flake8:
format:
@$(PYTHON3) -m black hypernetx

## Test
## Tests

pre-commit:
pre-commit install
pre-commit run --all-files


test:
coverage run --source=hypernetx -m pytest
coverage report -m

test-ci:
@$(PYTHON3) -m tox

test-ci: lint-deps lint pre-commit test-deps test
test-ci-stash: lint-deps lint pre-commit test-deps test-ci

test-ci-github: lint-deps lint pre-commit ci-github-deps test-deps test
test-ci-github: lint-deps lint pre-commit ci-github-deps test-deps test-ci

.PHONY: test, test-ci, test-ci-github, pre-commit
.PHONY: pre-commit test test-ci, test-ci-stash, test-ci-github

## Continuous Deployment
## Assumes that scripts are run on a container or test server VM
Expand Down
17 changes: 0 additions & 17 deletions hypernetx/classes/tests/test_entityset_on_dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import pandas as pd
import numpy as np

from pytest_lazyfixture import lazy_fixture

from hypernetx import EntitySet


Expand Down Expand Up @@ -48,11 +46,6 @@ def test_remove(self, es_from_df):
@pytest.mark.parametrize(
"props, multidx, expected_props",
[
(
lazy_fixture("props_dataframe"),
(0, "P"),
{"prop1": "propval1", "prop2": "propval2"},
),
(
{0: {"P": {"prop1": "propval1", "prop2": "propval2"}}},
(0, "P"),
Expand All @@ -77,16 +70,6 @@ def test_assign_properties(self, es_from_df, props, multidx, expected_props):
@pytest.mark.parametrize(
"cell_props, multidx, expected_cell_properties",
[
(
lazy_fixture("cell_props_dataframe"),
("P", "A"),
{"prop1": "propval1", "prop2": "propval2"},
),
(
lazy_fixture("cell_props_dataframe_multidx"),
("P", "A"),
{"prop1": "propval1", "prop2": "propval2"},
),
(
{"P": {"A": {"prop1": "propval1", "prop2": "propval2"}}},
("P", "A"),
Expand Down
78 changes: 37 additions & 41 deletions hypernetx/classes/tests/test_entityset_on_dict.py
Original file line number Diff line number Diff line change
@@ -1,56 +1,52 @@
import numpy as np
import pytest

from pytest_lazyfixture import lazy_fixture

from hypernetx.classes import EntitySet


@pytest.mark.parametrize(
"entity, data, data_cols, labels",
[
(lazy_fixture("sbs_dict"), None, (0, 1), None),
(("sbs_dict"), None, (0, 1), None),
(
lazy_fixture("sbs_dict"),
("sbs_dict"),
None,
(0, 1),
lazy_fixture("sbs_labels"),
("sbs_labels"),
), # labels are ignored if entity is provided
(lazy_fixture("sbs_dict"), None, ["edges", "nodes"], None),
(lazy_fixture("sbs_dict"), lazy_fixture("sbs_data"), (0, 1), None),
(None, lazy_fixture("sbs_data"), (0, 1), lazy_fixture("sbs_labels")),
("sbs_dict", None, ["edges", "nodes"], None)
],
)
class TestEntitySBSDict:
"""Tests on different use cases for combination of the following params: entity, data, data_cols, labels"""

def test_size(self, entity, data, data_cols, labels, sbs):
es = EntitySet(entity=entity, data=data, data_cols=data_cols, labels=labels)
def test_size(self, entity, data, data_cols, labels, sbs, request):
es = EntitySet(entity=request.getfixturevalue(entity), data=data, data_cols=data_cols, labels=labels)
assert es.size() == len(sbs.edgedict)

# check all the EntitySet properties
def test_isstatic(self, entity, data, data_cols, labels, sbs):
es = EntitySet(entity=entity, data=data, data_cols=data_cols, labels=labels)
def test_isstatic(self, entity, data, data_cols, labels, sbs, request):
es = EntitySet(entity=request.getfixturevalue(entity), data=data, data_cols=data_cols, labels=labels)
assert es.isstatic

def test_uid(self, entity, data, data_cols, labels, sbs):
es = EntitySet(entity=entity, data=data, data_cols=data_cols, labels=labels)
def test_uid(self, entity, data, data_cols, labels, sbs, request):
es = EntitySet(entity=request.getfixturevalue(entity), data=data, data_cols=data_cols, labels=labels)
assert es.uid is None

def test_empty(self, entity, data, data_cols, labels, sbs):
es = EntitySet(entity=entity, data=data, data_cols=data_cols, labels=labels)
def test_empty(self, entity, data, data_cols, labels, sbs, request):
es = EntitySet(entity=request.getfixturevalue(entity), data=data, data_cols=data_cols, labels=labels)
assert not es.empty

def test_uidset(self, entity, data, data_cols, labels, sbs):
es = EntitySet(entity=entity, data=data, data_cols=data_cols, labels=labels)
def test_uidset(self, entity, data, data_cols, labels, sbs, request):
es = EntitySet(entity=request.getfixturevalue(entity), data=data, data_cols=data_cols, labels=labels)
assert es.uidset == {"I", "R", "S", "P", "O", "L"}

def test_dimsize(self, entity, data, data_cols, labels, sbs):
es = EntitySet(entity=entity, data=data, data_cols=data_cols, labels=labels)
def test_dimsize(self, entity, data, data_cols, labels, sbs, request):
es = EntitySet(entity=request.getfixturevalue(entity), data=data, data_cols=data_cols, labels=labels)
assert es.dimsize == 2

def test_elements(self, entity, data, data_cols, labels, sbs):
es = EntitySet(entity=entity, data=data, data_cols=data_cols, labels=labels)
def test_elements(self, entity, data, data_cols, labels, sbs, request):
es = EntitySet(entity=request.getfixturevalue(entity), data=data, data_cols=data_cols, labels=labels)
assert len(es.elements) == 6
expected_elements = {
"I": ["K", "T2"],
Expand All @@ -64,8 +60,8 @@ def test_elements(self, entity, data, data_cols, labels, sbs):
assert expected_edge in es.elements
assert es.elements[expected_edge].sort() == expected_nodes.sort()

def test_incident_dict(self, entity, data, data_cols, labels, sbs):
es = EntitySet(entity=entity, data=data, data_cols=data_cols, labels=labels)
def test_incident_dict(self, entity, data, data_cols, labels, sbs, request):
es = EntitySet(entity=request.getfixturevalue(entity), data=data, data_cols=data_cols, labels=labels)
expected_incident_dict = {
"I": ["K", "T2"],
"L": ["E", "C"],
Expand All @@ -81,12 +77,12 @@ def test_incident_dict(self, entity, data, data_cols, labels, sbs):
assert "I" in es
assert "K" in es

def test_children(self, entity, data, data_cols, labels, sbs):
es = EntitySet(entity=entity, data=data, data_cols=data_cols, labels=labels)
def test_children(self, entity, data, data_cols, labels, sbs, request):
es = EntitySet(entity=request.getfixturevalue(entity), data=data, data_cols=data_cols, labels=labels)
assert es.children == {"C", "T1", "A", "K", "T2", "V", "E"}

def test_memberships(self, entity, data, data_cols, labels, sbs):
es = EntitySet(entity=entity, data=data, data_cols=data_cols, labels=labels)
def test_memberships(self, entity, data, data_cols, labels, sbs, request):
es = EntitySet(entity=request.getfixturevalue(entity), data=data, data_cols=data_cols, labels=labels)
assert es.memberships == {
"A": ["P", "R", "S"],
"C": ["P", "L"],
Expand All @@ -97,15 +93,15 @@ def test_memberships(self, entity, data, data_cols, labels, sbs):
"V": ["S"],
}

def test_cell_properties(self, entity, data, data_cols, labels, sbs):
es = EntitySet(entity=entity, data=data, data_cols=data_cols, labels=labels)
def test_cell_properties(self, entity, data, data_cols, labels, sbs, request):
es = EntitySet(entity=request.getfixturevalue(entity), data=data, data_cols=data_cols, labels=labels)
assert es.cell_properties.shape == (
15,
1,
)

def test_cell_weights(self, entity, data, data_cols, labels, sbs):
es = EntitySet(entity=entity, data=data, data_cols=data_cols, labels=labels)
def test_cell_weights(self, entity, data, data_cols, labels, sbs, request):
es = EntitySet(entity=request.getfixturevalue(entity), data=data, data_cols=data_cols, labels=labels)
assert es.cell_weights == {
("P", "C"): 1,
("P", "K"): 1,
Expand All @@ -124,8 +120,8 @@ def test_cell_weights(self, entity, data, data_cols, labels, sbs):
("I", "T2"): 1,
}

def test_labels(self, entity, data, data_cols, labels, sbs):
es = EntitySet(entity=entity, data=data, data_cols=data_cols, labels=labels)
def test_labels(self, entity, data, data_cols, labels, sbs, request):
es = EntitySet(entity=request.getfixturevalue(entity), data=data, data_cols=data_cols, labels=labels)
# check labeling based on given attributes for EntitySet
if data_cols == [
"edges",
Expand All @@ -145,8 +141,8 @@ def test_labels(self, entity, data, data_cols, labels, sbs):
1: ["A", "C", "E", "K", "T1", "T2", "V"],
}

def test_dataframe(self, entity, data, data_cols, labels, sbs):
es = EntitySet(entity=entity, data=data, data_cols=data_cols, labels=labels)
def test_dataframe(self, entity, data, data_cols, labels, sbs, request):
es = EntitySet(entity=request.getfixturevalue(entity), data=data, data_cols=data_cols, labels=labels)
# check dataframe
# size should be the number of rows times the number of columns, i.e 15 x 3
assert es.dataframe.size == 45
Expand All @@ -159,8 +155,8 @@ def test_dataframe(self, entity, data, data_cols, labels, sbs):
assert actual_node_row0 in ["A", "C", "K"]
assert actual_cell_weight_row0 == 1

def test_data(self, entity, data, data_cols, labels, sbs):
es = EntitySet(entity=entity, data=data, data_cols=data_cols, labels=labels)
def test_data(self, entity, data, data_cols, labels, sbs, request):
es = EntitySet(entity=request.getfixturevalue(entity), data=data, data_cols=data_cols, labels=labels)

actual_data = es.data

Expand Down Expand Up @@ -189,16 +185,16 @@ def test_data(self, entity, data, data_cols, labels, sbs):
np.sort(actual_data, axis=0), np.sort(expected_data, axis=0)
)

def test_properties(self, entity, data, data_cols, labels, sbs):
es = EntitySet(entity=entity, data=data, data_cols=data_cols, labels=labels)
def test_properties(self, entity, data, data_cols, labels, sbs, request):
es = EntitySet(entity=request.getfixturevalue(entity), data=data, data_cols=data_cols, labels=labels)
assert (
es.properties.size == 39
) # Properties has three columns and 13 rows of data (i.e. edges + nodes)
assert list(es.properties.columns) == ["uid", "weight", "properties"]


@pytest.mark.xfail(reason="Deprecated; to be removed in next released")
def test_level(sbs):
def test_level(sbs, request):
# at some point we are casting out and back to categorical dtype without
# preserving categories ordering from `labels` provided to constructor
ent_sbs = EntitySet(data=np.asarray(sbs.data), labels=sbs.labels)
Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ allowlist_externals = env
commands =
env
python --version
coverage run --source=hypernetx -m pytest
coverage run --source=hypernetx -m pytest --junitxml=pytest.xml
coverage report -m

[testenv:py38-notebooks]
Expand Down

0 comments on commit 6910e69

Please sign in to comment.