Beispiel #1
0
def test_vec3():
    assert vec3([1, 0, 0]) == [1., 0., 0.]
    assert vec3() == [0., 0., 0.]
    assert raises(ValueError, vec3, [1, 0])
    assert raises(ValueError, vec3, ['x', 'y', 'z'])
    # assert that the list is read-only
    assert raises(TypeError, vec3([0, 1, 2]).__setitem__, 0, 1)
Beispiel #2
0
 def test_limit_violations(self):
     """
     Test the limit violations after moving a motor beyond limits
     """
     (usrmin, usrmax) = self.device.userlimits
     assert raises(LimitError, self.device.maw, usrmin - 0.1)
     assert raises(LimitError, self.device.maw, usrmax + 0.1)
Beispiel #3
0
def test_host():
    assert host()('localhost') == 'localhost'
    assert host()('localhost:14869') == 'localhost:14869'
    assert host()('') == ''
    assert raises(ValueError, host(), None)
    assert raises(ValueError, host(), 123)
    assert raises(ValueError, host(), 'localhost:')
    assert raises(ValueError, host(), 'localhost:14869:')
    assert raises(ValueError, host(), 'localhost:0')
    assert raises(ValueError, host(), 'localhost:65536')
    assert raises(ValueError, host(), 'localhost:port')
    assert host(defaulthost='localhost')('') == ''
    assert host(defaulthost='localhost')(None) == 'localhost'
    assert host(defaulthost='localhost')('otherhost') == 'otherhost'

    assert host(defaultport=123)('') == ':123'
    assert host(defaultport=123)('otherhost') == 'otherhost:123'
    assert host(defaultport='456')('otherhost') == 'otherhost:456'
    assert host(defaultport=123)('otherhost:789') == 'otherhost:789'
    assert host()(('name', 1234)) == 'name:1234'

    assert host(defaulthost='localhost', defaultport=123)('') == ':123'
    assert host(defaulthost='localhost',
                defaultport='456')('otherhost') == 'otherhost:456'
    assert host(defaulthost='localhost',
                defaultport='456')('otherhost:789') == 'otherhost:789'

    assert raises(ValueError, host().__call__, ':123')

    assert raises(ValueError, host, **{'defaultport': 'abc'})
    assert raises(ValueError, host, **{'defaultport': '9999999'})
Beispiel #4
0
def test_params(session):
    dev2 = session.getDevice('dev2_1')
    # make sure adev instances are created
    assert isinstance(dev2._attached_attached, Dev1)
    # an inherited and writable parameter
    assert dev2.unit == 'mm'
    dev2.unit = 'deg'
    assert dev2.unit == 'deg'
    # a readonly parameter
    assert dev2.param1 == 42
    assert raises(ConfigurationError, setattr, dev2, 'param1', 21)
    # a parameter with custom getter and setter
    assert dev2.param2 == 21
    dev2.param2 = 5
    assert dev2.param2 == 6
    assert 'doWriteParam2' in methods_called
    assert 'doUpdateParam2' in methods_called
    # nonexisting parameters
    assert raises(NicosError, setattr, dev2, 'param3', 1)
    # test parameter value when in cache, but default value updated
    session.cache.put(dev2, 'param1', 50)
    session.createDevice('dev2_1', recreate=True)
    # restored the default value from the code?
    assert dev2.param1 == 42
    # test default values for userparam in respect to internal
    dev4 = session.getDevice('dev4')
    assert dev4.parameters['intparam'].userparam is False
    assert dev4.parameters['param'].userparam is True
    # test explicit userparam settings in respect to internal
    assert dev4.parameters['intexplicit'].userparam is True
    assert dev4.parameters['explicit'].userparam is False
    # ambiguous parameter settings of internal and mandatory should raise
    assert raises(ProgrammingError, Param, "ambiguous", internal=True,
                  mandatory=True)
Beispiel #5
0
def test_spm(session):
    # note the use of createDevice here; devices used via SPM must be in the
    # "explicit devices" list, but the TestSession does not autocreate devices
    axis = session.createDevice('axis', explicit=True)
    axis.maw(0)

    # normal command execution
    spmexec(session, 'maw axis 1')
    assert axis() == 1
    spmexec(session, 'scan axis 1 1 2')
    assert axis() == 2

    # "direct" invocation of devices
    spmexec(session, 'axis 2')
    assert axis() == 2

    # more complicated commands
    session.createDevice('slow_motor', explicit=True)
    # automatic stringification of some parameters
    spmexec(session, 'get slow_motor userlimits')
    # args in parentheses/brackets are preserved
    spmexec(session, 'set slow_motor userlimits (0, 2)')

    # invalid or missing syntax
    assert raises(SPMError, spmexec, session, 'get axis')
    assert raises(SPMError, spmexec, session, 'read @axis')
    # only one form allowed
    assert raises(SPMError, spmexec, session, 'scan axis [0, 1, 2]')
