bentoml / BentoML

The easiest way to serve AI apps and models - Build reliable Inference APIs, LLM apps, Multi-model chains, RAG service, and much more!
https://bentoml.com
Apache License 2.0
6.98k stars 776 forks source link

feature: add to documentation how to mock services and api endpoints #4902

Open smidm opened 1 month ago

smidm commented 1 month ago

Feature request

I really appreciated https://docs.bentoml.com/en/latest/guides/testing.html#unit-tests. It would be helpful to include also docs on mocking the bentoml decorated API methods.

The api method defined at package.module.Service.api_endpoint is located after the application of @bentoml.service and @bentoml.api decorators at package.module.Service.inner.api_endpoint.func, which is not clear.

I use:

def test_sample(monkeypatch):
    def mock_func():
        return None

    monkeypatch.setattr('package.module.Service.inner.api_endpoint.func', mock_func)

Motivation

No response

Other

No response

frostming commented 1 month ago

It depends on how you test your API, can you show an example of testing function?

smidm commented 2 weeks ago

I'm mocking following:

    def mock_pose(self, image, persons):
        return [
            Person(
                top_left=person.top_left,
                bottom_right=person.bottom_right,
                detection_confidence=person.detection_confidence,
                keypoints=[],
            ) for person in persons
        ]

    # bypass the pose estimation and return the detection results
    monkeypatch.setattr('pose_service.pose.MMPoseModel.inner.pose.func', mock_pose)

    sample_image = Image.new('RGB', (200, 100))  # width, height
    service = poseservice()

    # force both
    img, rotation_ccw, persons = service.detect_pose_and_image_rotation(
        sample_image,
        rotation_ccw=90,
        bounding_box=BoundingBox(
            top_left=Coordinate(x=0.1, y=0.1),
            bottom_right=Coordinate(x=0.2, y=0.2),
        )
    )
    assert img.shape == (200, 100, 3)
    assert rotation_ccw == 90
    assert len(persons) == 1
    assert isinstance(persons[0], Person)
    assert persons[0].top_left == Coordinate(x=0.1, y=0.1)
    assert persons[0].bottom_right == Coordinate(x=0.2, y=0.2)
frostming commented 2 weeks ago
service = poseservice()

What is poseservice() and how does it interact with the mocked service?