Skip to content

Commit

Permalink
adapt unit tests for split secagg_manager between common and node
Browse files Browse the repository at this point in the history
  • Loading branch information
mvesin committed Mar 1, 2023
1 parent 5b5883a commit 854abff
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 59 deletions.
4 changes: 2 additions & 2 deletions tests/test_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -1140,8 +1140,8 @@ def test_node_30_task_secagg_delete(
'sequence': req['sequence'],
'success': False,
'node_id': environ["ID"],
'msg': 'FB321: Secure aggregation delete error: Can not instantiate SecaggManager object FB621: '
f'Secure aggregation database error: received bad message: incorrect `element` {req["element"]}',
'msg': 'FB321: Secure aggregation delete error: Can not instantiate SecaggManager object FB318: '
f'Secure aggregation setup error: received bad message: incorrect `element` {req["element"]}',
'command': 'secagg-delete'
})
messaging_send_msg.reset_mock()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,8 @@
from unittest.mock import patch
import copy

#############################################################
# Import NodeTestCase before importing FedBioMed Module
from testsupport.base_case import NodeTestCase
#############################################################

from fedbiomed.common.constants import _BaseEnum
from fedbiomed.common.exceptions import FedbiomedSecaggError
from fedbiomed.node.secagg_manager import SecaggServkeyManager, SecaggBiprimeManager, SecaggManager
from fedbiomed.common.secagg_manager import SecaggServkeyManager, SecaggBiprimeManager


class FakeTinyDB:
Expand Down Expand Up @@ -56,12 +50,12 @@ def remove(self, *args, **kwargs):
return True


class TestBaseSecaggManager(NodeTestCase):
"""Test for SecaggManager node side module"""
class TestBaseSecaggManager(unittest.TestCase):
"""Test for common secagg_manager module"""

def setUp(self):
self.patcher_db = patch('fedbiomed.node.secagg_manager.TinyDB', FakeTinyDB)
self.patcher_query = patch('fedbiomed.node.secagg_manager.Query', FakeQuery)
self.patcher_db = patch('fedbiomed.common.secagg_manager.TinyDB', FakeTinyDB)
self.patcher_query = patch('fedbiomed.common.secagg_manager.Query', FakeQuery)

self.patcher_db.start()
self.patcher_query.start()
Expand All @@ -71,22 +65,22 @@ def tearDown(self) -> None:
self.patcher_db.stop()

def test_secagg_manager_01_init_ok(self):
"""Instantiate SecaggManager normal successful case"""
"""Instantiate SecaggServkeyManager / SecaggBiprimeManager normal successful case"""
# prepare
managers = [SecaggServkeyManager, SecaggBiprimeManager]

# action
for manager in managers:
manager()
manager('/path/to/dummy/file')

# test
# nothing to test at this point ...

@patch('fedbiomed.node.secagg_manager.TinyDB.__init__')
@patch('fedbiomed.common.secagg_manager.TinyDB.__init__')
def test_secagg_manager_02_init_error(
self,
patch_tinydb_init):
"""Instantiate SecaggManager fails with exception"""
"""Instantiate SecaggServkeyManager / SecaggBiprimeManager fails with exception"""
# prepare
managers = [SecaggServkeyManager, SecaggBiprimeManager]

