コード例 #1
0
def test_list_or_dict():
    record = EquipmentRecord(
        connection=ConnectionRecord(address='COM1', backend=Backend.MSL))
    c = connect([record], True)
    assert isinstance(c, ConnectionDemo)

    c = connect({'eq': record}, True)
    assert isinstance(c, ConnectionDemo)
コード例 #2
0
def test_demo_mode():
    Config.DEMO_MODE = True

    c = connect(EquipmentRecord(
        connection=ConnectionRecord(address='COM1', backend=Backend.MSL)),
                demo=True)
    assert isinstance(c, ConnectionDemo)

    c = connect(
        EquipmentRecord(
            connection=ConnectionRecord(address='COM1', backend=Backend.MSL)))
    assert isinstance(c, ConnectionDemo)
コード例 #3
0
    def connect(self, demo=None):
        """Establish a connection to the equipment.

        Calls the :func:`~msl.equipment.factory.connect` function.

        Parameters
        ----------
        demo : :class:`bool`, optional
            Whether to simulate a connection to the equipment by opening
            a connection in demo mode. This allows you to test your code
            if the equipment is not physically connected to a computer.

            If :data:`None` then the `demo` value is determined from the
            :attr:`~.config.Config.DEMO_MODE` attribute.

        Returns
        -------
        A :class:`~msl.equipment.connection.Connection` subclass.
        """
        from msl.equipment import factory  # import here to avoid circular imports
        return factory.connect(self, demo)
コード例 #4
0
def test_exceptions():
    Config.DEMO_MODE = False

    # not an EquipmentRecord
    for item in [None, ConnectionRecord()]:
        with pytest.raises(AttributeError, match=r"no attribute 'connection'"):
            connect(item)

    # only 1 EquipmentRecord is allowed
    with pytest.raises(AttributeError, match=r"no attribute 'connection'"):
        connect([
            EquipmentRecord(connection=ConnectionRecord(address='COM1',
                                                        backend=Backend.MSL)),
            EquipmentRecord(connection=ConnectionRecord(address='COM2',
                                                        backend=Backend.MSL))
        ])

    # only 1 EquipmentRecord is allowed
    with pytest.raises(AttributeError, match=r"no attribute 'connection'"):
        connect({
            'a':
            EquipmentRecord(connection=ConnectionRecord(address='COM1',
                                                        backend=Backend.MSL)),
            'b':
            EquipmentRecord(connection=ConnectionRecord(address='COM2',
                                                        backend=Backend.MSL))
        })

    # only 1 EquipmentRecord allowed
    c = Config(os.path.join(ROOT_DIR, 'db.xml'))
    with pytest.raises(AttributeError, match=r"no attribute 'connection'"):
        connect(c.database().equipment)

    # no ConnectionRecord defined for the EquipmentRecord
    with pytest.raises(ValueError, match='connection object'):
        connect(EquipmentRecord())

    # no address has been set
    with pytest.raises(ValueError, match='connection address'):
        connect(EquipmentRecord(connection=ConnectionRecord()))

    # no backend
    with pytest.raises(ValueError, match='connection backend'):
        connect(
            EquipmentRecord(connection=ConnectionRecord(
                address='COM1', backend=Backend.UNKNOWN)))

    # the SDK library does not exist
    with pytest.raises(IOError, match='Cannot find'):
        connect(
            EquipmentRecord(manufacturer='Thorlabs',
                            model='FW212C',
                            connection=ConnectionRecord(
                                address='SDK::invalid.dll',
                                backend=Backend.MSL)))
コード例 #5
0
def test_exceptions():
    Config.DEMO_MODE = False

    with pytest.raises(TypeError) as err:
        connect(None)  # not an EquipmentRecord
    assert 'EquipmentRecord' in str(err.value)

    with pytest.raises(TypeError) as err:
        connect(ConnectionRecord())  # not an EquipmentRecord
    assert 'EquipmentRecord' in str(err.value)

    with pytest.raises(TypeError) as err:
        connect([None])  # not an EquipmentRecord
    assert 'EquipmentRecord' in str(err.value)

    with pytest.raises(TypeError) as err:
        connect({None})  # not an EquipmentRecord
    assert 'EquipmentRecord' in str(err.value)

    with pytest.raises(TypeError) as err:
        connect([
            EquipmentRecord(connection=ConnectionRecord(address='COM1',
                                                        backend=Backend.MSL)),
            EquipmentRecord(connection=ConnectionRecord(address='COM2',
                                                        backend=Backend.MSL))
        ])  # only 1 EquipmentRecord allowed
    assert 'list' in str(err.value)

    with pytest.raises(TypeError) as err:
        connect({
            'a':
            EquipmentRecord(connection=ConnectionRecord(address='COM1',
                                                        backend=Backend.MSL)),
            'b':
            EquipmentRecord(connection=ConnectionRecord(address='COM2',
                                                        backend=Backend.MSL))
        })  # only 1 EquipmentRecord allowed
    assert 'dict' in str(err.value)

    c = Config(os.path.join(os.path.dirname(__file__), 'db.xml'))
    with pytest.raises(TypeError) as err:
        connect(c.database().equipment)  # only 1 EquipmentRecord allowed
    assert 'dict' in str(err.value)

    with pytest.raises(ValueError) as err:
        connect(EquipmentRecord()
                )  # no ConnectionRecord defined for the EquipmentRecord
    assert 'connection object' in str(err.value)

    with pytest.raises(ValueError) as err:
        connect(EquipmentRecord(
            connection=ConnectionRecord()))  # no address has been set
    assert 'connection address' in str(err.value)

    with pytest.raises(ValueError) as err:
        connect(
            EquipmentRecord(connection=ConnectionRecord(
                address='COM1', backend=Backend.UNKNOWN)))  # no backend
    assert 'connection backend' in str(err.value)

    # the SDK library does not exist
    with pytest.raises(IOError) as err:
        connect(
            EquipmentRecord(manufacturer='Thorlabs',
                            model='FW212C',
                            connection=ConnectionRecord(
                                address='SDK::invalid.dll',
                                backend=Backend.MSL)))
    assert 'loadlib' in str(err)