# -*- coding: utf-8 -*-
# Authors:
# Jason Gerard DeRose <jderose@redhat.com>
#
# Copyright (C) 2008 Red Hat
# see file 'COPYING' for use and warranty information
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
Test the `ipalib.parameters` module.
"""
# FIXME: Pylint errors
# pylint: disable=no-member
import datetime
import re
import sys
from decimal import Decimal
from inspect import isclass
from six.moves.xmlrpc_client import MAXINT, MININT
import pytest
import six
from ipatests.util import raises, ClassChecker, read_only
from ipatests.util import dummy_ugettext, assert_equal
from ipatests.data import binary_bytes, utf8_bytes, unicode_str
from ipalib import parameters, text, errors, config
from ipalib.constants import TYPE_ERROR, CALLABLE_ERROR
from ipalib.errors import ValidationError, ConversionError
from ipalib import _
if six.PY3:
unicode = str
long = int
NULLS = (None, b'', u'', tuple(), [])
pytestmark = pytest.mark.tier0
class test_DefaultFrom(ClassChecker):
"""
Test the `ipalib.parameters.DefaultFrom` class.
"""
_cls = parameters.DefaultFrom
def test_init(self):
"""
Test the `ipalib.parameters.DefaultFrom.__init__` method.
"""
def callback(*args):
return args
keys = ('givenname', 'sn')
o = self.cls(callback, *keys)
assert read_only(o, 'callback') is callback
assert read_only(o, 'keys') == keys
lam = lambda first, last: first[0] + last
o = self.cls(lam)
assert read_only(o, 'keys') == ('first', 'last')
# Test that TypeError is raised when callback isn't callable:
e = raises(TypeError, self.cls, 'whatever')
assert str(e) == CALLABLE_ERROR % ('callback', 'whatever', str)
# Test that TypeError is raised when a key isn't an str:
e = raises(TypeError, self.cls, callback, 'givenname', 17)
assert str(e) == TYPE_ERROR % ('keys', str, 17, int)
# Test that ValueError is raised when inferring keys from a callback
# which has *args:
e = raises(ValueError, self.cls, lambda foo, *args: None)
assert str(e) == "callback: variable-length argument list not allowed"
# Test that ValueError is raised when inferring keys from a callback
# which has **kwargs:
|