04 - Basic CRUD with rust using tide - tests improvements
Hi all again, in the last note we refactor the code to persist the information in a relational database, postgresql in our case.
Let’s start where we left last time (ci bonus) and tie those loose ends in the ci.
Improving tests
First, one thing to improve in our tests is start using surf as client since is the client recommended by http-rs.
So, let’s add surf
as dev-dependency
|
|
And then in our code, let’s use surf as client en each test
|
|
Great, let’s run the tests…
|
|
Awesome! But at this point we are only asserting the status code. Let’s check now also the returned payload, for that we will use the crate assert-json-diff
that add two macros:
assert_json_eq
: macro used to compare two JSON values for an exact match.assert_json_include
: macro used to compare two JSON values for an inclusive match.
For example, add this lines to the get_dino
test
|
|
Let’s run the tests again ones we add the json asserts
…
|
|
Great! now we are also validating the returned payload.
We have a couple more of TODOs
before finish the improvements. First, we need to clear the dinos
table before run each test since we always want to create an isolated test case. To accomplish that, let’s create a module (mod
) in our main file for the tests and add a helper function to clear the dinos
table.
|
|
And in each test we need to run clear_dinos
before make any change/request.
|
|
Great, so one more task to go. We need to set the ci
(gh actions) to run the tests. For that we set a new block in ci.yml
to run those tests
|
|
And we are ready to create a new PR with this test improvements and check if all the steps works as expected
Nice! we now have the ci configured.
Beyond the happy path
Until now our test only check the happy path and we are not testing errors. Let’s add some basic test cases for cover those
- Create a duplicate dino with an existing key, should return 409
We need to handler the insert error and return the appropriated error since using the ?
here will bubble the error to the caller.
|
|
- Get/Delete/Update dino with a non existing key, should return 404
In this cases we only need to send an invalid key ( e.g. a new one and should works )
|
|
Upgrade bonus
Also, this week a new version of tide was released with a new way to start the servers
Tide v0.15.0 introduces a new way to start servers: Server::bind. This enables separating “open the socket” from “start accepting connections” which Server::listen does for you in a single call.
Let’s update our code to use this new version, first the deps in cargo
|
|
And in our code the main function now looks like this
|
|
That’s all for today, I was planned to add the implementation of tera
as render engine but will cover that in the next note
and keep this one focused in tests.
As always, I write this as a learning journal and there could be another more elegant and correct way to do it and any feedback is welcome.
Thanks!
Author Javier Viola
LastMod 2020-11-20