Creating Artists
You have probably realized that our test doesn't actually assert that we are doing what we say we are doing. To do that we will need to check the response body
, and also check the expected data is in the database.
Our utility scripts connects to the database server
before and after the tests have run. Our app however only needs to connect to one specific database
within the server
.
For our app to connect to the database, we will define a db service
.
-
Create a new file
src/db/index.js
This exports an
async function
that we can use to connect to the database in other parts of our app.Now that we have a way to connect to the database within the app, we can modify our tests:
We are also going to want to delete all of the artists in our database after each test. To do this we can use a global afterEach hook
. To do this, create a new file in your tests
folder called helper.js
. Mocha will automatically detect this file and run the code before all of our other tests.
Our test suite has had a big jump in complexity, let's go over it to understand the role of the lines we added.
-
The
afterEach
hook will delete all the records in theArtists
table after each test has run, and close the database connection. -
The test will now query the database to see if there is an artist in the
Artists
table, and check it contains the data we expect.
Now if you run the tests, they will fail because we still need to...
Create the Artist Table
In order to write and read data from a table, that table needs to exist. Luckily we already have a script that connects to our database server
before our app runs.
We could alter the create-database.js
script to also create our tables, but in industry we use migrations to manage changes to the structure of our database.
Migrations are best thought of as a series of scripts which are run in a database exactly once. We run them when we want to set up a new database, or when we add a new feature to an existing database.
To make your first migration script, create a migrations
folder with a 01-create-artist-table.sql
file inside it. We will start each file with a number so we know which order to run them in.
Next we need code to actually run our migration scripts. For this project we will be using the postgres-migrations module. As we will require this in our production environment it should be saved as a dependency:
We will use this module in a script that we will run before our app starts, and before we run our tests. Create a new file in your scripts
folder called migrate.js
Next add a migrate
script to your package.json
, and then alter your prestart
and pretest
scripts so that they also run the migrate script like so:
If you run your application now you should seen an output like this:
The postgres-migrations
module has created a migrations
table to keep track of which scripts it has run, followed by our create-artist-table
script. If you use ctrl + c
to exit your app and run it again you will see that the migration script knows that all of our migrations have already run (it is idempotent
). You can also use pgAdmin4 to inspect your database and see that there is now a migrations
table.
Run your tests again and confirm that they fail for the correct reason.
Passing the Test
Now if we re-run the tests, they should fail because the data is not in the table. Can you use db.query()
to INSERT
a new artist into the Artist
table and return a 201
to the user?
To do this you will need to:
-
Require
db
fromsrc/db/index.js
at the top of your controller file:const db = require('../src/db/index.js');
-
Change your controller function declaration to use the
async
keyword. -
Use
db.query()
to send the correctSQL
to the database. -
Send a
201
status back to the user. Use atry/catch
block to send a500
if there is an error.
You can see the solution to this challenge here
Believe it or not, now that we have an app and test suite with a working database, we've done the most difficult part of this project.
From this point on we will just be adding additional routes, controllers and tests.
Commit and push ↗️ your progress to Github.
Run your app with npm start
and try adding some artists to your database using Postman. Use pgAdmin4
to confirm that they are being inserted into the Artist
table.