Testing your EWD.js back-end logic

On the face of it, one of the tricky parts of EWD.js development is testing and debugging your “back-end” logic: by that I mean the JavaScript code that will run within the Node.js environment.  You don’t have direct access to that code because it runs within the child processes created by EWD.js (or more correctly by the ewdgateway2 module).

It’s actually very simple to create what I refer to as a “test harness” that simulates the back-end run-time environment in which your Node.js module will run.  Once you’ve created such a test harness file for your particular environment, you can access your Mumps database in exactly the same way as your back-end code and run your back-end JavaScript logic, but do so in a standalone JavaScript file that you can invoke directly within Node.js.

Here’s the basic structure of such a test harness file for a number of typical EWD.js environments:

GT.M + dEWDrop Virtual Machine

var ewdGlobals = require('/home/vista/www/node/node_modules/ewdgateway2/lib/ewdGlobals');
var interface = require('nodem');
var db = new interface.Gtm();
db.open();
ewdGlobals.init(db);
var ewd = {
  mumps: ewdGlobals
};

// Environment is now set up exactly as within EWD.js
// for accessing Mumps globals and functions from within this
// test harness, eg:

var zewd = new ewd.mumps.GlobalNode('zewd', []);
console.log(JSON.stringify(zewd._getDocument(), null, 2)); 

// When finished, always close the database, otherwise you'll
// need to run tset in order to reset the terminal environment

db.close();

Save this as /home/vista/www/node/testHarness.js

You can run it by typing:

cd /home/vista/www/node
node testHarness

GlobalsDB + Mike Clayton’s Ubuntu Installer

var ewdGlobals = require('/opt/ewdlite/node_modules/ewdgateway2/lib/ewdGlobals');
var interface = require('cache');
var db = new interface.Cache();
db.open({
  path: '/opt/globalsdb/mgr',
  username: '_SYSTEM',
  password: 'SYS',
  namespace: 'USER'
});
ewdGlobals.init(db);
var ewd = {
  mumps: ewdGlobals
};

// Environment is now set up exactly as within EWD.js
// for accessing Mumps globals and functions from within this
// test harness, eg:

var zewd = new ewd.mumps.GlobalNode('zewd', []);
console.log(JSON.stringify(zewd._getDocument(), null, 2)); 

// When finished, always close the database, otherwise you'll
// need to run tset in order to reset the terminal environment

db.close();

Save this as /opt/ewdlite/testHarness.js

You can run it by typing:

cd /opt/ewdlite
node testHarness

Caché + Windows 7
I’ve assumed that the ewdgateway2 module has been installed under the directory c:\node and that the cache.node file has been copied to the c:\node\node_modules directory.

var ewdGlobals = require('c:\\node\\node_modules\\ewdgateway2\\lib\\ewdGlobals');
var interface = require('cache');
var params = {
  path: "c:\\InterSystems\\Cache\\Mgr",
  username: "_SYSTEM",
  password: "SYS",
  namespace: "USER"
};
var db = new interface.Cache();
db.open(params);
ewdGlobals.init(db);
var ewd = {
  mumps: ewdGlobals
};

// Environment is now set up exactly as within EWD.js
// for accessing Mumps globals and functions from within this
// test harness, eg:

var zewd = new ewd.mumps.GlobalNode('zewd', []);
console.log(JSON.stringify(zewd._getDocument(), null, 2)); 

// When finished, always close the database, otherwise you'll
// need to run tset in order to reset the terminal environment

db.close();

Save this as c:\node\testHarness.js

You can run it by typing:

cd c:\node
node testHarness

Raspberry Pi

var ewdGlobals = require('/home/pi/node/node_modules/ewdgateway2/lib/ewdGlobals');
var interface = require('noDB');
var db = new interface.Gtm();
db.open();
ewdGlobals.init(db);

var ewd = {
  mumps: ewdGlobals
};

// Environment is now set up exactly as within EWD.js
// for accessing Mumps globals and functions from within this
// test harness, eg:

var zewd = new ewd.mumps.GlobalNode('zewd', []);
console.log(JSON.stringify(zewd._getDocument(), null, 2)); 

// When finished, always close the database, otherwise you'll
// need to run tset in order to reset the terminal environment

db.close();

Save this as /home/pi/node/testHarness.js

You can run it by typing:

cd /home/pi/node
node testHarness

If your environment is different from the ones above, simply adapt the relevant example to reflect the file paths you’ve used and/or your Caché login parameters if relevant.

Change the example code I’ve provided with whatever you need to do.  For example, you could use your test harness file to try out some of the JSON / JSON-LD examples I’ve described in my previous two blog articles.

Suddenly, EWD.js testing and debugging has become a whole lot simpler!

 

Advertisements

3 comments

  1. Totally cool, Rob. As I move forward with these tools…this became a must. The first thing I was going to do today while I had a query running was to google “node.js testing simulator” or something similar. Your post was a better answer.

    Couple of things…took a long look at Mongo. I have no idea how fast it is but reading the description of the data structures, it sounds like mumps-2013. The notion of “embedded” data is right out of the globals playbook. If, under the hood, they actually parse the individual nodes of the JSON objects into B-trees…there is no inherent reason they could not match Cache/GTM speeds. Apparently, they must do something like this unlike other noSQL dbs that store document blocks. I don’t see any huge advantages to it at this point but those may emerge over time.

    Sorta miss mumps coding…but not so much. It seems clear that I may have to employ an IDE of some kind to keep all this stuff straight. It would be nice to be able to store js files in smaller increments and have an IDE build them into a single minimized file for deployment. Got any suggestions?

    Perhaps George James could extend Serengi?

    jb

  2. For the record, the testHarness works great.

  3. As far as IDEs for Node.js development is concerned, this question is actually currently being debated in the Node.js Google Group.

    https://groups.google.com/forum/#!topic/nodejs/tFDYbd7zLJo

    Several seem to be getting recommended:

    Cloud9 IDE: https://c9.io/
    I’ve used this before and it’s very nice, and a great demo of just what’s possible in a browser these days

    Sublime Text 2: http://www.sublimetext.com/2
    Not free but *very* popular amongst Node.js devs

    Vim is also popular! (not with me though!)

    Googling “IDE for Node.js” brings up lots of results

    I just use WinSCP and its basic notepad editor, but you can hook Notepad++ up to it also.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: