Skip to content

Commit

Permalink
improve ut coverage.
Browse files Browse the repository at this point in the history
  • Loading branch information
lkk12014402 committed Dec 12, 2024
1 parent 98227ea commit 6bf7210
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion tests/cores/mega/test_microservice.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from fastapi.testclient import TestClient

from comps import TextDoc, opea_microservices, register_microservice
from comps import TextDoc, opea_microservices, register_microservice, MicroService


@register_microservice(name="s1", host="0.0.0.0", port=8080, endpoint="/v1/add")
Expand All @@ -18,15 +18,21 @@ async def add(request: TextDoc) -> TextDoc:
text += "OPEA Project!"
return {"text": text}

def sum_test():
return 1 + 1

class TestMicroService(unittest.TestCase):
def setUp(self):
self.client = TestClient(opea_microservices["s1"].app)

opea_microservices["s1"].add_route("/v1/sum", sum_test, methods=["GET"])
self.process1 = multiprocessing.Process(target=opea_microservices["s1"].start, daemon=False, name="s1")

self.process1.start()

# test conflict port
self.assertRaises(RuntimeError, MicroService, name="s2", host="0.0.0.0", port=8080, endpoint="/v1/add")

def tearDown(self):
opea_microservices["s1"].stop()
self.process1.terminate()
Expand All @@ -38,6 +44,9 @@ def test_add_route(self):
response = self.client.get("/metrics")
self.assertEqual(response.status_code, 200)

response = self.client.get("/v1/sum")
self.assertEqual(response.json(), 2)


if __name__ == "__main__":
unittest.main()

0 comments on commit 6bf7210

Please sign in to comment.