Пример #1
0
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")
Пример #2
0
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)
Пример #3
0
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)]
Пример #4
0
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)
        ]
Пример #5
0
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)]
Пример #6
0
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
        )]
Пример #7
0
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)
        ]
Пример #8
0
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)
Пример #9
0
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"
Пример #10
0
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"
Пример #11
0
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")
Пример #12
0
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
Пример #13
0
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 = Subscribe(id=40, path=["hello_block", "greet"], delta=True)
        request.set_callback(q.put)
        self.controller.handle_request(request)
        # Get the initial subscribe value
        inital = q.get(timeout=0.1)
        self.assertIsInstance(inital, Delta)
        assert inital.changes[0][1]["took"]["value"] == dict(sleep=0, name="")
        assert inital.changes[0][1]["returned"]["value"] == {"return": ""}
        # Do a greet
        request = Post(id=44,
                       path=["hello_block", "greet"],
                       parameters=dict(name="me", sleep=1))
        request.set_callback(q.put)
        self.controller.handle_request(request)
        # Then an error
        request = Post(id=45, path=["hello_block", "error"])
        request.set_callback(q.put)
        self.controller.handle_request(request)
        # We should quickly get the error response first
        response = q.get(timeout=1.0)
        self.assertIsInstance(response, Error)
        assert response.id == 45
        # Then the long running greet delta
        response = q.get(timeout=3.0)
        self.assertIsInstance(response, Delta)
        assert len(response.changes) == 2
        assert response.changes[0][0] == ["took"]
        took = response.changes[0][1]
        assert took.value == dict(sleep=1, name="me")
        assert took.present == ["name", "sleep"]
        assert took.alarm == Alarm.ok
        assert response.changes[1][0] == ["returned"]
        returned = response.changes[1][1]
        assert returned.value == {"return": "Hello me"}
        assert returned.present == ["return"]
        assert returned.alarm == Alarm.ok
        # Check it took about 1s to run
        assert abs(1 - (returned.timeStamp.to_time() -
                        took.timeStamp.to_time())) < 0.4
        # And it's response
        response = q.get(timeout=1.0)
        self.assertIsInstance(response, Return)
        assert response.id == 44
        assert response.value == "Hello me"