def test_default( self, vws_client: VWS, cloud_reco_client: CloudRecoService, high_quality_image: io.BytesIO, ) -> None: """ By default, target data is only returned in the top match. """ target_id = vws_client.add_target( name=uuid.uuid4().hex, width=1, image=high_quality_image, active_flag=True, application_metadata=None, ) target_id_2 = vws_client.add_target( name=uuid.uuid4().hex, width=1, image=high_quality_image, active_flag=True, application_metadata=None, ) vws_client.wait_for_target_processed(target_id=target_id) vws_client.wait_for_target_processed(target_id=target_id_2) top_match, second_match = cloud_reco_client.query( image=high_quality_image, max_num_results=2, ) assert top_match.target_data is not None assert second_match.target_data is None
def test_processing_images( self, image_file_success_state_low_rating: io.BytesIO, ) -> None: """ The number of images in the processing state is returned. """ database = VuforiaDatabase() vws_client = VWS( server_access_key=database.server_access_key, server_secret_key=database.server_secret_key, ) with MockVWS() as mock: mock.add_database(database=database) vws_client.add_target( name=uuid.uuid4().hex, width=1, image=image_file_success_state_low_rating, active_flag=True, application_metadata=None, ) _wait_for_image_numbers( vws_client=vws_client, active_images=0, inactive_images=0, failed_images=0, processing_images=1, )
def test_all( self, vws_client: VWS, cloud_reco_client: CloudRecoService, high_quality_image: io.BytesIO, ) -> None: """ When ``CloudRecoIncludeTargetData.ALL`` is given, target data is returned in all matches. """ target_id = vws_client.add_target( name=uuid.uuid4().hex, width=1, image=high_quality_image, active_flag=True, application_metadata=None, ) target_id_2 = vws_client.add_target( name=uuid.uuid4().hex, width=1, image=high_quality_image, active_flag=True, application_metadata=None, ) vws_client.wait_for_target_processed(target_id=target_id) vws_client.wait_for_target_processed(target_id=target_id_2) top_match, second_match = cloud_reco_client.query( image=high_quality_image, max_num_results=2, include_target_data=CloudRecoIncludeTargetData.ALL, ) assert top_match.target_data is not None assert second_match.target_data is not None
def test_default( self, vws_client: VWS, cloud_reco_client: CloudRecoService, high_quality_image: io.BytesIO, ) -> None: """ By default the maximum number of results is 1. """ target_id = vws_client.add_target( name=uuid.uuid4().hex, width=1, image=high_quality_image, active_flag=True, application_metadata=None, ) target_id_2 = vws_client.add_target( name=uuid.uuid4().hex, width=1, image=high_quality_image, active_flag=True, application_metadata=None, ) vws_client.wait_for_target_processed(target_id=target_id) vws_client.wait_for_target_processed(target_id=target_id_2) matches = cloud_reco_client.query(image=high_quality_image) assert len(matches) == 1
def test_bad_target_request( self, high_quality_image: io.BytesIO, vws_client: VWS, ) -> None: """ The ``request_usage`` count does not increase with each request to the target API, even if it is a bad request. """ report = vws_client.get_database_summary_report() original_request_usage = report.request_usage with pytest.raises(Fail) as exc: vws_client.add_target( name='example', width=-1, image=high_quality_image, active_flag=True, application_metadata=None, ) assert exc.value.response.status_code == HTTPStatus.BAD_REQUEST report = vws_client.get_database_summary_report() new_request_usage = report.request_usage assert new_request_usage == original_request_usage
def test_authentication_failure(high_quality_image: io.BytesIO) -> None: """ An ``AuthenticationFailure`` exception is raised when the server access key exists but the server secret key is incorrect, or when a client key is incorrect. """ database = VuforiaDatabase() vws_client = VWS( server_access_key=database.server_access_key, server_secret_key='a', ) with MockVWS() as mock: mock.add_database(database=database) with pytest.raises(AuthenticationFailure) as exc: vws_client.add_target( name='x', width=1, image=high_quality_image, active_flag=True, application_metadata=None, ) assert exc.value.response.status_code == HTTPStatus.UNAUTHORIZED
def test_get_duplicate_targets( self, vws_client: VWS, high_quality_image: io.BytesIO, ) -> None: """ It is possible to get the IDs of similar targets. """ target_id = vws_client.add_target( name='x', width=1, image=high_quality_image, active_flag=True, application_metadata=None, ) similar_target_id = vws_client.add_target( name='a', width=1, image=high_quality_image, active_flag=True, application_metadata=None, ) vws_client.wait_for_target_processed(target_id=target_id) vws_client.wait_for_target_processed(target_id=similar_target_id) duplicates = vws_client.get_duplicate_targets(target_id=target_id) assert duplicates == [similar_target_id]
def test_target_name_exist( vws_client: VWS, high_quality_image: io.BytesIO, ) -> None: """ A ``TargetNameExist`` exception is raised after adding two targets with the same name. """ vws_client.add_target( name='x', width=1, image=high_quality_image, active_flag=True, application_metadata=None, ) with pytest.raises(TargetNameExist) as exc: vws_client.add_target( name='x', width=1, image=high_quality_image, active_flag=True, application_metadata=None, ) assert exc.value.response.status_code == HTTPStatus.FORBIDDEN assert exc.value.target_name == 'x'
def test_status( self, image_file_failed_state: io.BytesIO, vws_client: VWS, ) -> None: """ Targets are not duplicates if the status is not 'success'. """ original_target_id = vws_client.add_target( name=uuid.uuid4().hex, width=1, image=image_file_failed_state, active_flag=True, application_metadata=None, ) similar_target_id = vws_client.add_target( name=uuid.uuid4().hex, width=1, image=image_file_failed_state, active_flag=True, application_metadata=None, ) vws_client.wait_for_target_processed(target_id=original_target_id) vws_client.wait_for_target_processed(target_id=similar_target_id) target_details = vws_client.get_target_record( target_id=original_target_id, ) assert target_details.status == TargetStatuses.FAILED duplicates = vws_client.get_duplicate_targets( target_id=original_target_id, ) assert duplicates == []
def test_to_dict(self, high_quality_image: io.BytesIO) -> None: """ It is possible to dump a database to a dictionary and load it back. """ database = VuforiaDatabase() vws_client = VWS( server_access_key=database.server_access_key, server_secret_key=database.server_secret_key, ) # We test a database with a target added. with MockVWS() as mock: mock.add_database(database=database) vws_client.add_target( name='example', width=1, image=high_quality_image, active_flag=True, application_metadata=None, ) database_dict = database.to_dict() # The dictionary is JSON dump-able assert json.dumps(database_dict) new_database = VuforiaDatabase.from_dict(database_dict=database_dict) assert new_database == database
def test_custom( self, vws_client: VWS, high_quality_image: io.BytesIO, tmp_path: Path, mock_database: VuforiaDatabase, ) -> None: """ It is possible to set a custom ``--max-num-results``. """ runner = CliRunner(mix_stderr=False) target_id = vws_client.add_target( name=uuid.uuid4().hex, width=1, image=high_quality_image, active_flag=True, application_metadata=None, ) target_id_2 = vws_client.add_target( name=uuid.uuid4().hex, width=1, image=high_quality_image, active_flag=True, application_metadata=None, ) target_id_3 = vws_client.add_target( name=uuid.uuid4().hex, width=1, image=high_quality_image, active_flag=True, application_metadata=None, ) vws_client.wait_for_target_processed(target_id=target_id) vws_client.wait_for_target_processed(target_id=target_id_2) vws_client.wait_for_target_processed(target_id=target_id_3) new_file = tmp_path / uuid.uuid4().hex image_data = high_quality_image.getvalue() new_file.write_bytes(data=image_data) commands = [ str(new_file), '--max-num-results', str(2), '--client-access-key', mock_database.client_access_key, '--client-secret-key', mock_database.client_secret_key, ] result = runner.invoke( vuforia_cloud_reco, commands, catch_exceptions=False, ) assert result.exit_code == 0 result_data = yaml.load(result.stdout, Loader=yaml.FullLoader) assert len(result_data) == 2
def test_all( self, vws_client: VWS, high_quality_image: io.BytesIO, tmp_path: Path, mock_database: VuforiaDatabase, ) -> None: """ When 'all' is given, target data is returned in all matches. """ runner = CliRunner(mix_stderr=False) target_id = vws_client.add_target( name=uuid.uuid4().hex, width=1, image=high_quality_image, active_flag=True, application_metadata=None, ) target_id_2 = vws_client.add_target( name=uuid.uuid4().hex, width=1, image=high_quality_image, active_flag=True, application_metadata=None, ) vws_client.wait_for_target_processed(target_id=target_id) vws_client.wait_for_target_processed(target_id=target_id_2) new_file = tmp_path / uuid.uuid4().hex image_data = high_quality_image.getvalue() new_file.write_bytes(data=image_data) commands = [ str(new_file), '--max-num-results', str(2), '--include-target-data', 'all', '--client-access-key', mock_database.client_access_key, '--client-secret-key', mock_database.client_secret_key, ] result = runner.invoke( vuforia_cloud_reco, commands, catch_exceptions=False, ) assert result.exit_code == 0 matches = yaml.load(result.stdout, Loader=yaml.FullLoader) top_match, second_match = matches assert top_match['target_data'] is not None assert second_match['target_data'] is not None
def test_add_bad_name(vws_client: VWS, high_quality_image: io.BytesIO) -> None: """ When a name with a bad character is given, an ``UnknownVWSErrorPossiblyBadName`` exception is raised. """ max_char_value = 65535 bad_name = chr(max_char_value + 1) with pytest.raises(UnknownVWSErrorPossiblyBadName): vws_client.add_target( name=bad_name, width=1, image=high_quality_image, active_flag=True, application_metadata=None, )
def test_bad_image(vws_client: VWS) -> None: """ A ``BadImage`` exception is raised when a non-image is given. """ not_an_image = io.BytesIO(b'Not an image') with pytest.raises(BadImage) as exc: vws_client.add_target( name='x', width=1, image=not_an_image, active_flag=True, application_metadata=None, ) assert exc.value.response.status_code == HTTPStatus.UNPROCESSABLE_ENTITY
def test_default(self, image_file_failed_state: io.BytesIO) -> None: """ By default, targets in the mock take 0.5 seconds to be processed. """ database = VuforiaDatabase() vws_client = VWS( server_access_key=database.server_access_key, server_secret_key=database.server_secret_key, ) with MockVWS() as mock: mock.add_database(database=database) target_id = vws_client.add_target( name='example', width=1, image=image_file_failed_state, active_flag=True, application_metadata=None, ) start_time = datetime.now() while True: target_details = vws_client.get_target_record( target_id=target_id, ) status = target_details.status if status != TargetStatuses.PROCESSING: elapsed_time = datetime.now() - start_time # There is a race condition in this test - if it starts to # fail, maybe extend the acceptable range. assert elapsed_time < timedelta(seconds=0.55) assert elapsed_time > timedelta(seconds=0.49) return
def test_custom(self, image_file_failed_state: io.BytesIO) -> None: """ It is possible to set a custom processing time. """ database = VuforiaDatabase() vws_client = VWS( server_access_key=database.server_access_key, server_secret_key=database.server_secret_key, ) with MockVWS(processing_time_seconds=0.1) as mock: mock.add_database(database=database) target_id = vws_client.add_target( name='example', width=1, image=image_file_failed_state, active_flag=True, application_metadata=None, ) start_time = datetime.now() while True: target_details = vws_client.get_target_record( target_id=target_id, ) status = target_details.status if status != TargetStatuses.PROCESSING: elapsed_time = datetime.now() - start_time assert elapsed_time < timedelta(seconds=0.15) assert elapsed_time > timedelta(seconds=0.09) return
def test_success_status( self, image_file_success_state_low_rating: io.BytesIO, vws_client: VWS, ) -> None: """ When a random, large enough image is given, the status changes from 'processing' to 'success' after some time. The mock is much more lenient than the real implementation of VWS. The test image does not prove that what is counted as a success in the mock will be counted as a success in the real implementation. """ target_id = vws_client.add_target( name='example', width=1, image=image_file_success_state_low_rating, active_flag=True, application_metadata=None, ) vws_client.wait_for_target_processed(target_id=target_id) target_details = vws_client.get_target_record(target_id=target_id) assert target_details.status == TargetStatuses.SUCCESS # Tracking rating is between 0 and 5 when status is 'success' tracking_rating = target_details.target_record.tracking_rating assert tracking_rating in range(6) # The tracking rating stays stable across requests target_details = vws_client.get_target_record(target_id=target_id) new_tracking_rating = target_details.target_record.tracking_rating assert new_tracking_rating == tracking_rating
def test_empty_content_type( self, vuforia_database: VuforiaDatabase, vws_client: VWS, image_file_failed_state: io.BytesIO, ) -> None: """ An ``UNAUTHORIZED`` response is given if an empty ``Content-Type`` header is given. """ target_id = vws_client.add_target( name='example', width=1, image=image_file_failed_state, active_flag=True, application_metadata=None, ) response = update_target( vuforia_database=vuforia_database, data={'name': 'Adam'}, target_id=target_id, content_type='', ) assert_vws_failure( response=response, status_code=HTTPStatus.UNAUTHORIZED, result_code=ResultCodes.AUTHENTICATION_FAILURE, )
def processing_time_seconds( vuforia_database: VuforiaDatabase, image: io.BytesIO, ) -> float: """ Return the time taken to process a target in the database. """ vws_client = VWS( server_access_key=vuforia_database.server_access_key, server_secret_key=vuforia_database.server_secret_key, ) target_id = vws_client.add_target( name='example', width=1, image=image, active_flag=True, application_metadata=None, ) start_time = datetime.now() while (vws_client.get_target_record( target_id=target_id).status == TargetStatuses.PROCESSING): pass return (datetime.now() - start_time).total_seconds()
def test_request_time_too_skewed( vws_client: VWS, high_quality_image: io.BytesIO, ) -> None: """ A ``RequestTimeTooSkewed`` exception is raised when the request time is more than five minutes different from the server time. """ target_id = vws_client.add_target( name='x', width=1, image=high_quality_image, active_flag=True, application_metadata=None, ) vws_max_time_skew = 60 * 5 leeway = 10 time_difference_from_now = vws_max_time_skew + leeway # We use a custom tick because we expect the following: # # * At least one time check when creating the request # * At least one time check when processing the request # # >= 1 ticks are acceptable. with freeze_time(auto_tick_seconds=time_difference_from_now): with pytest.raises(RequestTimeTooSkewed) as exc: vws_client.get_target_record(target_id=target_id) assert exc.value.response.status_code == HTTPStatus.FORBIDDEN
def test_recognition( self, vws_client: VWS, cloud_reco_client: CloudRecoService, high_quality_image: io.BytesIO, ) -> None: """ The recognition counts stay at 0 even after recognitions. """ target_id = vws_client.add_target( name='example', width=1, image=high_quality_image, active_flag=True, application_metadata=None, ) vws_client.wait_for_target_processed(target_id=target_id) results = cloud_reco_client.query(image=high_quality_image) [result] = results assert result.target_id == target_id report = vws_client.get_target_summary_report(target_id=target_id) assert report.status == TargetStatuses.SUCCESS assert report.total_recos == 0 assert report.current_month_recos == 0 assert report.previous_month_recos == 0
def test_get_target_summary_report( self, vws_client: VWS, high_quality_image: io.BytesIO, ) -> None: """ Details of a target are returned by ``get_target_summary_report``. """ date = '2018-04-25' target_name = uuid.uuid4().hex with freeze_time(date): target_id = vws_client.add_target( name=target_name, width=1, image=high_quality_image, active_flag=True, application_metadata=None, ) result = vws_client.get_target_summary_report(target_id=target_id) expected_report = TargetSummaryReport( status=TargetStatuses.SUCCESS, database_name=result.database_name, target_name=target_name, upload_date=datetime.date(2018, 4, 25), active_flag=True, tracking_rating=result.tracking_rating, total_recos=0, current_month_recos=0, previous_month_recos=0, ) assert result == expected_report
def test_to_dict_deleted(self, high_quality_image: io.BytesIO) -> None: """ It is possible to dump a deleted target to a dictionary and load it back. """ database = VuforiaDatabase() vws_client = VWS( server_access_key=database.server_access_key, server_secret_key=database.server_secret_key, ) with MockVWS() as mock: mock.add_database(database=database) target_id = vws_client.add_target( name='example', width=1, image=high_quality_image, active_flag=True, application_metadata=None, ) vws_client.wait_for_target_processed(target_id=target_id) vws_client.delete_target(target_id=target_id) (target, ) = database.targets target_dict = target.to_dict() # The dictionary is JSON dump-able assert json.dumps(target_dict) new_target = Target.from_dict(target_dict=target_dict) assert new_target.delete_date == target.delete_date
def test_get_target_record( self, vws_client: VWS, high_quality_image: io.BytesIO, ) -> None: """ Details of a target are returned by ``get_target_record``. """ target_id = vws_client.add_target( name='x', width=1, image=high_quality_image, active_flag=True, application_metadata=None, ) result = vws_client.get_target_record(target_id=target_id) expected_target_record = TargetRecord( target_id=target_id, active_flag=True, name='x', width=1, tracking_rating=-1, reco_rating='', ) assert result.target_record == expected_target_record assert result.status == TargetStatuses.PROCESSING
def test_active_flag( self, vws_client: VWS, image_file_success_state_low_rating: io.BytesIO, initial_active_flag: bool, desired_active_flag: bool, ) -> None: """ Setting the active flag to a Boolean value changes it. """ target_id = vws_client.add_target( name=uuid.uuid4().hex, width=1, image=image_file_success_state_low_rating, active_flag=initial_active_flag, application_metadata=None, ) vws_client.wait_for_target_processed(target_id=target_id) vws_client.update_target( target_id=target_id, active_flag=desired_active_flag, ) target_details = vws_client.get_target_record(target_id=target_id) assert target_details.target_record.active_flag == desired_active_flag
def test_no_fields_given( self, mock_database: VuforiaDatabase, vws_client: VWS, high_quality_image: io.BytesIO, ) -> None: """ It is possible to give no update fields. """ runner = CliRunner(mix_stderr=False) old_name = uuid.uuid4().hex old_width = random.uniform(a=0.01, b=50) target_id = vws_client.add_target( name=old_name, width=old_width, image=high_quality_image, active_flag=True, application_metadata=None, ) vws_client.wait_for_target_processed(target_id=target_id) commands = [ 'update-target', '--target-id', target_id, '--server-access-key', mock_database.server_access_key, '--server-secret-key', mock_database.server_secret_key, ] result = runner.invoke(vws_group, commands, catch_exceptions=False) assert result.exit_code == 0 assert result.stdout == ''
def test_custom_base_url(self, high_quality_image: io.BytesIO) -> None: """ It is possible to use query a target to a database under a custom VWQ URL. """ base_vwq_url = 'http://example.com' with MockVWS(base_vwq_url=base_vwq_url) as mock: database = VuforiaDatabase() mock.add_database(database=database) vws_client = VWS( server_access_key=database.server_access_key, server_secret_key=database.server_secret_key, ) target_id = vws_client.add_target( name='x', width=1, image=high_quality_image, active_flag=True, application_metadata=None, ) vws_client.wait_for_target_processed(target_id=target_id) cloud_reco_client = CloudRecoService( client_access_key=database.client_access_key, client_secret_key=database.client_secret_key, base_vwq_url=base_vwq_url, ) matches = cloud_reco_client.query(image=high_quality_image) assert len(matches) == 1 match = matches[0] assert match.target_id == target_id
def test_same_name_given( self, image_file_success_state_low_rating: io.BytesIO, vuforia_database: VuforiaDatabase, vws_client: VWS, ) -> None: """ Updating a target with its own name does not give an error. """ name = 'example' target_id = vws_client.add_target( name=name, width=1, image=image_file_success_state_low_rating, active_flag=True, application_metadata=None, ) vws_client.wait_for_target_processed(target_id=target_id) response = update_target( vuforia_database=vuforia_database, data={'name': name}, target_id=target_id, ) assert_vws_failure( response=response, status_code=HTTPStatus.OK, result_code=ResultCodes.SUCCESS, ) target_details = vws_client.get_target_record(target_id=target_id) assert target_details.target_record.name == name
def test_target_status_not_success( vws_client: VWS, high_quality_image: io.BytesIO, mock_database: VuforiaDatabase, ) -> None: """ An error is given when updating a target which has a status which is not "Success". """ runner = CliRunner(mix_stderr=False) target_id = vws_client.add_target( name='x', width=1, image=high_quality_image, active_flag=True, application_metadata=None, ) commands = [ 'update-target', '--target-id', target_id, '--server-access-key', mock_database.server_access_key, '--server-secret-key', mock_database.server_secret_key, ] result = runner.invoke(vws_group, commands, catch_exceptions=False) assert result.exit_code == 1 expected_stderr = ( f'Error: The target "{target_id}" cannot be updated as it is in the ' 'processing state.\n') assert result.stderr == expected_stderr assert result.stdout == ''
def test_content_types( self, vuforia_database: VuforiaDatabase, vws_client: VWS, image_file_failed_state: io.BytesIO, content_type: str, ) -> None: """ The ``Content-Type`` header does not change the response as long as it is not empty. """ target_id = vws_client.add_target( name='example', width=1, image=image_file_failed_state, active_flag=True, application_metadata=None, ) response = update_target( vuforia_database=vuforia_database, data={'name': 'Adam'}, target_id=target_id, content_type=content_type, ) # Code is FORBIDDEN because the target is processing. assert_vws_failure( response=response, status_code=HTTPStatus.FORBIDDEN, result_code=ResultCodes.TARGET_STATUS_NOT_SUCCESS, )