mirror of
https://github.com/retspen/webvirtcloud
synced 2026-12-03 17:36:05 +00:00
test: each app keeps its tests in a tests package
131 test modules sat next to the code they test; instances alone had 51 of them beside views.py and utils.py. They move into a tests/ package per app, and the JavaScript checks into webvirtcloud/tests/js. Each non-empty tests.py becomes tests/test_views.py; the empty generated stubs are removed. Imports of the app's own modules become absolute, paths built from __file__ go one level up, the live helper is instances.tests.livetest, and coverage omits */tests/*. The suite discovers the same 960 tests as before.
This commit is contained in:
parent
0906838193
commit
c55f52f025
154 changed files with 27 additions and 39 deletions
77
webvirtcloud/tests/js/instance_tabs.test.js
Normal file
77
webvirtcloud/tests/js/instance_tabs.test.js
Normal file
|
|
@ -0,0 +1,77 @@
|
|||
// node instance_tabs.test.js path/to/static/js/instance_tabs.js
|
||||
const assert = require("assert");
|
||||
const {restoreInstanceTab} = require(process.argv[2]);
|
||||
|
||||
// A minimal DOM: elements with an id, classes, a parent, children, and the
|
||||
// few selectors restoreInstanceTab uses.
|
||||
function el(id, classes, children = [], attrs = {}) {
|
||||
const node = {id, classList: new Set(classes), children, attrs, parentElement: null};
|
||||
node.classList.contains = (c) => node.classList.has(c);
|
||||
node.getAttribute = (name) => attrs[name];
|
||||
for (const child of children) child.parentElement = node;
|
||||
node.querySelector = (selector) => find(node, selector);
|
||||
return node;
|
||||
}
|
||||
|
||||
function* walk(node) {
|
||||
for (const child of node.children) {
|
||||
yield child;
|
||||
yield* walk(child);
|
||||
}
|
||||
}
|
||||
|
||||
function matches(node, selector) {
|
||||
if (selector === ".nav-tabs") return node.classList.has("nav-tabs");
|
||||
if (selector === ".nav-link.active") return node.classList.has("nav-link") && node.classList.has("active");
|
||||
if (selector === ".nav-link:not(#osinfo-tab)") return node.classList.has("nav-link") && node.id !== "osinfo-tab";
|
||||
if (selector === "#osinfo-tab") return node.id === "osinfo-tab";
|
||||
throw new Error("unsupported selector " + selector);
|
||||
}
|
||||
|
||||
function find(root, selector) {
|
||||
for (const node of walk(root)) if (matches(node, selector)) return node;
|
||||
return null;
|
||||
}
|
||||
|
||||
function link(target, extra = []) {
|
||||
return el(target === "#osinfo" ? "osinfo-tab" : "", ["nav-link", ...extra], [], {"data-bs-target": target});
|
||||
}
|
||||
|
||||
// The VM page: top-level panes, Settings with its own tabs and panes.
|
||||
function page(settingsLinks) {
|
||||
const settings = el("settings", ["tab-pane"], [
|
||||
el("", ["nav-tabs"], settingsLinks),
|
||||
el("", ["tab-content"], [el("osinfo", ["tab-pane"]), el("options", ["tab-pane"]), el("vncsettings", ["tab-pane"])]),
|
||||
]);
|
||||
const root = el("", [], [el("", ["tab-content"], [el("power", ["tab-pane", "active"]), el("access", ["tab-pane"]), settings])]);
|
||||
root.getElementById = (id) => {
|
||||
for (const node of walk(root)) if (node.id === id) return node;
|
||||
return null;
|
||||
};
|
||||
return root;
|
||||
}
|
||||
|
||||
function run(doc, hash) {
|
||||
const shown = [];
|
||||
let loaded = 0;
|
||||
restoreInstanceTab(doc, hash, (target) => shown.push(target), () => loaded++);
|
||||
return {shown, loaded};
|
||||
}
|
||||
|
||||
const settled = () => page([link("#options", ["active"])]);
|
||||
|
||||
// a top-level pane opens directly
|
||||
assert.deepStrictEqual(run(settled(), "#access").shown, ["#access"]);
|
||||
// a pane inside Settings opens Settings first
|
||||
assert.deepStrictEqual(run(settled(), "#options").shown, ["#settings", "#options"]);
|
||||
// no hash, an unknown one, or one with odd characters opens nothing
|
||||
for (const hash of ["", "#", "#nosuchpane", '#a"]b']) {
|
||||
assert.deepStrictEqual(run(settled(), hash).shown, [], hash);
|
||||
}
|
||||
// Settings without an active section opens its first one, not OS info
|
||||
assert.deepStrictEqual(run(page([link("#osinfo"), link("#vncsettings")]), "").shown, ["#vncsettings"]);
|
||||
// OS info as the only section is selected and loaded
|
||||
assert.deepStrictEqual(run(page([link("#osinfo")]), ""), {shown: ["#osinfo"], loaded: 1});
|
||||
// a redirect to OS info loads it
|
||||
assert.deepStrictEqual(run(settled(), "#osinfo"), {shown: ["#settings", "#osinfo"], loaded: 1});
|
||||
console.log("ok");
|
||||
104
webvirtcloud/tests/js/poll.test.js
Normal file
104
webvirtcloud/tests/js/poll.test.js
Normal file
|
|
@ -0,0 +1,104 @@
|
|||
// Behaviour of static/js/poll.js with a fake clock, document and jQuery promise.
|
||||
// Run by webvirtcloud/tests/test_poll_js.py: node poll.test.js static/js/poll.js
|
||||
const fs = require("fs");
|
||||
const assert = require("assert");
|
||||
|
||||
let now = 0, timers = [], nextId = 1;
|
||||
global.setTimeout = (fn, ms) => { const id = nextId++; timers.push({ id, at: now + ms, fn }); return id; };
|
||||
global.clearTimeout = (id) => { timers = timers.filter((t) => t.id !== id); };
|
||||
function advance(ms) {
|
||||
const end = now + ms;
|
||||
for (;;) {
|
||||
timers.sort((a, b) => a.at - b.at);
|
||||
const t = timers[0];
|
||||
if (!t || t.at > end) break;
|
||||
timers.shift(); now = t.at; t.fn();
|
||||
}
|
||||
now = end;
|
||||
}
|
||||
const listeners = {};
|
||||
global.document = { hidden: false, addEventListener: (ev, fn) => { listeners[ev] = fn; } };
|
||||
function setHidden(h) { document.hidden = h; listeners.visibilitychange(); }
|
||||
|
||||
// requests: each returns a promise that the test settles. Like jQuery, it runs
|
||||
// the callbacks in the order they were added, and an exception in one stops
|
||||
// the ones after it.
|
||||
let requests = [];
|
||||
function request() {
|
||||
const callbacks = [];
|
||||
const r = {
|
||||
started: now,
|
||||
always(fn) { callbacks.push(fn); return r; },
|
||||
done(fn) { callbacks.push(fn); return r; },
|
||||
finish() {
|
||||
try { callbacks.forEach((fn) => fn({ ok: true })); } catch (e) { r.error = e; }
|
||||
},
|
||||
};
|
||||
requests.push(r);
|
||||
return r;
|
||||
}
|
||||
let answers = [];
|
||||
let failNext = false;
|
||||
function onData(data) {
|
||||
answers.push(data);
|
||||
if (failNext) { failNext = false; throw new Error("a chart for an unknown device"); }
|
||||
}
|
||||
eval(fs.readFileSync(process.argv[2], "utf8"));
|
||||
|
||||
const poll = pollWhileVisible(10000, request, onData);
|
||||
advance(9999); assert.strictEqual(requests.length, 0, "first request after the interval");
|
||||
advance(1); assert.strictEqual(requests.length, 1);
|
||||
|
||||
// a slow answer: no second request while the first is open
|
||||
advance(45000); assert.strictEqual(requests.length, 1, "no pile-up while a request is open");
|
||||
requests[0].finish();
|
||||
advance(9999); assert.strictEqual(requests.length, 1);
|
||||
advance(1); assert.strictEqual(requests.length, 2, "next one interval after the answer");
|
||||
requests[1].finish();
|
||||
|
||||
// hidden: nothing is asked
|
||||
setHidden(true);
|
||||
advance(60000); assert.strictEqual(requests.length, 2, "a hidden page asks nothing");
|
||||
// shown again: asks at once
|
||||
setHidden(false); assert.strictEqual(requests.length, 3, "asks at once when shown");
|
||||
requests[2].finish();
|
||||
advance(10000); assert.strictEqual(requests.length, 4);
|
||||
|
||||
// hidden while a request is open: no new one when it finishes, one on show
|
||||
setHidden(true); requests[3].finish();
|
||||
advance(60000); assert.strictEqual(requests.length, 4);
|
||||
setHidden(false); assert.strictEqual(requests.length, 5);
|
||||
|
||||
// briefly hidden while waiting: the pending timer still fires once, no double request
|
||||
requests[4].finish();
|
||||
setHidden(true); setHidden(false);
|
||||
advance(10000); assert.strictEqual(requests.length, 6, "no extra request for a short hide");
|
||||
|
||||
// stop: nothing more, also not on show
|
||||
poll.stop(); requests[5].finish();
|
||||
setHidden(true); setHidden(false); advance(60000);
|
||||
assert.strictEqual(requests.length, 6, "stopped");
|
||||
// start after stop: asks at once, then every interval
|
||||
poll.start(); assert.strictEqual(requests.length, 7, "start asks at once");
|
||||
requests[6].finish(); advance(10000); assert.strictEqual(requests.length, 8);
|
||||
// start while running or waiting adds nothing
|
||||
poll.start(); assert.strictEqual(requests.length, 8);
|
||||
requests[7].finish(); poll.start(); advance(9999); assert.strictEqual(requests.length, 8, "start while waiting");
|
||||
// start while hidden asks nothing until shown
|
||||
poll.stop(); setHidden(true); poll.start(); assert.strictEqual(requests.length, 8);
|
||||
setHidden(false); assert.strictEqual(requests.length, 9);
|
||||
// an error in onData does not stop the polling
|
||||
assert.strictEqual(answers.length, 8, "each answer reaches onData");
|
||||
failNext = true; requests[8].finish();
|
||||
assert.ok(requests[8].error, "the error reaches the page");
|
||||
advance(10000); assert.strictEqual(requests.length, 10, "polling goes on after an error in onData");
|
||||
|
||||
// a first delay of 0: the first request at once, then every interval
|
||||
poll.stop(); requests[9].finish();
|
||||
const atOnce = pollWhileVisible(5000, request, onData, 0);
|
||||
advance(0); assert.strictEqual(requests.length, 11, "first request after firstDelay");
|
||||
requests[10].finish();
|
||||
advance(4999); assert.strictEqual(requests.length, 11);
|
||||
advance(1); assert.strictEqual(requests.length, 12, "then every interval");
|
||||
atOnce.stop();
|
||||
console.log("poll.js: all checks passed");
|
||||
Loading…
Add table
Add a link
Reference in a new issue