def setUp(self): self.image_path = get_file_path("test_jpg.jpg") self.img_bytes = azure_services.get_file_bytes(self.image_path) self.ta = azure_services.TextAnalyser( image_data=self.img_bytes, subscription_key=settings.OCR_SUBSCRIPTION_KEY, api_url=settings.OCR_API_URL)
def test_none_returned_when_input_is_none(self): ta = azure_services.TextAnalyser( image_data=self.img_bytes, subscription_key=settings.OCR_SUBSCRIPTION_KEY, api_url=settings.OCR_API_URL) self.assertIsNone(ta.get_results(None))
def test_invalid_api_returns_none(self): ta = azure_services.TextAnalyser( image_data=self.img_bytes, subscription_key=settings.OCR_SUBSCRIPTION_KEY, api_url="invalid/api/url") response = ta.submit_image_for_processing() self.assertIsNone(response)
def test_bad_subscription_key_returns_none(self): """ Check that analyse_image returns None when prediction_key == None """ ta = azure_services.TextAnalyser(image_data=self.img_bytes, subscription_key="invalid_subs_key", api_url=settings.OCR_API_URL) response = ta.submit_image_for_processing() self.assertIsNone(response)
def test_non_image_input_returns_none(self): txt_bytes = azure_services.get_file_bytes( get_file_path("test_file.txt")) ta = azure_services.TextAnalyser( image_data=txt_bytes, subscription_key=settings.OCR_SUBSCRIPTION_KEY, api_url=settings.OCR_API_URL) response = ta.submit_image_for_processing() self.assertIsNone(response)
def test_successful_reponse_returned_with_correct_parameters(self): ta = azure_services.TextAnalyser( image_data=self.img_bytes, subscription_key=settings.OCR_SUBSCRIPTION_KEY, api_url=settings.OCR_API_URL) response = ta.submit_image_for_processing() # From the docs, a successful response will have status code 202: # https://westcentralus.dev.cognitive.microsoft.com/docs/services/computer-vision-v3-ga/operations/5d986960601faab4bf452005 self.assertEqual(response.status_code, 202)
def test_correct_results_returned_from_correct_input(self): ta = azure_services.TextAnalyser( image_data=self.img_bytes, subscription_key=settings.OCR_SUBSCRIPTION_KEY, api_url=settings.OCR_API_URL) response = ta.submit_image_for_processing() self.assertEqual( ta.get_results(response)["analyzeResult"], RESULTS.OCR_RESULTS["analyzeResult"])
def test_none_returned_with_wrong_response(self): """ Input is a valid response object, but not from the Read API """ ta = azure_services.TextAnalyser( image_data=self.img_bytes, subscription_key=settings.OCR_SUBSCRIPTION_KEY, api_url=settings.OCR_API_URL) response = requests.get("https://api.github.com") self.assertIsNone(ta.get_results(response))
def test_too_large_image_returns_none(self): """ Check that analyse_image returns None when image_path == None """ bmp_bytes = azure_services.get_file_bytes( get_file_path("test_bmp.bmp")) ta = azure_services.TextAnalyser( image_data=bmp_bytes, subscription_key=settings.OCR_SUBSCRIPTION_KEY, api_url=settings.OCR_API_URL) response = ta.submit_image_for_processing() self.assertIsNone(response)
def test_correct_results_returned_from_valid_input(self): """ Check that analyse_image returns the correct results when provided the correct arguments """ ta = azure_services.TextAnalyser( image_data=self.img_bytes, subscription_key=settings.OCR_SUBSCRIPTION_KEY, api_url=settings.OCR_API_URL) results = ta.analyse_image() expected_results = RESULTS.OCR_RESULTS self.assertDictEqual(results["analyzeResult"], expected_results["analyzeResult"])
def setUp(self): self.image_path = get_file_path("test_jpg.jpg") self.img_bytes = azure_services.get_file_bytes(self.image_path) self.ta = azure_services.TextAnalyser( image_data=self.img_bytes, subscription_key=settings.OCR_SUBSCRIPTION_KEY, api_url=settings.OCR_API_URL) # This took a long time to puzzle out... # If I assign without copy.deepcopy(), the changes that the tests # make on the 'status' key are carried through to RESULTS.OCR_RESULTS; # and are *NOT* reset for the next test!! # Particularly important when 'status' is popped... self.results = copy.deepcopy(RESULTS.OCR_RESULTS)
def test_none_returned_with_missing_key(self): """ Input is a valid response object from the Read API, but the value for the "Operation-Location" key is missing from the header """ ta = azure_services.TextAnalyser( image_data=self.img_bytes, subscription_key=settings.OCR_SUBSCRIPTION_KEY, api_url=settings.OCR_API_URL) response = ta.submit_image_for_processing() response.headers["Operation-Location"] = None self.assertIsNone(ta.get_results(response))
def test_use_words_false_extracts_correct_lines(self): # create a new ta object for the file we want to process image_path = get_file_path("lines_of_words.jpg") img_bytes = azure_services.get_file_bytes(image_path) ta = azure_services.TextAnalyser( image_data=img_bytes, subscription_key=settings.OCR_SUBSCRIPTION_KEY, api_url=settings.OCR_API_URL, use_words=False) # do the test test_input = RESULTS.OCR_TEXT_IN_LINES expected_results = RESULTS.PROCESSED_OCR_TEXT_IN_LINES actual_results = ta.process_output(test_input) self.assertListEqual(expected_results, actual_results)
def test_none_returned_with_malformed_url(self): """ Input is a valid response object from the Read API, but the value for the "Operation-Location" key is missing from the header """ ta = azure_services.TextAnalyser( image_data=self.img_bytes, subscription_key=settings.OCR_SUBSCRIPTION_KEY, api_url=settings.OCR_API_URL) response = ta.submit_image_for_processing() # get the correct URL and slice off the last digit new_url = response.headers["Operation-Location"][:-1] # set the 'Operation-Location' header to be this new URL response.headers["Operation-Location"] = new_url self.assertIsNone(ta.get_results(response))