Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace assertDictContainsSubset() which has been removed in Python 3.12 #2997

Merged
merged 2 commits into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 35 additions & 35 deletions testsuite/pytests/test_helper_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,39 +19,39 @@
# You should have received a copy of the GNU General Public License
# along with NEST. If not, see <http://www.gnu.org/licenses/>.

import unittest

import nest


class TestHelperFunctions(unittest.TestCase):
def test_get_verbosity(self):
verbosity = nest.get_verbosity()
self.assertTrue(isinstance(verbosity, int))

def test_set_verbosity(self):
levels = [
("M_ALL", 0),
("M_DEBUG", 5),
("M_STATUS", 7),
("M_INFO", 10),
("M_DEPRECATED", 18),
("M_WARNING", 20),
("M_ERROR", 30),
("M_FATAL", 40),
("M_QUIET", 100),
]
for level, code in levels:
nest.set_verbosity(level)
verbosity = nest.get_verbosity()
self.assertEqual(verbosity, code)


def suite():
suite = unittest.makeSuite(TestHelperFunctions, "test")
return suite


if __name__ == "__main__":
runner = unittest.TextTestRunner(verbosity=2)
runner.run(suite())
import pytest
import testutil


@pytest.mark.parametrize(
"level, value",
[
("M_ALL", 0),
("M_DEBUG", 5),
("M_STATUS", 7),
("M_INFO", 10),
("M_DEPRECATED", 18),
("M_WARNING", 20),
("M_ERROR", 30),
("M_FATAL", 40),
("M_QUIET", 100),
],
)
def test_set_verbosity(level, value):
nest.set_verbosity(level)
assert nest.get_verbosity() == value


@pytest.mark.parametrize(
"a, b, expected",
[
({}, {}, True),
({}, {"a": 5}, True),
({"a": 5}, {"a": 5}, True),
({"a": 7}, {"a": 5}, False),
({"a": 5, "b": 3}, {"a": 5}, False),
],
)
def test_dict_is_subset(a, b, expected):
assert testutil.dict_is_subset_of(a, b) == expected
9 changes: 5 additions & 4 deletions testsuite/pytests/test_sp/test_synaptic_elements.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import unittest

import nest
from testutil import dict_is_subset_of

__author__ = "naveau"

Expand All @@ -37,7 +38,7 @@ def test_set_status(self):
nest.SetStatus(neuron, {"synaptic_elements": synaptic_element_dict})
neuron_synaptic_elements = nest.GetStatus(neuron, "synaptic_elements")[0]
self.assertIn("SE", neuron_synaptic_elements)
self.assertDictContainsSubset(synaptic_element_dict["SE"], neuron_synaptic_elements["SE"])
self.assertTrue(dict_is_subset_of(synaptic_element_dict["SE"], neuron_synaptic_elements["SE"]))

def test_set_status_overwrite(self):
synaptic_element_dict1 = {"SE1": {"z": 15.0, "growth_curve": "linear"}}
Expand All @@ -50,7 +51,7 @@ def test_set_status_overwrite(self):
neuron_synaptic_elements = nest.GetStatus(neuron, "synaptic_elements")[0]
self.assertNotIn("SE1", neuron_synaptic_elements)
self.assertIn("SE2", neuron_synaptic_elements)
self.assertDictContainsSubset(synaptic_element_dict2["SE2"], neuron_synaptic_elements["SE2"])
self.assertTrue(dict_is_subset_of(synaptic_element_dict2["SE2"], neuron_synaptic_elements["SE2"]))

def test_set_defaults(self):
synaptic_element_dict = {"SE": {"z": 15.0, "growth_curve": "linear"}}
Expand All @@ -59,7 +60,7 @@ def test_set_defaults(self):
neuron = nest.Create("iaf_psc_alpha", 1)
neuron_synaptic_elements = nest.GetStatus(neuron, "synaptic_elements")[0]
self.assertIn("SE", neuron_synaptic_elements)
self.assertDictContainsSubset(synaptic_element_dict["SE"], neuron_synaptic_elements["SE"])
self.assertTrue(dict_is_subset_of(synaptic_element_dict["SE"], neuron_synaptic_elements["SE"]))

def test_set_defaults_overwrite(self):
synaptic_element_dict1 = {"SE1": {"z": 15.0, "growth_curve": "linear"}}
Expand All @@ -72,7 +73,7 @@ def test_set_defaults_overwrite(self):
neuron_synaptic_elements = nest.GetStatus(neuron, "synaptic_elements")[0]
self.assertNotIn("SE1", neuron_synaptic_elements)
self.assertIn("SE2", neuron_synaptic_elements)
self.assertDictContainsSubset(synaptic_element_dict2["SE2"], neuron_synaptic_elements["SE2"])
self.assertTrue(dict_is_subset_of(synaptic_element_dict2["SE2"], neuron_synaptic_elements["SE2"]))


def suite():
Expand Down
10 changes: 6 additions & 4 deletions testsuite/pytests/test_sp/test_update_synaptic_elements.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import unittest

import nest
from testutil import dict_is_subset_of


class TestUpdateSynapticElements(unittest.TestCase):
Expand Down Expand Up @@ -55,8 +56,8 @@ def test_update_synaptic_elements(self):
self.assertIn("Den_ex", neuron_synaptic_elements)
self.assertIn("Axon", neuron_synaptic_elements)

self.assertDictContainsSubset(structural_p_elements["Axon"], neuron_synaptic_elements["Axon"])
self.assertDictContainsSubset(structural_p_elements["Den_ex"], neuron_synaptic_elements["Den_ex"])
self.assertTrue(structural_p_elements["Axon"], neuron_synaptic_elements["Axon"])
self.assertTrue(structural_p_elements["Den_ex"], neuron_synaptic_elements["Den_ex"])

# Update Axon elements
nest.SetStatus(neuron, "synaptic_elements_param", elements_to_update)
Expand All @@ -65,9 +66,10 @@ def test_update_synaptic_elements(self):
self.assertIn("Axon", neuron_synaptic_elements)

# Should have been updated
self.assertDictContainsSubset(elements_to_update["Axon"], neuron_synaptic_elements["Axon"])
self.assertTrue(elements_to_update["Axon"], neuron_synaptic_elements["Axon"])

# Should be unchanged
self.assertDictContainsSubset(structural_p_elements["Den_ex"], neuron_synaptic_elements["Den_ex"])
self.assertTrue(structural_p_elements["Den_ex"], neuron_synaptic_elements["Den_ex"])


def suite():
Expand Down
17 changes: 17 additions & 0 deletions testsuite/pytests/utilities/testutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,23 @@ def parameter_fixture(name, default_factory=lambda: None):
return pytest.fixture(autouse=True, name=name)(lambda request: getattr(request, "param", default_factory()))


def dict_is_subset_of(small, big):
"""
Return true if dict `small` is subset of dict `big`.

`small` must contain all keys in `big` with the same values.
"""

# See
# https://stackoverflow.com/questions/20050913/python-unittests-assertdictcontainssubset-recommended-alternative
# https://peps.python.org/pep-0584/
#
# Note: | is **not** a symmetric operator for dicts. `small` must be the second operand to | as it determines
# the value of joint keys in the merged dictionary.

return big == big | small


def isin_approx(A, B, tol=1e-06):
A = np.asarray(A)
B = np.asarray(B)
Expand Down
Loading