Beispiel #6
0
def test_dictof():
    assert dictof(int, str)({1: 0, 2: 1}) == {1: '0', 2: '1'}
    assert dictof(int, str)() == {}
    assert raises(ValueError, dictof(int, str), ('a', 'b'))
    assert raises(ValueError, dictof(int, str), {'x': 'y'})
    # test that the dict is read-only
    assert raises(TypeError, dictof(int, str)({1: 'x'}).pop, 1)
Beispiel #7
0
def test_listof():
    assert listof(int)([0., 1, '2']) == [0, 1, 2]
    assert listof(int)() == []
    # should also accept tuples
    assert listof(int)((1, 2, 3)) == [1, 2, 3]
    assert raises(ValueError, listof(int), 10)
    # assert that the list is read-only
    assert raises(TypeError, listof(int)([0, 1, 2]).__setitem__, 0, 1)
Beispiel #8
0
def test_tacodev():
    assert tacodev('test/custom/device') == 'test/custom/device'
    assert tacodev('test/custom/device1') == 'test/custom/device1'
    assert tacodev('test1/custom1/device1') == 'test1/custom1/device1'
    assert tacodev('1/2/3') == '1/2/3'
    assert tacodev() == ''
    assert raises(ValueError, tacodev, '/taco23/test/custom/device')
    assert raises(ValueError, tacodev, 'test/device')
Beispiel #9
0
def test_oneofdict_or():
    m = dict(a=1, b=2)
    v = oneofdict_or(m, floatrange(0, 10))
    assert v('a') == 1.0
    assert v('b') == 2.0
    assert v(5) == 5.0
    assert raises(ValueError, v, 'c')
    assert raises(ValueError, v, 11)
Beispiel #10
0
def test_nonemptylistof():
    assert nonemptylistof(int)(['1']) == [1]
    assert nonemptylistof(int)() == [0]
    # should also accept tuples
    assert nonemptylistof(int)((1, 2)) == [1, 2]
    assert raises(ValueError, nonemptylistof(int), [])
    assert raises(ValueError, nonemptylistof(int), 10)
    # assert that the list is read-only
    assert raises(TypeError, nonemptylistof(int)([0, 1, 2]).__setitem__, 0, 1)
Beispiel #11
0
def test_keystore_auth(session, KeystoreAuth):
    assert KeystoreAuth.userdomain == 'test_nicos_user'

    assert KeystoreAuth.authenticate('admin', 'admin') == User('admin', ADMIN)
    assert KeystoreAuth.authenticate('joedoe', 'userpass') == User('joedoe', USER)
    assert raises(AuthenticationError, KeystoreAuth.authenticate, 'user', 'user_')
    assert raises(AuthenticationError, KeystoreAuth.authenticate, 'admin', 'user_')
    assert User('admin', ADMIN).data == {}
    assert User('joedoe', USER).data == {}
Beispiel #12
0
def test_oneofdict():
    assert oneofdict({'A': 1, 'B': 2})('A') == 1
    assert oneofdict({'A': 1, 'B': 2})(1) == 1
    assert oneofdict({})() is None
    assert raises(ValueError, oneofdict({'A': 1}), 2)

    assert none_or(int)(None) is None
    assert none_or(int)(5.0) == 5
    assert raises(ValueError, none_or(int), 'x')
Beispiel #13
0
def test_path():
    assert absolute_path('/tmp') == '/tmp'
    assert relative_path('tmp') == 'tmp'
    assert subdir('tmp') == 'tmp'
    assert raises(ValueError, absolute_path, 'tmp')
    assert raises(ValueError, absolute_path, '../')
    assert raises(ValueError, relative_path, '/tmp')
    assert raises(ValueError, relative_path, '../')
    assert raises(ValueError, subdir, 'tmp/')
Beispiel #14
0
def test_floatrange():
    assert floatrange(0, 10)(5) == 5.0
    assert floatrange(1, 3)() == 1.0
    assert raises(ValueError, floatrange(0, 10), 15.)
    assert raises(ValueError, floatrange(0, 10), 'x')
    assert raises(ValueError, floatrange, 2, 1)

    assert floatrange(0)(5) == 5.0
    assert raises(ValueError, floatrange(0), -5)
