FAQ | This is a LIVE service | Changelog

Commit 55168ec6 authored by Robin Goodall's avatar Robin Goodall 💬
Browse files

Additional tests for group creation

parent 79dee0db
......@@ -6,7 +6,9 @@ import ibisclient
from . import temp_password_file
from lookupsync.lookup import (
create_lookup_connection, compare_with_lookup_groups, update_lookup_groups)
create_lookup_connection, compare_with_lookup_groups,
create_lookup_groups, update_lookup_groups,
GROUP_DESCRIPTION, TRANSACTION_COMMENT)
@patch('ibisclient.createConnection')
......@@ -20,6 +22,7 @@ def test_create_lookup_connection_var(ibis_connect_mock: MagicMock):
'--lookup-username': 'TEST_USER',
'--lookup-password': 'TEST_PASSWORD',
'--lookup-test': False,
'--lookup-local': False,
})
# createConnection called once
ibis_connect_mock.assert_called_once()
......@@ -42,6 +45,7 @@ def test_create_lookup_connection_file(ibis_connect_mock: MagicMock):
'--lookup-username': 'TEST_USER',
'--lookup-password-from': password_file,
'--lookup-test': False,
'--lookup-local': False,
})
# createConnection called once
ibis_connect_mock.assert_called_once()
......@@ -65,6 +69,7 @@ def test_create_lookup_test_connection(
'--lookup-username': 'TEST_USER',
'--lookup-password': 'TEST_PASSWORD',
'--lookup-test': True,
'--lookup-local': False,
})
# createConnection is not called
ibis_connect_mock.assert_not_called()
......@@ -160,17 +165,18 @@ def test_compare_with_lookup_groups_missing_group(
"""
# Add a group in mock_student_by_groups that is not in mock_lookup_group_members
NEW_GROUP = 'baz-sis-pg'
USN_TO_ADD = '999999999'
assert mock_lookup_group_members.get(NEW_GROUP) is None
mock_students_by_group[NEW_GROUP] = {'999999999'}
mock_students_by_group[NEW_GROUP] = {USN_TO_ADD}
# Perform comparison
mock_group_methods = MockGroupMethods(mock_lookup_group_members)
missing_groups, group_changes = compare_with_lookup_groups(
mock_group_methods, mock_students_by_group)
# Just new group reported missing
# Just new group reported missing and all members appear in changes to be added
assert missing_groups == {NEW_GROUP}
assert group_changes == dict()
assert group_changes == {NEW_GROUP: {'add': {USN_TO_ADD}, 'remove': set()}}
def test_compare_with_lookup_groups_changes(
......@@ -256,3 +262,115 @@ def test_update_lookup_groups():
# however have matching contents
assert expected_to_add == set(mock_group_methods.updates[group]['to_add'])
assert expected_to_remove == set(mock_group_methods.updates[group]['to_remove'])
# Mock ibisclient.InstitutionMethods
class MockInstitutionMethods:
def __init__(self, insts: List[Dict[str, ibisclient.IbisInstitution]] = list()) -> None:
self.insts = insts
self.group_creations = {}
self.conn = MagicMock()
self.conn.username = 'TEST_USER'
def getInst(self, inst):
return self.insts.get(inst)
def createGroup(self, instid, name, title, description,
managedBy=None, commitComment=None):
# Should only be called with known institution
assert instid in self.insts
# Record all parts of the creation
self.group_creations.setdefault(instid, []).append({
'name': name,
'title': title,
'description': description,
'managedBy': managedBy,
'commitComment': commitComment,
})
@pytest.fixture
def mock_instids_to_names():
"""
An example set of Lookup institution instids
"""
return {
'FOO': 'Department of Foo',
'BAR': 'Faculty of Bar',
'MISC': 'School of Miscellaneous'
}
@pytest.fixture
def mock_lookup_institutions(mock_instids_to_names):
"""
An example dict of Lookup instids to ibisclient.IbisInstitution records matching those
in mock_instids fixture
"""
def _make_ibis_inst(instid: str, name: str):
inst = ibisclient.IbisInstitution({'instid': instid})
setattr(inst, 'name', name)
return inst
return {
instid: _make_ibis_inst(instid, name)
for instid, name in mock_instids_to_names.items()
}
def test_create_lookup_groups(mock_lookup_institutions, mock_instids_to_names):
"""
Groups in known institution get created. Those in unknown institutions don't
get created and the institution is included in the returned set.
"""
# New groups (2 for 1 known inst, 1 for another known inst, 1 for unknown inst)
NEW_GROUPS = {'foo-sis-ug', 'foo-sis-pg', 'bar-sis-ug', 'bad-sis-pg'}
EXPECTED_MISSING_INSTS = {'BAD'}
EXPECTED_CREATED_GROUPS_BY_INST = {
'FOO': {'foo-sis-ug', 'foo-sis-pg'},
'BAR': {'bar-sis-ug'},
}
mock_inst_methods = MockInstitutionMethods(mock_lookup_institutions)
# Make create calls (dry-run)
create_lookup_groups(mock_inst_methods, NEW_GROUPS, True)
# No createGroup calls made
assert mock_inst_methods.group_creations == {}
# Make create calls (not dry-run)
missing_insts = create_lookup_groups(mock_inst_methods, NEW_GROUPS, False)
# Set of missing institutions returned
assert EXPECTED_MISSING_INSTS == missing_insts
# Only groups with a known institution are created
assert (
set(EXPECTED_CREATED_GROUPS_BY_INST.keys()) ==
set(mock_inst_methods.group_creations.keys())
)
for instid, groups in mock_inst_methods.group_creations.items():
# Expected groups created for each institution
assert (
set(EXPECTED_CREATED_GROUPS_BY_INST[instid]) ==
{group['name'] for group in groups}
)
inst_name = mock_instids_to_names[instid]
for group in groups:
# Each group would be created with:
# ... a title containing institution name and (Under|Post)graduates
assert inst_name in group['title']
if group['name'][-2:] == 'ug':
assert 'Undergraduates' in group['title']
else:
assert 'Postgraduates' in group['title']
# ... a fixed description and commit message
assert group['description'] == GROUP_DESCRIPTION
assert group['commitComment'] == TRANSACTION_COMMENT
# ... and be managed by the connection's user
assert group['managedBy'] == 'TEST_USER'
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment