Beispiel #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)
Beispiel #2
0
    def test_delete_all(self):
        m_session = mock.Mock()

        # TODO(Dantali0n): change into setting attributes directly
        m_atribs = {
            "id": 1,
            "name": "test1",
            "interface": DeviceInterfaces.SERIAL,
            "implementation": "ArduinoGeigerPCB",
        }

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

        m_session.query.return_value.filter_by.return_value = m_query
        m_query.all.return_value = [m_device]

        test_obj = DeviceObject(**m_atribs)
        DeviceObject.delete(m_session, test_obj, True)

        m_session.delete.assert_has_calls(
            [
                mock.call(m_device),
            ],
            any_order=True
        )
        m_session.commit.assert_called_once()
Beispiel #3
0
    def test_delete_none(self):
        m_session = mock.Mock()

        # TODO(Dantali0n): change into setting attributes directly
        m_atribs = {
            "id": 1,
            "name": "test1",
            "interface": DeviceInterfaces.SERIAL,
            "implementation": "ArduinoGeigerPCB",
        }

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

        m_session.query.return_value.filter_by.return_value = m_query
        m_query.one_or_none.return_value = None

        test_obj = DeviceObject(**m_atribs)
        DeviceObject.delete(m_session, test_obj)

        m_session.delete.assert_not_called()
        m_session.commit.assert_not_called()
    def take_action(self, parsed_args):
        args = dict(parsed_args._get_kwargs())

        if 'device' in args or 'name' in args:
            """Set device for MeasurementObject if any device params are set"""
            dev_obj = DeviceObject()
            if args['device']:
                dev_obj.id = args['device']
                del args['device']
            if args['name']:
                dev_obj.name = args['name']
                del args['name']
            args['device'] = dev_obj

        measure_obj = MeasurementObject(**args)

        data = MeasurementObject.find(self.app.database_session, measure_obj,
                                      True)

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

        fields = ('timestamp', 'device', 'cpm', 'μSv/h')
        values = []
        for result in data:
            value = (result.timestamp, result.device.id, result.cpm,
                     result.svh)
            values.append(value)

        return [fields, values]
Beispiel #5
0
    def test_build_object_unset(self):

        test_obj = DeviceObject()
        test_obj._build_object()

        self.assertIsNone(None, test_obj.m_device.id)
        self.assertIsNone(None, test_obj.m_device.name)
        self.assertIsNone(None, test_obj.m_device.interface)
        self.assertIsNone(None, test_obj.m_device.implementation)
Beispiel #6
0
    def test_build_attributes_none(self):

        test_obj = DeviceObject()
        test_obj.m_device = Device()
        test_obj._build_attributes()

        self.assertIsNone(test_obj.id)
        self.assertIsNone(test_obj.name)
        self.assertIsNone(test_obj.interface)
        self.assertIsNone(test_obj.implementation)
Beispiel #7
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)
Beispiel #8
0
    def test_filter(self):

        m_atribs = {
            "name": "value1",
            "attributeskip": "none",
        }

        test_obj = DeviceObject(**m_atribs)

        m_result = test_obj._filter(test_obj)

        self.assertEqual(
            {"name": "value1"}, m_result)
Beispiel #9
0
    def test_build_object_keys(self):

        m_atribs = {
            "id": 2,
            "name": "value2",
            "interface": DeviceInterfaces.SERIAL,
            "implementation": "ArduinoGeigerPCB",
        }

        test_obj = DeviceObject(**m_atribs)
        test_obj._build_object()

        self.assertEqual(2, test_obj.m_device.id)
        self.assertEqual("value2", test_obj.m_device.name)
        self.assertEqual(DeviceInterfaces.SERIAL, test_obj.m_device.interface)
Beispiel #10
0
    def _build_attributes(self):
        if self.m_measurement.id:
            self.id = self.m_measurement.id
        if self.m_measurement.timestamp:
            self.timestamp = self.m_measurement.timestamp

        if self.m_measurement.base_device:
            dev_obj = DeviceObject()
            dev_obj.m_device = self.m_measurement.base_device
            dev_obj._build_attributes()
            self.device = dev_obj

        if self.m_measurement.cpm:
            self.cpm = self.m_measurement.cpm
        if self.m_measurement.svh:
            self.svh = self.m_measurement.svh
    def test_take_action(self, m_dev_obj):

        # remove ShowOne from the DeviceShow inheritance
        bases = copy(mc.MeasurementList.__bases__)
        f_bases = tuple(base for base in bases if base != Lister)

        m_args = mock.Mock()
        m_args._get_kwargs.return_value = {}

        m_mod_dev = mock.Mock()
        m_mod_dev.id = 1337
        m_mod_dev.timestamp = datetime.utcnow()
        m_mod_dev.cpm = 12
        m_mod_dev.svh = 0.12
        m_mod_dev.device = DeviceObject(**{'id': 1})

        m_dev_obj.find.return_value = [m_mod_dev]

        m_base = mock.patch.object(mc.MeasurementList, '__bases__', f_bases)
        with m_base:
            m_base.is_local = True
            t_device = mc.MeasurementList()

            t_device.app = mock.Mock()

            t_result = t_device.take_action(m_args)

            self.assertEqual(t_result[1][0][0], m_mod_dev.timestamp)
            self.assertEqual(t_result[1][0][1], m_mod_dev.device.id)
            self.assertEqual(t_result[1][0][2], m_mod_dev.cpm)
            self.assertEqual(t_result[1][0][3], m_mod_dev.svh)

        # ensure that is_local on the patch does not modify the actual bases
        self.assertEqual(bases, mc.MeasurementList.__bases__)
