10 A change, through both images
Read the tutorial. Build it yourself is the other way to read this part.
This walk goes through rahuldave/cheesecracker on GitHub. This part starts from the code at step-09. The code before this part
To look at this code on your machine, run these commands. The first one makes a clone, if this folder has none:
[ -d cheesecracker ] || git clone https://github.com/rahuldave/cheesecracker
cd cheesecracker
git fetch --quiet --tags --force origin
git switch --detach step-09A change of the code goes through both images. The dev image checks it, and the run image has to be built again.
just setupThe Cheese App needs to add two numbers. The endpoint /add/ takes x and y, and returns their sum. An integration
test calls it. Then every check of the project runs again, on this machine, in the dev image, and against a new run
image.
± See the change to src/api-service/api/service.py
± See the change to tests/integration/test_api.py
add_numbers(), at the end of service.py, answers /add/ with {"result": x + y}. test_add_endpoint(), at the
end of test_api.py, asks for /add/?x=10&y=20, and expects 30.
just checkThe checks pass on this machine. The integration tests are now eleven.
just docker-testThe same checks pass in the dev image.
The run image still has the old code, because an image is built once and does not change. Build it again, and start its server.
just docker-build run
just docker-serve
curl -s "localhost:9000/add/?x=10&y=20"The server in Docker answers {"result":30.0}.
just test-system -qThe system tests print 5 passed against the new run image.
just docker-stopWhat changed
src/api-service/api/service.py: the endpoint/add/. 6 lines added. The file- look at
return {"result": x + y}
- look at
tests/integration/test_api.py: the test of/add/. 6 lines added. The file- look at
response = client.get("/add/?x=10&y=20")
- look at
The diff of src/api-service/api/service.py
@@ -44,3 +44,9 @@ async def calculate_euclidean_distance(x: float = 1, y: float = 2):
"distance": result,
"message": "This is a very long line that exceeds 120 characters blah",
}
+
+
+@app.get("/add/")
+async def add_numbers(x: float = 0, y: float = 0):
+ """Add two numbers"""
+ return {"result": x + y}The diff of tests/integration/test_api.py
@@ -87,3 +87,9 @@ async def test_api_health_check():
assert response.status_code == 200
# API should respond quickly
assert response.elapsed.total_seconds() < 1 if hasattr(response, "elapsed") else True
+
+
+def test_add_endpoint():
+ response = client.get("/add/?x=10&y=20")
+ assert response.status_code == 200
+ assert response.json()["result"] == 30This part ends at the code at step-10. The code after this part