| 
									
										
										
										
											2022-02-17 15:30:48 -03:30
										 |  |  | """Unit tests for textract-supported APIs.""" | 
					
						
							| 
									
										
										
										
											2022-02-21 11:43:36 -03:30
										 |  |  | from random import randint | 
					
						
							|  |  |  | from botocore.exceptions import ClientError, ParamValidationError | 
					
						
							|  |  |  | import pytest | 
					
						
							| 
									
										
										
										
											2022-02-17 15:30:48 -03:30
										 |  |  | import boto3 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-21 14:26:50 -03:30
										 |  |  | from unittest import SkipTest | 
					
						
							| 
									
										
										
										
											2022-02-21 11:43:36 -03:30
										 |  |  | from moto.textract.models import TextractBackend | 
					
						
							| 
									
										
										
										
											2022-02-21 14:26:50 -03:30
										 |  |  | from moto import settings, mock_textract | 
					
						
							| 
									
										
										
										
											2022-02-17 15:30:48 -03:30
										 |  |  | 
 | 
					
						
							|  |  |  | # See our Development Tips on writing tests for hints on how to write good tests: | 
					
						
							|  |  |  | # http://docs.getmoto.org/en/latest/docs/contributing/development_tips/tests.html | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-21 11:43:36 -03:30
										 |  |  | @mock_textract | 
					
						
							|  |  |  | def test_get_document_text_detection(): | 
					
						
							| 
									
										
										
										
											2022-02-21 14:26:50 -03:30
										 |  |  |     if settings.TEST_SERVER_MODE: | 
					
						
							|  |  |  |         raise SkipTest("Cannot set textract backend values in server mode") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-21 11:43:36 -03:30
										 |  |  |     TextractBackend.JOB_STATUS = "SUCCEEDED" | 
					
						
							|  |  |  |     TextractBackend.PAGES = randint(5, 500) | 
					
						
							|  |  |  |     TextractBackend.BLOCKS = [ | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             "Text": "This is a test", | 
					
						
							|  |  |  |             "Id": "0", | 
					
						
							|  |  |  |             "Confidence": "100", | 
					
						
							|  |  |  |             "Geometry": { | 
					
						
							|  |  |  |                 "BoundingBox": { | 
					
						
							|  |  |  |                     "Width": "0.5", | 
					
						
							|  |  |  |                     "Height": "0.5", | 
					
						
							|  |  |  |                     "Left": "0.5", | 
					
						
							|  |  |  |                     "Top": "0.5", | 
					
						
							|  |  |  |                 }, | 
					
						
							|  |  |  |             }, | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     ] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     client = boto3.client("textract", region_name="us-east-1") | 
					
						
							|  |  |  |     job = client.start_document_text_detection( | 
					
						
							| 
									
										
										
										
											2022-03-10 13:39:59 -01:00
										 |  |  |         DocumentLocation={"S3Object": {"Bucket": "bucket", "Name": "name"}} | 
					
						
							| 
									
										
										
										
											2022-02-21 11:43:36 -03:30
										 |  |  |     ) | 
					
						
							| 
									
										
										
										
											2022-02-17 15:30:48 -03:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-21 11:43:36 -03:30
										 |  |  |     resp = client.get_document_text_detection(JobId=job["JobId"]) | 
					
						
							| 
									
										
										
										
											2022-02-17 15:30:48 -03:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-09 17:01:29 +00:00
										 |  |  |     assert resp["Blocks"][0]["Text"] == "This is a test" | 
					
						
							|  |  |  |     assert resp["Blocks"][0]["Id"] == "0" | 
					
						
							|  |  |  |     assert resp["Blocks"][0]["Confidence"] == "100" | 
					
						
							|  |  |  |     assert resp["Blocks"][0]["Geometry"]["BoundingBox"]["Width"] == "0.5" | 
					
						
							|  |  |  |     assert resp["Blocks"][0]["Geometry"]["BoundingBox"]["Height"] == "0.5" | 
					
						
							|  |  |  |     assert resp["Blocks"][0]["Geometry"]["BoundingBox"]["Left"] == "0.5" | 
					
						
							|  |  |  |     assert resp["Blocks"][0]["Geometry"]["BoundingBox"]["Top"] == "0.5" | 
					
						
							|  |  |  |     assert resp["JobStatus"] == "SUCCEEDED" | 
					
						
							|  |  |  |     assert resp["DocumentMetadata"]["Pages"] == TextractBackend.PAGES | 
					
						
							| 
									
										
										
										
											2022-02-17 15:30:48 -03:30
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @mock_textract | 
					
						
							|  |  |  | def test_start_document_text_detection(): | 
					
						
							| 
									
										
										
										
											2022-02-17 16:01:40 -03:30
										 |  |  |     client = boto3.client("textract", region_name="us-east-1") | 
					
						
							| 
									
										
										
										
											2022-02-17 15:30:48 -03:30
										 |  |  |     resp = client.start_document_text_detection( | 
					
						
							| 
									
										
										
										
											2022-03-10 13:39:59 -01:00
										 |  |  |         DocumentLocation={"S3Object": {"Bucket": "bucket", "Name": "name"}} | 
					
						
							| 
									
										
										
										
											2022-02-17 15:30:48 -03:30
										 |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-09 17:01:29 +00:00
										 |  |  |     assert "JobId" in resp | 
					
						
							| 
									
										
										
										
											2022-02-21 11:43:36 -03:30
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @mock_textract | 
					
						
							|  |  |  | def test_get_document_text_detection_without_job_id(): | 
					
						
							|  |  |  |     client = boto3.client("textract", region_name="us-east-1") | 
					
						
							|  |  |  |     with pytest.raises(ClientError) as e: | 
					
						
							|  |  |  |         client.get_document_text_detection(JobId="Invalid Job Id") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-09 17:01:29 +00:00
										 |  |  |     assert e.value.response["Error"]["Code"] == "InvalidJobIdException" | 
					
						
							| 
									
										
										
										
											2022-02-21 11:43:36 -03:30
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @mock_textract | 
					
						
							|  |  |  | def test_get_document_text_detection_without_document_location(): | 
					
						
							|  |  |  |     client = boto3.client("textract", region_name="us-east-1") | 
					
						
							|  |  |  |     with pytest.raises(ParamValidationError) as e: | 
					
						
							|  |  |  |         client.start_document_text_detection() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     assert e.typename == "ParamValidationError" | 
					
						
							|  |  |  |     assert ( | 
					
						
							|  |  |  |         'Parameter validation failed:\nMissing required parameter in input: "DocumentLocation"' | 
					
						
							|  |  |  |         in e.value.args | 
					
						
							|  |  |  |     ) |