Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
smidm opened this issue Aug 6, 2024 · 3 comments
Open

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

smidm opened this issue Aug 6, 2024 · 3 comments
Assignees
Labels
enhancement Enhancement proposals

Comments

@smidm
Copy link
Contributor

smidm commented Aug 6, 2024

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 smidm added the enhancement Enhancement proposals label Aug 6, 2024
@frostming
Copy link
Contributor

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

@smidm
Copy link
Contributor Author

smidm commented Aug 27, 2024

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)

@frostming
Copy link
Contributor

frostming commented Aug 27, 2024

service = poseservice()

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Enhancement proposals
Projects
None yet
Development

No branches or pull requests

2 participants