Revert "revert commits done before MZ holidays"

This reverts commit d2885bdd72.
This commit is contained in:
2023-08-18 16:27:55 +02:00
parent c4a39306e4
commit db9ce02028
23 changed files with 1236 additions and 234 deletions

View File

@ -75,6 +75,4 @@ def test_attach():
assert m.propertyValues['att'] == 'a'
srv.dispatcher.register_module(a, 'a')
srv.dispatcher.register_module(m, 'm')
assert m.att == 'a'
m.attachedModules = {'att': a}
assert m.att == a

141
test/test_config.py Normal file
View File

@ -0,0 +1,141 @@
# *****************************************************************************
#
# 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 2 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, write to the Free Software Foundation, Inc.,
# 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
# Module authors:
# Alexander Zaft <a.zaft@fz-juelich.de>
#
# *****************************************************************************
# false positive with fixtures
# pylint: disable=redefined-outer-name
import pytest
from frappy.config import Collector, Config, Mod, NodeCollector, load_config, \
process_file, to_config_path
from frappy.errors import ConfigError
from frappy.lib import generalConfig
class LoggerStub:
def debug(self, fmt, *args):
pass
info = warning = exception = error = debug
handlers = []
@pytest.fixture
def log():
return LoggerStub()
PY_FILE = """Node('foonode', 'fodesc', 'fooface')
Mod('foo', 'frappy.modules.Readable', 'description', value=5)
Mod('bar', 'frappy.modules.Readable', 'about me', export=False)
Mod('baz', 'frappy.modules.Readable', 'things', value=Param(3, unit='BAR'))
"""
# fixture file system, TODO: make a bit nicer?
@pytest.fixture
def direc(tmp_path_factory):
d = tmp_path_factory.mktemp('cfgdir')
a = d / 'a'
b = d / 'b'
a.mkdir()
b.mkdir()
f = a / 'config_cfg.py'
pyfile = a / 'pyfile_cfg.py'
ff = b / 'test_cfg.py'
fff = b / 'alsoworks.py'
f.touch()
ff.touch()
fff.touch()
pyfile.write_text(PY_FILE)
generalConfig.testinit(confdir=f'{a}:{b}', piddir=str(d))
return d
files = [('config', 'a/config_cfg.py'),
('config_cfg', 'a/config_cfg.py'),
('config_cfg.py', 'a/config_cfg.py'),
('test', 'b/test_cfg.py'),
('test_cfg', 'b/test_cfg.py'),
('test_cfg.py', 'b/test_cfg.py'),
('alsoworks', 'b/alsoworks.py'),
('alsoworks.py', 'b/alsoworks.py'),
]
@pytest.mark.parametrize('file, res', files)
def test_to_cfg_path(log, direc, file, res):
assert to_config_path(file, log).endswith(res)
def test_cfg_not_existing(direc, log):
with pytest.raises(ConfigError):
to_config_path('idonotexist', log)
def collector_helper(node, mods):
n = NodeCollector()
n.add(*node)
m = Collector(Mod)
m.list = [Mod(module, '', '') for module in mods]
return n, m
configs = [
(['n1', 'desc', 'iface'], ['foo', 'bar', 'baz'], ['n2', 'foo', 'bar'],
['foo', 'more', 'other'], ['n1', 'iface', 5, {'foo'}]),
(['n1', 'desc', 'iface'], ['foo', 'bar', 'baz'], ['n2', 'foo', 'bar'],
['different', 'more', 'other'], ['n1', 'iface', 6, set()]),
]
@pytest.mark.parametrize('n1, m1, n2, m2, res', configs)
def test_merge(n1, m1, n2, m2, res):
name, iface, num_mods, ambig = res
c1 = Config(*collector_helper(n1, m1))
c2 = Config(*collector_helper(n2, m2))
c1.merge_modules(c2)
assert c1['node']['equipment_id'] == name
assert c1['node']['interface'] == iface
assert len(c1.module_names) == num_mods
assert c1.ambiguous == ambig
def do_asserts(ret):
assert len(ret.module_names) == 3
assert set(ret.module_names) == set(['foo', 'bar', 'baz'])
assert ret['node']['equipment_id'] == 'foonode'
assert ret['node']['interface'] == 'fooface'
assert ret['foo'] == {'cls': 'frappy.modules.Readable',
'description': 'description', 'value': {'value': 5}}
assert ret['bar'] == {'cls': 'frappy.modules.Readable',
'description': 'about me', 'export': {'value': False}}
assert ret['baz'] == {'cls': 'frappy.modules.Readable',
'description': 'things',
'value': {'value': 3, 'unit': 'BAR'}}
def test_process_file(direc, log):
ret = process_file(str(direc / 'a' / 'pyfile_cfg.py'), log)
do_asserts(ret)
def test_full(direc, log):
ret = load_config('pyfile_cfg.py', log)
do_asserts(ret)

132
test/test_ctrlpars.py Normal file
View File

