Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

events and protocol

  • Loading branch information...
commit 9afc7abb1d0def0b230fe67eb7738ac357fa9e59 1 parent 7b9d178
@apb2006 authored
View
71 readme.md → README.md
@@ -1,8 +1,7 @@
-[![build status](https://secure.travis-ci.org/apb2006/basex-node.png)](http://travis-ci.org/apb2006/basex-node)
# basex - A BaseX client for node.js
===========================
-This is BaseX client for node.js. It is work in progress.
+This is BaseX client for Node.js. It is work in progress.
[BaseX](http://basex.org/) is a very light-weight, high-performance and scalable
XML Database engine and XPath/XQuery 3.0 Processor,
@@ -10,7 +9,6 @@ This is BaseX client for node.js. It is work in progress.
Built as a lightweight Java server, BaseX also supports XSLT, Webdav and RestXQ.
-
## Installing the BaseX Node client
To install with npm:
@@ -21,7 +19,7 @@ To install with npm:
$ mkdir myproject
cd myproject
$ npm install basex
- basex@0.5.0 ./node_modules/basex
+ basex@0.6.0 ./node_modules/basex
```
Once BaseX is installed and the BaseX server is running, test it.
@@ -39,7 +37,7 @@ Once BaseX is installed and the BaseX server is running, test it.
## Installing BaseX
1. Java is required
1. [Download](http://basex.org/products/download/all-downloads/) and install BaseX
-(tested against version 7.3)
+(tested against version 7.5)
1. Run `basexserver -S`
## API specification
@@ -48,32 +46,47 @@ See [commands.md](https://github.com/apb2006/basex-node/blob/master/docs/command
in the docs folder for details of the API.
## Tests
-There is a test suite, using [vows](http://vowsjs.org/) .
+There is a test suite using [mocha](http://visionmedia.github.com/mocha/)
+, [should](https://github.com/visionmedia/should.js) and
+[sinon](http://sinonjs.org/).
```bash
- vows tests/* --spec
-
- ♢ BaseX interface test
-
- Request info
- ✓ we get no error
- ✓ we get a reply
- Send an valid xquery statement: 2+2
- ✓ we get no error
- ✓ and the answer is 4
- Send an invalid command: 2+
- ✓ we get an error
- Create a database
- ✓ we get no error
- Add a document
- ✓ we get no error
- Drop the database
- ✓ we get no error
- Send a xquery and iterate over the result items
- ✓ we get no error
- ✓ and the result is an array
-
- ✓ OK » 10 honored (0.253s)
+mocha -R spec test/test-commands.js
+
+
+ Execute info command
+ ✓ should not error
+ ✓ should have reply
+
+ Send an valid xquery statement: 2+2
+ ✓ It should not error
+ ✓ It should equal 4
+
+ Send an invalid command: 2+
+ ✓ It should error
+
+ Create a database
+ ✓ It should not error
+
+ Add a document
+ ✓ It should not error
+
+ drop db database
+ ✓ It should not error
+
+ drop db database
+ ✓ It should not error
+
+ Send a xquery and iterate over the result items
+ ✓ It should not error
+ ✓ It should return an array
+
+ create query and bind
+ ✓ It should not error
+ ✓ It should return a string
+
+ 13 tests complete (408 ms)
+
```
View
4 changelog.md
@@ -1,3 +1,7 @@
+## v0.6.0 - 2012-12-29
+- implement BaseX escape protocol
+- switch from Vows to Mocha for tests
+
## v0.5.1 - 2012-07-19
- readline fixed
View
10 docs/commands.md
@@ -74,18 +74,24 @@ Closes the session.
# The query object
##bind
- query.bind(name,value,callback);
+ query.bind(name,value,type,callback);
+Binds a `name` to a `value`. Currently `type` is ignored.
+
##close
query.close();
##results
query.results(callback);
+Returns results as an array.
+
##execute
query.execute(callback);
+
##info
query.info(callback);
+
##options
query.options(callback);
# Debugging
-The basex module variable `debug_mode` can be set to true to
+The `index.js` module variable `debug_mode` can be set to true to
print diagnostic info to the console.
View
46 examples/EventExample.js
@@ -10,31 +10,39 @@ var session1 = new basex.Session("localhost", 1984, "admin", "admin");
var session2 = new basex.Session("localhost", 1984, "admin", "admin");
basex.debug_mode = false;
+// create the event
+session1.execute("create event test_evt", afterCreate);
+// watch for it in second session
+function afterCreate(err, reply) {
+ console.log("running afterCreate...");
+ if (err)
+ console.log("Error: " + err);
+ session2.watch("test_evt", watchCallback, afterWatch);
+};
-session1.execute("create event messenger",afterEvent);
+// run long query in sessions, and fire event from session1
+function afterWatch(err, reply) {
+ console.log("running afterWatch...");
+ if (err)
+ console.log("Error: " + err, reply);
-function afterEvent(err, reply){
- if (err) console.log("Error: " + err);
- console.log("afterevent");
- session2.watch("messenger",watchCallback,afterWatch);
+ var xq = "for $i in 1 to 10000000 where $i=7 return $i"
+ session2.query(xq).execute(d.printMsg("S2:execute complete"));
+ session1.query("db:event('test_evt', 'Hello World!')").execute(
+ d.printMsg("S1:event sent"));
};
-function afterWatch(err, reply){
- if (err)console.log("Error: " + err);
- console.log("afterwatch");
- var xq="for $i in 1 to 10000000 where $i=0 return $i"
- session2.query(xq).execute(d.printMsg("S2:execute"));
- session1.query("db:event('messenger', 'Hello World!')").execute(d.printMsg("S1:event"));
+// on event received show and unwatch
+function watchCallback(name, msg) {
+ console.log("watch event received-----> ", msg)
+ session2.unwatch("test_evt", teardown);
};
-
-function watchCallback(name,msg){
- console.log("watch update-----> ",msg)
- session2.unwatch("messenger",function(){
- session1.execute("drop event messenger",d.printMsg("S1:drop event"));
+// close all
+function teardown(err, reply) {
+ console.log("unwatch:", err, reply)
+ session1.execute("drop event test_evt", d.printMsg("S1:drop event"));
// close session
session1.close(d.printMsg("S1:close"));
- session2.close(d.printMsg("S2:close"));
- });
-
+ session2.close(d.printMsg("S2:close"));
};
View
16 examples/RawExample.js
@@ -9,10 +9,20 @@ function print(err, reply) {
if (err) {
console.log("Error: " + err);
} else {
- console.log(reply);
+ console.log("Reply: ",reply);
+ var str = reply.result;
+ var buf = new Buffer(str.length);
+
+ for (var i = 0; i < str.length ; i++) {
+ buf[i] = str.charCodeAt(i);
+ }
+
+ console.log(buf);
}
};
-var xq="let $av:= (0 to 127,-128 to -1)!xs:byte(.)" +
- " return convert:bytes-to-hex($av)";
+//var xq="let $av:= (0 to 255)!xs:unsignedByte(.)" +
+var xq="let $av:=(0 to 127,-127 to -1)!xs:byte(.)" +
+ " return convert:bytes-to-base64($av)";
+
client.execute("xquery declare option output:method 'raw';"+xq,print);
client.close();
View
28 examples/bxstream.js
@@ -0,0 +1,28 @@
+// test basexstream class
+var bxs=require("../lib/basexstream");
+var fs = require('fs');
+var rs=new bxs.ReceiveStream();
+
+
+rs.on("data",function(data){
+console.log(data)
+});
+rs.on("marker",function(data){
+console.log("marker")
+});
+
+var writeStream = fs.createWriteStream('someFile.txt', { flags : 'w' });
+writeStream.on('close', function () {
+ console.log('All done!');
+});
+rs.pipe(writeStream);
+var b=new Buffer("aaaaaaaaaaaaaaaaaaab\x00c\xFF\x00fg\x0099","binary");
+rs.write(b);
+console.log("-------------------------")
+
+var ss=new bxs.SendStream();
+ss.on("data",function(data){
+console.log(data)
+});
+var b=new Buffer("aaaaa\x00bbb\xFFcccc ","binary");
+ss.write(b);
View
1  examples/manysessions.js
@@ -1,4 +1,5 @@
// start multiple sessions
+// fails with around 200 sessions
var basex =require("../index");
var log = require("../debug");
basex.debug_mode = false;
View
2  examples/raw.js
@@ -1,5 +1,5 @@
/*
- * This example shows the use of raw data. (NOT working)
+ * This example shows the use of raw data.
*/
var basex = require("../index");
var client = new basex.Session();
View
86 index.js
@@ -4,7 +4,7 @@
*/
// set this to true to enable console.log msgs for all connections
-debug_mode = false;
+exports.debug_mode =false;
var net = require("net"),
util = require("util"),
@@ -47,80 +47,77 @@ var Session = function(host, port, username, password) {
this.event = null;
// initial parser for auth
this.parser = function() {
- return self.xx.need(["data"],false) //timestamp
+ return self.bxp.need(["data"],false) //timestamp
};
};
this.reset();
var stream = net.createConnection(this.port, this.host);
- stream.setEncoding('utf-8');
- this.xx=new parser2.parse(stream);
-
+ //stream.setEncoding('utf-8');
+ this.bxp=new parser2.parse(stream);
+ //this.bxp.on("data",function(d){console.log("ping",d.toString())});
this.stream = stream;
this.stream.on("connect", function() {
self.state = states.CONNECTING;
- if (debug_mode) {
+ if (exports.debug_mode) {
console.log(self.tag + ": stream connected");
}
});
this.stream.on("data", function(reply) {
- self.buffer += reply;
- if (debug_mode) {
+ if (exports.debug_mode) {
console.log(self.tag + "<<");
- console.dir(self.buffer);
+ console.dir(reply.toString());
}
-
- if (self.state == states.CONNECTING) {
+ if (self.state == states.CONNECTED) {
+ self.onData()
+ }else if (self.state == states.CONNECTING) {
var r=self.parser();
if(r){
- //console.log("$$$$$$$$$$$",r)
self.send(self.username+"\0");
var s = md5(md5(self.password) + r.data);
self.send(s+"\0");
self.state = states.AUTHORIZE;
}
} else if (self.state == states.AUTHORIZE) {
- if (!self.xx.popByte()){
- //console.log("data",self.xx.data,"buff: ",self.xx.buffer)
+ if (!self.bxp.popByte()){
+ //console.log("data",self.bxp.data,"buff: ",self.bxp.buffer)
throw "Access denied.";
}
self.state = states.CONNECTED;
- if (debug_mode) {
+ if (exports.debug_mode) {
console.log(self.tag + ": authorized");
}
self.emit("connect", 1);
self.sendQueueItem();
} else {
- self.onData()
+ throw "Bad state.";
}
});
// respond to data arrival
this.onData=function(){
// console.log("onData");
- var r;
- if (!self.current_command) return; //bug or not ??
- while ( r = self.current_command.parser()) {
- if (debug_mode) {
+ var r,cc=self.current_command;
+ while (cc && (r = cc.parser())) {
+ if (exports.debug_mode) {
console.log("response: ", r);
}
- if (self.current_command.callback) {
- self.current_command.callback(r.ok ? null : r.info, r);
+ if (cc.callback) {
+ cc.callback(r.ok ? null : r.info, r);
}
- self.current_command = self.q_sent.shift();
+ cc=self.current_command = self.q_sent.shift();
//console.log("next is:");
//console.dir(self.current_command);
- if (!self.current_command) break;
}
};
this.stream.on("error",socketError);
this.stream.on("close", function() {
- if (debug_mode) {
+ if (exports.debug_mode) {
console.log(self.tag + ": stream closed");
}
if (self.event) self.event.close()
@@ -144,7 +141,7 @@ var Session = function(host, port, username, password) {
if (typeof s === "function")
s = s();
- if (debug_mode) {
+ if (exports.debug_mode) {
console.log(self.tag + ">>");
console.dir(s );
}
@@ -156,17 +153,17 @@ var Session = function(host, port, username, password) {
// standard parser read 2 lines and byte
this.parser1 = function() {
- return self.xx.need([ "result", "info" ],true)
+ return self.bxp.need([ "result", "info" ],true)
};
// read status byte
this.parserOk = function() {
- return self.xx.popByte()
+ return self.bxp.popByte()
};
// read line and byte possible error info
this.parser2 = function() {
if(!self.parser2part){
- var r=self.xx.need(["result"],true)
+ var r=self.bxp.need(["result"],true)
if(!r)return
if(r.ok){
return {ok:true,result:r.result}
@@ -174,7 +171,7 @@ var Session = function(host, port, username, password) {
self.parser2part=r;
}
}else{
- var r=self.xx.need(["info"],false)
+ var r=self.bxp.need(["info"],false)
if(!r)return
var res={ok:false,
info:r.info,
@@ -199,32 +196,23 @@ var Session = function(host, port, username, password) {
};
// Creates a database from an input stream:
this.create = function(name, input, callback) {
- self.send_command({
- send : "\x08" + name + "\0" + input+"\0",
- parser : self.parser2,
- callback : callback
- });
+ self._dbop("\x08" ,path , input,callback)
};
// Adds a document to the current database from an input stream:
this.add = function(path, input, callback) {
- self.send_command({
- send : "\x09" + path + "\0" + input+"\0",
- parser : self.parser2,
- callback : callback
- });
+ self._dbop("\x09" ,path , input,callback)
};
// Replaces a document with the specified input stream:
this.replace = function(path, input, callback) {
- self.send_command({
- send : "\x0C" + path + "\0" + input+"\0",
- parser : self.parser2,
- callback : callback
- });
+ self._dbop("\x0C" ,path , input,callback)
};
// Stores raw data at the specified path:
this.store = function(path, input, callback) {
+ self._dbop("\x0D" ,path , input,callback)
+ };
+ this._dbop=function(op,path, input, callback) {
self.send_command({
- send : "\x0D" + path + "\0" + input+"\0",
+ send : op + path + "\0" + input+"\0",
parser : self.parser2,
callback : callback
});
@@ -260,8 +248,8 @@ var Session = function(host, port, username, password) {
};
// parse 1st watch response, expect port,id
- this.parsewatch = function() {
- var flds = parser.popmsg(self, [ "eport", "id" ],false)
+ this.parsewatch = function() {
+ var flds=self.bxp.need([ "eport", "id" ],false)
if (flds) {
if (self.event == null) {
self.event = new Watch(self.host, flds.eport,flds.id)
@@ -277,7 +265,7 @@ var Session = function(host, port, username, password) {
// wait info and connected
//console.log("qqqq",self.event.isConnected)
//console.log(".....parsewatch2",self.buffer)
- if (self.event.isConnected) return parser.popmsg(self, [ "info" ])
+ if (self.event.isConnected) return self.bxp.need([ "info" ],true)
};
// unwatch
View
84 lib/basexstream.js
@@ -1,35 +1,33 @@
/*
- * Send and receive streams for BaseX
+ * Send and receive streams for BaseX. They expect and emit Buffers
* SendStream: write 0xFF before 00 or FF
* ReceiveStream: read next byte if 0xFF is received
* andy bunce 2011-2012
* see http://loose-bits.com/2012/08/02/nodejs-read-write-streams-pipes.html
*/
-
+
+// ----------SendStream-------------------------
var SendStream = function() {
this.readable = true;
this.writable = true;
};
-
require("util").inherits(SendStream, require("stream"));
-/**
- *
- */
SendStream.prototype._transform = function(data) {
- // Here, we'll just shortcut to a string.
- console.log("dddddddddddddd")
- data = data ? data.toString() : "";
- var d2 = "";
+ var lastpos=0;
// write 0xFF before 00 or FF
+ var ef=new Buffer([255])
for ( var i = 0; i < data.length; i++) {
- var c = data.charAt(i)
- if (c == "\x00" || c == "\xFF" ) d2+="\xFF"
- d2 += c
+ var c = data[i]
+ if (c == 0 || c == 255 ) {
+ if(i>lastpos) this.emit("data", data.slice(lastpos, i-1));
+ this.emit("data", ef);
+ lastpos=i;
+ }
}
- // emit data event with transformed data.
- this.emit("data", d2);
+ // emit remaining data.
+ if(data.length>lastpos) this.emit("data", data.slice(lastpos));
};
/**
@@ -46,36 +44,36 @@ SendStream.prototype.end = function() {
this._transform.apply(this, arguments);
this.emit("end");
};
-// -----------------------------------
+
+
+// ----------ReceiveStream-------------------------
var ReceiveStream = function() {
this.readable = true;
this.writable = true;
this.ff=false; // true when ff received
};
-
require("util").inherits(ReceiveStream, require("stream"));
ReceiveStream.prototype._transform = function(data) {
- // Here, we'll just shortcut to a string.
- data = data ? data.toString() : "";
- var d2 = "";
+ if(!data)return
+ var lastpos=0;
// read next byte if 0xFF is received
for ( var i = 0; i < data.length; i++) {
-
- var c = data.charAt(i)
- if(c=="\x00" && ! this.ff){
- if(d2.length) this.emit("data", d2);
+ var c = data[i]
+ if(this.ff){
+ this.ff=false;
+ }else if(c==0 ){
+ if(i>lastpos) this.emit("data", data.slice(lastpos, i));
this.emit("marker"); //got \0 eom
- d2=""
- }else if(c=="\xFF"){
+ lastpos=i+1;
+ }else if(c==255){
this.ff=true;
- }else{
- d2 += c;
- this.ff=false;
+ if(i>lastpos) this.emit("data", data.slice(lastpos, i));
+ lastpos=i
}
}
- // emit data event with transformed data.
- if(d2.length) this.emit("data", d2);
+ // emit remaining.
+ if(data.length>lastpos) this.emit("data", data.slice(lastpos));
};
/**
@@ -92,5 +90,29 @@ ReceiveStream.prototype.end = function() {
this._transform.apply(this, arguments);
this.emit("end");
};
+
+//----------NopStream-------------------------------
+var NopStream = function () {
+ this.readable = true;
+ this.writable = true;
+};
+// Inherit from base stream class.
+require('util').inherits(NopStream, require('stream'));
+
+// Extract args to `write` and emit as `data` event.
+NopStream.prototype.write = function () {
+ var args = Array.prototype.slice.call(arguments, 0);
+ this.emit.apply(this, ['data'].concat(args))
+};
+
+// Extract args to `end` and emit as `end` event.
+NopStream.prototype.end = function () {
+ var args = Array.prototype.slice.call(arguments, 0);
+ this.emit.apply(this, ['end'].concat(args))
+};
+
+
+//-----------------------------------------------
+exports.NopStream = NopStream;
exports.SendStream = SendStream;
exports.ReceiveStream = ReceiveStream;
View
12 lib/parser2.js
@@ -9,10 +9,7 @@ var BaseXStream = require("./basexstream"),
util = require("util"),
events = require("events");
-// @param stream
-// take fields named in array flds delimited by \0 from buffer
-// @param popStatus boolean if true read 1byte status 0/1 into ok:
-// @return object or empty if message not fully present in buffer.
+// @param stream socket stream
var parse = function(stream) {
var self=this;
var r=new BaseXStream.ReceiveStream();
@@ -24,8 +21,7 @@ var parse = function(stream) {
});
r.on("data",function(data){
self.buffer+=data;
- console.log("<<EE<<")
- console.dir(data);
+ self.emit("data",data) // not currently used
});
stream.pipe(r);
this.receive=r;
@@ -33,7 +29,9 @@ var parse = function(stream) {
this.data=[];
this.popStatus=false;
this.buffer="";
- // return {} with flds [ok] or null
+ // take fields named in array flds delimited by \0 from buffer
+ // @param popStatus boolean if true read 1byte status 0/1 into ok:
+ // @return object or empty if message not fully present in buffer.
this.need=function(flds,popStatus){
if(self.data.length<flds.length)return;
if(popStatus && this.buffer=="" && self.data.length==flds.length) return;
View
7 lib/query.js
@@ -11,7 +11,7 @@ function Query(session, query) {
this.cache = [];
this.state = "type";
var self = this;
- this.popByte=session.xx.popByte;
+ this.popByte=session.bxp.popByte;
this.close = function(callback) {
self.session.send_command({
send : function() { // send is function as needs id
@@ -47,6 +47,7 @@ function Query(session, query) {
// Iterator: returns array of items:
this.parseResults = function() {
+ var progress;
do {
// console.log("state",self.state,self.cache)
progress = false;
@@ -59,7 +60,7 @@ function Query(session, query) {
}
break
case "item":
- var r = self.session.xx.need(["data"],false);
+ var r = self.session.bxp.need(["data"],false);
if (r) {
self.state = "type";
self.cache.push(r.data);
@@ -81,7 +82,7 @@ function Query(session, query) {
}
break
case "error":
- var r = self.session.xx.need(["result"],false)
+ var r = self.session.bxp.need(["result"],false)
if (r) {
return {
ok : false,
View
9 lib/watch.js
@@ -2,7 +2,7 @@
* http://docs.basex.org/wiki/Server_Protocol
* andy bunce 2011-2012
*/
-var parser = require("./parser2"),
+var parser2 = require("./parser2"),
util = require("util"),
events = require("events"),
net = require("net");
@@ -33,7 +33,7 @@ function Watch(host,port,id ) {
};
this.parse = function() { // intial \0 from event
- var r = self.xx.popByte()
+ var r = self.bxp.popByte()
if (r.data!="\0")
throw "Bad event protocol";
this.isConnected=true;
@@ -43,7 +43,7 @@ function Watch(host,port,id ) {
// watch response
this.parseEvent = function() {
- var r = self.need([ "name", "msg" ], false)
+ var r = self.bxp.need([ "name", "msg" ], false)
if (r) {
var f = self.en[r.name];
f(r.name, r.msg)
@@ -51,8 +51,7 @@ function Watch(host,port,id ) {
};
var stream = net.createConnection(port, host);
- stream.setEncoding('utf-8');
- this.xx=new parser2.parse(stream);
+ this.bxp=new parser2.parse(stream);
stream.on("data", function(reply) {
if (exports.debug_mode) {
View
0  npm-debug.log
No changes.
View
13 package.json
@@ -1,14 +1,19 @@
{ "name" : "basex",
- "version" : "0.5.2",
+ "version" : "0.6.0",
"description" : "A BaseX (XML database) client library",
"keywords": ["xml", "xquery", "xslt", "search", "database" ],
"author": "Andy Bunce ",
"contributors": [ "Andy Bunce" ],
"main": "./index.js",
- "scripts": { "test": "vows tests/*.js" },
- "repository": { "type": "git","url": "git://github.com/apb2006/basex-node.git" },
+ "repository": { "type": "git"
+ ,"url": "git://github.com/apb2006/basex-node.git"
+ },
"dependencies" : { },
- "devDependencies": { "vows" : "0.6.x" },
+ "devDependencies": {
+ "mocha": "*"
+ ,"should": "*"
+ ,"sinon": "*"
+ },
"engines": { "node": ">= 0.6 " },
"license" : "BSD"
}
View
BIN  someFile.txt
Binary file not shown
View
0  tests/redsq.png → test/redsq.png
File renamed without changes
View
177 test/test-commands.js
@@ -0,0 +1,177 @@
+/* basex-node test interface using mocha
+ *
+ */
+
+var basex = require('../index.js');
+var should = require("should");
+
+var session = new basex.Session();
+
+describe('Execute info command', function() {
+ var reply, err;
+ before(function(done) {
+ session.execute("info", function(e, r) {
+ reply = r;
+ err = e;
+ done();
+ });
+ });
+
+ it('should not error', function() {
+ should.not.exist(err);
+ });
+ it('should have reply', function() {
+ should.exist(reply);
+ });
+
+});
+
+describe('Send an valid xquery statement: 2+2', function() {
+ var reply, err;
+ before(function(done) {
+ session.execute("xquery 2+2", function(e, r) {
+ reply = r;
+ err = e;
+ done();
+ });
+ });
+
+ it('It should not error', function() {
+ should.not.exist(err);
+ });
+ it('It should equal 4', function() {
+ reply.result.should.equal("4");
+ });
+
+});
+
+describe('Send an invalid command: 2+', function() {
+ var reply, err;
+ before(function(done) {
+ session.execute("xquery 2+", function(e, r) {
+ reply = r;
+ err = e;
+ done();
+ });
+ });
+
+ it('It should error', function() {
+ should.exist(err);
+ });
+});
+
+describe('Create a database', function() {
+ var reply, err;
+ before(function(done) {
+ session.execute("create db database", function(e, r) {
+ reply = r;
+ err = e;
+ done();
+ });
+ });
+
+ it('It should not error', function() {
+ should.not.exist(err);
+ });
+});
+
+describe('Add a document', function() {
+ var reply, err;
+ before(function(done) {
+ session.add("/world/World.xml", "<x>Hello World!</x>", function(e, r) {
+ reply = r;
+ err = e;
+ done();
+ });
+ });
+
+ it('It should not error', function() {
+ should.not.exist(err);
+ });
+});
+
+describe('drop db database', function() {
+ var reply, err;
+ before(function(done) {
+ session.execute("drop db database", function(e, r) {
+ reply = r;
+ err = e;
+ done();
+ });
+ });
+
+ it('It should not error', function() {
+ should.not.exist(err);
+ });
+});
+
+describe('drop db database', function() {
+ var reply, err;
+ before(function(done) {
+ session.execute("drop db database", function(e, r) {
+ reply = r;
+ err = e;
+ done();
+ });
+ });
+
+ it('It should not error', function() {
+ should.not.exist(err);
+ });
+});
+
+describe('Send a xquery and iterate over the result items', function() {
+ var reply, err;
+ before(function(done) {
+ var input = 'for $i in 1 to 10 return <xml>Text { $i }</xml>';
+ var query = session.query(input);
+
+ query.results(function(e, r) {
+ reply = r;
+ err = e;
+ done();
+ });
+ });
+
+ it('It should not error', function() {
+ should.not.exist(err);
+ });
+ it('It should return an array', function() {
+ reply.result.should.be.an.instanceof(Array);
+ });
+});
+
+describe('create query and bind ', function() {
+ var reply, err;
+ before(function(done) {
+ var input = "declare variable $name external; for $i in 1 to 10 return element { $name } { $i }";
+ var query = session.query(input);
+
+ // bind variable
+ query.bind("name", "nodex");
+
+ // print results
+ query.execute(function(e, r) {
+ reply = r;
+ err = e;
+ done();
+ });
+ });
+
+ it('It should not error', function() {
+ should.not.exist(err);
+ });
+ it('It should return a string', function() {
+ reply.result.should.be.a('string');
+ });
+});
+
+//-----------------------------
+beforeEach(function() {
+ // console.log('before every test')
+});
+
+after(function() {
+ session.close();
+ // console.log('after..');
+});
View
23 test/test-parser.js
@@ -0,0 +1,23 @@
+/* basex-node test parser using mocha
+ */
+var basex = require('../index.js');
+var should = require("should");
+var parser2 = require('../lib/parser2.js');
+var bxs = require('../lib/basexstream.js');
+
+
+
+describe('Parser test', function() {
+ var b= new Buffer("abc\0\0");
+ var s2=new bxs.NopStream()
+ it("should pop abc",function (){
+ var p=new parser2.parse(s2)
+ s2.write(b);
+ var r=p.need(["test"],false);
+ r.test.should.equal("abc");
+ });
+});
+
+after(function() {
+
+});
View
76 test/test-stress.js
@@ -0,0 +1,76 @@
+/* basex-node test interface using vows large data volumes
+ *
+ */
+
+var basex = require('../index.js');
+var should = require("should");
+
+var session = new basex.Session();
+
+describe('Send a xquery and iterate over the 10000 result items ', function() {
+ var reply, err;
+ before(function(done) {
+ // create query instance
+ var input = 'for $i in 1 to 10000 return <xml>Text { $i }</xml>';
+ var query = session.query(input);
+
+ query.results( function(e, r) {
+ reply = r;
+ err = e;
+ done();
+ });
+ });
+
+ it('should not error', function() {
+ should.not.exist(err);
+ });
+});
+
+describe('return megabyte result from execute ', function() {
+ var reply, err;
+ before(function(done) {
+ // create query instance
+ var input = 'xquery (1 to 100000)!"abcdefghij"';
+ session.execute(input, function(e, r) {
+ reply = r;
+ err = e;
+ done();
+ });
+ });
+
+ it('should not error', function() {
+ should.not.exist(err);
+ });
+});
+
+describe('return megabyte result from query ', function() {
+ var reply, err;
+ before(function(done) {
+ // create query instance
+ var input = 'xquery (1 to 100000)!"abcdefghij"';
+ // create query instance
+ var input = '(1 to 100000)!"abcdefghij"';
+ var query = session.query(input);
+
+ query.results( function(e, r) {
+ reply = r;
+ err = e;
+ done();
+ });
+ });
+
+ it('should not error', function() {
+ should.not.exist(err);
+ });
+});
+
+
+//-----------------------------
+beforeEach(function() {
+ // console.log('before every test')
+});
+
+after(function() {
+ session.close();
+ // console.log('after..');
+});
View
123 tests/test-commands.js
@@ -1,123 +0,0 @@
-/* basex-node test interface using vows
- *
- * @TODO teardown connection
- */
-
-var vows = require('vows')
- ,assert = require('assert')
- ,basex = require('../index.js');
-
-var session = new basex.Session();
-
-// Create a Test Suite
-vows.describe('BaseX interface test').addBatch({
- 'Request info' : {
- topic : function() {
- session.execute("info", this.callback);
- },
-
- 'we get no error' : function(err, reply) {
- assert.equal(err, null);
- },
- 'we get a reply' : function(err, reply) {
- assert.isObject(reply);
- }
- }
-}).addBatch({
- 'Send an valid xquery statement: 2+2' : {
- topic : function() {
- session.execute("xquery 2+2", this.callback);
- },
-
- 'we get no error' : function(err, reply) {
- assert.equal(err, null);
- },
- 'and the answer is 4' : function(err, reply) {
- assert.equal(reply.result, "4");
- }
- }
-}).addBatch({
- 'Send an invalid command: 2+' : {
- topic : function() {
- session.execute("xquery 2+", this.callback);
- },
-
- 'we get an error' : function(err, reply) {
- assert.notEqual(err, null);
- }
- }
-}).addBatch({
- 'Create a database ' : {
- topic : function() {
- session.execute("create db database", this.callback);
- },
-
- 'we get no error' : function(err, reply) {
- assert.equal(err, null);
- }
- }
-}).addBatch(
- {
- 'Add a document ' : {
- topic : function() {
- session.add("/world/World.xml", "<x>Hello World!</x>",
- this.callback);
- },
-
- 'we get no error' : function(err, reply) {
- assert.equal(err, null);
- }
- }
- }).addBatch({
- 'Drop the database ' : {
- topic : function() {
- session.execute("drop db database", this.callback);
- },
-
- 'we get no error' : function(err, reply) {
- assert.equal(err, null);
- }
- }
-}).addBatch({
- 'Send a xquery and iterate over the result items ' : {
- topic : function() {
- // create query instance
- var input = 'for $i in 1 to 10 return <xml>Text { $i }</xml>';
- var query = session.query(input);
-
- query.results(this.callback);
- },
-
- 'we get no error' : function(err, reply) {
- assert.equal(err, null);
-
- },
- 'and the result is an array' : function(err, reply) {
- assert.isArray(reply.result);
- }
- }
-}).addBatch({
- 'create query and bind ' : {
- topic : function() {
- // create query instance
- // create query instance
- var input = "declare variable $name external; for $i in 1 to 10 return element { $name } { $i }";
- var query = session.query(input);
-
- // bind variable
- query.bind("name", "nodex");
-
- // print results
- query.execute(this.callback);
-
- },
-
- 'we get no error' : function(err, reply) {
- assert.equal(err, null);
-
- },
- 'and the result is an array' : function(err, reply) {
- assert.isString(reply.result);
- }
- }
-}).export(module); // Run it
View
51 tests/test-parser.js
@@ -1,51 +0,0 @@
-/* basex-node test parser using vows
- */
-var vows = require('vows')
- , assert = require('assert')
- ,parser = require('../lib/parser.js');
-
-//Create a Test Suite
-vows.describe('Parser test').addBatch({
- 'read fld' : {
- topic : {buffer:"abc\0\0"},
-
- 'got a fld' : function(topic){
- var r=parser.popmsg(topic,["test"])
- assert.equal(r.test, "abc");
- }
- },
- "after read":{
- topic : {buffer:"abc\0\0"},
-
- 'buffer changed' : function(topic){
- var r=parser.popmsg(topic,["test"])
- assert.equal(topic.buffer, "");
- }
- },
- "status":{
- topic : {buffer:"\0"},
-
- 'got' : function(topic){
- var r=parser.popmsg(topic,[])
- assert.equal(r.ok, true);
- }
- },
- "status wrong":{
- topic : {buffer:"\2"},
-
- 'error thrown' : function(topic){
- assert.throws(function (){
- var r=parser.popmsg(topic,[])
- },"expected status marker")
- }
- }
-}).addBatch({
- 'no status' : {
- topic : {buffer:'a\0b\0'},
-
- 'is parsed' : function(topic) {
- var r=parser.popmsg(topic,["name","msg"],false);
- assert.isObject(r);
- }
- }
-}).export(module); // Run it
View
50 tests/test-stress.js
@@ -1,50 +0,0 @@
-/* basex-node test interface using vows large data volumes
- *
- * @TODO teardown connection
- */
-
-var vows = require('vows')
- ,assert = require('assert')
- ,basex = require('../index.js');
-
-var session = new basex.Session();
-
-// Create a Test Suite
-vows.describe('BaseX stress tests').addBatch({
- 'Send a xquery and iterate over the 10000 result items ' : {
- topic : function() {
- // create query instance
- var input = 'for $i in 1 to 10000 return <xml>Text { $i }</xml>';
- var query = session.query(input);
-
- query.results(this.callback);
- },
-
- 'we get no error' : function(err, reply) {
- assert.equal(err, null);
- }
- },
- 'return megabyte result from Query' : {
- topic : function() {
- // create query instance
- var input = '(1 to 100000)!"abcdefghij"';
- var query = session.query(input);
-
- query.results(this.callback);
- },
-
- 'we get no error' : function(err, reply) {
- assert.equal(err, null);
- }
- },
- 'return megabyte execute result ' : {
- topic : function() {
- var input = 'xquery (1 to 100000)!"abcdefghij"';
- session.execute(input,this.callback);
- },
-
- 'we get no error' : function(err, reply) {
- assert.equal(err, null);
- }
- }
-}).export(module); // Run it
Please sign in to comment.
Something went wrong with that request. Please try again.