Example #1
0
    def test_find_obj_multiple(self):
        m_device1 = Device()
        m_device2 = Device()
        m_query = mock.Mock()
        m_session = mock.Mock()
        m_session.query.return_value.filter_by.return_value = m_query

        m_query.all.return_value = [m_device1, m_device2]

        m_device1.id = 1
        m_device1.name = "test1"
        m_device1.interface = DeviceInterfaces.SERIAL
        m_device1.implementation = mock.Mock(
            code="ArduinoGeigerPCB", value="arduinogeigerpcb")

        m_device2.id = 2
        m_device2.name = "test2"
        m_device2.interface = DeviceInterfaces.SERIAL
        m_device2.implementation = mock.Mock(
            code="ArduinoGeigerPCB", value="arduinogeigerpcb")

        test_obj = DeviceObject(**{"interface": "serial"})
        result_obj = DeviceObject.find(m_session, test_obj, True)

        self.assertEqual(1, result_obj[0].id)
        self.assertEqual("test1", result_obj[0].name)
        self.assertEqual("serial", result_obj[0].interface)
        self.assertEqual(ArduinoGeigerPcb.NAME, result_obj[0].implementation)

        self.assertEqual(2, result_obj[1].id)
        self.assertEqual("test2", result_obj[1].name)
        self.assertEqual("serial", result_obj[1].interface)
        self.assertEqual(ArduinoGeigerPcb.NAME, result_obj[1].implementation)
Example #2
0
    def test_find_obj_multiple_none(self):
        m_query = mock.Mock()
        m_session = mock.Mock()
        m_session.query.return_value.filter_by.return_value = m_query

        m_query.all.return_value = None

        test_obj = DeviceObject(**{"id": 1})
        result_obj = DeviceObject.find(m_session, test_obj, True)

        self.assertIsNone(result_obj)
Example #3
0
    def take_action(self, parsed_args):
        args = dict(parsed_args._get_kwargs())
        device_obj = DeviceObject(**args)

        data = DeviceObject.find(self.app.database_session, device_obj, True)

        if len(data) == 0:
            raise RuntimeWarning(_("No devices found"))

        fields = ('id', 'name', 'measurement type', 'interface',
                  'implementation')
        values = []
        for result in data:
            value = (result.id, result.name, result.type, result.interface,
                     result.implementation)
            values.append(value)

        return [fields, values]
Example #4
0
    def test_find_obj(self):

        """Represents mocked device as it will be retrieved from db """
        m_device = Device()
        m_device.id = 1
        m_device.name = "test"
        m_device.interface = DeviceInterfaces.SERIAL
        m_device.implementation = mock.Mock(
            code="ArduinoGeigerPCB", value="arduinogeigerpcb")

        """Setup query and session to return mocked device"""
        m_query = mock.Mock()
        m_session = mock.Mock()
        m_session.query.return_value.filter_by.return_value = m_query
        m_query.one_or_none.return_value = m_device

        test_obj = DeviceObject(**{"id": 1})
        result_obj = DeviceObject.find(m_session, test_obj, False)

        self.assertEqual(1, result_obj.id)
        self.assertEqual("test", result_obj.name)
        self.assertEqual("serial", result_obj.interface)
        self.assertEqual(ArduinoGeigerPcb.NAME, result_obj.implementation)
Example #5
0
    def add(session, reference):
        reference._build_object()
        """Measurement.device_id must be set to populate the field"""
        if reference.m_measurement.device_id is None \
                and hasattr(reference.device, 'id') and reference.device.id:
            """If no device_id is set find it through device id"""
            reference.m_measurement.device_id = reference.device.id
        elif reference.m_measurement.device_id is None and reference.device:
            """If no device_id find it through device"""
            dev = DeviceObject.find(session, reference.device, False)
            if dev is None:
                raise RuntimeError(_("No associateable Device found"))
            reference.m_measurement.device_id = dev.id

        session.add(reference.m_measurement)

        try:
            return session.commit()
        except Exception:
            session.rollback()
            # TODO(Dantali0n): These errors are horrendous for users to
            #                  understand an error abstraction is needed.
            raise
Example #6
0
    def take_action(self, parsed_args):
        args = dict(parsed_args._get_kwargs())
        device_obj = DeviceObject(**args)

        details = args['detailed']

        try:
            data = DeviceObject.find(
                self.app.database_session, device_obj, False)
        except MultipleResultsFound:
            raise RuntimeWarning(_("Multiple devices found"))

        if data is None:
            raise RuntimeWarning(_("Device could not be found"))

        fields = (
            'id', 'name', 'measurement type', 'interface', 'implementation')
        values = (
            data.id, data.name, data.type, data.interface, data.implementation)

        if details and data.interface == \
                INTERFACE_CHOICES[DeviceInterfaces.SERIAL]:
            data = SerialDeviceObject.find(
                self.app.database_session, device_obj, False)
            fields += ('port', 'baudrate', 'bytesize', 'parity',
                       'stopbits', 'timeout')
            values += (data.port, data.baudrate, data.bytesize, data.parity,
                       data.stopbits, data.timeout)
        elif details and data.interface == \
                INTERFACE_CHOICES[DeviceInterfaces.ETHERNET]:
            pass
        elif details and data.interface == \
                INTERFACE_CHOICES[DeviceInterfaces.USB]:
            pass

        return (fields, values)