コード例 #1
0
    def test_twoPolicies_deleteMeasurements_oneSensor(self):
        mockedCrud = Mock()

        with patch.dict('sys.modules', **{'logic.database.Crud': mockedCrud}):
            mockedCrud.DATE_FORMAT = '%Y-%m-%d %H:%M:%S'
            mockedCrud.get_measurements_for_sensor.side_effect = self.get_measurements_mocked
            mockedCrud.get_sensors.return_value = [
                Schemas.Sensor(id=1,
                               name="myTempSensor",
                               type="temperature",
                               device_id=1)
            ]
            mockedCrud.get_first_measurement_for_sensor.return_value = self.FIRST_MEASUREMENT

            database = Mock()
            from logic.database.DatabaseCleaner import DatabaseCleaner
            from logic.database.DatabaseCleaner import RetentionPolicy

            policy1 = RetentionPolicy(numberOfMeasurementsPerDay=4,
                                      ageInDays=1)
            policy2 = RetentionPolicy(numberOfMeasurementsPerDay=2,
                                      ageInDays=1)
            DatabaseCleaner([policy1, policy2], False).clean(
                database,
                datetime(year=2021, month=8, day=19).date())

            calls = mockedCrud.delete_multiple_measurements.call_args_list
            self.assertEqual(2, len(calls))
            self.assertEqual((database, {3}), calls[0].args)
            self.assertEqual((database, {3, 4}), calls[1].args)
コード例 #2
0
    def test_onePolicy_deleteMeasurements_oneSensor(self):
        mockedCrud = Mock()
        with patch.dict('sys.modules', **{'logic.database.Crud': mockedCrud}):
            mockedCrud.DATE_FORMAT = '%Y-%m-%d %H:%M:%S'
            mockedCrud.get_measurements_for_sensor.side_effect = self.get_measurements_mocked
            mockedCrud.get_sensors.return_value = [
                Schemas.Sensor(id=1,
                               name="myTempSensor",
                               type="temperature",
                               device_id=1)
            ]
            mockedCrud.get_first_measurement_for_sensor.return_value = self.FIRST_MEASUREMENT

            database = Mock()
            from logic.database.DatabaseCleaner import DatabaseCleaner
            from logic.database.DatabaseCleaner import RetentionPolicy

            policy = RetentionPolicy(numberOfMeasurementsPerDay=4, ageInDays=1)
            DatabaseCleaner([policy], False).clean(
                database,
                datetime(year=2021, month=8, day=19).date())

            mockedCrud.delete_multiple_measurements.assert_called_once_with(
                database, {3})

            calls = mockedCrud.get_measurements_for_sensor.call_args_list
            self.assertNotIn(
                (database, '2020-01-01 00:00:00', '2020-01-01 06:00:00', 1),
                [call.args for call in calls])
コード例 #3
0
    def test__GetClosestMeasurementForPoint_getClosest_EqualDistance(self):
        mockedCrud = Mock()
        with patch.dict('sys.modules', **{'logic.database.Crud': mockedCrud}):
            mockedCrud.DATE_FORMAT = DATE_FORMAT

            from logic.database.DatabaseCleaner import DatabaseCleaner

            expected = Schemas.Measurement(id=1,
                                           value=5,
                                           sensor_id=15,
                                           timestamp=datetime(
                                               year=2021,
                                               month=8,
                                               day=18,
                                               hour=21,
                                               minute=55,
                                               second=0).strftime(DATE_FORMAT))
            measurements = [
                expected,
                Schemas.Measurement(id=1,
                                    value=5,
                                    sensor_id=15,
                                    timestamp=datetime(
                                        year=2021,
                                        month=8,
                                        day=18,
                                        hour=22,
                                        minute=5,
                                        second=0).strftime(DATE_FORMAT))
            ]

            result = DatabaseCleaner._get_closest_measurement_for_point(
                measurements, CURRENT_DATE_TIME)
            self.assertEqual(expected, result)
コード例 #4
0
    def test_GetMeasurementsForDay(self):
        mockedCrud = Mock()
        with patch.dict('sys.modules', **{'logic.database.Crud': mockedCrud}):
            mockedCrud.DATE_FORMAT = DATE_FORMAT
            mockedCrud.get_measurements_for_sensor.side_effect = self.get_measurements_mocked

            from logic.database.DatabaseCleaner import DatabaseCleaner

            database = Mock()
            policy = RetentionPolicy(numberOfMeasurementsPerDay=4,
                                     ageInDays=10)
            measurementIds, idsToDelete = DatabaseCleaner._categorize_measurements_for_day(
                database, CURRENT_DATE_TIME.date(), policy, 1)
            self.assertEqual([
                self.MEASUREMENT1.id, self.MEASUREMENT1.id,
                self.MEASUREMENT2.id, self.MEASUREMENT4.id
            ], measurementIds)
            self.assertEqual({self.MEASUREMENT3.id}, idsToDelete)
コード例 #5
0
    def test_forceBackupAfterCleanup(self):
        mockedCrud = Mock()

        with patch.dict('sys.modules', **{'logic.database.Crud': mockedCrud}):
            mockedCrud.DATE_FORMAT = '%Y-%m-%d %H:%M:%S'
            mockedCrud.get_measurements_for_sensor.return_value = []
            mockedCrud.get_sensors.return_value = [
                Schemas.Sensor(id=1,
                               name="myTempSensor",
                               type="temperature",
                               device_id=1)
            ]
            mockedCrud.get_first_measurement_for_sensor.return_value = self.FIRST_MEASUREMENT

            database = Mock()
            from logic.database.DatabaseCleaner import DatabaseCleaner
            from logic.database.DatabaseCleaner import RetentionPolicy

            policy = RetentionPolicy(numberOfMeasurementsPerDay=4, ageInDays=1)
            DatabaseCleaner([policy], True).clean(
                database,
                datetime(year=2021, month=8, day=19).date())

            mockedCrud.BACKUP_SERVICE.backup.assert_called_once()