NotImplementedError: analyze_document方法尚未实现

huangapple go评论78阅读模式
英文:

NotImplementedError: The analyze_document has not been Implemented

问题

我正在尝试使用moto来模拟textract

我有lambda_function:

from textractcaller.t_call import call_textract, Textract_Features
def lambda_function(event,context)
    s3InputDocPath = "s3://test"
    jsonObject = call_textract(
         input_document = s3InputDocPath,
         features = [Textract_Features.FORMS, Textract_Features.Tables],
         job_done_polling_interval=5,
   )

textract.py

@pytest.fixtures
def callTextract()
  textract = boto3.client("textract", region_name="us-east-1")
  bucket = "inputBucket"
  textract.analyze_document(
           Document = {"S3Object":{"Bucket":bucket, "Name" = "test_doc.pdf"}},
           FeatureTypes=["TABLES", "FORMS"]
)

最后是我的测试文件:
test_lambda_function.py

@mock_textract
@mock_s3
def test_lambda_function(event, callTextract, s3_object)
    callTextract()
    s3_object()
    result = lambda_function(event, None)

但是textract调用没有被模拟
我得到以下错误

NotImplementedError: The analyze_document has not been Implemented

有人能帮忙吗?

英文:

I'm trying to mock textract using moto

I have lambda_function:

from textractcaller.t_call import call_textract, Textract_Features
def lambda_function(event,context)
    s3InputDocPath = "s3://test"
    jsonObject = call_textract(
         input_document = s3InputDocPath,
         features = [Textract_Features.FORMS, Textract_Features.Tables],
         job_done_polling_interval=5,
   )

textract.py

@pytest.fixtures
def callTextract()
  textract = boto3.client("textract", region_name="us-east-1")
  bucket = "inputBucket"
  textract.analyze_document(
           Document = {"S3Object":{"Bucket":bucket, "Name" = "test_doc.pdf"}},
           FeatureTypes=["TABLES", "FORMS"]
)

finally my testfile:
test_lambda_function.py

@mock_textract
@mock_s3
def test_lambda_function(event, callTextract, s3_object)
    callTextract()
    s3_object()
    result = lambda_function(event, None)

But textract calls are not getting mocked
I'm getting below error

NotImplementedError: The analyze_document has not been Implemented

Anyone can help please?

答案1

得分: 1

你可以像这样修补不受支持的功能:

import boto3
import botocore
from unittest.mock import patch

orig = botocore.client.BaseClient._make_api_call

def mock_make_api_call(self, operation_name, kwarg):
    if operation_name == 'AnalyzeDocument':
        # 进行您期望在此操作期间发生的任何更改
        return {
            "expected": "response",
            "for this": "operation"
        }
    # 如果不想修补API调用,调用原始API
    return orig(self, operation_name, kwarg)


@mock_textract
@mock_s3
def test_lambda_function(event, callTextract, s3_object)

    with patch('botocore.client.BaseClient._make_api_call', new=mock_make_api_call):
        callTextract()
    s3_object()
    result = lambda_function(event, None)

这允许您完全自定义boto3的行为,根据每个方法的需求。

这段代码摘自Moto文档,并经过修改以适应您的用例。您可以在这里找到原始文档:http://docs.getmoto.org/en/latest/docs/services/patching_other_services.html

英文:

You can patch unsupported features like this:

import boto3
import botocore
from unittest.mock import patch

orig = botocore.client.BaseClient._make_api_call

def mock_make_api_call(self, operation_name, kwarg):
    if operation_name == 'AnalyzeDocument':
        # Make whatever changes you expect to happen during this operation
        return {
            "expected": "response",
            "for this": "operation"
        }
    # If we don't want to patch the API call, call the original API
    return orig(self, operation_name, kwarg)


@mock_textract
@mock_s3
def test_lambda_function(event, callTextract, s3_object)

    with patch('botocore.client.BaseClient._make_api_call', new=mock_make_api_call):
        callTextract()
    s3_object()
    result = lambda_function(event, None)

This allows you to completely customize the boto3 behaviour, on a per-method basis.

Taken (and modified to fit your usecase) from the Moto documentation here: http://docs.getmoto.org/en/latest/docs/services/patching_other_services.html

huangapple
  • 本文由 发表于 2023年2月9日 02:32:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/75390275.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定