def bacprop_service(mocker: MockFixture) -> BacPropagator: mocker.patch("bacprop.service.SensorStream") mocker.patch("bacprop.service.VirtualSensorNetwork") service = BacPropagator() service._sensor_net = mocker.create_autospec(VirtualSensorNetwork) service._stream = mocker.create_autospec(SensorStream) return service
def test_main_interrupt(self, mocker: MockFixture, bacprop_service: BacPropagator) -> None: mocker.patch.object(bacprop_service, "_main_loop", autospec=True) mocker.patch.object(bacprop_service, "_fault_check_loop", autospec=True) mocker.patch.object(bacprop_service, "_start_bacnet_thread", autospec=True) backnet_thread = mocker.create_autospec(Thread) bacprop_service._start_bacnet_thread.return_value = ( # type: ignore backnet_thread) def throw() -> NoReturn: raise KeyboardInterrupt() bacprop_service._main_loop = lambda *args: throw() # type: ignore bacprop_service._fault_check_loop.return_value = async_return( # type: ignore None) bacprop_service._stream.stop.return_value = async_return( None) # type: ignore bacprop_service.start() bacprop_service._sensor_net.stop.assert_called_once() # type: ignore backnet_thread.join.assert_called_once()
async def test_faulty_sensor(self, mocker: MockFixture, bacprop_service: BacPropagator) -> None: sensors = { 1: mocker.create_autospec(Sensor), 2: mocker.create_autospec(Sensor) } sensors[1].has_fault.return_value = False sensors[2].has_fault.return_value = False sensors[1].get_update_time.return_value = ( time.time() - BacPropagator.SENSOR_OUTDATED_TIME - 1) sensors[2].get_update_time.return_value = time.time() bacprop_service._sensor_net.get_sensors.return_value = sensors # type: ignore bacprop_service._running = True asyncio.ensure_future(bacprop_service._fault_check_loop()) await asyncio.sleep(0) # Sensor 2 should not be marked as faulty, as it's update time is in range sensors[1].mark_fault.assert_called_once() sensors[2].mark_fault.assert_not_called() bacprop_service._running = False await asyncio.sleep(0)
def test_service_start(self, mocker: MockFixture) -> None: mock_init = mocker.patch("bacprop.cli.BacPropagator") mock_service = mocker.create_autospec(BacPropagator) mock_init.return_value = mock_service cli.main() mock_service.start.assert_called_once()
def test_gitlab_publisher_with_reviewers_mapping( test_file_settings: OverhaveFileSettings, test_project_settings: OverhaveProjectSettings, test_gitlab_project_settings_with_reviewers_mapping: OverhaveGitlabPublisherSettings, mocked_file_manager: FileManager, mocked_gitlab_client: GitlabHttpClient, mocker: MockFixture, ) -> GitlabVersionPublisher: return GitlabVersionPublisher( file_settings=test_file_settings, project_settings=test_project_settings, feature_storage=mocker.create_autospec(IFeatureStorage), scenario_storage=mocker.create_autospec(IScenarioStorage), test_run_storage=mocker.create_autospec(ITestRunStorage), draft_storage=mocker.create_autospec(IDraftStorage), file_manager=mocked_file_manager, gitlab_publisher_settings=test_gitlab_project_settings_with_reviewers_mapping, client=mocked_gitlab_client, )
def test_stash_publisher_with_default_reviewers( test_file_settings: OverhaveFileSettings, test_project_settings: OverhaveProjectSettings, test_stash_publisher_settings_with_default_reviewers: OverhaveStashPublisherSettings, mocked_file_manager: FileManager, mocked_stash_client: StashHttpClient, mocker: MockFixture, ) -> StashVersionPublisher: return StashVersionPublisher( file_settings=test_file_settings, project_settings=test_project_settings, feature_storage=mocker.create_autospec(IFeatureStorage), scenario_storage=mocker.create_autospec(IScenarioStorage), test_run_storage=mocker.create_autospec(ITestRunStorage), draft_storage=mocker.create_autospec(IDraftStorage), file_manager=mocked_file_manager, stash_publisher_settings= test_stash_publisher_settings_with_default_reviewers, client=mocked_stash_client, )
def test_handle_weird_data(self, mocker: MockFixture, bacprop_service: BacPropagator) -> None: data = {"sensorId": 5, "something": 2, "badUnit": "2"} sensor = mocker.create_autospec(Sensor) sensor.has_fault.return_value = False # type: ignore bacprop_service._sensor_net.get_sensor.return_value = sensor # type: ignore bacprop_service._handle_sensor_data(data) sensor.set_values.assert_called_with({"something": 2})
def test_handle_new_data_faulty_sensor( self, mocker: MockFixture, bacprop_service: BacPropagator) -> None: data = {"sensorId": 5, "somethingNew": 2} sensor = mocker.create_autospec(Sensor) sensor.has_fault.return_value = True # type: ignore bacprop_service._sensor_net.get_sensor.return_value = sensor # type: ignore bacprop_service._handle_sensor_data(data) sensor.set_values.assert_called_with({"somethingNew": 2}) sensor.mark_ok.assert_called_once()
def test_handle_data_new_sensor(self, mocker: MockFixture, bacprop_service: BacPropagator) -> None: data = {"somethingElse": 0.2, "sensorId": 1} sensor = mocker.create_autospec(Sensor) sensor.has_fault.return_value = False # type: ignore bacprop_service._sensor_net.create_sensor.return_value = sensor # type: ignore bacprop_service._sensor_net.get_sensor.return_value = None # type: ignore bacprop_service._handle_sensor_data(data) bacprop_service._sensor_net.create_sensor.assert_called_with( 1) # type: ignore sensor.has_fault.assert_called_once() sensor.set_values.assert_called_with({"somethingElse": 0.2})
async def test_fault_checking(self, mocker: MockFixture, bacprop_service: BacPropagator) -> None: sensors = { 1: mocker.create_autospec(Sensor), 2: mocker.create_autospec(Sensor) } bacprop_service._sensor_net.get_sensors.return_value = sensors # type: ignore bacprop_service._running = True asyncio.ensure_future(bacprop_service._fault_check_loop()) await asyncio.sleep(0) bacprop_service._sensor_net.get_sensors.assert_called_once( ) # type: ignore sensors[1].has_fault.assert_called_once() sensors[2].has_fault.assert_called_once() # Finish the loop bacprop_service._running = False await asyncio.sleep(0)
def mocked_stash_client(mocker: MockFixture) -> StashHttpClient: return cast(StashHttpClient, mocker.create_autospec(StashHttpClient))
def mocked_gitlab_client(mocker: MockFixture) -> GitlabHttpClient: return cast(GitlabHttpClient, mocker.create_autospec(GitlabHttpClient))
def test_password(mocker: MockFixture, faker: Faker) -> PasswordField: field = mocker.create_autospec(PasswordField) field.data = faker.word() return field
def test_username(mocker: MockFixture, faker: Faker) -> StringField: field = mocker.create_autospec(StringField) field.data = faker.word() return field
def mocked_ldap_authenticator(mocker: MockFixture) -> LDAPAuthenticator: return cast(LDAPAuthenticator, mocker.create_autospec(LDAPAuthenticator))
def mocked_file_manager(mocker: MockFixture) -> FileManager: return cast(FileManager, mocker.create_autospec(FileManager))