Skip to content

FAOSTAT/faostat-api-client-tests

Repository files navigation

License Build Status

Automated Tests for the FAOSTAT API Client

This project contains automated tests to be run by TravisCI to test the feature of the FAOSTAT API and its JavaScript client.

Installation

This project uses Grunt JS to run the tests. To install the required libraries run: npm install from the project's root folder. The latest version of the FAOSTAT API client must be generated before running the tests. This can be done through the grunt build command. The script will generate two files, FAOSTATAPIClient.js and FAOSTATAPIClient.min.js in the src/js folder. The client is generated by the grunt-jsonschema-amd-restclient-generator NPM package.

How to add new tests

A new test requires a test/js/spec/*Spec.js file and some edits in the Gruntfile.js and test/js/main.js files. This example shows the configuration of the Abbreviations service test.

The *Spec.js File

This is the file that defines the test itself. The test is composed by a series of describe object, each one of them containing a beforeEach and it section. The beforeEach section is where the FAOSTAT API are invoked and the result is stored in a global variable. The it section tests the results against the expected value. The beforeEach section is required because the API client is asynchronous and the test must wait for the response before its execution. For instance, a simple test to query the abbreviations is defined as:

describe('can query the TEST database', function () {
  ...            
});

The beforeEach section executes the query and stores the result in the services.abbreviations variable:

beforeEach(function (done) {
  c.abbreviations({
    datasource: 'test'
  }).then(function (response) {
    services.abbreviations = response;
    done();
  });
});

While the it section tests the results against the expected value:

it('that returns 100 values', function () {
  expect(services.abbreviations.data.length).toEqual(100);
});

Gruntfile.js

The AbbreviationsSpec.js file must be dcleared in the jasmine section of the Grunt configuration file:

jasmine: {
    src: 'src/*.js',
    options: {
        specs: 'test/js/spec/*Spec.js',
        host: 'http://127.0.0.1:8000/',
        template: require('grunt-template-jasmine-requirejs'),
        templateOptions: {
            requireConfig: {
                baseUrl: './src',
                paths: {
                    ...
                    'abbreviations-spec': ['../test/js/spec/AbbreviationsSpec'],
                    ...
                },
                shim: {
                    'jasmine-html': {
                        deps: ['jasmine']
                    },
                    'jasmine-boot': {
                        deps: ['jasmine', 'jasmine-html']
                    }
                }
            }
        }
    }
}

RequireJS: main.js

The spec file must be declared in the RequireJS configuration file as well, test/js/main.js as follows:

require.config({
    baseUrl: '../src',
    paths: {
        ...
        'abbreviations-spec': ['../test/js/spec/AbbreviationsSpec'],
        ...
    },
    ...
});