Skip to content

Test non-png exports #307

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 1, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions circle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ dependencies:
test:
override:
- sudo lxc-attach -n "$(docker inspect --format '{{.Id}}' mytestbed)" -- bash -c "cd /var/www/streambed/image_server/plotly.js && node test/image/compare_pixels_test.js"
- sudo lxc-attach -n "$(docker inspect --format '{{.Id}}' mytestbed)" -- bash -c "cd /var/www/streambed/image_server/plotly.js && node test/image/export_test.js"
- npm run citest-jasmine
- npm run test-bundle
- npm run test-syntax
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"test-jasmine": "karma start test/jasmine/karma.conf.js",
"citest-jasmine": "karma start test/jasmine/karma.ciconf.js",
"test-image": "./tasks/test_image.sh",
"test-export": "./tasks/test_export.sh",
"test-syntax": "node test/syntax_test.js",
"test-bundle": "node tasks/test_bundle.js",
"test": "npm run citest-jasmine && npm run test-image && npm run test-syntax && npm run test-bundle",
Expand Down
20 changes: 20 additions & 0 deletions tasks/test_export.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#! /bin/bash
#
# TODO adapt this for Windows
#
# ===============================================================================

CONTAINER_NAME="imagetest" # same as in docker-compose.yml

# create/run/start docker container with:
# $ docker-compose up -d

CMD=(
"cd /var/www/streambed/image_server/plotly.js &&"
"cp -f test/image/index.html ../server_app/index.html &&"
"supervisorctl restart nw1 && "
"wget --server-response --spider --tries=8 --retry-connrefused http://localhost:9010/ping &&"
"node test/image/export_test.js $1"
)

docker exec -i $CONTAINER_NAME /bin/bash -c "${CMD[*]}"
105 changes: 105 additions & 0 deletions test/image/export_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
var fs = require('fs');
var path = require('path');

var request = require('request');
var test = require('tape');

var constants = require('../../tasks/util/constants');
var getOptions = require('../../tasks/util/get_image_request_options');

var userFileName = process.argv[2];

/**
* Image formats to test
*
* N.B. 'png' is tested in `compare_pixels_test.js`
*
* TODO figure why 'jpeg' and 'webp' lead to errors
*
*/
var FORMATS = ['svg', 'pdf', 'eps'];

/**
* The tests below determine whether the images are properly
* exported by (only) checking the file size of the generated images.
*
* MIN_SIZE is the minimum satisfactory file size.
*/
var MIN_SIZE = 100;


// make artifact folder
if(!fs.existsSync(constants.pathToTestImages)) {
fs.mkdirSync(constants.pathToTestImages);
}

if(!userFileName) runAll();
else {

if(path.extname(userFileName) !== '.json') {
throw new Error('user-specified mock must end with \'.json\'');
}

runSingle(userFileName);
}

function runAll() {
test('testing export formats', function(t) {

// non-exhaustive list of mocks to test
var fileNames = [
'0.json',
'geo_first.json',
'gl3d_z-range.json'
];

t.plan(fileNames.length * FORMATS.length);

fileNames.forEach(function(fileName) {
testExports(fileName, t);
});
});
}

function runSingle(userFileName) {
test('testing export for: ' + userFileName, function(t) {
t.plan(FORMATS.length);

testExports(userFileName, t);
});
}

function testExports(fileName, t) {
FORMATS.forEach(function(format) {
testExport(fileName, format, t);
});
}

function testExport(fileName, format, t) {
var figure = require(path.join(constants.pathToTestImageMocks, fileName));
var bodyMock = {
figure: figure,
format: format,
scale: 1
};

var imageFileName = fileName.split('.')[0] + '.' + format;
var savedImagePath = path.join(constants.pathToTestImages, imageFileName);
var savedImageStream = fs.createWriteStream(savedImagePath);

var options = getOptions(bodyMock, 'http://localhost:9010/');

function checkExport(err) {
if(err) throw err;

fs.stat(savedImagePath, function(err, stats) {
var didExport = stats.size > MIN_SIZE;

t.ok(didExport, imageFileName + ' should be properly exported');
});
}

request(options)
.pipe(savedImageStream)
.on('close', checkExport);
}