Skip to content

Commit

Permalink
Merge pull request #1177 from briehl/test-refactor
Browse files Browse the repository at this point in the history
TASK-1050 some frontend test refactoring
  • Loading branch information
sychan committed Oct 25, 2017
2 parents fc75682 + dc0ae3e commit 5f4983b
Show file tree
Hide file tree
Showing 17 changed files with 185 additions and 97 deletions.
3 changes: 3 additions & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[run]
omit =
src/biokbase/narrative/tests/*
7 changes: 6 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ branches:
- travis-firefox

before_install:
- pip install virtualenv
- pip install virtualenv coveralls
- gem install coveralls-lcov
- nvm install $TRAVIS_NODE_VERSION
- npm install -g bower
- npm install -g grunt-cli
Expand All @@ -35,3 +36,7 @@ before_script:
- sleep 3 # give xvfb some time to start

script: make build-travis-narrative && make test

after_script:
- coveralls-lcov -v -n js-coverage/lcov/lcov.info > js-coverage.json
- coveralls --merge js-coverage.json
18 changes: 9 additions & 9 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,15 @@ module.exports = function(grunt) {
'karma': {
unit: {
configFile: 'test/unit/karma.conf.js',
reporters: ['progress', 'coverage'],
coverageReporter: {
dir: 'build/test-coverage/',
reporters: [
{
type: 'html', subdir: 'html'
}
]
}
// reporters: ['progress'], //, 'coverage'],
// coverageReporter: {
// dir: 'build/test-coverage/',
// reporters: [
// {
// type: 'html', subdir: 'html'
// }
// ]
// }
},
dev: {
// to do - add watch here
Expand Down
1 change: 0 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ test: test-backend test-frontend-unit test-frontend-e2e
test-backend:
@echo "running backend tests"
sh $(BACKEND_TEST_SCRIPT)
# @echo "THIS IS A NO-OP UNTIL THE AUTH2 SWITCH"
@echo "done"

# test-frontend-unit should use karma and jasmine to test
Expand Down
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
| Branch | Status |
| :--- | :--- |
| master | [![Build Status](https://travis-ci.org/kbase/narrative.svg?branch=master)](https://travis-ci.org/kbase/narrative) [![Coverage Status](https://coveralls.io/repos/kbase/narrative/badge.svg?branch=master)](https://coveralls.io/r/kbase/narrative?branch=master) |
| staging | [![Build Status](https://travis-ci.org/kbase/narrative.svg?branch=staging)](https://travis-ci.org/kbase/narrative) [![Coverage Status](https://coveralls.io/repos/kbase/narrative/badge.svg?branch=staging)](https://coveralls.io/r/kbase/narrative?branch=staging) |
| develop | [![Build Status](https://travis-ci.org/kbase/narrative.svg?branch=develop)](https://travis-ci.org/kbase/narrative) [![Coverage Status](https://coveralls.io/repos/kbase/narrative/badge.svg?branch=develop)](https://coveralls.io/r/kbase/narrative?branch=develop)|

This is the repository for the KBase Narrative Interface.
Expand Down
49 changes: 49 additions & 0 deletions docs/testing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Narrative Unit Testing

### Short version
1. Install the Narrative and activate its virtualenv (if appropriate)
a. (optional) set credentials in test/
2. Run `make test` at a prompt.
3. Lament the coverage is so low.

### Long version
**About**
Because the Narrative Interface is built on both the front and back ends of the Jupyter Notebook, there's two sides to Narrative testing. The front end tests are in JavaScript, and make use of [Karma](http://karma-runner.github.io/1.0/index.html) as a test runner, and [Jasmine](https://jasmine.github.io/2.0/introduction.html) as a testing language. The back end tests are written in Python using the unittest framework, and run with [nose](http://nose.readthedocs.io/en/latest/).

**How to run tests**
We aren't testing the underlying Jupyter code, but we are making use of it, so a test run requires a Narrative install as described in the [installation](../install/local_install.md) docs. If you're installing in a Virtualenv, make sure it's activated. You don't need to run the narrative yourself for tests to work, it just needs to be installed.

Then, simply run (from the narrative root directory) `make test`.

This calls a few subcommands, and those can be run independently for specific uses:
* `make test-frontend-unit` will run only the unit tests on the frontend (i.e. those with the Karma runner)
* `make test-frontend-e2e` will run only the frontend tests that make use of Selenium to simulate a browser on the real Narrative site.
* `make test-backend` will run only the backend Python tests.

**Add credentials for tests**
The Narrative Interface is one of the hubs of KBase - it touches several different services, all of which need real authentication. Some of those have been mocked in various tests (like running apps), but others (the Workspace service) still require a real Auth token. If you have a KBase Developer account, you can create a Developer Token in the Account tab of the main KBase interface.

You can store auth token files in test/. These are single line files, containing only the Auth token used for testing a single user. For example, `test/narrativetest.tok` would be for the narrativetest user, and would only contain that user's Auth token. DO NOT CHECK THESE IN TO GITHUB.

Next, these credentials need to be referenced for both the back and front end. This version requires two configs - one for Python, and one for JavaScript.

* Python:
`src/biokbase/narrative/tests/test.cfg`
In the `[users]` and `[token_files]` blocks, two sets of values are needed: test_user and private_user. They don't have any special permissions, they just need to be different users.

* JavaScript:
`test/unit/testConfig.json`
This just needs the path to the token file (with pre-pended slash), such as `"/test/narrativetest.tok"`

* *TODO (10/24/2017): Unify these token configs!*

**Testing with Travis-CI / Coveralls**
These tests are run (without credentials) automatically on a pull request to the Narrative Github repo. These are currently run through [Travis-CI](https://travis-ci.org/) and the coverage reported with [Coveralls](https://coveralls.io/). There should be nothing you need to do to make this work.

**Adding your own tests**
* **Python**
Python tests should be per module, and should all be added to the `src/biokbase/narrative/tests`. The `test.cfg` file there is in INI file format, and should be added to, as necessary.

There are some service client Mocks available using the `mock` library. Check out `test_appmanager.py` for some examples of how these can be used.
* **JavaScript**
JavaScript tests follow the common Test Spec idiom. Here, we create a new spec file for each JavaScript module. These all live under `test/unit/spec` in roughly the same subdirectory as found under `kbase-extension/static/kbase/js`. There's an example spec in `test/unit/specTemplate.js` - you can just copy this to a new module, and modify to fit your needs.
16 changes: 8 additions & 8 deletions kbase-extension/static/kbase/js/common/spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
define([
'require',
'bluebird',
'./lang',
'./sdk',
'./specValidation',
'../widgets/appWidgets2/validators/resolver'
'common/lang',
'common/sdk',
'common/specValidation',
'widgets/appWidgets2/validators/resolver'
], function(require, Promise, lang, sdk, Validation, validationResolver) {
'use strict';

Expand All @@ -33,7 +33,7 @@ define([
/*
* Make a "shell" model based on the spec. Recursively build an object
* with properties as defined by the spec.
* Effectively this means that only the top level is represented, since
* Effectively this means that only the top level is represented, since
*/
function makeEmptyModel() {
var model = {};
Expand All @@ -48,9 +48,9 @@ define([
from the given spec.
It does this by:
The top level spec is treated as a struct.
The default value for each paramater is simply set as the value for the given parameter
The default value for each paramater is simply set as the value for the given parameter
on a model object.
One exception is that if a parameter is a
One exception is that if a parameter is a
*/
function makeDefaultedModel() {
var model = {};
Expand Down Expand Up @@ -124,4 +124,4 @@ define([
return factory(config);
}
};
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -602,13 +602,13 @@ this._rewireIds($tables, this);

var $networkGraph = this.data('network')
.css({width : 700, height : 600})
.attr('align', 'center')
new kbaseForcedNetwork( , {
linkDistance : 200,
filter : true,
nodeHighlightColor : '#002200',
relatedNodeHighlightColor : 'black',
});
.attr('align', 'center');
new kbaseForcedNetwork($networkGraph, {
linkDistance : 200,
filter : true,
nodeHighlightColor : '#002200',
relatedNodeHighlightColor : 'black',
});
$networkGraph.$elem.hide();
this.networkGraph($networkGraph);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,13 +236,13 @@ define (

var $networkGraph = $.jqElem('div')
.css({width : 700, height : 600})
.attr('align', 'center')
new kbaseForcedNetwork( , {
linkDistance : 200,
filter : true,
nodeHighlightColor : '#002200',
relatedNodeHighlightColor : 'black',
});
.attr('align', 'center');
new kbaseForcedNetwork($networkGraph, {
linkDistance : 200,
filter : true,
nodeHighlightColor : '#002200',
relatedNodeHighlightColor : 'black',
});

this.networkGraph($networkGraph);

Expand Down
1 change: 0 additions & 1 deletion kbase-extension/static/narrative_paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,6 @@ require.config({
'kbaseExpressionVolcanoPlot': 'kbase/js/widgets/function_output/kbaseExpressionVolcanoPlot',

'kbaseRESKESearchResultDemo': 'kbase/js/widgets/function_output/kbaseRESKESearchResultDemo',
'kbaseGenericSetViewer': 'kbase/js/widgets/function_output/kbaseGenericSetViewer',
'kbaseDifferentialExpressionMatrixSetViewer': 'kbase/js/widgets/function_output/kbaseDifferentialExpressionMatrixSetViewer',


Expand Down
41 changes: 29 additions & 12 deletions test/unit/karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,45 @@ module.exports = function (config) {
'karma-jasmine',
// 'karma-chrome-launcher',
'karma-phantomjs-launcher',
'karma-coverage',
'karma-requirejs'
'karma-requirejs',
'karma-coverage'
],
preprocessors: {
'kbase-extension/static/kbase/**/*.js': ['coverage']
'kbase-extension/static/kbase/js/**/*.js': ['coverage']
},
files: [
// {pattern: 'kbase-extension/static/components/**/*.js', included: false},
// {pattern: 'kbase-extension/static/kbase/js/*.js', included: false},
// {pattern: 'kbase-extension/static/kbase/js/widgets/**/*.js', included: false},
// {pattern: 'kbase-extension/static/**/*.json', included: false},

{pattern: 'test/unit/spec/**/*.js', included: false},
{pattern: 'kbase-extension/static/**/*.css', included: false, served: true},
{pattern: 'kbase-extension/static/kbase/templates/**/*.html', included: false, served: true},
{pattern: 'kbase-extension/static/kbase/config/**/*.json', included: false, served: true},
{pattern: 'kbase-extension/static/**/*.js', included: false, served: true},
{pattern: 'kbase-extension/static/**/*.gif', included: false, served: true},
'kbase-extension/static/narrative_paths.js',
{pattern: 'test/unit/testConfig.json', included: false, served: true, nocache: true},
{pattern: 'test/*.tok', included: false, served: true, nocache: true},
'test/unit/testUtil.js',
'test/unit/test-main.js'
],
exclude: [
'kbase-extension/static/buildTools/*.js',
'kbase-extension/static/ext_components/**/test/**/*.js',
'kbase-extension/static/kbase/js/patched-components/**/*'
],
// test results reporter to use
// possible values: 'dots', 'progress'
// available reporters: https://npmjs.org/browse/keyword/karma-reporter
reporters: ['progress', 'coverage'],
coverageReporter: {
type: 'html',
dir: 'js-coverage/'
dir: 'js-coverage/',
reporters: [{
type: 'html',
subdir: 'html'
}, {
type: 'lcov',
subdir: 'lcov'
}],
includeAllSources: true
},
// web server port
port: 9876,
Expand All @@ -59,9 +72,13 @@ module.exports = function (config) {
// },
singleRun: true,
proxies: {
'/narrative/static/': 'http://localhost:9999/narrative/static/',
'/static/': 'http://localhost:9999/narrative/static/',
'/test/': '/base/test/'
'/narrative/static/': '/base/kbase-extension/static/',
'/narrative/static/base': 'http://localhost:9999/narrative/static/base',
'/narrative/static/notebook': 'http://localhost:9999/narrative/static/notebook',
'/narrative/static/components': 'http://localhost:9999/narrative/static/components',
'/narrative/static/services': 'http://localhost:9999/narrative/static/services',
'/static/kbase/config': '/base/kbase-extension/static/kbase/config',
'/test/': '/base/test/',
}

});
Expand Down
2 changes: 1 addition & 1 deletion test/unit/spec/appWidgets/infoPanelSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,4 @@ define([
});
});
});
});
});
15 changes: 7 additions & 8 deletions test/unit/spec/narrativeConfigSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@
/*global beforeEach, afterEach*/
/*jslint white: true*/