Expand All @@ -95,10 +89,10 @@ def test_secagg_manager_02_init_error(
# action + check
for manager in managers:
with self.assertRaises(FedbiomedSecaggError):
manager()
manager('/path/to/dummy/file')

def test_secagg_manager_03_get_ok(self):
"""Using `get()` method from SecaggManager successfully"""
"""Using `get()` method from SecaggServkeyManager / SecaggBiprimeManager successfully"""
# preparation
managers = [SecaggServkeyManager, SecaggBiprimeManager]
entries_list = [
Expand All @@ -111,7 +105,7 @@ def test_secagg_manager_03_get_ok(self):
for m in managers:
for entries, job_id in entries_list:
# preparation (continued)
manager = m()
manager = m('/path/to/dummy/file')
# should not be accessing private variable, but got no getter + avoid writing a specific fake class
# for each test
manager._db.db_table.entries = entries
Expand Down Expand Up @@ -147,7 +141,7 @@ def test_secagg_manager_04_get_remove_error_bad_input(self):
if not test_for_biprime and m == SecaggBiprimeManager:
continue

manager = m()
manager = m('/path/to/dummy/file')
# should not be accessing private variable, but got no getter + avoid writing a specific fake class
# for each test
manager._db.db_table.entries = entries
Expand Down Expand Up @@ -178,7 +172,7 @@ def test_secagg_manager_05_get_error_table_access_error(self):
for m in managers:
for entries, job_id in entries_list:
# preparation (continued)
manager = m()
manager = m('/path/to/dummy/file')
# should not be accessing private variable, but avoids writing a specific fake class
# for each test
manager._db.db_table.exception_search = True
Expand Down Expand Up @@ -209,7 +203,7 @@ def test_secagg_manager_06_add_ok_remove_ok(self):
for parties in parties_list:
for m, specific, kwargs in specific_list:
# preparation (continued)
manager = m()
manager = m('/path/to/dummy/file')
expected_entries = copy.deepcopy(specific)
expected_entries.update({'secagg_id': secagg_id, 'parties': parties})

Expand Down Expand Up @@ -263,7 +257,7 @@ def test_secagg_manager_07_add_error_re_inserting(self):
for parties in parties_list:
for m, specific, alt_specific, kwargs in specific_list:
# preparation (continued)
manager = m()
manager = m('/path/to/dummy/file')
manager.add(secagg_id, parties, **specific)

# action + check
Expand Down Expand Up @@ -291,7 +285,7 @@ def test_secagg_manager_08_add_table_access_error(self):
for parties in parties_list:
for m, specific, kwargs in specific_list:
# preparation (continued)
manager = m()
manager = m('/path/to/dummy/file')
# should not be accessing private variable, but avoids writing a specific fake class
# for each test
manager._db.db_table.exception_insert = True
Expand All @@ -312,39 +306,5 @@ def test_secagg_manager_08_add_table_access_error(self):
manager.remove('my_secagg_id', **kwargs)


class TestSecaggManager(NodeTestCase):

def setUp(self) -> None:
pass

def tearDown(self) -> None:
pass

def test_secagg_manager_01_initialization(self):

# Test server key manager
secagg_setup = SecaggManager(0)()
self.assertIsInstance(secagg_setup, SecaggServkeyManager)

# Test biprime manager
secagg_setup = SecaggManager(1)()
self.assertIsInstance(secagg_setup, SecaggBiprimeManager)

# Raise element type erro
with self.assertRaises(FedbiomedSecaggError):
SecaggManager(2)()

# Raise missing component for element type error
with patch('fedbiomed.node.secagg_manager.SecaggElementTypes') as element_types_patch:
class FakeSecaggElementTypes(_BaseEnum):
DUMMY: int = 0
element_types_patch.return_value = FakeSecaggElementTypes(0)
element_types_patch.__iter__.return_value = [
FakeSecaggElementTypes(0)
]

with self.assertRaises(FedbiomedSecaggError):
SecaggManager(0)()

if __name__ == '__main__': # pragma: no cover
unittest.main()
50 changes: 50 additions & 0 deletions tests/test_secagg_manager_node.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import unittest
from unittest.mock import patch

#############################################################
# Import NodeTestCase before importing FedBioMed Module
from testsupport.base_case import NodeTestCase
#############################################################

from fedbiomed.common.constants import _BaseEnum
from fedbiomed.common.exceptions import FedbiomedSecaggError
from fedbiomed.common.secagg_manager import SecaggServkeyManager, SecaggBiprimeManager
from fedbiomed.node.secagg_manager import SecaggManager


class TestSecaggManager(NodeTestCase):

def setUp(self) -> None:
pass

def tearDown(self) -> None:
pass

def test_secagg_manager_01_initialization(self):

# Test server key manager
secagg_setup = SecaggManager(0)()
self.assertIsInstance(secagg_setup, SecaggServkeyManager)

# Test biprime manager
secagg_setup = SecaggManager(1)()
self.assertIsInstance(secagg_setup, SecaggBiprimeManager)

# Raise element type erro
with self.assertRaises(FedbiomedSecaggError):
SecaggManager(2)()

# Raise missing component for element type error
with patch('fedbiomed.node.secagg_manager.SecaggElementTypes') as element_types_patch:
class FakeSecaggElementTypes(_BaseEnum):
DUMMY: int = 0
element_types_patch.return_value = FakeSecaggElementTypes(0)
element_types_patch.__iter__.return_value = [
FakeSecaggElementTypes(0)
]

with self.assertRaises(FedbiomedSecaggError):
SecaggManager(0)()

if __name__ == '__main__': # pragma: no cover
unittest.main()

0 comments on commit 854abff

Please sign in to comment.