@ -0,0 +1,132 @@
# *****************************************************************************
#
# 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 2 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, write to the Free Software Foundation, Inc.,
# 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
# Module authors:
# Markus Zolliker <markus.zolliker@psi.ch>
#
# *****************************************************************************
"""test frappy.mixins.HasCtrlPars"""
from test.test_modules import LoggerStub, ServerStub
from frappy.core import FloatRange, Module, Parameter
from frappy.structparam import StructParam
def test_with_read_ctrlpars():
class Mod(Module):
ctrlpars = StructParam('ctrlpar struct', dict(
p = Parameter('control parameter p', FloatRange()),
i = Parameter('control parameter i', FloatRange()),
d = Parameter('control parameter d', FloatRange()),
), 'pid_', readonly=False)
def read_ctrlpars(self):
return self._ctrlpars
def write_ctrlpars(self, value):
self._ctrlpars = value
return self.read_ctrlpars()
logger = LoggerStub()
updates = {}
srv = ServerStub(updates)
ms = Mod('ms', logger, {'description':''}, srv)
value = {'p': 1, 'i': 2, 'd': 3}
assert ms.write_ctrlpars(value) == value
assert ms.read_ctrlpars() == value
assert ms.read_pid_p() == 1
assert ms.read_pid_i() == 2
assert ms.read_pid_d() == 3
assert ms.write_pid_i(5) == 5
assert ms.write_pid_d(0) == 0
assert ms.read_ctrlpars() == {'p': 1, 'i': 5, 'd': 0}
assert set(Mod.ctrlpars.influences) == {'pid_p', 'pid_i', 'pid_d'}
assert Mod.pid_p.influences == ('ctrlpars',)
assert Mod.pid_i.influences == ('ctrlpars',)
assert Mod.pid_d.influences == ('ctrlpars',)
def test_without_read_ctrlpars():
class Mod(Module):
ctrlpars = StructParam('ctrlpar struct', dict(
p = Parameter('control parameter p', FloatRange()),
i = Parameter('control parameter i', FloatRange()),
d = Parameter('control parameter d', FloatRange()),
), readonly=False)
_pid_p = 0
_pid_i = 0
def read_p(self):
return self._pid_p
def write_p(self, value):
self._pid_p = value
return self.read_p()
def read_i(self):
return self._pid_i
def write_i(self, value):
self._pid_i = value
return self.read_i()
logger = LoggerStub()
updates = {}
srv = ServerStub(updates)
ms = Mod('ms', logger, {'description': ''}, srv)
value = {'p': 1, 'i': 2, 'd': 3}
assert ms.write_ctrlpars(value) == value
assert ms.read_ctrlpars() == value
assert ms.read_p() == 1
assert ms.read_i() == 2
assert ms.read_d() == 3
assert ms.write_i(5) == 5
assert ms.write_d(0) == 0
assert ms.read_ctrlpars() == {'p': 1, 'i': 5, 'd': 0}
assert set(Mod.ctrlpars.influences) == {'p', 'i', 'd'}
assert Mod.p.influences == ('ctrlpars',)
assert Mod.i.influences == ('ctrlpars',)
assert Mod.d.influences == ('ctrlpars',)
def test_readonly():
class Mod(Module):
ctrlpars = StructParam('ctrlpar struct', dict(
p = Parameter('control parameter p', FloatRange()),
i = Parameter('control parameter i', FloatRange()),
d = Parameter('control parameter d', FloatRange()),
), {'p': 'pp', 'i':'ii', 'd': 'dd'}, readonly=True)
assert Mod.ctrlpars.readonly is True
assert Mod.pp.readonly is True
assert Mod.ii.readonly is True
assert Mod.dd.readonly is True
def test_order_dependence1():
test_without_read_ctrlpars()
test_with_read_ctrlpars()
def test_order_dependence2():
test_with_read_ctrlpars()
test_without_read_ctrlpars()

View File

@ -22,7 +22,7 @@
import pytest
from frappy.lib import parse_host_port
from frappy.lib import parse_host_port, merge_status
@pytest.mark.parametrize('hostport, defaultport, result', [
@ -46,3 +46,19 @@ def test_parse_host(hostport, defaultport, result):
parse_host_port(hostport, defaultport)
else:
assert result == parse_host_port(hostport, defaultport)
@pytest.mark.parametrize('args, result', [
([(100, 'idle'), (200, 'warning')],
(200, 'warning')),
([(300, 'ramping'), (300, 'within tolerance')],
(300, 'ramping, within tolerance')),
([(300, 'ramping, within tolerance'), (300, 'within tolerance, slow'), (200, 'warning')],
(300, 'ramping, within tolerance, slow')),
# when a comma is used for other purposes than separating individual status texts,
# the behaviour might not be as desired. However, this case is somewhat constructed.
([(100, 'blue, yellow is my favorite'), (100, 'white, blue, red is a bad color mix')],
(100, 'blue, yellow is my favorite, white, red is a bad color mix')),
])
def test_merge_status(args, result):
assert merge_status(*args) == result

55
test/test_server.py Normal file
View File

@ -0,0 +1,55 @@
# *****************************************************************************
#
# 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 2 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, write to the Free Software Foundation, Inc.,
# 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
# Module authors:
# Alexander Zaft <a.zaft@fz-juelich.de>
#
# *****************************************************************************
import pytest
# pylint: disable=redefined-outer-name
from frappy.server import Server
from .test_config import direc # pylint: disable=unused-import
class LoggerStub:
def debug(self, fmt, *args):
pass
def getChild(self, *args):
return self
info = warning = exception = error = debug
handlers = []
@pytest.fixture
def log():
return LoggerStub()
def test_name_only(direc, log):
"""only see that this does not throw. get config from name."""
s = Server('pyfile', log)
s._processCfg()
def test_file(direc, log):
"""only see that this does not throw. get config from cfgfiles."""
s = Server('foo', log, cfgfiles='pyfile_cfg.py')
s._processCfg()