Write a test
First let's see if after the new change our tests still pass
Note
You can use either npm
or yarn
to run your tests.
Before running tests, make sure you executed npm install
or yarn install
npm test
You'll notice that 2 tests are failing now.
Open tests/db/structure.sql
, find the test for all views are present in api schema
and change it to
select * from check_test(
views_are('api', array['todos','comments'], 'tables present' ),
true,
'all views are present in api schema',
'tables present',
''
);
'comments'
item to the array. If you run the tests again they should be green.
OK, we've fixed the tests, but let's also add one test for our comments endpoint.
To be able to test, we need some sample data.
Open db/src/sample_data/reset.sql
Add this line after set client_min_messages to warning;
truncate data.comment restart identity cascade;
db/src/sample_data/data.sql
and add this line at the end
INSERT INTO data.comment(id,body,todo_id,user_id)
VALUES (1,'a comment',2,1);
Try this request, this is the request we want to run in our tests and make sure it works
curl --cookie cookies.txt http://localhost:8080/rest/comments
The output should look like
[{"id":1,"body":"a comment","todo_id":1,"user_id":1}]
Note
If you are wondering where all those previous comments that we added using curl went, well, they were deleted since we changed a file in db/src/..
and that triggered a database reset.
Now that we know what we want to test and the sample data is always there, let's add a "integration" test.
Open tests/rest/read.js
and add the following test code (it should be within the to level function body)
it('can read comments', function(done) {
rest_service()
.get('/comments?id=eq.1')
.withRole('webuser')
.expect('Content-Type', /json/)
.expect(200, done)
.expect( r => {
r.body[0].id.should.equal(1)
r.body[0].body.should.equal('a comment')
r.body[0].todo_id.should.equal(2)
r.body[0].user_id.should.equal(1)
})
});
Let's check the new test, and this time run only the "REST" tests
npm run test_rest
If all went well, the result should look like this
auth
✓ login
✓ logout
✓ me
✓ refresh_token
✓ signup
read
✓ basic
✓ by primary key
✓ can read comments
root endpoint
✓ returns json
9 passing (176ms)
Don't forget to save all this hard work ;)
git add db/ tests/
git commit -m "implement comments model and endpoint"
git push