UNPKG

smc-hub

Version:

CoCalc: Backend webserver component

247 lines (226 loc) 6.43 kB
// Generated by CoffeeScript 2.5.1 (function() { //######################################################################## // This file is part of CoCalc: Copyright © 2020 Sagemath, Inc. // License: AGPLv3 s.t. "Commons Clause" – see LICENSE.md for details //######################################################################## var DATABASE, DEBUG, RESET, async, dropdb, log, postgres, ref, ref1; /* Test suite for PostgreSQL interface and functionality. WARNING: The server timezone **MUST BE** UTC everywhere, or tests will fail! COPYRIGHT : (c) 2017 SageMath, Inc. LICENSE : AGPLv3 */ require('ts-node').register(); require('coffee2-cache'); DEBUG = !!((ref = process.env['SMC_DEBUG']) != null ? ref : false); //DEBUG = true // if true, completely deletes database before running tests -- do on schema change for now. RESET = !!((ref1 = process.env['SMC_DB_RESET']) != null ? ref1 : false); //RESET = true DATABASE = 'test-fubar'; async = require('async'); postgres = require('../../postgres'); if (DEBUG) { log = function(...args) { return console.log('pgtest: ', ...args); }; } else { log = function() {}; } exports.log = log; exports.db = void 0; exports.setup = function(cb) { return async.series([ function(cb) { if (exports.db != null) { log("db already defined"); return cb(); } else { exports.db = postgres.db({ database: DATABASE, debug: DEBUG, connect: false }); if (RESET) { log("delete the entire database (e.g., since schema could have changed from previous runs)"); return dropdb(cb); } else { return cb(); } } }, function(cb) { log("ensure db defined and we are connected"); return exports.db.connect({ cb: cb }); }, function(cb) { log("connected"); return exports.db.update_schema({ cb: cb }); }, function(cb) { log("drop contents of tables"); return exports.teardown(cb); } ], function(err) { if (err) { log("ERROR running setup", err); } return cb(err); }); }; exports.teardown = function(cb) { var ref2; // just deletes contents of tables, not schema. return (ref2 = exports.db) != null ? ref2.delete_all({ cb: cb, confirm: 'yes' }) : void 0; }; // create n accounts exports.create_accounts = function(n, m, cb) { var f, ref2; if (typeof m === 'function') { cb = m; m = 0; } f = function(i, cb) { return exports.db.create_account({ first_name: `Firstname${i}`, last_name: `Lastname${i}`, email_address: `sage+${i}@sagemath.com`, cb: cb }); }; return async.map((function() { var results = []; for (var j = m, ref2 = n + m; m <= ref2 ? j < ref2 : j > ref2; m <= ref2 ? j++ : j--){ results.push(j); } return results; }).apply(this), f, cb); }; // create n projects owned by the account_id's in the array account_ids (or string account_id) exports.create_projects = function(n, account_ids, cb) { var account_id, collabs, f; if (typeof account_ids === "string") { account_id = account_ids; collabs = []; } else { account_id = account_ids[0]; collabs = account_ids.slice(1); } f = function(i, cb) { var project_id; project_id = void 0; return async.series([ function(cb) { return exports.db.create_project({ title: `Project ${i}`, description: `Description ${i}`, account_id: account_id, cb: function(err, _project_id) { project_id = _project_id; return cb(err); } }); }, function(cb) { var g; g = function(id, cb) { return exports.db.add_user_to_project({ account_id: id, project_id: project_id, cb: cb }); }; return async.map(collabs, g, cb); } ], function(err) { return cb(err, project_id); }); }; return async.map((function() { var results = []; for (var j = 0; 0 <= n ? j < n : j > n; 0 <= n ? j++ : j--){ results.push(j); } return results; }).apply(this), f, cb); }; // Used to test a sequence of results from a changefeed (see usage below) exports.changefeed_series = function(v, cb) { var done, f, n; n = -1; done = function(err) { if (typeof cb === "function") { cb(err); } return cb = void 0; }; f = function(err, x) { var h; if (DEBUG) { if (err) { console.log("changefeed_series: err=", err); } else { console.log(`changefeed_series: x=${JSON.stringify(x)}`); } } n += 1; if (err) { done(err); return; } h = v[n]; if (h == null) { done(); return; } if (typeof h !== 'function') { throw Error(`each element of v must be a function, but v[${n}]='${h}' is not!`); } return h(x, function(err) { if (err) { return done(err); } else { if (n + 1 >= v.length) { // success return done(); } } }); }; return f; }; // Start with a clean slate -- delete the test database dropdb = function(cb) { var host, misc_node, port; misc_node = require('smc-util-node/misc_node'); log('delete the test database'); if (exports.db == null) { console.log("ERROR -- exports.db is not defined!"); cb(); return; } port = exports.db._port; host = exports.db._host; return misc_node.execute_code({ command: 'dropdb', args: ['--port', port, '--host', host, DATABASE], cb: function(err) { exports.db.disconnect(); log('done deleting test database'); if (err) { log('WARNING: dropdb error', err); } return cb(); // non-fatal -- would give error if db doesn't exist } }); }; }).call(this); //# sourceMappingURL=pgtest.js.map