"""
$URL: svn+ssh://svn.mems-exchange.org/repos/trunk/dulcinea/lib/property/test/utest_property.py $
$Id: utest_property.py 29888 2007-06-15 19:52:55Z rmasse $
"""
from dulcinea.common import CommonTest
from dulcinea.property import property_type as PT, get_property_db
from dulcinea.property.errors import PropertyTypeError
from dulcinea.property.property import Property
from dulcinea.property.property_template import MasterTemplate
from dulcinea.physical_unit import get_standard_unit
from dulcinea.physical_value import PhysicalValue
from dulcinea.tolerance import Tolerance
from sancho.utest import raises

class PropertyTest (CommonTest):

    def _pre (self):
        CommonTest._pre(self)
        if get_property_db() is None:
            return
        self.int_type = PT.get_int_property_type()
        sec = get_standard_unit('s')
        self.pv_type = PT.get_physical_value_property_type('s')
        um =  get_standard_unit('um')
        self.pv_um_type = PT.get_physical_value_property_type('um')
        self.str_list_type = PT.get_list_of_string_property_type()
        self.str_int_table_type = PT.get_string_int_table_property_type()

    def check_basics (self):
        if get_property_db() is None:
            return
        tmpl = MasterTemplate("foo", self.int_type)
        Property(tmpl)
        raises(TypeError, Property, None)
        Property(tmpl, 1)
        raises(PropertyTypeError, Property, tmpl, 1.0)
        Property(tmpl, None)
        p = Property(tmpl)
        assert p.get_name() == tmpl.get_name()
        assert p.get_type() == tmpl.get_type()
        assert p.get_title() == tmpl.get_title()
        assert p.get_name() == tmpl.get_name()
        assert p.get_type() == tmpl.get_type()
        raises(AttributeError, getattr, p, 'not')

    def check_values (self):
        if get_property_db() is None:
            return
        int_property = Property(MasterTemplate('foo', self.int_type))
        assert int_property.get_type() == self.int_type
        assert int_property.format() == None
        int_property.set_value(1)
        assert str(int_property) == "foo = 1"
        assert int_property.get_value() == 1
        int_property.set_value(2)
        assert int_property.get_value() == 2
        assert int_property.format() == "2"
        raises(PropertyTypeError, int_property.set_value, 2.1)

        pv = PhysicalValue(1.0, "s")
        tmpl = MasterTemplate("bar", self.pv_type)
        pv_property = Property(tmpl, pv)
        assert pv_property.format() == "1 s"
        raises(PropertyTypeError, pv_property.set_value, 1)
        pv2 = PhysicalValue(2.0, "s")

        list_property = Property(MasterTemplate("baz", self.str_list_type))
        list_property.set_value(['a', 'b'])
        assert list_property.format() == "a, b"

        v = {"hello": 37}
        p1 = Property(MasterTemplate('foo', self.str_int_table_type), v)
        assert p1.format() == "hello: 37"
        assert str(p1) == "foo = hello: 37"

    def check_check (self):
        if get_property_db() is None:
            return
        # Most of the work of value-checking is done by
        # MasterTemplate, so most testing is done in
        # test_template.py.  All we need to do here is ensure that
        # Property.check_value() correctly falls back on property's
        # current value if no value is passed in.
        templ = MasterTemplate('foo', self.int_type,
                               constraint=[1, 2, 3])
        property = Property(templ)
        property.set_value(3)
        raises(PropertyTypeError, property.set_value, 'foo')

    def check_copy (self):
        if get_property_db() is None:
            return
        foo_templ = MasterTemplate("foo", self.int_type,
                                   constraint=[1, 5])
        foo1 = Property(foo_templ, value=3)
        foo2 = foo1.copy()
        assert foo1 == foo2
        assert foo1.template is foo2.template
        assert foo1.value is foo2.value

        bar_templ = MasterTemplate("bar", self.pv_um_type)
        bar1 = Property(bar_templ, value=PhysicalValue(3.5, "um"))

        assert foo1 != bar1
        bar2 = bar1.copy()
        assert bar1.template is bar2.template
        assert bar1.value is bar2.value
        assert bar1.value.unit is bar2.value.unit

        foo2.set_tolerance(Tolerance(9.0))
        assert foo2 != foo1

if __name__ == "__main__":
    PropertyTest()
