"""
$URL: svn+ssh://svn.mems-exchange.org/repos/trunk/dulcinea/lib/param/test/utest_list.py $
$Id: utest_list.py 28105 2006-03-30 16:24:19Z dbinger $
"""
from dulcinea.common import CommonTest
from dulcinea.param import get_parameter_db
from dulcinea.param.parameter import Parameter
from dulcinea.param.parameter_list import ParameterList
from dulcinea.param.parameter_template import MasterTemplate
from dulcinea.param.parameter_type import ParameterType
from dulcinea.physical_value import PhysicalValue
from sancho.utest import raises

class TestParameterList (CommonTest):

    def _pre(self):
        CommonTest._pre(self)
        if get_parameter_db() is None:
            return
        get_type = get_parameter_db().get_parameter_type
        self.int_type = get_type(ParameterType.ATOMIC,
                                 ParameterType.INT)
        sec = PhysicalValue(1, 's').unit
        self.pv_type = get_type(ParameterType.ATOMIC,
                                ParameterType.PHYSICAL_VALUE,
                                element_sub_type=sec)
        um =  PhysicalValue(1, 'um').unit
        self.pv_um_type = get_type(ParameterType.ATOMIC,
                                ParameterType.PHYSICAL_VALUE,
                                element_sub_type=um)
        self.str_list_type = get_type(ParameterType.LIST,
                                      ParameterType.STRING)
        self.str_int_table_type = get_type(ParameterType.TABLE,
                                           ParameterType.INT,
                                           key_type=ParameterType.STRING)


    def check_basic (self):
        if get_parameter_db() is None:
            return
        template = MasterTemplate('downtime', self.pv_type)
        parameter = template.create_value()
        ParameterList([parameter])
        ParameterList([template])
        ParameterList([parameter])
        ParameterList([template])
        raises(TypeError, ParameterList, [parameter, object()])

    def check_filtering (self):
        if get_parameter_db() is None:
            return
        template = MasterTemplate("a", self.int_type)
        parameter = Parameter(MasterTemplate("b", self.pv_type))
        plist = ParameterList([template, parameter])
        assert list(plist.get_value_parameters()) == [parameter]
        assert list(plist.get_templates()) == [template]
        assert list(plist.get_visible()) == [template, parameter]
        assert list(plist + []) == list(plist)

if __name__ == "__main__":
    TestParameterList()
