def test_role_deserializer_parenting_existing_parent(db):
    parent_role_dict = {
        'name': 'grand parent role',
        'slug': 'grand-parent-role',
        'uuid': get_hex_uuid(),
        'ou': None,
        'service': None
    }
    parent_role = Role.objects.create(**parent_role_dict)
    child_role_dict = {
        'name': 'child role',
        'slug': 'child-role',
        'parents': [parent_role_dict],
        'uuid': get_hex_uuid(),
        'ou': None,
        'service': None
    }

    rd = RoleDeserializer(child_role_dict, ImportContext())
    child_role, status = rd.deserialize()
    created, deleted = rd.parentings()

    assert len(created) == 1
    parenting = created[0]
    assert parenting.direct is True
    assert parenting.parent == parent_role
    assert parenting.child == child_role
def test_role_deserializer_parenting_non_existing_parent(db):
    parent_role_dict = {
        'name': 'grand parent role',
        'slug': 'grand-parent-role',
        'uuid': get_hex_uuid(),
        'ou': None,
        'service': None
    }
    child_role_dict = {
        'name': 'child role',
        'slug': 'child-role',
        'parents': [parent_role_dict],
        'uuid': get_hex_uuid(),
        'ou': None,
        'service': None
    }
    rd = RoleDeserializer(child_role_dict, ImportContext())
    rd.deserialize()
    with pytest.raises(DataImportError) as excinfo:
        rd.parentings()

    assert "Could not find role" in str(excinfo.value)