Пример #1
0
    def test_add_find_device(self):
        m_session = mock.Mock()
        m_date = datetime.utcnow()

        # TODO(Dantali0n): change into setting attributes directly
        m_atribs = {
            "cpm": 12,
            "svh": 0.0045,
            "timestamp": m_date,
            "device": device.DeviceObject(**{'name': 'test'})
        }

        with mock.patch.object(ms, 'DeviceObject') as m_find:
            test_obj = ms.MeasurementObject(**m_atribs)

            m_dev = Device()
            m_dev.id = 1
            m_find.find.return_value = m_dev

            ms.MeasurementObject.add(m_session, test_obj)

            m_session.add.assert_has_calls([
                mock.call(test_obj.m_measurement),
            ],
                                           any_order=True)
            m_session.commit.assert_called_once()

        m_find.find.assert_called_once()
Пример #2
0
    def test_find_obj_multiple(self):
        m_measurement_1 = Measurement()
        m_measurement_1.id = 1
        m_measurement_1.cpm = 12
        m_measurement_1.base_device = Device()
        m_measurement_1.base_device.id = 1

        m_measurement_2 = Measurement()
        m_measurement_2.id = 2
        m_measurement_2.cpm = 34
        m_measurement_2.base_device = Device()
        m_measurement_2.base_device.id = 1

        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.all.return_value = [m_measurement_1, m_measurement_2]

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

        self.assertEqual(1, result_obj[0].id)
        self.assertEqual(12, result_obj[0].cpm)
        self.assertEqual(1, result_obj[0].device.id)

        self.assertEqual(2, result_obj[1].id)
        self.assertEqual(34, result_obj[1].cpm)
        self.assertEqual(1, result_obj[1].device.id)
Пример #3
0
    def _build_object(self):
        self.m_device = Device()

        if self.id:
            self.m_device.id = self.id
        if self.name:
            self.m_device.name = self.name

        if self.enabled:
            self.m_device.enabled = self.enabled

        if self.type in DEVICE_TYPE_CHOICES.keys():
            self.m_device.type = self.type
        elif self.type in DEVICE_TYPE_CHOICES.values():
            index = list(DEVICE_TYPE_CHOICES.values()).index(self.type)
            self.m_device.type = list(DEVICE_TYPE_CHOICES.keys())[index]

        if self.interface in INTERFACE_CHOICES.keys():
            self.m_device.interface = self.interface
        elif self.interface in INTERFACE_CHOICES.values():
            index = list(INTERFACE_CHOICES.values()).index(self.interface)
            self.m_device.interface = list(INTERFACE_CHOICES.keys())[index]

        if self.implementation:
            self.m_device.implementation = self.implementation
Пример #4
0
    def test_find_obj(self):
        m_date = datetime.utcnow()
        """Represents mocked device as it will be retrieved from db """
        m_measurement = Measurement()
        m_measurement.id = 1
        m_measurement.timestamp = m_date
        m_measurement.cpm = 12
        m_measurement.svh = 0.0045
        m_measurement.base_device = Device()
        m_measurement.base_device.id = 1
        """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_measurement

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

        self.assertEqual(1, result_obj.id)
        self.assertEqual(m_date, result_obj.timestamp)
        self.assertEqual(12, result_obj.cpm)
        self.assertEqual(0.0045, result_obj.svh)
        self.assertEqual(1, result_obj.device.id)
Пример #5
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 = "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 = sd.SerialDeviceObject(**{"baudrate": 115200})
        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)
Пример #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)
Пример #7
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()
Пример #8
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()
Пример #9
0
    def test_build_attributes_none(self):

        test_obj = sd.SerialDeviceObject()
        test_obj.m_device = Device()
        test_obj.m_serial_device = SerialDevice()
        test_obj._build_attributes()

        self.assertIsNone(test_obj.port)
        self.assertIsNone(test_obj.baudrate)
        self.assertIsNone(test_obj.bytesize)
        self.assertIsNone(test_obj.parity)
        self.assertIsNone(test_obj.stopbits)
        self.assertIsNone(test_obj.timeout)
Пример #10
0
    def test_delete_obj_multiple(self):
        m_measurement_1 = Measurement()
        m_measurement_1.id = 1
        m_measurement_1.cpm = 12
        m_measurement_1.base_device = Device()
        m_measurement_1.base_device.id = 1

        m_measurement_2 = Measurement()
        m_measurement_2.id = 2
        m_measurement_2.cpm = 34
        m_measurement_2.base_device = Device()
        m_measurement_2.base_device.id = 1

        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_measurement_1, m_measurement_2]

        test_obj = ms.MeasurementObject(
            **{"device": device.DeviceObject(**{'id': 1})})
        result_obj = ms.MeasurementObject.delete(m_session, test_obj, True)

        m_session.delete.assert_has_calls([
            mock.call(m_measurement_1),
            mock.call(m_measurement_2),
        ],
                                          any_order=True)
        m_session.commit.assert_called_once()

        self.assertEqual(1, result_obj[0].id)
        self.assertEqual(12, result_obj[0].cpm)
        self.assertEqual(1, result_obj[0].device.id)

        self.assertEqual(2, result_obj[1].id)
        self.assertEqual(34, result_obj[1].cpm)
        self.assertEqual(1, result_obj[1].device.id)
Пример #11
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)
Пример #12
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()
Пример #13
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)
Пример #14
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. \
            join.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_device_serial1 = SerialDevice()
        m_device_serial1.port = "/dev/ttyUSB0"
        m_device_serial1.baudrate = 115200
        m_device_serial1.bytesize = SerialBytesizeTypes.EIGHTBITS
        m_device_serial1.parity = SerialParityTypes.PARITY_ODD
        m_device_serial1.stopbits = SerialStopbitTypes.STOPBITS_ONE

        m_device1.serial = [m_device_serial1]

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

        m_device_serial2 = SerialDevice()
        m_device_serial2.port = "/dev/ttyUSB2"
        m_device_serial2.baudrate = 9600
        m_device_serial2.bytesize = SerialBytesizeTypes.SEVENBITS
        m_device_serial2.parity = SerialParityTypes.PARITY_EVEN
        m_device_serial2.stopbits = SerialStopbitTypes.STOPBITS_TWO

        m_device2.serial = [m_device_serial2]

        test_obj = sd.SerialDeviceObject(**{"interface": "serial"})
        result_obj = sd.SerialDeviceObject.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("/dev/ttyUSB0", result_obj[0].port)
        self.assertEqual(8, result_obj[0].bytesize)
        self.assertEqual("odd", result_obj[0].parity)
        self.assertEqual(1, result_obj[0].stopbits)

        self.assertEqual(2, result_obj[1].id)
        self.assertEqual("test2", result_obj[1].name)
        self.assertEqual("serial", result_obj[1].interface)
        self.assertEqual("/dev/ttyUSB2", result_obj[1].port)
        self.assertEqual(7, result_obj[1].bytesize)
        self.assertEqual("even", result_obj[1].parity)
        self.assertEqual(2, result_obj[1].stopbits)