Beispiel #15
0
def test_params(session):
    pulse1 = session.getDevice('pulse1')
    # check well defined device
    assert pulse1.onvalue == 'up'
    assert pulse1.offvalue == 'down'
    assert pulse1.ontime == 0.01

    # check the test for 'up' and 'down' values
    assert raises(ConfigurationError, session.getDevice, 'pulse2')
    assert raises(ConfigurationError, session.getDevice, 'pulse3')
Beispiel #16
0
def test_oneof():
    assert oneof(0, 1)(1) == 1
    assert oneof(2, 3)() == 2
    assert oneof(None)() is None
    assert oneof(None)(None) is None
    assert oneof()() is None
    assert oneof()(None) is None
    assert raises(ValueError, oneof(0, 1), '0')
    assert raises(ValueError, oneof(0, 1), 2)
    assert raises(ValueError, oneof(0, 1), 'x')
Beispiel #17
0
def test_nok(session):
    nok2 = session.getDevice('nok2')
    assert nok2.read(0) == [0, 0]

    # nok2.reference()
    nok2.maw((1, 1))
    assert nok2.read(0) == [1, 1]

    assert raises(LimitError, nok2.maw, (0, 20))
    assert raises(LimitError, nok2.maw, (-30, -20))
Beispiel #18
0
def test_multi_switcher_fails(session, log):
    assert raises(ConfigurationError, session.getDevice, 'msw3')
    assert raises(ConfigurationError, session.getDevice, 'msw4')

    msw5 = session.getDevice('msw5')
    msw5.move('1')
    # msw5 has a precision of None for motor 'y', but that motor has
    # a jitter set so that it will never be exactly at 0
    with log.allow_errors():
        assert raises(PositionError, msw5.wait)
Beispiel #19
0
def test_mailaddress():
    assert mailaddress() == ''
    assert mailaddress('*****@*****.**') == '*****@*****.**'
    assert mailaddress('*****@*****.**') == '*****@*****.**'
    assert mailaddress('*****@*****.**') == '*****@*****.**'
    assert mailaddress(
        '*****@*****.**') == '*****@*****.**'
    assert mailaddress(
        '*****@*****.**') == '*****@*****.**'
    assert mailaddress(
        '[email protected]ömäin.my') == '*****@*****.**'
    assert mailaddress('myaddress@وزارة-الأتصالات.مصر') == \
        '[email protected]'
    assert mailaddress('M. Address <*****@*****.**>'
                       ) == 'M. Address <*****@*****.**>'
    assert mailaddress('M. Address <*****@*****.**> '
                       ) == 'M. Address <*****@*****.**> '
    assert mailaddress('W. Lohstroh, G. Simeoni '
                       '<*****@*****.**>') ==  \
                       'W. Lohstroh, G. Simeoni <*****@*****.**>'
    assert raises(ValueError, mailaddress, 'M. Address [email protected]>')
    assert raises(ValueError, mailaddress, 'M. Address <*****@*****.**')
    assert raises(ValueError, mailaddress, 'my.name.domain.my')
    assert raises(ValueError, mailaddress, '@my.domain')
    assert raises(ValueError, mailaddress, 'my@domain')
    assert raises(ValueError, mailaddress, '[email protected]')
    assert raises(ValueError, mailaddress, 'my@[email protected]')
    assert raises(ValueError, mailaddress, 'my@nonsens@dömain.my')
    assert raises(ValueError, mailaddress,
                  'M. Address <*****@*****.**>,')
Beispiel #20
0
def test_raisers(session):
    assert raises(ConfigurationError, getattr, session.experiment, 'envlist')
    assert raises(ConfigurationError, setattr, session.experiment, 'envlist',
                  [])
    assert raises(ConfigurationError, getattr, session.instrument,
                  'instrument')

    assert bool(session.experiment) is False

    assert session._experiment is None
    assert session._instrument is None
Beispiel #21
0
    def test_move(self, session, log):
        """Check move() command."""
        motor = session.getDevice('motor')
        for pos in (min(motor.userlimits), 0, max(motor.userlimits)):
            move(motor, pos)
            motor.wait()
            assert motor.curvalue == pos

        assert raises(LimitError, move, motor, max(motor.userlimits) + 1)

        assert raises(UsageError, move)
        assert raises(UsageError, move, motor, 1, motor)