Пример #1
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)
Пример #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 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()
Пример #4
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. \
            join.return_value.filter_by.return_value = m_query

        m_query.all.return_value = None

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

        self.assertIsNone(result_obj)
Пример #5
0
    def test_add_error(self):
        m_session = mock.Mock()
        m_session.commit.side_effect = RuntimeError

        # TODO(Dantali0n): change into setting attributes directly
        m_atribs = {"cpm": 12, "device": device.DeviceObject(**{'id': 1})}

        test_obj = ms.MeasurementObject(**m_atribs)
        self.assertRaises(RuntimeError, 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_session.rollback.assert_called_once()
Пример #6
0
    def test_add_find_device_error(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_find.find.return_value = None
            self.assertRaises(RuntimeError, ms.MeasurementObject.add,
                              m_session, test_obj)

        m_find.find.assert_called_once()
Пример #7
0
    def test_add(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(**{'id': 1})
        }

        test_obj = ms.MeasurementObject(**m_atribs)
        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()
Пример #8
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)