def test_get_state(self):
        file_like_object_callback = Mock(
            return_value=create_state_file(random.randint(10, 100), 10, 20, 1, False, 'Test', 5))
        state = State.get_state(file_like_object_callback, Mock())

        file_like_object_callback.assert_called_once_with('r')
        self.assertIsNotNone(state)
    def setUp(self) -> None:
        self.data_per_hour = 1
        self.keep_raw = 3
        self.keep_day = 48
        self.keep_month = 30
        self.keep_year = 12
        self.keep_years = 3

        self.plugin_type = 'plugin1'

        self.db_file = FileLikeMock()
        self.state_file = FileLikeMock()

        self.database: Database = Database.create_database(
            self.database_file_callback, self.data_per_hour, self.keep_raw,
            self.keep_day, self.keep_month, self.keep_year, self.keep_years)
        self.plugin_loader: PluginLoader = PluginLoader(
            {'ElectricMeterPlugins': {
                self.plugin_type: 'TestPlugin'
            }}, 'testplugins')

        self.state: State = State.get_state(self.state_file_callback,
                                            self.deserialize_electric_meter)

        self.logic = Logic(self.state, self.database, self.plugin_loader,
                           self.data_per_hour)
    def test_load_empty_file(self):
        empty_file = FileLikeMock()
        file_like_object_callback = Mock(return_value=empty_file)
        deserialize_electric_meter_callback = Mock()

        # Test
        state = State.get_state(file_like_object_callback, deserialize_electric_meter_callback)

        # Assertion
        self.assertIsNotNone(state)
        deserialize_electric_meter_callback.assert_not_called()
        empty_file.seek(0)
        self.assertEqual({
            'next_id': 10,
            'electric_meters': []
        }, json.load(empty_file))
    def test_load_electric_meter(self):
        # Setup
        meter_id = 1
        value = 321
        pin = 1
        active_low = False
        name = 'test'
        count = 456

        serialized_electric_meter = {
            "name": name,
            "custom": {
                "pin": pin,
                "active_low": active_low,
                "value": value,
                "count": count,
            }
        }

        deserialized_electric_meter = Mock()
        deserialize_electric_meter_callback = Mock(return_value=deserialized_electric_meter)

        # Test
        state = State.get_state(
            Mock(return_value=create_state_file(meter_id+1, meter_id, value, pin, active_low, name, count)),
            deserialize_electric_meter_callback)

        # Assert
        self.assertIsNotNone(state)
        deserialize_electric_meter_callback.assert_called_once_with(serialized_electric_meter)
        loaded_meters_dict = state.get_electric_meters_dict()
        self.assertIn(meter_id, loaded_meters_dict.keys())
        loaded_meter_dict = loaded_meters_dict[meter_id]
        loaded_meters = state.get_electric_meters()
        self.assertIn(meter_id, loaded_meters.keys())
        loaded_meter = loaded_meters[meter_id]

        self.assertEqual(deserialized_electric_meter, loaded_meter)
        self.assertEqual(serialized_electric_meter, loaded_meter_dict)