Write a test

First let's see if after the new change our tests still pass


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' ),
    'all views are present in api schema',
    'tables present',
All we've done here is add '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;
Open 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}]


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) {
      .expect('Content-Type', /json/)
      .expect(200, done)
      .expect( r => {
        r.body[0].body.should.equal('a comment')

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

    ✓ login
    ✓ logout
    ✓ me
    ✓ refresh_token
    ✓ signup

    ✓ 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