class TestCounterDemoSystem(unittest.TestCase): def setUp(self): self.process = Process("proc") parts = [call_with_params(CounterPart, name="cpart")] self.controller = Controller(self.process, "counting", parts) self.process.start() def tearDown(self): self.process.stop(timeout=1) def test_counter_subscribe(self): q = Queue() sub = Subscribe(id=20, path=["counting", "counter"], delta=False, callback=q.put) self.controller.handle_request(sub) response = q.get(timeout=1.0) self.assertIsInstance(response, Update) assert response.id == 20 assert response.value["typeid"] == "epics:nt/NTScalar:1.0" assert response.value["value"] == 0 post = Post(id=21, path=["counting", "increment"], callback=q.put) self.controller.handle_request(post) response = q.get(timeout=1) self.assertIsInstance(response, Update) assert response.id == 20 assert response.value["value"] == 1 response = q.get(timeout=1) self.assertIsInstance(response, Return) assert response.id == 21 assert response.value == None with self.assertRaises(TimeoutError): q.get(timeout=0.05)
def setUp(self): self.process = Process("proc") self.part = MyPart("test_part") self.o = Controller("mri") self.o.add_part(self.part) self.process.add_controller(self.o) self.process.start()
class TestChoicePart(unittest.TestCase): def setUp(self): self.o = ChoicePart(name="cp", description="desc", choices=["a", "b"], value="a", writeable=True) self.c = Controller("mri") self.c.add_part(self.o) self.c.setup(Process("proc")) def test_init(self): assert self.o.name == "cp" assert self.o.attr.value == "a" assert self.o.attr.meta.description == "desc" assert self.o.attr.meta.choices == ["a", "b"] assert self.o.attr.meta.tags == ["widget:combo", "config:1"] assert self.c.field_registry.fields[self.o] == [ ("cp", self.o.attr, self.o.attr.set_value, False) ] def test_setter(self): assert self.o.attr.value == "a" self.o.attr.set_value("b") assert self.o.attr.value == "b" with self.assertRaises(ValueError): self.o.attr.set_value("c")
def setUp(self): svg_name = "/tmp/test_icon.svg" self.svg_text = '<svg><rect width="300" height="100"/></svg>' with open(svg_name, "w") as f: f.write(self.svg_text) self.o = IconPart(svg=svg_name) self.c = Controller("mri") self.c.add_part(self.o)
def setUp(self): self.o = Float64Part(name="fp", description="desc", value=2.3, writeable=True) self.c = Controller("mri") self.c.add_part(self.o) self.c.setup(Process("proc"))
def setUp(self): self.process = Process("proc") self.part = MyPart("test_part") self.controller = Controller("mri") self.controller.add_part(self.part) self.process.add_controller(self.controller) self.block = self.controller.block_view() self.process.start()
def setUp(self): self.o = ChoicePart(name="cp", description="desc", choices=["a", "b"], value="a", writeable=True) self.c = Controller("mri") self.c.add_part(self.o) self.c.setup(Process("proc"))
def setUp(self): self.o = call_with_params(LabelPart, initialValue="My label", group="mygroup") self.p = Process("proc") self.c = Controller(self.p, "mri", [self.o]) self.p.add_controller("mri", self.c) self.p.start() self.b = self.c.block_view()
class TestHelpPart(unittest.TestCase): def setUp(self): self.o = HelpPart(help_url="/BLOCK.html") self.c = Controller("mri") self.c.add_part(self.o) def test_init(self): assert self.o.name == "help" assert self.o.attr.value == "/BLOCK.html" assert self.o.attr.meta.tags == ["widget:help"] assert self.c.field_registry.fields[self.o] == [("help", self.o.attr, None)]
class TestStringPart(unittest.TestCase): def setUp(self): self.o = StringPart(name="sp", description="desc") self.c = Controller("mri") self.c.add_part(self.o) self.c.setup(Process("proc")) def test_init(self): assert self.o.name == "sp" assert self.o.attr.value == "" assert self.o.attr.meta.description == "desc" assert self.o.attr.meta.tags == ["widget:textupdate"] assert self.c.field_registry.fields[self.o] == [ ("sp", self.o.attr, None, False) ]
class TestLabelPart(unittest.TestCase): def setUp(self): self.o = call_with_params(LabelPart, initialValue="My label", group="mygroup") self.p = Process("proc") self.c = Controller(self.p, "mri", [self.o]) self.p.add_controller("mri", self.c) self.p.start() self.b = self.c.block_view() def tearDown(self): self.p.stop(1) def test_init(self): assert self.o.name == "label" assert self.o.attr.value == "My label" assert self.o.attr.meta.tags == ("widget:textinput", "config", "group:mygroup") assert self.b.meta.label == "My label" def test_setter(self): self.b.label.put_value("My label2") assert self.b.label.value == "My label2" assert self.b.meta.label == "My label2"
class TestIconPart(unittest.TestCase): def setUp(self): svg_name = "/tmp/test_icon.svg" self.svg_text = '<svg><rect width="300" height="100"/></svg>' with open(svg_name, "w") as f: f.write(self.svg_text) self.o = IconPart(svg=svg_name) self.c = Controller("mri") self.c.add_part(self.o) def test_init(self): assert self.o.name == "icon" assert self.o.attr.value == self.svg_text assert self.o.attr.meta.description == "SVG icon for the Block" assert self.o.attr.meta.tags == ["widget:icon"] assert self.c.field_registry.fields[self.o] == [("icon", self.o.attr, None)]
class TestFloat64Part(unittest.TestCase): def setUp(self): self.o = Float64Part(name="fp", description="desc", value=2.3, writeable=True) self.c = Controller("mri") self.c.add_part(self.o) def test_init(self): assert self.o.name == "fp" assert self.o.attr.value == 2.3 assert self.o.attr.meta.description == "desc" assert self.o.attr.meta.dtype == "float64" assert self.o.attr.meta.tags == ["widget:textinput", "config:1"] assert self.c.field_registry.fields[self.o] == [( "fp", self.o.attr, self.o.attr.set_value )]
class TestCounterDemoSystem(unittest.TestCase): def setUp(self): self.process = Process("proc") self.controller = Controller("counting") self.controller.add_part(CounterPart("cpart")) self.process.add_controller(self.controller) self.process.start() def tearDown(self): self.process.stop(timeout=1) def test_counter_subscribe(self): q = Queue() # Subscribe to the value sub = Subscribe(id=20, path=["counting", "counter"], delta=False) sub.set_callback(q.put) self.controller.handle_request(sub) # Check initial return response = q.get(timeout=1.0) self.assertIsInstance(response, Update) assert response.id == 20 assert response.value["typeid"] == "epics:nt/NTScalar:1.0" assert response.value["value"] == 0 # Post increment() post = Post(id=21, path=["counting", "increment"]) post.set_callback(q.put) self.controller.handle_request(post) # Check the value updates... response = q.get(timeout=1) self.assertIsInstance(response, Update) assert response.id == 20 assert response.value["value"] == 1 # ... then we get the return response = q.get(timeout=1) self.assertIsInstance(response, Return) assert response.id == 21 assert response.value is None # Check we can put too put = Put(id=22, path=["counting", "counter", "value"], value=31) put.set_callback(q.put) self.controller.handle_request(put) # Check the value updates... response = q.get(timeout=1) self.assertIsInstance(response, Update) assert response.id == 20 assert response.value["value"] == 31 # ... then we get the return response = q.get(timeout=1) self.assertIsInstance(response, Return) assert response.id == 22 assert response.value is None # And that there isn't anything else with self.assertRaises(TimeoutError): q.get(timeout=0.05)
class TestBlockPart(unittest.TestCase): def setUp(self): self.o = BlockPart(name="panda", description="desc") self.c = Controller("mri") self.c.add_part(self.o) self.c.setup(Process("proc")) def test_init(self): assert self.o.name == "panda" assert self.o.attr.value == "" assert self.o.attr.meta.description == "desc" assert self.o.attr.meta.tags == [ "widget:textinput", "config:1", "sinkPort:block:", ] assert self.c.field_registry.fields[self.o] == [ ("panda", self.o.attr, self.o.attr.set_value, False) ]
class TestHelloDemoSystem(unittest.TestCase): def setUp(self): self.process = Process("proc") parts = [call_with_params(HelloPart, name="hpart")] self.controller = Controller(self.process, "hello_block", parts) self.process.start() def tearDown(self): self.process.stop(timeout=1) def test_hello_good_input(self): q = Queue() request = Post(id=44, path=["hello_block", "greet"], parameters=dict(name="thing"), callback=q.put) self.controller.handle_request(request) response = q.get(timeout=1.0) self.assertIsInstance(response, Return) assert response.id == 44 assert response.value["greeting"] == "Hello thing"
class TestPart(unittest.TestCase): def setUp(self): self.c = Controller("c") def test_init(self): p = Part("name") assert p.name == "name" self.c.add_part(p) assert p.registrar def test_bad_name(self): with self.assertRaises(AssertionError): Part("dotted.name") def test_good_name(self): Part("Part-With-dashes_43") def test_bad_field_name(self): p = BadPart("name") with self.assertRaises(AssertionError): self.c.add_part(p)
class TestMethod(unittest.TestCase): def setUp(self): self.process = Process("proc") self.part = MyPart("test_part") self.controller = Controller("mri") self.controller.add_part(self.part) self.process.add_controller(self.controller) self.block = self.controller.block_view() self.process.start() def tearDown(self): self.process.stop(timeout=1) def test_post(self): method_view = self.block.myMethod result = method_view.post(param1="testPost", param2="y") assert result == "testPosty" def test_post_async(self): method_view = self.block.myMethod f = method_view.post_async("testAsync", "y") assert f.result() == "testAsyncy"
class TestGroupPart(unittest.TestCase): def setUp(self): self.o = GroupPart(name="things", description="A group of things") self.c = Controller("mri") self.c.add_part(self.o) def test_init(self): assert self.o.name == "things" assert self.o.attr.value == "expanded" assert self.o.attr.meta.description == "A group of things" assert self.o.attr.meta.tags == ["widget:group", "config:1"] assert self.c.field_registry.fields[self.o] == [( "things", self.o.attr, self.o.attr.set_value )] def test_setter(self): assert self.o.attr.value == "expanded" self.o.attr.set_value("collapsed") assert self.o.attr.value == "collapsed" with self.assertRaises(ValueError): self.o.attr.set_value("anything else")
class TestHelloDemoSystem(unittest.TestCase): def setUp(self): self.process = Process("proc") self.controller = Controller("hello_block") self.controller.add_part(HelloPart("hpart")) self.process.add_controller(self.controller) self.process.start() def tearDown(self): self.process.stop(timeout=1) def test_hello_good_input(self): q = Queue() request = Post(id=44, path=["hello_block", "greet"], parameters=dict(name="thing")) request.set_callback(q.put) self.controller.handle_request(request) response = q.get(timeout=1.0) self.assertIsInstance(response, Return) assert response.id == 44 assert response.value == "Hello thing" def test_concurrent(self): q = Queue() request = Post(id=44, path=["hello_block", "greet"], parameters=dict(name="me", sleep=1)) request.set_callback(q.put) self.controller.handle_request(request) request = Post(id=45, path=["hello_block", "error"]) request.set_callback(q.put) self.controller.handle_request(request) response = q.get(timeout=1.0) self.assertIsInstance(response, Error) assert response.id == 45 response = q.get(timeout=3.0) self.assertIsInstance(response, Return) assert response.id == 44 assert response.value == "Hello me"
def setUp(self): self.process = Process("proc") self.controller = Controller("hello_block") self.controller.add_part(HelloPart("hpart")) self.process.add_controller(self.controller) self.process.start()
def setUp(self): self.process = Process("proc") self.controller = Controller("counting") self.controller.add_part(CounterPart("cpart")) self.process.add_controller(self.controller) self.process.start()
def setUp(self): self.o = StringPart(name="sp", description="desc") self.c = Controller("mri") self.c.add_part(self.o) self.c.setup(Process("proc"))
def setUp(self): self.o = HelpPart(help_url="/BLOCK.html") self.c = Controller("mri") self.c.add_part(self.o) self.c.setup(Process("proc"))
class TestController(unittest.TestCase): maxDiff = None def setUp(self): self.process = Process("proc") self.part = MyPart("test_part") self.o = Controller("mri") self.o.add_part(self.part) self.process.add_controller(self.o) self.process.start() def tearDown(self): self.process.stop(timeout=1) def test_init(self): assert self.o.mri == "mri" assert self.o.process == self.process def test_two_parts_same_attribute_fails(self): p2 = MyPart("another_part") with self.assertRaises(AssertionError) as cm: self.o.add_part(p2) assert str(cm.exception) == ( "Field 'myAttribute' published by MyPart(name='another_part') " "would overwrite one made by MyPart(name='test_part')" ) def test_make_view(self): b = self.process.block_view("mri") method_view = b.method attribute_view = b.myAttribute dict_view = b.method.meta.returns.elements list_view = b.method.meta.returns.required assert method_view() == "world" assert attribute_view.value == "hello_block" assert dict_view["return"].description == "The return value" assert list_view[0] == "return" assert b.meta.tags == ["version:pymalcolm:%s" % __version__] def test_handle_request(self): q = Queue() request = Get(id=41, path=["mri", "myAttribute"]) request.set_callback(q.put) self.o.handle_request(request) response = q.get(timeout=0.1) self.assertIsInstance(response, Return) assert response.id == 41 assert response.value["value"] == "hello_block" self.part.my_attribute.meta.writeable = False request = Put( id=42, path=["mri", "myAttribute"], value="hello_block2", get=True ) request.set_callback(q.put) self.o.handle_request(request) response = q.get(timeout=0.1) self.assertIsInstance(response, Error) # not writeable assert response.id == 42 self.part.my_attribute.meta.writeable = True self.o.handle_request(request) response = q.get(timeout=0.1) self.assertIsInstance(response, Return) assert response.id == 42 assert response.value == "hello_block2" request = Post(id=43, path=["mri", "method"]) request.set_callback(q.put) self.o.handle_request(request) response = q.get(timeout=0.1) self.assertIsInstance(response, Return) assert response.id == 43 assert response.value == "world" # cover the controller._handle_post path for parameters request = Post(id=43, path=["mri", "method"], parameters={"dummy": 1}) request.set_callback(q.put) self.o.handle_request(request) response = q.get(timeout=0.1) self.assertIsInstance(response, Error) assert response.id == 43 assert ( str(response.message) == "Given keys ['dummy'], some of which aren't in allowed keys []" ) request = Subscribe(id=44, path=["mri", "myAttribute"], delta=False) request.set_callback(q.put) self.o.handle_request(request) response = q.get(timeout=0.1) self.assertIsInstance(response, Update) assert response.id == 44 assert response.value["typeid"] == "epics:nt/NTScalar:1.0" assert response.value["value"] == "hello_block2" request = Unsubscribe(id=44) request.set_callback(q.put) self.o.handle_request(request) response = q.get(timeout=0.1) self.assertIsInstance(response, Return) assert response.id == 44
def setUp(self): self.c = Controller("c")
def setUp(self): self.o = GroupPart(name="things", description="A group of things") self.c = Controller("mri") self.c.add_part(self.o)
def setUp(self): self.process = Process("proc") parts = [call_with_params(CounterPart, name="cpart")] self.controller = Controller(self.process, "counting", parts) self.process.start()
def setUp(self): self.c = Controller("c") self.p = Process("proc")
def setUp(self): self.process = Process("proc") parts = [call_with_params(HelloPart, name="hpart")] self.controller = Controller(self.process, "hello_block", parts) self.process.start()