define (
[
'narrativeConfig',
'bluebird'
], function(
Config,
Promise
) {
define ([
'narrativeConfig',
'bluebird'
], function(
Config,
Promise
) {
'use strict';
describe('Tests for narrativeConfig', function() {
it('loaded the config module', function() {
Expand Down
61 changes: 30 additions & 31 deletions test/unit/test-main.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/*jslint white: true*/
// var baseUrl = "http://localhost:8888/static/";

var tests = [
'text', 'json'
Expand All @@ -11,7 +10,6 @@ for (var file in window.__karma__.files) {
}
}
}
console.log('starting...');

requirejs.config({
baseUrl: '/narrative/static/',
Expand All @@ -21,12 +19,13 @@ requirejs.config({
codemirror: 'components/codemirror',
bootstraptour: 'components/bootstrap-tour/build/js/bootstrap-tour.min',
bootstrap: 'components/bootstrap/js/bootstrap.min',
testUtil: '../../test/unit/testUtil'
testUtil: '../../test/unit/testUtil',
bluebird: 'ext_components/bluebird/js/browser/bluebird.min',
},
map: {
'*': {
'jquery-ui': 'jqueryui'
}
},
},

deps: tests,
Expand Down Expand Up @@ -56,30 +55,30 @@ requirejs.config({
});


function addCdnModules(baseUrl) {
if (!baseUrl) {
baseUrl = 'https://ci.kbase.us/cdn/files';
// baseUrl = 'http://cdn.kbase.us/cdn';
}
var modules = {
kb_common: 'kbase-common-js/1.7.0/',
kb_service: 'kbase-service-clients-js/2.9.1/',
uuid: 'pure-uuid/1.3.0/uuid',
// TODO: we need to reconcile Jupyter and KBase external deps
// text: 'requirejs-text/2.0.14/text',
css: 'require-css/0.1.8/css',
'font-awesome': 'font-awesome/4.5.0/css/font-awesome',
handlebars: 'handlebars/4.0.5/handlebars',
'google-code-prettify': 'google-code-prettify/1.2.0/'
},
paths = {};

Object.keys(modules).forEach(function (key) {
paths[key] = [baseUrl, modules[key]].join('/');
});

require.config({
paths: paths
});
}
addCdnModules();
// function addCdnModules(baseUrl) {
// if (!baseUrl) {
// baseUrl = 'https://ci.kbase.us/cdn/files';
// // baseUrl = 'http://cdn.kbase.us/cdn';
// }
// var modules = {
// kb_common: 'kbase-common-js/1.7.0/',
// kb_service: 'kbase-service-clients-js/2.9.1/',
// uuid: 'pure-uuid/1.3.0/uuid',
// // TODO: we need to reconcile Jupyter and KBase external deps
// // text: 'requirejs-text/2.0.14/text',
// css: 'require-css/0.1.8/css',
// 'font-awesome': 'font-awesome/4.5.0/css/font-awesome',
// handlebars: 'handlebars/4.0.5/handlebars',
// 'google-code-prettify': 'google-code-prettify/1.2.0/'
// },
// paths = {};
//
// Object.keys(modules).forEach(function (key) {
// paths[key] = [baseUrl, modules[key]].join('/');
// });
//
// require.config({
// paths: paths
// });
// }
// addCdnModules();
Loading

0 comments on commit 5f4983b

Please sign in to comment.