Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit eba8bd7

Browse files
etpinardtheengineear
authored andcommitted
first pass js jupyter tests
1 parent 1f1272d commit eba8bd7

File tree

6 files changed

+197
-0
lines changed

6 files changed

+197
-0
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
fixtures/*.html
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
var http = require('http');
2+
var url = require('url');
3+
var fs = require('fs');
4+
var path = require('path');
5+
var ecstatic = require('ecstatic');
6+
var browserify = require('browserify');
7+
var cheerio = require('cheerio');
8+
var chrome = require('chrome-launch');
9+
var tapParser = require('tap-parser');
10+
11+
var PORT = 8080;
12+
var PATH_ROOT = path.join(__dirname, '..');
13+
var PATH_FIXTURES = path.join(PATH_ROOT, 'fixtures');
14+
var PATH_INDEX = path.join(PATH_FIXTURES, 'test.html');
15+
var PATH_INDEX_STUB = path.join(PATH_FIXTURES, 'test.tmp.html');
16+
var PATH_TEST_FILE = path.join(PATH_ROOT, 'test.js');
17+
var PATH_TEST_BUNDLE = path.join(PATH_ROOT, 'test.tmp.js');
18+
var URL = 'http://localhost:' + PORT + '/fixtures/test.tmp.html';
19+
var EXIT_CODE = 0;
20+
21+
// main
22+
stubIndex()
23+
.then(bundleTests)
24+
.then(startServer)
25+
.then(launch)
26+
27+
function stubIndex() {
28+
return new Promise(function(resolve, reject) {
29+
var html = fs.readFileSync(PATH_INDEX, 'utf-8');
30+
var $ = cheerio.load(html);
31+
32+
$('body').append('<script type="text/javascript" src="../test.tmp.js"></script>');
33+
34+
fs.writeFile(PATH_INDEX_STUB, $.html(), resolve);
35+
});
36+
}
37+
38+
function bundleTests() {
39+
return new Promise(function(resolve, reject) {
40+
var wsBundle = fs.createWriteStream(PATH_TEST_BUNDLE);
41+
42+
browserify(PATH_TEST_FILE, { debug: true })
43+
.bundle()
44+
.pipe(wsBundle);
45+
46+
wsBundle.on('close', resolve);
47+
});
48+
}
49+
50+
function startServer() {
51+
return new Promise(function(resolve, reject) {
52+
var server = http.createServer(ecstatic({ root: PATH_ROOT }));
53+
54+
server.on('request', handle);
55+
56+
server.listen(PORT, resolve);
57+
});
58+
}
59+
60+
function handle(req, res) {
61+
var query = url.parse(req.url).query || '';
62+
var parser = tapParser();
63+
64+
function is(query, root) {
65+
return query.indexOf(root) !== -1;
66+
}
67+
68+
if(is(query, 'data')) handleData(req, res);
69+
if(is(query, 'done')) handleDone();
70+
71+
function handleData(req, res) {
72+
req.pipe(parser);
73+
req.pipe(process.stdout);
74+
}
75+
76+
parser.on('assert', function(assert) {
77+
if(EXIT_CODE === 0 && assert.ok === false) EXIT_CODE = 1;
78+
})
79+
80+
function handleDone() {
81+
removeBuildFiles();
82+
process.exit(EXIT_CODE);
83+
}
84+
}
85+
86+
function launch() {
87+
chrome(URL);
88+
}
89+
90+
function removeBuildFiles() {
91+
fs.unlinkSync(PATH_INDEX_STUB);
92+
fs.unlinkSync(PATH_TEST_BUNDLE);
93+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
var test = require('tape');
2+
var xhr = require('xhr');
3+
4+
var cnt = 0;
5+
var noop = function() {};
6+
7+
var post = function(query, data) {
8+
var opts = data ? { body: data } : {};
9+
xhr.post('/?' + query + '&' + (cnt++), opts, noop);
10+
};
11+
12+
var ws = test.createStream();
13+
14+
ws.on('data', function(data) {
15+
post('data', data)
16+
});
17+
18+
test.onFinish(function() {
19+
post('done');
20+
});
21+
22+
module.exports = test;
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "plotly.py-jupyter-tests",
3+
"version": "1.0.0",
4+
"description": "js deps for plotly.py jupyter tests",
5+
"scripts": {
6+
"test": "node lib/server.js"
7+
},
8+
"author": "Plotly Inc.",
9+
"license": "MIT",
10+
"dependencies": {
11+
"browserify": "^13.1.0",
12+
"cheerio": "^0.20.0",
13+
"chrome-launch": "^1.1.4",
14+
"ecstatic": "^2.1.0",
15+
"tap-parser": "^2.0.0",
16+
"tape": "^4.6.0",
17+
"xhr": "^2.2.2"
18+
}
19+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
var test = require('./lib/tape-wrapper');
2+
3+
test('should have the correct number of script tags', function(t) {
4+
t.plan(1);
5+
6+
var nodes = document.querySelectorAll('script');
7+
t.equal(nodes.length, 8);
8+
});
9+
10+
test('should have one plotly.js graph', function(t) {
11+
t.plan(1);
12+
13+
var nodes = document.querySelectorAll('.js-plotly-plot');
14+
t.equal(nodes.length, 1);
15+
});
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
"""
2+
test__jupyter
3+
4+
"""
5+
from __future__ import absolute_import
6+
7+
import nbformat
8+
from nbconvert import HTMLExporter
9+
from nbconvert.preprocessors import ExecutePreprocessor
10+
11+
from unittest import TestCase
12+
from os import path
13+
import subprocess
14+
15+
import plotly
16+
17+
PATH_ROOT = path.dirname(__file__)
18+
PATH_FIXTURES = path.join(PATH_ROOT, 'fixtures')
19+
PATH_TEST_NB = path.join(PATH_FIXTURES, 'test.ipynb')
20+
PATH_TEST_HTML = path.join(PATH_FIXTURES, 'test.html')
21+
22+
23+
class PlotlyJupyterTestCase(TestCase):
24+
def setUp(self):
25+
with open(PATH_TEST_NB, 'r') as f:
26+
self.nb = nbformat.read(f, as_version=4)
27+
28+
self.ep = ExecutePreprocessor(timeout=600)
29+
self.html_exporter = HTMLExporter()
30+
31+
self.ep.preprocess(self.nb, {'metadata': {'path': '.'}})
32+
(self.body, _) = self.html_exporter.from_notebook_node(self.nb)
33+
34+
with open(PATH_TEST_HTML, 'w') as f:
35+
f.write(self.body)
36+
37+
def test_one(self):
38+
proc = subprocess.Popen(['npm', 'test'],
39+
cwd=PATH_ROOT,
40+
stdout=subprocess.PIPE,
41+
stderr=subprocess.PIPE)
42+
43+
(_, stderr) = proc.communicate()
44+
45+
if stderr:
46+
self.fail('One or more javascript test failed')

0 commit comments

Comments
 (0)