From 2bd2c84c16e38f20c333703026131b0d133db5df Mon Sep 17 00:00:00 2001 From: Jakob Kraus Date: Wed, 16 Oct 2024 16:00:08 +0200 Subject: [PATCH] fix: linting issues --- medmodels/medrecord/__init__.py | 14 +- medmodels/medrecord/_overview.py | 30 +- medmodels/medrecord/indexers.py | 14 +- medmodels/medrecord/medrecord.py | 2 +- medmodels/medrecord/querying.py | 3 +- medmodels/medrecord/schema.py | 10 +- medmodels/medrecord/tests/test_builder.py | 7 +- medmodels/medrecord/tests/test_datatype.py | 2 +- medmodels/medrecord/tests/test_indexers.py | 770 +++++++++----------- medmodels/medrecord/tests/test_medrecord.py | 406 +++++------ medmodels/medrecord/tests/test_overview.py | 100 ++- 11 files changed, 623 insertions(+), 735 deletions(-) diff --git a/medmodels/medrecord/__init__.py b/medmodels/medrecord/__init__.py index 87cbbb9..bca5a7c 100644 --- a/medmodels/medrecord/__init__.py +++ b/medmodels/medrecord/__init__.py @@ -25,21 +25,21 @@ "Bool", "DateTime", "EdgeIndex", + "EdgeIndex", + "EdgeOperand", + "EdgeQuery", "Float", "GroupSchema", + "GroupSchema", "Int", "MedRecord", "NodeIndex", + "NodeIndex", + "NodeOperand", + "NodeQuery", "Null", "Option", "Schema", - "GroupSchema", - "NodeIndex", - "EdgeIndex", - "EdgeQuery", - "NodeQuery", - "NodeOperand", - "EdgeOperand", "String", "Union", ] diff --git a/medmodels/medrecord/_overview.py b/medmodels/medrecord/_overview.py index f487646..9b6478d 100644 --- a/medmodels/medrecord/_overview.py +++ b/medmodels/medrecord/_overview.py @@ -1,21 +1,25 @@ +from __future__ import annotations + import copy from datetime import datetime -from typing import Dict, List, Optional, Union +from typing import TYPE_CHECKING, Dict, List, Optional, Union import polars as pl from medmodels.medrecord.schema import AttributesSchema, AttributeType -from medmodels.medrecord.types import ( - AttributeInfo, - Attributes, - EdgeIndex, - Group, - MedRecordAttribute, - NodeIndex, - NumericAttributeInfo, - StringAttributeInfo, - TemporalAttributeInfo, -) + +if TYPE_CHECKING: + from medmodels.medrecord.types import ( + AttributeInfo, + Attributes, + EdgeIndex, + Group, + MedRecordAttribute, + NodeIndex, + NumericAttributeInfo, + StringAttributeInfo, + TemporalAttributeInfo, + ) def extract_attribute_summary( @@ -210,7 +214,7 @@ def prettify_table( row[2] = str(attribute) if first_line else "" # displaying info based on the type - if "values" in info.keys(): + if "values" in info: row[3] = info[key] else: if isinstance(info[key], float): diff --git a/medmodels/medrecord/indexers.py b/medmodels/medrecord/indexers.py index ad2f02f..034f33c 100644 --- a/medmodels/medrecord/indexers.py +++ b/medmodels/medrecord/indexers.py @@ -2,7 +2,6 @@ from typing import TYPE_CHECKING, Callable, Dict, Tuple, Union, overload -from medmodels.medrecord.querying import EdgeQuery, NodeQuery from medmodels.medrecord.types import ( Attributes, AttributesInput, @@ -22,6 +21,7 @@ if TYPE_CHECKING: from medmodels import MedRecord + from medmodels.medrecord.querying import EdgeQuery, NodeQuery class NodeIndexer: @@ -63,7 +63,7 @@ def __getitem__( key: Tuple[Union[NodeIndexInputList, NodeQuery, slice], MedRecordAttribute], ) -> Dict[NodeIndex, MedRecordValue]: ... - def __getitem__( + def __getitem__( # noqa: C901 self, key: Union[ NodeIndex, @@ -252,7 +252,7 @@ def __setitem__( value: MedRecordValue, ) -> None: ... - def __setitem__( + def __setitem__( # noqa: C901 self, key: Union[ NodeIndex, @@ -523,7 +523,7 @@ def __setitem__( return None return None - def __delitem__( + def __delitem__( # noqa: C901 self, key: Tuple[ Union[NodeIndex, NodeIndexInputList, NodeQuery, slice], @@ -712,7 +712,7 @@ def __getitem__( key: Tuple[Union[EdgeIndexInputList, EdgeQuery, slice], MedRecordAttribute], ) -> Dict[EdgeIndex, MedRecordValue]: ... - def __getitem__( + def __getitem__( # noqa: C901 self, key: Union[ EdgeIndex, @@ -901,7 +901,7 @@ def __setitem__( value: MedRecordValue, ) -> None: ... - def __setitem__( + def __setitem__( # noqa: C901 self, key: Union[ EdgeIndex, @@ -1170,7 +1170,7 @@ def __setitem__( return None return None - def __delitem__( + def __delitem__( # noqa: C901 self, key: Tuple[ Union[EdgeIndex, EdgeIndexInputList, EdgeQuery, slice], diff --git a/medmodels/medrecord/medrecord.py b/medmodels/medrecord/medrecord.py index 063ef32..1f2de72 100644 --- a/medmodels/medrecord/medrecord.py +++ b/medmodels/medrecord/medrecord.py @@ -86,7 +86,7 @@ def __init__( data: Dict[Group, AttributeInfo], group_header: str, decimal: int, - ): + ) -> None: """Initializes the OverviewTable class. Args: diff --git a/medmodels/medrecord/querying.py b/medmodels/medrecord/querying.py index 5794c6b..0e37788 100644 --- a/medmodels/medrecord/querying.py +++ b/medmodels/medrecord/querying.py @@ -1,6 +1,5 @@ from __future__ import annotations -import sys from enum import Enum from typing import TYPE_CHECKING, Callable, List, Union @@ -27,6 +26,8 @@ ) if TYPE_CHECKING: + import sys + if sys.version_info >= (3, 10): from typing import TypeAlias else: diff --git a/medmodels/medrecord/schema.py b/medmodels/medrecord/schema.py index 7d704e9..4641d10 100644 --- a/medmodels/medrecord/schema.py +++ b/medmodels/medrecord/schema.py @@ -1,7 +1,7 @@ from __future__ import annotations from enum import Enum, auto -from typing import TYPE_CHECKING, Dict, List, Optional, Tuple, Union, overload +from typing import TYPE_CHECKING, Dict, Iterator, List, Optional, Tuple, Union, overload from medmodels._medmodels import ( PyAttributeDataType, @@ -143,7 +143,7 @@ def __contains__(self, key: MedRecordAttribute) -> bool: """ return key in self._attributes_schema - def __iter__(self): + def __iter__(self) -> Iterator[MedRecordAttribute]: """Returns an iterator over the attributes schema. Returns: @@ -190,7 +190,7 @@ def __eq__(self, value: object) -> bool: return True - def keys(self): + def keys(self): # noqa: ANN201 """Returns the attribute keys in the schema. Returns: @@ -198,7 +198,7 @@ def keys(self): """ return self._attributes_schema.keys() - def values(self): + def values(self): # noqa: ANN201 """Returns the attribute values in the schema. Returns: @@ -206,7 +206,7 @@ def values(self): """ return self._attributes_schema.values() - def items(self): + def items(self): # noqa: ANN201 """Returns the attribute key-value pairs in the schema. Returns: diff --git a/medmodels/medrecord/tests/test_builder.py b/medmodels/medrecord/tests/test_builder.py index bc92ac0..d838c81 100644 --- a/medmodels/medrecord/tests/test_builder.py +++ b/medmodels/medrecord/tests/test_builder.py @@ -1,5 +1,7 @@ import unittest +import pytest + import medmodels.medrecord as mr @@ -60,7 +62,10 @@ def test_with_schema(self) -> None: medrecord.add_nodes(("node1", {"attribute": 1})) - with self.assertRaises(ValueError): + with pytest.raises( + ValueError, + match="Attribute attribute of node with index node2 is of type String. Expected Integer.", + ): medrecord.add_nodes(("node2", {"attribute": "1"})) diff --git a/medmodels/medrecord/tests/test_datatype.py b/medmodels/medrecord/tests/test_datatype.py index 6fd3c1c..05a6bb1 100644 --- a/medmodels/medrecord/tests/test_datatype.py +++ b/medmodels/medrecord/tests/test_datatype.py @@ -116,7 +116,7 @@ def test_union(self) -> None: assert mr.Union(mr.String(), mr.Int()) != mr.Union(mr.Int(), mr.String()) def test_invalid_union(self) -> None: - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="Union must have at least two arguments"): mr.Union(mr.String()) def test_option(self) -> None: diff --git a/medmodels/medrecord/tests/test_indexers.py b/medmodels/medrecord/tests/test_indexers.py index 490fb4a..c663573 100644 --- a/medmodels/medrecord/tests/test_indexers.py +++ b/medmodels/medrecord/tests/test_indexers.py @@ -6,7 +6,7 @@ from medmodels.medrecord.querying import EdgeOperand, NodeOperand -def create_medrecord(): +def create_medrecord() -> MedRecord: return MedRecord.from_tuples( [ (0, {"foo": "bar", "bar": "foo", "lorem": "ipsum"}), @@ -23,27 +23,27 @@ def create_medrecord(): ) -def node_greater_than_or_equal_two(node: NodeOperand): +def node_greater_than_or_equal_two(node: NodeOperand) -> None: node.index().greater_than_or_equal_to(2) -def node_greater_than_three(node: NodeOperand): +def node_greater_than_three(node: NodeOperand) -> None: node.index().greater_than(3) -def node_less_than_two(node: NodeOperand): +def node_less_than_two(node: NodeOperand) -> None: node.index().less_than(2) -def edge_greater_than_or_equal_two(edge: EdgeOperand): +def edge_greater_than_or_equal_two(edge: EdgeOperand) -> None: edge.index().greater_than_or_equal_to(2) -def edge_greater_than_three(edge: EdgeOperand): +def edge_greater_than_three(edge: EdgeOperand) -> None: edge.index().greater_than(3) -def edge_less_than_two(edge: EdgeOperand): +def edge_less_than_two(edge: EdgeOperand) -> None: edge.index().less_than(2) @@ -71,11 +71,11 @@ def test_node_getitem(self) -> None: assert medrecord.node[0, :] == {"foo": "bar", "bar": "foo", "lorem": "ipsum"} - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="Invalid slice, only ':' is allowed"): medrecord.node[0, 1:] - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="Invalid slice, only ':' is allowed"): medrecord.node[0, :1] - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="Invalid slice, only ':' is allowed"): medrecord.node[0, ::1] assert medrecord.node[[0, 1]] == { @@ -114,62 +114,55 @@ def test_node_getitem(self) -> None: 1: {"foo": "bar", "bar": "foo"}, } - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="Invalid slice, only ':' is allowed"): medrecord.node[[0, 1], 1:] - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="Invalid slice, only ':' is allowed"): medrecord.node[[0, 1], :1] - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="Invalid slice, only ':' is allowed"): medrecord.node[[0, 1], ::1] - self.assertEqual( - {2: {"foo": "bar", "bar": "foo"}, 3: {"foo": "bar", "bar": "test"}}, - medrecord.node[node_greater_than_or_equal_two], - ) + assert medrecord.node[node_greater_than_or_equal_two] == { + 2: {"foo": "bar", "bar": "foo"}, + 3: {"foo": "bar", "bar": "test"}, + } # Empty query should not fail - self.assertEqual( - {}, - medrecord.node[node_greater_than_three], - ) + assert medrecord.node[node_greater_than_three] == {} - self.assertEqual( - {2: "bar", 3: "bar"}, - medrecord.node[node_greater_than_or_equal_two, "foo"], - ) + assert medrecord.node[node_greater_than_or_equal_two, "foo"] == { + 2: "bar", + 3: "bar", + } # Accessing a non-existing key should fail - with self.assertRaises(KeyError): + with pytest.raises( + KeyError, + ): medrecord.node[node_greater_than_or_equal_two, "test"] - self.assertEqual( - { - 2: {"foo": "bar", "bar": "foo"}, - 3: {"foo": "bar", "bar": "test"}, - }, - medrecord.node[node_greater_than_or_equal_two, ["foo", "bar"]], - ) + assert medrecord.node[node_greater_than_or_equal_two, ["foo", "bar"]] == { + 2: {"foo": "bar", "bar": "foo"}, + 3: {"foo": "bar", "bar": "test"}, + } # Accessing a non-existing key should fail - with self.assertRaises(KeyError): + with pytest.raises(KeyError): medrecord.node[node_greater_than_or_equal_two, ["foo", "test"]] # Accessing a key that doesn't exist in all nodes should fail - with self.assertRaises(KeyError): + with pytest.raises(KeyError): medrecord.node[node_less_than_two, ["foo", "lorem"]] - self.assertEqual( - { - 2: {"foo": "bar", "bar": "foo"}, - 3: {"foo": "bar", "bar": "test"}, - }, - medrecord.node[node_greater_than_or_equal_two, :], - ) + assert medrecord.node[node_greater_than_or_equal_two, :] == { + 2: {"foo": "bar", "bar": "foo"}, + 3: {"foo": "bar", "bar": "test"}, + } - with self.assertRaises(ValueError): + with pytest.raises(ValueError, match="Invalid slice, only ':' is allowed"): medrecord.node[node_greater_than_or_equal_two, 1:] - with self.assertRaises(ValueError): + with pytest.raises(ValueError, match="Invalid slice, only ':' is allowed"): medrecord.node[node_greater_than_or_equal_two, :1] - with self.assertRaises(ValueError): + with pytest.raises(ValueError, match="Invalid slice, only ':' is allowed"): medrecord.node[node_greater_than_or_equal_two, ::1] assert medrecord.node[:] == { @@ -179,11 +172,11 @@ def test_node_getitem(self) -> None: 3: {"foo": "bar", "bar": "test"}, } - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="Invalid slice, only ':' is allowed"): medrecord.node[1:] - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="Invalid slice, only ':' is allowed"): medrecord.node[:1] - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="Invalid slice, only ':' is allowed"): medrecord.node[::1] assert medrecord.node[:, "foo"] == {0: "bar", 1: "bar", 2: "bar", 3: "bar"} @@ -192,11 +185,11 @@ def test_node_getitem(self) -> None: with pytest.raises(KeyError): medrecord.node[:, "test"] - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="Invalid slice, only ':' is allowed"): medrecord.node[1:, "foo"] - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="Invalid slice, only ':' is allowed"): medrecord.node[:1, "foo"] - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="Invalid slice, only ':' is allowed"): medrecord.node[::1, "foo"] assert medrecord.node[:, ["foo", "bar"]] == { @@ -214,11 +207,11 @@ def test_node_getitem(self) -> None: with pytest.raises(KeyError): medrecord.node[:, ["foo", "lorem"]] - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="Invalid slice, only ':' is allowed"): medrecord.node[1:, ["foo", "bar"]] - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="Invalid slice, only ':' is allowed"): medrecord.node[:1, ["foo", "bar"]] - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="Invalid slice, only ':' is allowed"): medrecord.node[::1, ["foo", "bar"]] assert medrecord.node[:, :] == { @@ -228,17 +221,17 @@ def test_node_getitem(self) -> None: 3: {"foo": "bar", "bar": "test"}, } - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="Invalid slice, only ':' is allowed"): medrecord.node[1:, :] - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="Invalid slice, only ':' is allowed"): medrecord.node[:1, :] - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="Invalid slice, only ':' is allowed"): medrecord.node[::1, :] - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="Invalid slice, only ':' is allowed"): medrecord.node[:, 1:] - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="Invalid slice, only ':' is allowed"): medrecord.node[:, :1] - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="Invalid slice, only ':' is allowed"): medrecord.node[:, ::1] def test_node_setitem(self) -> None: @@ -285,11 +278,11 @@ def test_node_setitem(self) -> None: 3: {"foo": "bar", "bar": "test"}, } - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="Invalid slice, only ':' is allowed"): medrecord.node[0, 1:] = "test" - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="Invalid slice, only ':' is allowed"): medrecord.node[0, :1] = "test" - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="Invalid slice, only ':' is allowed"): medrecord.node[0, ::1] = "test" medrecord = create_medrecord() @@ -319,24 +312,21 @@ def test_node_setitem(self) -> None: 3: {"foo": "bar", "bar": "test"}, } - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="Invalid slice, only ':' is allowed"): medrecord.node[[0, 1], 1:] = "test" - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="Invalid slice, only ':' is allowed"): medrecord.node[[0, 1], :1] = "test" - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="Invalid slice, only ':' is allowed"): medrecord.node[[0, 1], ::1] = "test" medrecord = create_medrecord() medrecord.node[node_greater_than_or_equal_two] = {"foo": "bar", "bar": "test"} - self.assertEqual( - { - 0: {"foo": "bar", "bar": "foo", "lorem": "ipsum"}, - 1: {"foo": "bar", "bar": "foo"}, - 2: {"foo": "bar", "bar": "test"}, - 3: {"foo": "bar", "bar": "test"}, - }, - medrecord.node[:], - ) + assert medrecord.node[:] == { + 0: {"foo": "bar", "bar": "foo", "lorem": "ipsum"}, + 1: {"foo": "bar", "bar": "foo"}, + 2: {"foo": "bar", "bar": "test"}, + 3: {"foo": "bar", "bar": "test"}, + } medrecord = create_medrecord() # Empty query should not fail @@ -344,45 +334,36 @@ def test_node_setitem(self) -> None: medrecord = create_medrecord() medrecord.node[node_greater_than_or_equal_two, "foo"] = "test" - self.assertEqual( - { - 0: {"foo": "bar", "bar": "foo", "lorem": "ipsum"}, - 1: {"foo": "bar", "bar": "foo"}, - 2: {"foo": "test", "bar": "foo"}, - 3: {"foo": "test", "bar": "test"}, - }, - medrecord.node[:], - ) + assert medrecord.node[:] == { + 0: {"foo": "bar", "bar": "foo", "lorem": "ipsum"}, + 1: {"foo": "bar", "bar": "foo"}, + 2: {"foo": "test", "bar": "foo"}, + 3: {"foo": "test", "bar": "test"}, + } medrecord = create_medrecord() medrecord.node[node_greater_than_or_equal_two, ["foo", "bar"]] = "test" - self.assertEqual( - { - 0: {"foo": "bar", "bar": "foo", "lorem": "ipsum"}, - 1: {"foo": "bar", "bar": "foo"}, - 2: {"foo": "test", "bar": "test"}, - 3: {"foo": "test", "bar": "test"}, - }, - medrecord.node[:], - ) + assert medrecord.node[:] == { + 0: {"foo": "bar", "bar": "foo", "lorem": "ipsum"}, + 1: {"foo": "bar", "bar": "foo"}, + 2: {"foo": "test", "bar": "test"}, + 3: {"foo": "test", "bar": "test"}, + } medrecord = create_medrecord() medrecord.node[node_greater_than_or_equal_two, :] = "test" - self.assertEqual( - { - 0: {"foo": "bar", "bar": "foo", "lorem": "ipsum"}, - 1: {"foo": "bar", "bar": "foo"}, - 2: {"foo": "test", "bar": "test"}, - 3: {"foo": "test", "bar": "test"}, - }, - medrecord.node[:], - ) + assert medrecord.node[:] == { + 0: {"foo": "bar", "bar": "foo", "lorem": "ipsum"}, + 1: {"foo": "bar", "bar": "foo"}, + 2: {"foo": "test", "bar": "test"}, + 3: {"foo": "test", "bar": "test"}, + } - with self.assertRaises(ValueError): + with pytest.raises(ValueError, match="Invalid slice, only ':' is allowed"): medrecord.node[node_greater_than_or_equal_two, 1:] = "test" - with self.assertRaises(ValueError): + with pytest.raises(ValueError, match="Invalid slice, only ':' is allowed"): medrecord.node[node_greater_than_or_equal_two, :1] = "test" - with self.assertRaises(ValueError): + with pytest.raises(ValueError, match="Invalid slice, only ':' is allowed"): medrecord.node[node_greater_than_or_equal_two, ::1] = "test" medrecord = create_medrecord() @@ -394,11 +375,11 @@ def test_node_setitem(self) -> None: 3: {"foo": "test", "bar": "test"}, } - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="Invalid slice, only ':' is allowed"): medrecord.node[1:, "foo"] = "test" - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="Invalid slice, only ':' is allowed"): medrecord.node[:1, "foo"] = "test" - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="Invalid slice, only ':' is allowed"): medrecord.node[::1, "foo"] = "test" medrecord = create_medrecord() @@ -410,11 +391,11 @@ def test_node_setitem(self) -> None: 3: {"foo": "test", "bar": "test"}, } - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="Invalid slice, only ':' is allowed"): medrecord.node[1:, ["foo", "bar"]] = "test" - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="Invalid slice, only ':' is allowed"): medrecord.node[:1, ["foo", "bar"]] = "test" - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="Invalid slice, only ':' is allowed"): medrecord.node[::1, ["foo", "bar"]] = "test" medrecord = create_medrecord() @@ -426,17 +407,17 @@ def test_node_setitem(self) -> None: 3: {"foo": "test", "bar": "test"}, } - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="Invalid slice, only ':' is allowed"): medrecord.node[1:, :] = "test" - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="Invalid slice, only ':' is allowed"): medrecord.node[:1, :] = "test" - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="Invalid slice, only ':' is allowed"): medrecord.node[::1, :] = "test" - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="Invalid slice, only ':' is allowed"): medrecord.node[:, 1:] = "test" - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="Invalid slice, only ':' is allowed"): medrecord.node[:, :1] = "test" - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="Invalid slice, only ':' is allowed"): medrecord.node[:, ::1] = "test" # Adding new attributes @@ -491,37 +472,21 @@ def test_node_setitem(self) -> None: medrecord = create_medrecord() medrecord.node[node_greater_than_or_equal_two, "test"] = "test" - self.assertEqual( - { - 0: {"foo": "bar", "bar": "foo", "lorem": "ipsum"}, - 1: {"foo": "bar", "bar": "foo"}, - 2: {"foo": "bar", "bar": "foo", "test": "test"}, - 3: {"foo": "bar", "bar": "test", "test": "test"}, - }, - medrecord.node[:], - ) + assert medrecord.node[:] == { + 0: {"foo": "bar", "bar": "foo", "lorem": "ipsum"}, + 1: {"foo": "bar", "bar": "foo"}, + 2: {"foo": "bar", "bar": "foo", "test": "test"}, + 3: {"foo": "bar", "bar": "test", "test": "test"}, + } medrecord = create_medrecord() medrecord.node[node_greater_than_or_equal_two, ["test", "test2"]] = "test" - self.assertEqual( - { - 0: {"foo": "bar", "bar": "foo", "lorem": "ipsum"}, - 1: {"foo": "bar", "bar": "foo"}, - 2: { - "foo": "bar", - "bar": "foo", - "test": "test", - "test2": "test", - }, - 3: { - "foo": "bar", - "bar": "test", - "test": "test", - "test2": "test", - }, - }, - medrecord.node[:], - ) + assert medrecord.node[:] == { + 0: {"foo": "bar", "bar": "foo", "lorem": "ipsum"}, + 1: {"foo": "bar", "bar": "foo"}, + 2: {"foo": "bar", "bar": "foo", "test": "test", "test2": "test"}, + 3: {"foo": "bar", "bar": "test", "test": "test", "test2": "test"}, + } medrecord = create_medrecord() medrecord.node[:, "test"] = "test" @@ -569,27 +534,21 @@ def test_node_setitem(self) -> None: medrecord = create_medrecord() medrecord.node[node_less_than_two, "lorem"] = "test" - self.assertEqual( - { - 0: {"foo": "bar", "bar": "foo", "lorem": "test"}, - 1: {"foo": "bar", "bar": "foo", "lorem": "test"}, - 2: {"foo": "bar", "bar": "foo"}, - 3: {"foo": "bar", "bar": "test"}, - }, - medrecord.node[:], - ) + assert medrecord.node[:] == { + 0: {"foo": "bar", "bar": "foo", "lorem": "test"}, + 1: {"foo": "bar", "bar": "foo", "lorem": "test"}, + 2: {"foo": "bar", "bar": "foo"}, + 3: {"foo": "bar", "bar": "test"}, + } medrecord = create_medrecord() medrecord.node[node_less_than_two, ["lorem", "test"]] = "test" - self.assertEqual( - { - 0: {"foo": "bar", "bar": "foo", "lorem": "test", "test": "test"}, - 1: {"foo": "bar", "bar": "foo", "lorem": "test", "test": "test"}, - 2: {"foo": "bar", "bar": "foo"}, - 3: {"foo": "bar", "bar": "test"}, - }, - medrecord.node[:], - ) + assert medrecord.node[:] == { + 0: {"foo": "bar", "bar": "foo", "lorem": "test", "test": "test"}, + 1: {"foo": "bar", "bar": "foo", "lorem": "test", "test": "test"}, + 2: {"foo": "bar", "bar": "foo"}, + 3: {"foo": "bar", "bar": "test"}, + } medrecord = create_medrecord() medrecord.node[:, "lorem"] = "test" @@ -652,11 +611,11 @@ def test_node_delitem(self) -> None: 3: {"foo": "bar", "bar": "test"}, } - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="Invalid slice, only ':' is allowed"): del medrecord.node[0, 1:] - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="Invalid slice, only ':' is allowed"): del medrecord.node[0, :1] - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="Invalid slice, only ':' is allowed"): del medrecord.node[0, ::1] medrecord = create_medrecord() @@ -706,82 +665,70 @@ def test_node_delitem(self) -> None: 3: {"foo": "bar", "bar": "test"}, } - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="Invalid slice, only ':' is allowed"): del medrecord.node[[0, 1], 1:] - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="Invalid slice, only ':' is allowed"): del medrecord.node[[0, 1], :1] - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="Invalid slice, only ':' is allowed"): del medrecord.node[[0, 1], ::1] medrecord = create_medrecord() del medrecord.node[node_greater_than_or_equal_two, "foo"] - self.assertEqual( - { - 0: {"foo": "bar", "bar": "foo", "lorem": "ipsum"}, - 1: {"foo": "bar", "bar": "foo"}, - 2: {"bar": "foo"}, - 3: {"bar": "test"}, - }, - medrecord.node[:], - ) + assert medrecord.node[:] == { + 0: {"foo": "bar", "bar": "foo", "lorem": "ipsum"}, + 1: {"foo": "bar", "bar": "foo"}, + 2: {"bar": "foo"}, + 3: {"bar": "test"}, + } medrecord = create_medrecord() # Empty query should not fail del medrecord.node[node_greater_than_three, "foo"] - self.assertEqual( - { - 0: {"foo": "bar", "bar": "foo", "lorem": "ipsum"}, - 1: {"foo": "bar", "bar": "foo"}, - 2: {"foo": "bar", "bar": "foo"}, - 3: {"foo": "bar", "bar": "test"}, - }, - medrecord.node[:], - ) + assert medrecord.node[:] == { + 0: {"foo": "bar", "bar": "foo", "lorem": "ipsum"}, + 1: {"foo": "bar", "bar": "foo"}, + 2: {"foo": "bar", "bar": "foo"}, + 3: {"foo": "bar", "bar": "test"}, + } medrecord = create_medrecord() # Removing a non-existing key should fail - with self.assertRaises(KeyError): + with pytest.raises(KeyError): del medrecord.node[node_greater_than_or_equal_two, "test"] medrecord = create_medrecord() del medrecord.node[node_greater_than_or_equal_two, ["foo", "bar"]] - self.assertEqual( - { - 0: {"foo": "bar", "bar": "foo", "lorem": "ipsum"}, - 1: {"foo": "bar", "bar": "foo"}, - 2: {}, - 3: {}, - }, - medrecord.node[:], - ) + assert medrecord.node[:] == { + 0: {"foo": "bar", "bar": "foo", "lorem": "ipsum"}, + 1: {"foo": "bar", "bar": "foo"}, + 2: {}, + 3: {}, + } medrecord = create_medrecord() # Removing a non-existing key should fail - with self.assertRaises(KeyError): + with pytest.raises(KeyError): del medrecord.node[node_greater_than_or_equal_two, ["foo", "test"]] medrecord = create_medrecord() # Removing a key that doesn't exist in all nodes should fail - with self.assertRaises(KeyError): + with pytest.raises(KeyError): del medrecord.node[node_less_than_two, ["foo", "lorem"]] medrecord = create_medrecord() del medrecord.node[node_greater_than_or_equal_two, :] - self.assertEqual( - { - 0: {"foo": "bar", "bar": "foo", "lorem": "ipsum"}, - 1: {"foo": "bar", "bar": "foo"}, - 2: {}, - 3: {}, - }, - medrecord.node[:], - ) + assert medrecord.node[:] == { + 0: {"foo": "bar", "bar": "foo", "lorem": "ipsum"}, + 1: {"foo": "bar", "bar": "foo"}, + 2: {}, + 3: {}, + } - with self.assertRaises(ValueError): + with pytest.raises(ValueError, match="Invalid slice, only ':' is allowed"): del medrecord.node[node_greater_than_or_equal_two, 1:] - with self.assertRaises(ValueError): + with pytest.raises(ValueError, match="Invalid slice, only ':' is allowed"): del medrecord.node[node_greater_than_or_equal_two, :1] - with self.assertRaises(ValueError): + with pytest.raises(ValueError, match="Invalid slice, only ':' is allowed"): del medrecord.node[node_greater_than_or_equal_two, ::1] medrecord = create_medrecord() @@ -798,11 +745,11 @@ def test_node_delitem(self) -> None: with pytest.raises(KeyError): del medrecord.node[:, "test"] - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="Invalid slice, only ':' is allowed"): del medrecord.node[1:, "foo"] - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="Invalid slice, only ':' is allowed"): del medrecord.node[:1, "foo"] - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="Invalid slice, only ':' is allowed"): del medrecord.node[::1, "foo"] medrecord = create_medrecord() @@ -819,28 +766,28 @@ def test_node_delitem(self) -> None: with pytest.raises(KeyError): del medrecord.node[:, ["foo", "lorem"]] - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="Invalid slice, only ':' is allowed"): del medrecord.node[1:, ["foo", "bar"]] - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="Invalid slice, only ':' is allowed"): del medrecord.node[:1, ["foo", "bar"]] - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="Invalid slice, only ':' is allowed"): del medrecord.node[::1, ["foo", "bar"]] medrecord = create_medrecord() del medrecord.node[:, :] assert medrecord.node[:] == {0: {}, 1: {}, 2: {}, 3: {}} - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="Invalid slice, only ':' is allowed"): del medrecord.node[1:, :] - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="Invalid slice, only ':' is allowed"): del medrecord.node[:1, :] - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="Invalid slice, only ':' is allowed"): del medrecord.node[::1, :] - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="Invalid slice, only ':' is allowed"): del medrecord.node[:, 1:] - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="Invalid slice, only ':' is allowed"): del medrecord.node[:, :1] - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="Invalid slice, only ':' is allowed"): del medrecord.node[:, ::1] def test_edge_getitem(self) -> None: @@ -866,11 +813,11 @@ def test_edge_getitem(self) -> None: assert medrecord.edge[0, :] == {"foo": "bar", "bar": "foo", "lorem": "ipsum"} - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="Invalid slice, only ':' is allowed"): medrecord.edge[0, 1:] - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="Invalid slice, only ':' is allowed"): medrecord.edge[0, :1] - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="Invalid slice, only ':' is allowed"): medrecord.edge[0, ::1] assert medrecord.edge[[0, 1]] == { @@ -909,62 +856,53 @@ def test_edge_getitem(self) -> None: 1: {"foo": "bar", "bar": "foo"}, } - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="Invalid slice, only ':' is allowed"): medrecord.edge[[0, 1], 1:] - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="Invalid slice, only ':' is allowed"): medrecord.edge[[0, 1], :1] - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="Invalid slice, only ':' is allowed"): medrecord.edge[[0, 1], ::1] - self.assertEqual( - {2: {"foo": "bar", "bar": "foo"}, 3: {"foo": "bar", "bar": "test"}}, - medrecord.edge[edge_greater_than_or_equal_two], - ) + assert medrecord.edge[edge_greater_than_or_equal_two] == { + 2: {"foo": "bar", "bar": "foo"}, + 3: {"foo": "bar", "bar": "test"}, + } # Empty query should not fail - self.assertEqual( - {}, - medrecord.edge[edge_greater_than_three], - ) + assert medrecord.edge[edge_greater_than_three] == {} - self.assertEqual( - {2: "bar", 3: "bar"}, - medrecord.edge[edge_greater_than_or_equal_two, "foo"], - ) + assert medrecord.edge[edge_greater_than_or_equal_two, "foo"] == { + 2: "bar", + 3: "bar", + } # Accessing a non-existing key should fail - with self.assertRaises(KeyError): + with pytest.raises(KeyError): medrecord.edge[edge_greater_than_or_equal_two, "test"] - self.assertEqual( - { - 2: {"foo": "bar", "bar": "foo"}, - 3: {"foo": "bar", "bar": "test"}, - }, - medrecord.edge[edge_greater_than_or_equal_two, ["foo", "bar"]], - ) + assert medrecord.edge[edge_greater_than_or_equal_two, ["foo", "bar"]] == { + 2: {"foo": "bar", "bar": "foo"}, + 3: {"foo": "bar", "bar": "test"}, + } # Accessing a non-existing key should fail - with self.assertRaises(KeyError): + with pytest.raises(KeyError): medrecord.edge[edge_greater_than_or_equal_two, ["foo", "test"]] # Accessing a key that doesn't exist in all edges should fail - with self.assertRaises(KeyError): + with pytest.raises(KeyError): medrecord.edge[edge_less_than_two, ["foo", "lorem"]] - self.assertEqual( - { - 2: {"foo": "bar", "bar": "foo"}, - 3: {"foo": "bar", "bar": "test"}, - }, - medrecord.edge[edge_greater_than_or_equal_two, :], - ) + assert medrecord.edge[edge_greater_than_or_equal_two, :] == { + 2: {"foo": "bar", "bar": "foo"}, + 3: {"foo": "bar", "bar": "test"}, + } - with self.assertRaises(ValueError): + with pytest.raises(ValueError, match="Invalid slice, only ':' is allowed"): medrecord.edge[edge_greater_than_or_equal_two, 1:] - with self.assertRaises(ValueError): + with pytest.raises(ValueError, match="Invalid slice, only ':' is allowed"): medrecord.edge[edge_greater_than_or_equal_two, :1] - with self.assertRaises(ValueError): + with pytest.raises(ValueError, match="Invalid slice, only ':' is allowed"): medrecord.edge[edge_greater_than_or_equal_two, ::1] assert medrecord.edge[:] == { @@ -974,11 +912,11 @@ def test_edge_getitem(self) -> None: 3: {"foo": "bar", "bar": "test"}, } - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="Invalid slice, only ':' is allowed"): medrecord.edge[1:] - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="Invalid slice, only ':' is allowed"): medrecord.edge[:1] - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="Invalid slice, only ':' is allowed"): medrecord.edge[::1] assert medrecord.edge[:, "foo"] == {0: "bar", 1: "bar", 2: "bar", 3: "bar"} @@ -987,11 +925,11 @@ def test_edge_getitem(self) -> None: with pytest.raises(KeyError): medrecord.edge[:, "test"] - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="Invalid slice, only ':' is allowed"): medrecord.edge[1:, "foo"] - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="Invalid slice, only ':' is allowed"): medrecord.edge[:1, "foo"] - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="Invalid slice, only ':' is allowed"): medrecord.edge[::1, "foo"] assert medrecord.edge[:, ["foo", "bar"]] == { @@ -1009,11 +947,11 @@ def test_edge_getitem(self) -> None: with pytest.raises(KeyError): medrecord.edge[:, ["foo", "lorem"]] - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="Invalid slice, only ':' is allowed"): medrecord.edge[1:, ["foo", "bar"]] - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="Invalid slice, only ':' is allowed"): medrecord.edge[:1, ["foo", "bar"]] - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="Invalid slice, only ':' is allowed"): medrecord.edge[::1, ["foo", "bar"]] assert medrecord.edge[:, :] == { @@ -1023,17 +961,17 @@ def test_edge_getitem(self) -> None: 3: {"foo": "bar", "bar": "test"}, } - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="Invalid slice, only ':' is allowed"): medrecord.edge[1:, :] - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="Invalid slice, only ':' is allowed"): medrecord.edge[:1, :] - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="Invalid slice, only ':' is allowed"): medrecord.edge[::1, :] - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="Invalid slice, only ':' is allowed"): medrecord.edge[:, 1:] - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="Invalid slice, only ':' is allowed"): medrecord.edge[:, :1] - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="Invalid slice, only ':' is allowed"): medrecord.edge[:, ::1] def test_edge_setitem(self) -> None: @@ -1080,11 +1018,11 @@ def test_edge_setitem(self) -> None: 3: {"foo": "bar", "bar": "test"}, } - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="Invalid slice, only ':' is allowed"): medrecord.edge[0, 1:] = "test" - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="Invalid slice, only ':' is allowed"): medrecord.edge[0, :1] = "test" - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="Invalid slice, only ':' is allowed"): medrecord.edge[0, ::1] = "test" medrecord = create_medrecord() @@ -1114,24 +1052,21 @@ def test_edge_setitem(self) -> None: 3: {"foo": "bar", "bar": "test"}, } - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="Invalid slice, only ':' is allowed"): medrecord.edge[[0, 1], 1:] = "test" - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="Invalid slice, only ':' is allowed"): medrecord.edge[[0, 1], :1] = "test" - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="Invalid slice, only ':' is allowed"): medrecord.edge[[0, 1], ::1] = "test" medrecord = create_medrecord() medrecord.edge[edge_greater_than_or_equal_two] = {"foo": "bar", "bar": "test"} - self.assertEqual( - { - 0: {"foo": "bar", "bar": "foo", "lorem": "ipsum"}, - 1: {"foo": "bar", "bar": "foo"}, - 2: {"foo": "bar", "bar": "test"}, - 3: {"foo": "bar", "bar": "test"}, - }, - medrecord.edge[:], - ) + assert medrecord.edge[:] == { + 0: {"foo": "bar", "bar": "foo", "lorem": "ipsum"}, + 1: {"foo": "bar", "bar": "foo"}, + 2: {"foo": "bar", "bar": "test"}, + 3: {"foo": "bar", "bar": "test"}, + } medrecord = create_medrecord() # Empty query should not fail @@ -1139,45 +1074,36 @@ def test_edge_setitem(self) -> None: medrecord = create_medrecord() medrecord.edge[edge_greater_than_or_equal_two, "foo"] = "test" - self.assertEqual( - { - 0: {"foo": "bar", "bar": "foo", "lorem": "ipsum"}, - 1: {"foo": "bar", "bar": "foo"}, - 2: {"foo": "test", "bar": "foo"}, - 3: {"foo": "test", "bar": "test"}, - }, - medrecord.edge[:], - ) + assert medrecord.edge[:] == { + 0: {"foo": "bar", "bar": "foo", "lorem": "ipsum"}, + 1: {"foo": "bar", "bar": "foo"}, + 2: {"foo": "test", "bar": "foo"}, + 3: {"foo": "test", "bar": "test"}, + } medrecord = create_medrecord() medrecord.edge[edge_greater_than_or_equal_two, ["foo", "bar"]] = "test" - self.assertEqual( - { - 0: {"foo": "bar", "bar": "foo", "lorem": "ipsum"}, - 1: {"foo": "bar", "bar": "foo"}, - 2: {"foo": "test", "bar": "test"}, - 3: {"foo": "test", "bar": "test"}, - }, - medrecord.edge[:], - ) + assert medrecord.edge[:] == { + 0: {"foo": "bar", "bar": "foo", "lorem": "ipsum"}, + 1: {"foo": "bar", "bar": "foo"}, + 2: {"foo": "test", "bar": "test"}, + 3: {"foo": "test", "bar": "test"}, + } medrecord = create_medrecord() medrecord.edge[edge_greater_than_or_equal_two, :] = "test" - self.assertEqual( - { - 0: {"foo": "bar", "bar": "foo", "lorem": "ipsum"}, - 1: {"foo": "bar", "bar": "foo"}, - 2: {"foo": "test", "bar": "test"}, - 3: {"foo": "test", "bar": "test"}, - }, - medrecord.edge[:], - ) + assert medrecord.edge[:] == { + 0: {"foo": "bar", "bar": "foo", "lorem": "ipsum"}, + 1: {"foo": "bar", "bar": "foo"}, + 2: {"foo": "test", "bar": "test"}, + 3: {"foo": "test", "bar": "test"}, + } - with self.assertRaises(ValueError): + with pytest.raises(ValueError, match="Invalid slice, only ':' is allowed"): medrecord.edge[edge_greater_than_or_equal_two, 1:] = "test" - with self.assertRaises(ValueError): + with pytest.raises(ValueError, match="Invalid slice, only ':' is allowed"): medrecord.edge[edge_greater_than_or_equal_two, :1] = "test" - with self.assertRaises(ValueError): + with pytest.raises(ValueError, match="Invalid slice, only ':' is allowed"): medrecord.edge[edge_greater_than_or_equal_two, ::1] = "test" medrecord = create_medrecord() @@ -1189,11 +1115,11 @@ def test_edge_setitem(self) -> None: 3: {"foo": "test", "bar": "test"}, } - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="Invalid slice, only ':' is allowed"): medrecord.edge[1:, "foo"] = "test" - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="Invalid slice, only ':' is allowed"): medrecord.edge[:1, "foo"] = "test" - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="Invalid slice, only ':' is allowed"): medrecord.edge[::1, "foo"] = "test" medrecord = create_medrecord() @@ -1205,11 +1131,11 @@ def test_edge_setitem(self) -> None: 3: {"foo": "test", "bar": "test"}, } - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="Invalid slice, only ':' is allowed"): medrecord.edge[1:, ["foo", "bar"]] = "test" - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="Invalid slice, only ':' is allowed"): medrecord.edge[:1, ["foo", "bar"]] = "test" - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="Invalid slice, only ':' is allowed"): medrecord.edge[::1, ["foo", "bar"]] = "test" medrecord = create_medrecord() @@ -1221,17 +1147,17 @@ def test_edge_setitem(self) -> None: 3: {"foo": "test", "bar": "test"}, } - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="Invalid slice, only ':' is allowed"): medrecord.edge[1:, :] = "test" - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="Invalid slice, only ':' is allowed"): medrecord.edge[:1, :] = "test" - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="Invalid slice, only ':' is allowed"): medrecord.edge[::1, :] = "test" - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="Invalid slice, only ':' is allowed"): medrecord.edge[:, 1:] = "test" - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="Invalid slice, only ':' is allowed"): medrecord.edge[:, :1] = "test" - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="Invalid slice, only ':' is allowed"): medrecord.edge[:, ::1] = "test" # Adding new attributes @@ -1286,37 +1212,21 @@ def test_edge_setitem(self) -> None: medrecord = create_medrecord() medrecord.edge[edge_greater_than_or_equal_two, "test"] = "test" - self.assertEqual( - { - 0: {"foo": "bar", "bar": "foo", "lorem": "ipsum"}, - 1: {"foo": "bar", "bar": "foo"}, - 2: {"foo": "bar", "bar": "foo", "test": "test"}, - 3: {"foo": "bar", "bar": "test", "test": "test"}, - }, - medrecord.edge[:], - ) + assert medrecord.edge[:] == { + 0: {"foo": "bar", "bar": "foo", "lorem": "ipsum"}, + 1: {"foo": "bar", "bar": "foo"}, + 2: {"foo": "bar", "bar": "foo", "test": "test"}, + 3: {"foo": "bar", "bar": "test", "test": "test"}, + } medrecord = create_medrecord() medrecord.edge[edge_greater_than_or_equal_two, ["test", "test2"]] = "test" - self.assertEqual( - { - 0: {"foo": "bar", "bar": "foo", "lorem": "ipsum"}, - 1: {"foo": "bar", "bar": "foo"}, - 2: { - "foo": "bar", - "bar": "foo", - "test": "test", - "test2": "test", - }, - 3: { - "foo": "bar", - "bar": "test", - "test": "test", - "test2": "test", - }, - }, - medrecord.edge[:], - ) + assert medrecord.edge[:] == { + 0: {"foo": "bar", "bar": "foo", "lorem": "ipsum"}, + 1: {"foo": "bar", "bar": "foo"}, + 2: {"foo": "bar", "bar": "foo", "test": "test", "test2": "test"}, + 3: {"foo": "bar", "bar": "test", "test": "test", "test2": "test"}, + } medrecord = create_medrecord() medrecord.edge[:, "test"] = "test" @@ -1364,27 +1274,21 @@ def test_edge_setitem(self) -> None: medrecord = create_medrecord() medrecord.edge[edge_less_than_two, "lorem"] = "test" - self.assertEqual( - { - 0: {"foo": "bar", "bar": "foo", "lorem": "test"}, - 1: {"foo": "bar", "bar": "foo", "lorem": "test"}, - 2: {"foo": "bar", "bar": "foo"}, - 3: {"foo": "bar", "bar": "test"}, - }, - medrecord.edge[:], - ) + assert medrecord.edge[:] == { + 0: {"foo": "bar", "bar": "foo", "lorem": "test"}, + 1: {"foo": "bar", "bar": "foo", "lorem": "test"}, + 2: {"foo": "bar", "bar": "foo"}, + 3: {"foo": "bar", "bar": "test"}, + } medrecord = create_medrecord() medrecord.edge[edge_less_than_two, ["lorem", "test"]] = "test" - self.assertEqual( - { - 0: {"foo": "bar", "bar": "foo", "lorem": "test", "test": "test"}, - 1: {"foo": "bar", "bar": "foo", "lorem": "test", "test": "test"}, - 2: {"foo": "bar", "bar": "foo"}, - 3: {"foo": "bar", "bar": "test"}, - }, - medrecord.edge[:], - ) + assert medrecord.edge[:] == { + 0: {"foo": "bar", "bar": "foo", "lorem": "test", "test": "test"}, + 1: {"foo": "bar", "bar": "foo", "lorem": "test", "test": "test"}, + 2: {"foo": "bar", "bar": "foo"}, + 3: {"foo": "bar", "bar": "test"}, + } medrecord = create_medrecord() medrecord.edge[:, "lorem"] = "test" @@ -1447,11 +1351,11 @@ def test_edge_delitem(self) -> None: 3: {"foo": "bar", "bar": "test"}, } - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="Invalid slice, only ':' is allowed"): del medrecord.edge[0, 1:] - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="Invalid slice, only ':' is allowed"): del medrecord.edge[0, :1] - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="Invalid slice, only ':' is allowed"): del medrecord.edge[0, ::1] medrecord = create_medrecord() @@ -1501,82 +1405,70 @@ def test_edge_delitem(self) -> None: 3: {"foo": "bar", "bar": "test"}, } - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="Invalid slice, only ':' is allowed"): del medrecord.edge[[0, 1], 1:] - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="Invalid slice, only ':' is allowed"): del medrecord.edge[[0, 1], :1] - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="Invalid slice, only ':' is allowed"): del medrecord.edge[[0, 1], ::1] medrecord = create_medrecord() del medrecord.edge[edge_greater_than_or_equal_two, "foo"] - self.assertEqual( - { - 0: {"foo": "bar", "bar": "foo", "lorem": "ipsum"}, - 1: {"foo": "bar", "bar": "foo"}, - 2: {"bar": "foo"}, - 3: {"bar": "test"}, - }, - medrecord.edge[:], - ) + assert medrecord.edge[:] == { + 0: {"foo": "bar", "bar": "foo", "lorem": "ipsum"}, + 1: {"foo": "bar", "bar": "foo"}, + 2: {"bar": "foo"}, + 3: {"bar": "test"}, + } medrecord = create_medrecord() # Empty query should not fail del medrecord.edge[edge_greater_than_three, "foo"] - self.assertEqual( - { - 0: {"foo": "bar", "bar": "foo", "lorem": "ipsum"}, - 1: {"foo": "bar", "bar": "foo"}, - 2: {"foo": "bar", "bar": "foo"}, - 3: {"foo": "bar", "bar": "test"}, - }, - medrecord.edge[:], - ) + assert medrecord.edge[:] == { + 0: {"foo": "bar", "bar": "foo", "lorem": "ipsum"}, + 1: {"foo": "bar", "bar": "foo"}, + 2: {"foo": "bar", "bar": "foo"}, + 3: {"foo": "bar", "bar": "test"}, + } medrecord = create_medrecord() # Removing a non-existing key should fail - with self.assertRaises(KeyError): + with pytest.raises(KeyError): del medrecord.edge[edge_greater_than_or_equal_two, "test"] medrecord = create_medrecord() del medrecord.edge[edge_greater_than_or_equal_two, ["foo", "bar"]] - self.assertEqual( - { - 0: {"foo": "bar", "bar": "foo", "lorem": "ipsum"}, - 1: {"foo": "bar", "bar": "foo"}, - 2: {}, - 3: {}, - }, - medrecord.edge[:], - ) + assert medrecord.edge[:] == { + 0: {"foo": "bar", "bar": "foo", "lorem": "ipsum"}, + 1: {"foo": "bar", "bar": "foo"}, + 2: {}, + 3: {}, + } medrecord = create_medrecord() # Removing a non-existing key should fail - with self.assertRaises(KeyError): + with pytest.raises(KeyError): del medrecord.edge[edge_greater_than_or_equal_two, ["foo", "test"]] medrecord = create_medrecord() # Removing a key that doesn't exist in all edges should fail - with self.assertRaises(KeyError): + with pytest.raises(KeyError): del medrecord.edge[edge_less_than_two, ["foo", "lorem"]] medrecord = create_medrecord() del medrecord.edge[edge_greater_than_or_equal_two, :] - self.assertEqual( - { - 0: {"foo": "bar", "bar": "foo", "lorem": "ipsum"}, - 1: {"foo": "bar", "bar": "foo"}, - 2: {}, - 3: {}, - }, - medrecord.edge[:], - ) + assert medrecord.edge[:] == { + 0: {"foo": "bar", "bar": "foo", "lorem": "ipsum"}, + 1: {"foo": "bar", "bar": "foo"}, + 2: {}, + 3: {}, + } - with self.assertRaises(ValueError): + with pytest.raises(ValueError, match="Invalid slice, only ':' is allowed"): del medrecord.edge[edge_greater_than_or_equal_two, 1:] - with self.assertRaises(ValueError): + with pytest.raises(ValueError, match="Invalid slice, only ':' is allowed"): del medrecord.edge[edge_greater_than_or_equal_two, :1] - with self.assertRaises(ValueError): + with pytest.raises(ValueError, match="Invalid slice, only ':' is allowed"): del medrecord.edge[edge_greater_than_or_equal_two, ::1] medrecord = create_medrecord() @@ -1593,11 +1485,11 @@ def test_edge_delitem(self) -> None: with pytest.raises(KeyError): del medrecord.edge[:, "test"] - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="Invalid slice, only ':' is allowed"): del medrecord.edge[1:, "foo"] - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="Invalid slice, only ':' is allowed"): del medrecord.edge[:1, "foo"] - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="Invalid slice, only ':' is allowed"): del medrecord.edge[::1, "foo"] medrecord = create_medrecord() @@ -1614,26 +1506,26 @@ def test_edge_delitem(self) -> None: with pytest.raises(KeyError): del medrecord.edge[:, ["foo", "lorem"]] - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="Invalid slice, only ':' is allowed"): del medrecord.edge[1:, ["foo", "bar"]] - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="Invalid slice, only ':' is allowed"): del medrecord.edge[:1, ["foo", "bar"]] - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="Invalid slice, only ':' is allowed"): del medrecord.edge[::1, ["foo", "bar"]] medrecord = create_medrecord() del medrecord.edge[:, :] assert medrecord.edge[:] == {0: {}, 1: {}, 2: {}, 3: {}} - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="Invalid slice, only ':' is allowed"): del medrecord.edge[1:, :] - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="Invalid slice, only ':' is allowed"): del medrecord.edge[:1, :] - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="Invalid slice, only ':' is allowed"): del medrecord.edge[::1, :] - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="Invalid slice, only ':' is allowed"): del medrecord.edge[:, 1:] - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="Invalid slice, only ':' is allowed"): del medrecord.edge[:, :1] - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="Invalid slice, only ':' is allowed"): del medrecord.edge[:, ::1] diff --git a/medmodels/medrecord/tests/test_medrecord.py b/medmodels/medrecord/tests/test_medrecord.py index 5db2b22..b95fe89 100644 --- a/medmodels/medrecord/tests/test_medrecord.py +++ b/medmodels/medrecord/tests/test_medrecord.py @@ -264,7 +264,10 @@ def test_schema(self) -> None: medrecord.add_nodes(("0", {"attribute": 1})) - with self.assertRaises(ValueError): + with pytest.raises( + ValueError, + match=r"Attribute [^\s]+ of node with index [^\s]+ is of type [^\s]+. Expected [^\s]+.", + ): medrecord.add_nodes(("1", {"attribute": "1"})) medrecord.add_nodes(("1", {"attribute": 1, "attribute2": 1})) @@ -273,12 +276,18 @@ def test_schema(self) -> None: medrecord.add_nodes(("2", {"attribute": 1, "attribute2": "1"})) - with self.assertRaises(ValueError): + with pytest.raises( + ValueError, + match=r"Attribute [^\s]+ of node with index [^\s]+ is of type [^\s]+. Expected [^\s]+.", + ): medrecord.add_nodes_to_group("group", "2") medrecord.add_edges(("0", "1", {"attribute": 1})) - with self.assertRaises(ValueError): + with pytest.raises( + ValueError, + match=r"Attribute [^\s]+ of edge with index [^\s]+ is of type [^\s]+. Expected [^\s]+.", + ): medrecord.add_edges(("0", "1", {"attribute": "1"})) edge_index = medrecord.add_edges(("0", "1", {"attribute": 1, "attribute2": 1})) @@ -293,7 +302,10 @@ def test_schema(self) -> None: ) ) - with self.assertRaises(ValueError): + with pytest.raises( + ValueError, + match=r"Attribute [^\s]+ of edge with index [^\s]+ is of type [^\s]+. Expected [^\s]+.", + ): medrecord.add_edges_to_group("group", edge_index) def test_nodes(self) -> None: @@ -362,7 +374,7 @@ def test_outgoing_edges(self) -> None: "1": [1, 2], } - def query(node: NodeOperand): + def query(node: NodeOperand) -> None: node.index().is_in(["0", "1"]) edges = medrecord.outgoing_edges(query) @@ -394,7 +406,7 @@ def test_incoming_edges(self) -> None: assert edges == {"1": [0], "2": [2]} - def query(node: NodeOperand): + def query(node: NodeOperand) -> None: node.index().is_in(["1", "2"]) edges = medrecord.incoming_edges(query) @@ -423,7 +435,7 @@ def test_edge_endpoints(self) -> None: assert endpoints == {0: ("0", "1"), 1: ("1", "0")} - def query(edge: EdgeOperand): + def query(edge: EdgeOperand) -> None: edge.index().is_in([0, 1]) endpoints = medrecord.edge_endpoints(query) @@ -452,7 +464,7 @@ def test_edges_connecting(self) -> None: assert edges == [0] - def query1(node: NodeOperand): + def query1(node: NodeOperand) -> None: node.index().is_in(["0", "1"]) edges = medrecord.edges_connecting(query1, "1") @@ -463,7 +475,7 @@ def query1(node: NodeOperand): assert sorted([0, 3]) == sorted(edges) - def query2(node: NodeOperand): + def query2(node: NodeOperand) -> None: node.index().is_in(["1", "3"]) edges = medrecord.edges_connecting("0", query2) @@ -474,10 +486,10 @@ def query2(node: NodeOperand): assert sorted([0, 2, 3]) == sorted(edges) - def query3(node: NodeOperand): + def query3(node: NodeOperand) -> None: node.index().is_in(["0", "1"]) - def query4(node: NodeOperand): + def query4(node: NodeOperand) -> None: node.index().is_in(["1", "2", "3"]) edges = medrecord.edges_connecting(query3, query4) @@ -488,7 +500,7 @@ def query4(node: NodeOperand): assert sorted(edges) == [0, 1] - def test_remove_nodes(self): + def test_remove_nodes(self) -> None: medrecord = create_medrecord() assert medrecord.node_count() == 4 @@ -507,7 +519,7 @@ def test_remove_nodes(self): assert medrecord.node_count() == 4 - def query(node: NodeOperand): + def query(node: NodeOperand) -> None: node.index().is_in(["0", "1"]) attributes = medrecord.remove_nodes(query) @@ -515,15 +527,15 @@ def query(node: NodeOperand): assert medrecord.node_count() == 2 assert attributes == {"0": create_nodes()[0][1], "1": create_nodes()[1][1]} - def test_invalid_remove_nodes(self): + def test_invalid_remove_nodes(self) -> None: medrecord = create_medrecord() # Removing a non-existing node should fail - with self.assertRaises(IndexError): + with pytest.raises(IndexError): medrecord.remove_nodes("50") # Removing a non-existing node should fail - with self.assertRaises(IndexError): + with pytest.raises(IndexError): medrecord.remove_nodes(["0", "50"]) def test_add_nodes(self) -> None: @@ -538,38 +550,38 @@ def test_add_nodes(self) -> None: # Adding node tuple medrecord = MedRecord() - self.assertEqual(0, medrecord.node_count()) + assert medrecord.node_count() == 0 medrecord.add_nodes(("0", {})) - self.assertEqual(1, medrecord.node_count()) - self.assertEqual(0, len(medrecord.groups)) + assert medrecord.node_count() == 1 + assert len(medrecord.groups) == 0 medrecord = MedRecord() medrecord.add_nodes(("0", {}), "0") - self.assertIn("0", medrecord.nodes_in_group("0")) - self.assertEqual(1, len(medrecord.groups)) + assert "0" in medrecord.nodes_in_group("0") + assert len(medrecord.groups) == 1 # Adding tuple to a group medrecord = MedRecord() medrecord.add_nodes(create_nodes(), "0") - self.assertIn("0", medrecord.nodes_in_group("0")) - self.assertIn("1", medrecord.nodes_in_group("0")) - self.assertIn("2", medrecord.nodes_in_group("0")) - self.assertIn("3", medrecord.nodes_in_group("0")) - self.assertIn("0", medrecord.groups) + assert "0" in medrecord.nodes_in_group("0") + assert "1" in medrecord.nodes_in_group("0") + assert "2" in medrecord.nodes_in_group("0") + assert "3" in medrecord.nodes_in_group("0") + assert "0" in medrecord.groups # Adding group without nodes medrecord = MedRecord() medrecord.add_nodes([], "0") - self.assertEqual(0, medrecord.node_count()) - self.assertIn("0", medrecord.groups) + assert medrecord.node_count() == 0 + assert "0" in medrecord.groups # Adding pandas dataframe medrecord = MedRecord() @@ -585,8 +597,8 @@ def test_add_nodes(self) -> None: medrecord.add_nodes((create_pandas_nodes_dataframe(), "index"), "0") - self.assertIn("0", medrecord.nodes_in_group("0")) - self.assertIn("1", medrecord.nodes_in_group("0")) + assert "0" in medrecord.nodes_in_group("0") + assert "1" in medrecord.nodes_in_group("0") # Adding polars dataframe medrecord = MedRecord() @@ -604,8 +616,8 @@ def test_add_nodes(self) -> None: medrecord.add_nodes((nodes, "index"), "0") - self.assertIn("0", medrecord.nodes_in_group("0")) - self.assertIn("1", medrecord.nodes_in_group("0")) + assert "0" in medrecord.nodes_in_group("0") + assert "1" in medrecord.nodes_in_group("0") # Adding multiple pandas dataframes medrecord = MedRecord() @@ -632,27 +644,27 @@ def test_add_nodes(self) -> None: group="0", ) - self.assertIn("0", medrecord.nodes_in_group("0")) - self.assertIn("1", medrecord.nodes_in_group("0")) - self.assertIn("2", medrecord.nodes_in_group("0")) - self.assertIn("3", medrecord.nodes_in_group("0")) + assert "0" in medrecord.nodes_in_group("0") + assert "1" in medrecord.nodes_in_group("0") + assert "2" in medrecord.nodes_in_group("0") + assert "3" in medrecord.nodes_in_group("0") # Checking if nodes can be added to a group that already exists medrecord = MedRecord() medrecord.add_nodes((create_pandas_nodes_dataframe(), "index"), group="0") - self.assertIn("0", medrecord.nodes_in_group("0")) - self.assertIn("1", medrecord.nodes_in_group("0")) - self.assertNotIn("2", medrecord.nodes_in_group("0")) - self.assertNotIn("3", medrecord.nodes_in_group("0")) + assert "0" in medrecord.nodes_in_group("0") + assert "1" in medrecord.nodes_in_group("0") + assert "2" not in medrecord.nodes_in_group("0") + assert "3" not in medrecord.nodes_in_group("0") medrecord.add_nodes( (create_second_pandas_nodes_dataframe(), "index"), group="0" ) - self.assertIn("2", medrecord.nodes_in_group("0")) - self.assertIn("3", medrecord.nodes_in_group("0")) + assert "2" in medrecord.nodes_in_group("0") + assert "3" in medrecord.nodes_in_group("0") # Adding multiple polars dataframes medrecord = MedRecord() @@ -681,12 +693,12 @@ def test_add_nodes(self) -> None: group="0", ) - self.assertIn("0", medrecord.nodes_in_group("0")) - self.assertIn("1", medrecord.nodes_in_group("0")) - self.assertIn("2", medrecord.nodes_in_group("0")) - self.assertIn("3", medrecord.nodes_in_group("0")) + assert "0" in medrecord.nodes_in_group("0") + assert "1" in medrecord.nodes_in_group("0") + assert "2" in medrecord.nodes_in_group("0") + assert "3" in medrecord.nodes_in_group("0") - def test_invalid_add_nodes(self): + def test_invalid_add_nodes(self) -> None: medrecord = create_medrecord() with pytest.raises(AssertionError): @@ -718,26 +730,26 @@ def test_add_nodes_pandas(self) -> None: medrecord.add_nodes_pandas(nodes, group="0") - self.assertIn("0", medrecord.nodes_in_group("0")) - self.assertIn("1", medrecord.nodes_in_group("0")) + assert "0" in medrecord.nodes_in_group("0") + assert "1" in medrecord.nodes_in_group("0") medrecord = MedRecord() medrecord.add_nodes_pandas([], group="0") - self.assertEqual(0, medrecord.node_count()) - self.assertIn("0", medrecord.groups) + assert medrecord.node_count() == 0 + assert "0" in medrecord.groups medrecord = MedRecord() medrecord.add_nodes_pandas([nodes, second_nodes], group="0") - self.assertIn("0", medrecord.nodes_in_group("0")) - self.assertIn("1", medrecord.nodes_in_group("0")) - self.assertIn("2", medrecord.nodes_in_group("0")) - self.assertIn("3", medrecord.nodes_in_group("0")) + assert "0" in medrecord.nodes_in_group("0") + assert "1" in medrecord.nodes_in_group("0") + assert "2" in medrecord.nodes_in_group("0") + assert "3" in medrecord.nodes_in_group("0") - def test_add_nodes_polars(self): + def test_add_nodes_polars(self) -> None: medrecord = MedRecord() nodes = pl.from_pandas(create_pandas_nodes_dataframe()) @@ -763,15 +775,15 @@ def test_add_nodes_polars(self): medrecord.add_nodes_polars((nodes, "index"), group="0") - self.assertIn("0", medrecord.nodes_in_group("0")) - self.assertIn("1", medrecord.nodes_in_group("0")) + assert "0" in medrecord.nodes_in_group("0") + assert "1" in medrecord.nodes_in_group("0") medrecord = MedRecord() medrecord.add_nodes_polars([], group="0") - self.assertEqual(0, medrecord.node_count()) - self.assertIn("0", medrecord.groups) + assert medrecord.node_count() == 0 + assert "0" in medrecord.groups medrecord = MedRecord() @@ -779,12 +791,12 @@ def test_add_nodes_polars(self): [(nodes, "index"), (second_nodes, "index")], group="0" ) - self.assertIn("0", medrecord.nodes_in_group("0")) - self.assertIn("1", medrecord.nodes_in_group("0")) - self.assertIn("2", medrecord.nodes_in_group("0")) - self.assertIn("3", medrecord.nodes_in_group("0")) + assert "0" in medrecord.nodes_in_group("0") + assert "1" in medrecord.nodes_in_group("0") + assert "2" in medrecord.nodes_in_group("0") + assert "3" in medrecord.nodes_in_group("0") - def test_invalid_add_nodes_polars(self): + def test_invalid_add_nodes_polars(self) -> None: medrecord = MedRecord() nodes = pl.from_pandas(create_pandas_nodes_dataframe()) @@ -798,7 +810,7 @@ def test_invalid_add_nodes_polars(self): with pytest.raises(RuntimeError): medrecord.add_nodes_polars([(nodes, "index"), (second_nodes, "invalid")]) - def test_remove_edges(self): + def test_remove_edges(self) -> None: medrecord = create_medrecord() assert medrecord.edge_count() == 4 @@ -817,7 +829,7 @@ def test_remove_edges(self): assert medrecord.edge_count() == 4 - def query(edge: EdgeOperand): + def query(edge: EdgeOperand) -> None: edge.index().is_in([0, 1]) attributes = medrecord.remove_edges(query) @@ -825,11 +837,11 @@ def query(edge: EdgeOperand): assert medrecord.edge_count() == 2 assert attributes == {0: create_edges()[0][2], 1: create_edges()[1][2]} - def test_invalid_remove_edges(self): + def test_invalid_remove_edges(self) -> None: medrecord = create_medrecord() # Removing a non-existing edge should fail - with self.assertRaises(IndexError): + with pytest.raises(IndexError): medrecord.remove_edges(50) def test_add_edges(self) -> None: @@ -848,16 +860,16 @@ def test_add_edges(self) -> None: # Adding single edge tuple medrecord = create_medrecord() - self.assertEqual(4, medrecord.edge_count()) + assert medrecord.edge_count() == 4 medrecord.add_edges(("0", "3", {})) - self.assertEqual(5, medrecord.edge_count()) + assert medrecord.edge_count() == 5 medrecord.add_edges(("3", "0", {}), group="0") - self.assertEqual(6, medrecord.edge_count()) - self.assertIn(5, medrecord.edges_in_group("0")) + assert medrecord.edge_count() == 6 + assert 5 in medrecord.edges_in_group("0") # Adding tuple to a group medrecord = MedRecord() @@ -866,10 +878,10 @@ def test_add_edges(self) -> None: medrecord.add_edges(create_edges(), "0") - self.assertIn(0, medrecord.edges_in_group("0")) - self.assertIn(1, medrecord.edges_in_group("0")) - self.assertIn(2, medrecord.edges_in_group("0")) - self.assertIn(3, medrecord.edges_in_group("0")) + assert 0 in medrecord.edges_in_group("0") + assert 1 in medrecord.edges_in_group("0") + assert 2 in medrecord.edges_in_group("0") + assert 3 in medrecord.edges_in_group("0") # Adding pandas dataframe medrecord = MedRecord() @@ -889,8 +901,8 @@ def test_add_edges(self) -> None: medrecord.add_edges((create_pandas_edges_dataframe(), "source", "target"), "0") - self.assertIn(0, medrecord.edges_in_group("0")) - self.assertIn(1, medrecord.edges_in_group("0")) + assert 0 in medrecord.edges_in_group("0") + assert 1 in medrecord.edges_in_group("0") # Adding polars dataframe medrecord = MedRecord() @@ -912,8 +924,8 @@ def test_add_edges(self) -> None: medrecord.add_edges((edges, "source", "target"), "0") - self.assertIn(0, medrecord.edges_in_group("0")) - self.assertIn(1, medrecord.edges_in_group("0")) + assert 0 in medrecord.edges_in_group("0") + assert 1 in medrecord.edges_in_group("0") # Adding multiple pandas dataframe medrecord = MedRecord() @@ -944,10 +956,10 @@ def test_add_edges(self) -> None: "0", ) - self.assertIn(0, medrecord.edges_in_group("0")) - self.assertIn(1, medrecord.edges_in_group("0")) - self.assertIn(2, medrecord.edges_in_group("0")) - self.assertIn(3, medrecord.edges_in_group("0")) + assert 0 in medrecord.edges_in_group("0") + assert 1 in medrecord.edges_in_group("0") + assert 2 in medrecord.edges_in_group("0") + assert 3 in medrecord.edges_in_group("0") # Adding multiple polars dataframe medrecord = MedRecord() @@ -980,12 +992,12 @@ def test_add_edges(self) -> None: "0", ) - self.assertIn(0, medrecord.edges_in_group("0")) - self.assertIn(1, medrecord.edges_in_group("0")) - self.assertIn(2, medrecord.edges_in_group("0")) - self.assertIn(3, medrecord.edges_in_group("0")) + assert 0 in medrecord.edges_in_group("0") + assert 1 in medrecord.edges_in_group("0") + assert 2 in medrecord.edges_in_group("0") + assert 3 in medrecord.edges_in_group("0") - def test_invalid_add_edges(self): + def test_invalid_add_edges(self) -> None: medrecord = MedRecord() nodes = create_nodes() @@ -993,14 +1005,14 @@ def test_invalid_add_edges(self): medrecord.add_nodes(nodes) # Adding an edge pointing to a non-existent node should fail - with self.assertRaises(IndexError): + with pytest.raises(IndexError): medrecord.add_edges(("0", "50", {})) # Adding an edge from a non-existing node should fail - with self.assertRaises(IndexError): + with pytest.raises(IndexError): medrecord.add_edges(("50", "0", {})) - def test_add_edges_pandas(self): + def test_add_edges_pandas(self) -> None: medrecord = MedRecord() nodes = create_nodes() @@ -1022,8 +1034,8 @@ def test_add_edges_pandas(self): medrecord.add_edges(edges, "0") - self.assertIn(0, medrecord.edges_in_group("0")) - self.assertIn(1, medrecord.edges_in_group("0")) + assert 0 in medrecord.edges_in_group("0") + assert 1 in medrecord.edges_in_group("0") medrecord = MedRecord() @@ -1033,12 +1045,12 @@ def test_add_edges_pandas(self): medrecord.add_edges([edges, second_edges], "0") - self.assertIn(0, medrecord.edges_in_group("0")) - self.assertIn(1, medrecord.edges_in_group("0")) - self.assertIn(2, medrecord.edges_in_group("0")) - self.assertIn(3, medrecord.edges_in_group("0")) + assert 0 in medrecord.edges_in_group("0") + assert 1 in medrecord.edges_in_group("0") + assert 2 in medrecord.edges_in_group("0") + assert 3 in medrecord.edges_in_group("0") - def test_add_edges_polars(self): + def test_add_edges_polars(self) -> None: medrecord = MedRecord() nodes = create_nodes() @@ -1060,8 +1072,8 @@ def test_add_edges_polars(self): medrecord.add_edges_polars((edges, "source", "target"), "0") - self.assertIn(0, medrecord.edges_in_group("0")) - self.assertIn(1, medrecord.edges_in_group("0")) + assert 0 in medrecord.edges_in_group("0") + assert 1 in medrecord.edges_in_group("0") medrecord = MedRecord() @@ -1073,12 +1085,12 @@ def test_add_edges_polars(self): [(edges, "source", "target"), (second_edges, "source", "target")], "0" ) - self.assertIn(0, medrecord.edges_in_group("0")) - self.assertIn(1, medrecord.edges_in_group("0")) - self.assertIn(2, medrecord.edges_in_group("0")) - self.assertIn(3, medrecord.edges_in_group("0")) + assert 0 in medrecord.edges_in_group("0") + assert 1 in medrecord.edges_in_group("0") + assert 2 in medrecord.edges_in_group("0") + assert 3 in medrecord.edges_in_group("0") - def test_invalid_add_edges_polars(self): + def test_invalid_add_edges_polars(self) -> None: medrecord = MedRecord() nodes = create_nodes() @@ -1116,10 +1128,10 @@ def test_add_group(self) -> None: assert sorted(["0", "1"]) == sorted(nodes_and_edges["nodes"]) assert sorted([0, 1]) == sorted(nodes_and_edges["edges"]) - def query1(node: NodeOperand): + def query1(node: NodeOperand) -> None: node.index().is_in(["0", "1"]) - def query2(edge: EdgeOperand): + def query2(edge: EdgeOperand) -> None: edge.index().is_in([0, 1]) medrecord.add_group( @@ -1158,14 +1170,14 @@ def test_invalid_add_group(self) -> None: with pytest.raises(AssertionError): medrecord.add_group("0", ["1", "0"]) - def query(node: NodeOperand): + def query(node: NodeOperand) -> None: node.index().equal_to("0") # Adding a node to a group that already is in the group should fail - with self.assertRaises(AssertionError): + with pytest.raises(AssertionError): medrecord.add_group("0", query) - def test_remove_groups(self): + def test_remove_groups(self) -> None: medrecord = create_medrecord() medrecord.add_group("0") @@ -1176,14 +1188,14 @@ def test_remove_groups(self): assert medrecord.group_count() == 0 - def test_invalid_remove_groups(self): + def test_invalid_remove_groups(self) -> None: medrecord = create_medrecord() # Removing a non-existing group should fail - with self.assertRaises(IndexError): + with pytest.raises(IndexError): medrecord.remove_groups("0") - def test_add_nodes_to_group(self): + def test_add_nodes_to_group(self) -> None: medrecord = create_medrecord() medrecord.add_group("0") @@ -1198,50 +1210,50 @@ def test_add_nodes_to_group(self): assert sorted(["0", "1", "2"]) == sorted(medrecord.nodes_in_group("0")) - def query(node: NodeOperand): + def query(node: NodeOperand) -> None: node.index().equal_to("3") medrecord.add_nodes_to_group("0", query) assert sorted(["0", "1", "2", "3"]) == sorted(medrecord.nodes_in_group("0")) - def test_invalid_add_nodes_to_group(self): + def test_invalid_add_nodes_to_group(self) -> None: medrecord = create_medrecord() medrecord.add_group("0", ["0"]) # Adding to a non-existing group should fail - with self.assertRaises(IndexError): + with pytest.raises(IndexError): medrecord.add_nodes_to_group("50", "1") # Adding to a non-existing group should fail - with self.assertRaises(IndexError): + with pytest.raises(IndexError): medrecord.add_nodes_to_group("50", ["1", "2"]) # Adding a non-existing node to a group should fail - with self.assertRaises(IndexError): + with pytest.raises(IndexError): medrecord.add_nodes_to_group("0", "50") # Adding a non-existing node to a group should fail - with self.assertRaises(IndexError): + with pytest.raises(IndexError): medrecord.add_nodes_to_group("0", ["1", "50"]) # Adding a node to a group that already is in the group should fail - with self.assertRaises(AssertionError): + with pytest.raises(AssertionError): medrecord.add_nodes_to_group("0", "0") # Adding a node to a group that already is in the group should fail - with self.assertRaises(AssertionError): + with pytest.raises(AssertionError): medrecord.add_nodes_to_group("0", ["1", "0"]) - def query(node: NodeOperand): + def query(node: NodeOperand) -> None: node.index().equal_to("0") # Adding a node to a group that already is in the group should fail - with self.assertRaises(AssertionError): + with pytest.raises(AssertionError): medrecord.add_nodes_to_group("0", query) - def test_add_edges_to_group(self): + def test_add_edges_to_group(self) -> None: medrecord = create_medrecord() medrecord.add_group("0") @@ -1256,50 +1268,50 @@ def test_add_edges_to_group(self): assert sorted([0, 1, 2]) == sorted(medrecord.edges_in_group("0")) - def query(edge: EdgeOperand): + def query(edge: EdgeOperand) -> None: edge.index().equal_to(3) medrecord.add_edges_to_group("0", query) assert sorted([0, 1, 2, 3]) == sorted(medrecord.edges_in_group("0")) - def test_invalid_add_edges_to_group(self): + def test_invalid_add_edges_to_group(self) -> None: medrecord = create_medrecord() medrecord.add_group("0", edges=[0]) # Adding to a non-existing group should fail - with self.assertRaises(IndexError): + with pytest.raises(IndexError): medrecord.add_edges_to_group("50", 1) # Adding to a non-existing group should fail - with self.assertRaises(IndexError): + with pytest.raises(IndexError): medrecord.add_edges_to_group("50", [1, 2]) # Adding a non-existing edge to a group should fail - with self.assertRaises(IndexError): + with pytest.raises(IndexError): medrecord.add_edges_to_group("0", 50) # Adding a non-existing edge to a group should fail - with self.assertRaises(IndexError): + with pytest.raises(IndexError): medrecord.add_edges_to_group("0", [1, 50]) # Adding an edge to a group that already is in the group should fail - with self.assertRaises(AssertionError): + with pytest.raises(AssertionError): medrecord.add_edges_to_group("0", 0) # Adding an edge to a group that already is in the group should fail - with self.assertRaises(AssertionError): + with pytest.raises(AssertionError): medrecord.add_edges_to_group("0", [1, 0]) - def query(edge: EdgeOperand): + def query(edge: EdgeOperand) -> None: edge.index().equal_to(0) # Adding an edge to a group that already is in the group should fail - with self.assertRaises(AssertionError): + with pytest.raises(AssertionError): medrecord.add_edges_to_group("0", query) - def test_remove_nodes_from_group(self): + def test_remove_nodes_from_group(self) -> None: medrecord = create_medrecord() medrecord.add_group("0", ["0", "1"]) @@ -1322,42 +1334,42 @@ def test_remove_nodes_from_group(self): assert sorted(["0", "1"]) == sorted(medrecord.nodes_in_group("0")) - def query(node: NodeOperand): + def query(node: NodeOperand) -> None: node.index().is_in(["0", "1"]) medrecord.remove_nodes_from_group("0", query) assert medrecord.nodes_in_group("0") == [] - def test_invalid_remove_nodes_from_group(self): + def test_invalid_remove_nodes_from_group(self) -> None: medrecord = create_medrecord() medrecord.add_group("0", ["0", "1"]) # Removing a node from a non-existing group should fail - with self.assertRaises(IndexError): + with pytest.raises(IndexError): medrecord.remove_nodes_from_group("50", "0") # Removing a node from a non-existing group should fail - with self.assertRaises(IndexError): + with pytest.raises(IndexError): medrecord.remove_nodes_from_group("50", ["0", "1"]) - def query(node: NodeOperand): + def query(node: NodeOperand) -> None: node.index().equal_to("0") # Removing a node from a non-existing group should fail - with self.assertRaises(IndexError): + with pytest.raises(IndexError): medrecord.remove_nodes_from_group("50", query) # Removing a non-existing node from a group should fail - with self.assertRaises(IndexError): + with pytest.raises(IndexError): medrecord.remove_nodes_from_group("0", "50") # Removing a non-existing node from a group should fail - with self.assertRaises(IndexError): + with pytest.raises(IndexError): medrecord.remove_nodes_from_group("0", ["0", "50"]) - def test_remove_edges_from_group(self): + def test_remove_edges_from_group(self) -> None: medrecord = create_medrecord() medrecord.add_group("0", edges=[0, 1]) @@ -1380,39 +1392,39 @@ def test_remove_edges_from_group(self): assert sorted([0, 1]) == sorted(medrecord.edges_in_group("0")) - def query(edge: EdgeOperand): + def query(edge: EdgeOperand) -> None: edge.index().is_in([0, 1]) medrecord.remove_edges_from_group("0", query) assert medrecord.edges_in_group("0") == [] - def test_invalid_remove_edges_from_group(self): + def test_invalid_remove_edges_from_group(self) -> None: medrecord = create_medrecord() medrecord.add_group("0", edges=[0, 1]) # Removing an edge from a non-existing group should fail - with self.assertRaises(IndexError): + with pytest.raises(IndexError): medrecord.remove_edges_from_group("50", 0) # Removing an edge from a non-existing group should fail - with self.assertRaises(IndexError): + with pytest.raises(IndexError): medrecord.remove_edges_from_group("50", [0, 1]) - def query(edge: EdgeOperand): + def query(edge: EdgeOperand) -> None: edge.index().equal_to(0) # Removing an edge from a non-existing group should fail - with self.assertRaises(IndexError): + with pytest.raises(IndexError): medrecord.remove_edges_from_group("50", query) # Removing a non-existing edge from a group should fail - with self.assertRaises(IndexError): + with pytest.raises(IndexError): medrecord.remove_edges_from_group("0", 50) # Removing a non-existing edge from a group should fail - with self.assertRaises(IndexError): + with pytest.raises(IndexError): medrecord.remove_edges_from_group("0", [0, 50]) def test_nodes_in_group(self) -> None: @@ -1452,13 +1464,10 @@ def test_groups_of_node(self) -> None: assert medrecord.groups_of_node(["0", "1"]) == {"0": ["0"], "1": ["0"]} - def query(node: NodeOperand): + def query(node: NodeOperand) -> None: node.index().is_in(["0", "1"]) - self.assertEqual( - {"0": ["0"], "1": ["0"]}, - medrecord.groups_of_node(query), - ) + assert medrecord.groups_of_node(query) == {"0": ["0"], "1": ["0"]} def test_invalid_groups_of_node(self) -> None: medrecord = create_medrecord() @@ -1480,13 +1489,10 @@ def test_groups_of_edge(self) -> None: assert medrecord.groups_of_edge([0, 1]) == {0: ["0"], 1: ["0"]} - def query(edge: EdgeOperand): + def query(edge: EdgeOperand) -> None: edge.index().is_in([0, 1]) - self.assertEqual( - {0: ["0"], 1: ["0"]}, - medrecord.groups_of_edge(query), - ) + assert medrecord.groups_of_edge(query) == {0: ["0"], 1: ["0"]} def test_invalid_groups_of_edge(self) -> None: medrecord = create_medrecord() @@ -1566,7 +1572,7 @@ def test_neighbors(self) -> None: "1": ["0", "2"], } - def query1(node: NodeOperand): + def query1(node: NodeOperand) -> None: node.index().is_in(["0", "1"]) neighbors = medrecord.neighbors(query1) @@ -1587,7 +1593,7 @@ def query1(node: NodeOperand): "1": ["0", "2"], } - def query2(node: NodeOperand): + def query2(node: NodeOperand) -> None: node.index().is_in(["0", "1"]) neighbors = medrecord.neighbors(query2, directed=False) @@ -1629,68 +1635,62 @@ def test_clear(self) -> None: assert medrecord.edge_count() == 0 assert medrecord.group_count() == 0 - def test_clone(self): + def test_clone(self) -> None: medrecord = create_medrecord() cloned_medrecord = medrecord.clone() - self.assertEqual(medrecord.node_count(), cloned_medrecord.node_count()) - self.assertEqual(medrecord.edge_count(), cloned_medrecord.edge_count()) - self.assertEqual(medrecord.group_count(), cloned_medrecord.group_count()) + assert medrecord.node_count() == cloned_medrecord.node_count() + assert medrecord.edge_count() == cloned_medrecord.edge_count() + assert medrecord.group_count() == cloned_medrecord.group_count() cloned_medrecord.add_nodes(("new_node", {"attribute": "value"})) cloned_medrecord.add_edges(("0", "new_node", {"attribute": "value"})) cloned_medrecord.add_group("new_group", ["new_node"]) - self.assertNotEqual(medrecord.node_count(), cloned_medrecord.node_count()) - self.assertNotEqual(medrecord.edge_count(), cloned_medrecord.edge_count()) - self.assertNotEqual(medrecord.group_count(), cloned_medrecord.group_count()) + assert medrecord.node_count() != cloned_medrecord.node_count() + assert medrecord.edge_count() != cloned_medrecord.edge_count() + assert medrecord.group_count() != cloned_medrecord.group_count() - def test_describe_group_nodes(self): + def test_describe_group_nodes(self) -> None: medrecord = create_medrecord() medrecord.add_group("Float") medrecord.add_group(1, nodes=["2", "0"]) medrecord.add_group("Odd", nodes=["1", "3"]) - self.assertDictEqual( - medrecord._describe_group_nodes(), - { - 1: { - "count": 2, - "attribute": { - "adipiscing": {"values": "Values: elit"}, - "dolor": {"values": "Values: sit"}, - "lorem": {"values": "Values: ipsum"}, - }, - }, - "Float": {"count": 0, "attribute": {}}, - "Odd": { - "count": 2, - "attribute": {"amet": {"values": "Values: consectetur"}}, + assert medrecord._describe_group_nodes() == { + 1: { + "count": 2, + "attribute": { + "adipiscing": {"values": "Values: elit"}, + "dolor": {"values": "Values: sit"}, + "lorem": {"values": "Values: ipsum"}, }, }, - ) + "Float": {"count": 0, "attribute": {}}, + "Odd": { + "count": 2, + "attribute": {"amet": {"values": "Values: consectetur"}}, + }, + } - def test_describe_group_edges(self): + def test_describe_group_edges(self) -> None: medrecord = create_medrecord() medrecord.add_group("Even", edges=[0, 2]) - self.assertDictEqual( - medrecord._describe_group_edges(), - { - "Even": { - "count": 2, - "attribute": { - "eiusmod": {"values": "Values: tempor"}, - "incididunt": {"values": "Values: ut"}, - "sed": {"values": "Values: do"}, - }, + assert medrecord._describe_group_edges() == { + "Even": { + "count": 2, + "attribute": { + "eiusmod": {"values": "Values: tempor"}, + "incididunt": {"values": "Values: ut"}, + "sed": {"values": "Values: do"}, }, - "Ungrouped Edges": {"count": 2, "attribute": {}}, }, - ) + "Ungrouped Edges": {"count": 2, "attribute": {}}, + } if __name__ == "__main__": diff --git a/medmodels/medrecord/tests/test_overview.py b/medmodels/medrecord/tests/test_overview.py index ff822bd..15259f0 100644 --- a/medmodels/medrecord/tests/test_overview.py +++ b/medmodels/medrecord/tests/test_overview.py @@ -10,7 +10,7 @@ from medmodels.medrecord.querying import EdgeOperand, NodeOperand -def create_medrecord(): +def create_medrecord() -> mm.MedRecord: patients = pd.DataFrame( { "index": ["P1", "P2", "P3"], @@ -69,19 +69,19 @@ def create_medrecord(): class TestOverview(unittest.TestCase): - def test_extract_attribute_summary(self): + def test_extract_attribute_summary(self) -> None: # medrecord without schema medrecord = create_medrecord() - def query1(node: NodeOperand): + def query1(node: NodeOperand) -> None: node.in_group("Stroke") # No attributes no_attributes = extract_attribute_summary(medrecord.node[query1]) - self.assertDictEqual(no_attributes, {}) + assert no_attributes == {} - def query2(node: NodeOperand): + def query2(node: NodeOperand) -> None: node.in_group("Patients") # numeric type @@ -89,44 +89,39 @@ def query2(node: NodeOperand): numeric_expected = {"age": {"min": 20, "max": 70, "mean": 40.0}} - self.assertDictEqual(numeric_attribute, numeric_expected) + assert numeric_attribute == numeric_expected - def query3(node: NodeOperand): + def query3(node: NodeOperand) -> None: node.in_group("Medications") # string attributes str_attributes = extract_attribute_summary(medrecord.node[query3]) - self.assertDictEqual( - str_attributes, {"ATC": {"values": "Values: B01AA03, B01AF01"}} - ) + assert str_attributes == {"ATC": {"values": "Values: B01AA03, B01AF01"}} - def query4(node: NodeOperand): + def query4(node: NodeOperand) -> None: node.in_group("Aspirin") # nan attribute nan_attributes = extract_attribute_summary(medrecord.node[query4]) - self.assertDictEqual(nan_attributes, {"ATC": {"values": "-"}}) + assert nan_attributes == {"ATC": {"values": "-"}} - def query5(edge: EdgeOperand): + def query5(edge: EdgeOperand) -> None: edge.source_node().in_group("Medications") edge.target_node().in_group("Patients") # temporal attributes temp_attributes = extract_attribute_summary(medrecord.edge[query5]) - self.assertDictEqual( - temp_attributes, - { - "time": { - "max": datetime(1999, 10, 15, 0, 0), - "min": datetime(1999, 10, 15, 0, 0), - } - }, - ) + assert temp_attributes == { + "time": { + "max": datetime(1999, 10, 15, 0, 0), + "min": datetime(1999, 10, 15, 0, 0), + } + } - def query6(edge: EdgeOperand): + def query6(edge: EdgeOperand) -> None: edge.source_node().in_group("Stroke") edge.target_node().in_group("Patients") @@ -134,16 +129,13 @@ def query6(edge: EdgeOperand): mixed_attributes = extract_attribute_summary( medrecord.edge[medrecord.select_edges(query6)] ) - self.assertDictEqual( - mixed_attributes, - { - "time": { - "min": datetime(1999, 12, 15, 0, 0), - "max": datetime(2000, 1, 1, 0, 0), - }, - "intensity": {"values": "Values: 1, low"}, + assert mixed_attributes == { + "time": { + "min": datetime(1999, 12, 15, 0, 0), + "max": datetime(2000, 1, 1, 0, 0), }, - ) + "intensity": {"values": "Values: 1, low"}, + } # with schema mr_schema = mm.MedRecord.from_example_dataset() @@ -154,15 +146,12 @@ def query6(edge: EdgeOperand): schema=mr_schema.schema.group("patient").nodes, ) - self.assertDictEqual( - node_info, - { - "age": {"min": 19, "max": 96, "mean": 43.20}, - "gender": {"values": "Categories: F, M"}, - }, - ) + assert node_info == { + "age": {"min": 19, "max": 96, "mean": 43.20}, + "gender": {"values": "Categories: F, M"}, + } - def query7(edge: EdgeOperand): + def query7(edge: EdgeOperand) -> None: edge.in_group("patient_diagnosis") # compare schema and not schema @@ -171,22 +160,19 @@ def query7(edge: EdgeOperand): schema=mr_schema.schema.group("patient_diagnosis").edges, ) - self.assertDictEqual( - patient_diagnosis, - { - "diagnosis_time": { - "min": datetime(1962, 10, 21, 0, 0), - "max": datetime(2024, 4, 12, 0, 0), - }, - "duration_days": { - "min": 0.0, - "max": 3416.0, - "mean": 405.0232558139535, - }, + assert patient_diagnosis == { + "diagnosis_time": { + "min": datetime(1962, 10, 21, 0, 0), + "max": datetime(2024, 4, 12, 0, 0), }, - ) + "duration_days": { + "min": 0.0, + "max": 3416.0, + "mean": 405.0232558139535, + }, + } - def test_prettify_table(self): + def test_prettify_table(self) -> None: medrecord = create_medrecord() header = ["group nodes", "count", "attribute", "info"] @@ -205,9 +191,9 @@ def test_prettify_table(self): "---------------------------------------------------------", ] - self.assertEqual( - prettify_table(medrecord._describe_group_nodes(), header, decimal=2), - expected_empty, + assert ( + prettify_table(medrecord._describe_group_nodes(), header, decimal=2) + == expected_empty )