BentoML icon indicating copy to clipboard operation
BentoML copied to clipboard

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

Open smidm opened this issue 1 year ago • 3 comments

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

smidm avatar Aug 06 '24 20:08 smidm

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

frostming avatar Aug 07 '24 09:08 frostming

I'm mocking following:

  • package pose_service
  • module pose
  • @bentoml.service MMPoseModel
  • @bentoml.api endpoint pose
    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)

smidm avatar Aug 27 '24 09:08 smidm

service = poseservice()

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

frostming avatar Aug 27 '24 13:08 frostming