Beispiel #12
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]
Beispiel #13
0
    def test_init(self):

        m_atribs = {
            "name": "value1",
            "attributeskip": "none",
        }

        test_obj = DeviceObject(**m_atribs)

        self.assertEqual("value1", test_obj.name)
        self.assertIsNone(None, getattr(test_obj, "attributeskip", None))
Beispiel #14
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)
Beispiel #15
0
    def take_action(self, parsed_args):
        args = dict(parsed_args._get_kwargs())
        device_obj = DeviceObject(**args)

        if device_obj.id is None and device_obj.name is None:
            raise RuntimeWarning(
                _("Either the id or name must be specified to "
                  "remove a device"))

        try:
            data = DeviceObject.delete(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', 'interface', 'implementation')
        values = (data.id, data.name, data.interface, data.implementation)

        self.app.LOG.info(_("Device removed successfully"))
        return (fields, values)
Beispiel #16
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)
Beispiel #17
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
Beispiel #18
0
    def test_get_device_class(self, m_get_device_map):
        """Ensure class can be found for instances of DeviceObject

        This checks that instances of:
        :py:class:`radloggerpy.database.objects.device.DeviceObject` can have
        their corresponding class found by get_device_class.
        """

        m_class = mock.Mock(NAME="test")
        m_get_device_map.return_value = {
            DeviceInterfaces.SERIAL: [m_class,
                                      mock.Mock(NAME="different")]
        }

        # Create actual DeviceObject instead of mock as to not upset type
        # hinting.
        args = {
            "implementation": 'test',
            "interface": INTERFACE_CHOICES[DeviceInterfaces.SERIAL]
        }
        m_obj = DeviceObject(**args)

        self.assertEqual(m_class, dm.DeviceManager.get_device_class(m_obj))
Beispiel #19
0
    def test_delete_exception(self):
        m_session = mock.Mock()

        # TODO(Dantali0n): change into setting attributes directly
        m_atribs = {
            "id": 1,
            "name": "test1",
            "interface": DeviceInterfaces.SERIAL,
            "implementation": "ArduinoGeigerPCB",
        }

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

        m_session.query.return_value.filter_by.return_value = m_query
        m_query.one_or_none.return_value = m_device

        m_session.commit.side_effect = RuntimeWarning

        test_obj = DeviceObject(**m_atribs)
        self.assertRaises(
            RuntimeWarning, DeviceObject.delete, m_session, test_obj)

        m_session.delete.assert_has_calls(
            [
                mock.call(m_device),
            ],
            any_order=True
        )
        m_session.commit.assert_called_once()
        m_session.rollback.assert_called_once()
Beispiel #20
0
    def test_find_obj_deprecated(self, m_log):

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

        m_device_serial = SerialDevice()
        m_device_serial.port = "/dev/ttyUSB0"
        m_device_serial.baudrate = 115200
        m_device_serial.bytesize = SerialBytesizeTypes.EIGHTBITS
        m_device_serial.parity = SerialParityTypes.PARITY_ODD
        m_device_serial.stopbits = SerialStopbitTypes.STOPBITS_ONE

        m_device.serial = [m_device_serial]

        """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.\
            join.return_value.filter_by.return_value = m_query
        m_query.one_or_none.return_value = m_device

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

        self.assertEqual(1, result_obj.id)
        self.assertEqual("/dev/ttyUSB0", result_obj.port)
        self.assertEqual(8, result_obj.bytesize)
        self.assertEqual("odd", result_obj.parity)
        self.assertEqual(1, result_obj.stopbits)

        m_log.warning.assert_called_once()
Beispiel #21
0
 def __init__(self):
     m_dev = DeviceObject()
     m_condition = Condition()
     super(TestDevice.FakeDevice, self).__init__(m_dev, m_condition)