Skip to content
Browse files

Modified the existing test case by mocking it and adding correspondin…

…g files
  • Loading branch information...
1 parent d476b40 commit f30a9be9f6e86b18bb28bbbe83742333e0578c86 @mtipnis mtipnis committed Mar 26, 2012
View
85 modules/engine/test/engine-emitter-new-test.js
@@ -17,7 +17,10 @@
'use strict';
var _ = require('underscore'),
- Engine = require('../lib/engine');
+ Engine = require('../lib/engine'),
+ util = require('util'),
+ http = require('http'),
+ fs = require('fs');
module.exports = {
@@ -103,7 +106,8 @@ module.exports = {
config: __dirname + '/config/dev.json'
});
var script;
- script = 'select * from ebay.finding.items';
+ //The table below doesn't exist. The test checks for due errors hence.
+ script = 'select * from first';
var inFlight = 0, success = 0, error = 0;
engine.execute(script, function(req) {
req.on(Engine.Events.STATEMENT_IN_FLIGHT, function() {
@@ -124,37 +128,56 @@ module.exports = {
},
'select-ok': function(test) {
- var engine = new Engine({
- tables : __dirname + '/tables',
- config: __dirname + '/config/dev.json'
- });
- var script;
- script = 'select * from ebay.finding.items where keywords = "ipad"';
- var inFlight, success, error, request, response;
- engine.execute(script, function(req) {
- req.on(Engine.Events.STATEMENT_IN_FLIGHT, function() {
- inFlight = true;
- });
- req.on(Engine.Events.STATEMENT_SUCCESS, function() {
- success = true;
- });
- req.on(Engine.Events.STATEMENT_REQUEST, function() {
- request = true;
+ var server = http.createServer(function(req, res) {
+ var file = __dirname + '/mock' + req.url;
+ var stat = fs.statSync(file);
+ res.writeHead(200, req.headers, {
+ 'Content-Type' : 'application/json',
+ 'Content-Length' : stat.size
+ });
+ var readStream = fs.createReadStream(file);
+ util.pump(readStream, res, function(e) {
+ if(e) {
+ console.log(e.stack || e);
+ }
+ res.end();
});
- req.on(Engine.Events.STATEMENT_RESPONSE, function() {
- response = true;
- });
- req.on(Engine.Events.STATEMENT_ERROR, function() {
- error = true;
- });
- req.on('end', function() {
- test.ok(inFlight);
- test.ok(request);
- test.ok(response);
- test.ok(success, 'Failed');
- test.done();
+ });
+ server.listen(3000, function() {
+ var engine = new Engine({
+ tables : __dirname + '/tables',
+ config: __dirname + '/config/dev.json'
+ });
+ var script = fs.readFileSync(__dirname + '/mock/eng-emit2.ql', 'UTF-8');
+ var inFlight, success, error, request, response;
+ engine.execute(script, function(req) {
+ req.on(Engine.Events.STATEMENT_IN_FLIGHT, function() {
+ inFlight = true;
+ });
+ req.on(Engine.Events.STATEMENT_SUCCESS, function() {
+ success = true;
+ });
+ req.on(Engine.Events.STATEMENT_REQUEST, function() {
+ request = true;
+ });
+ req.on(Engine.Events.STATEMENT_RESPONSE, function() {
+ response = true;
+ });
+ req.on(Engine.Events.STATEMENT_ERROR, function() {
+ error = true;
+ });
+ req.on('end', function() {
+ test.ok(inFlight);
+ test.ok(request);
+ test.ok(response);
+ test.ok(success, 'Failed');
+ test.done();
+ server.close();
+ })
})
- })
+
+ });
+
},
'define': function(test) {
View
29 modules/engine/test/engine-emitter-test.js
@@ -18,7 +18,10 @@
var _ = require('underscore'),
Engine = require('../lib/engine'),
- EventEmitter = require('events').EventEmitter;
+ EventEmitter = require('events').EventEmitter,
+ http = require('http'),
+ fs = require('fs'),
+ util = require('util');
module.exports = {
@@ -134,7 +137,8 @@ module.exports = {
config: __dirname + '/config/dev.json'
});
var script;
- script = 'select * from ebay.finding.items';
+ //The table below doesn't exist. The test checks for due errors hence.
+ script = 'select * from first';
var emitter = new EventEmitter();
var inFlight = 0, success = 0, error = 0;
emitter.on(Engine.Events.STATEMENT_IN_FLIGHT, function() {
@@ -165,12 +169,27 @@ module.exports = {
},
'select-ok': function(test) {
+ var server = http.createServer(function(req, res) {
+ var file = __dirname + '/mock' + req.url;
+ var stat = fs.statSync(file);
+ res.writeHead(200, req.headers, {
+ 'Content-Type' : 'application/json',
+ 'Content-Length' : stat.size
+ });
+ var readStream = fs.createReadStream(file);
+ util.pump(readStream, res, function(e) {
+ if(e) {
+ console.log(e.stack || e);
+ }
+ res.end();
+ });
+ });
+ server.listen(3000, function() {
var engine = new Engine({
tables : __dirname + '/tables',
config: __dirname + '/config/dev.json'
});
- var script;
- script = 'select * from ebay.finding.items where keywords = "ipad"';
+ var script = fs.readFileSync(__dirname + '/mock/eng-emit2.ql', 'UTF-8');
var emitter = new EventEmitter();
var inFlight, success, error, request, response;
emitter.on(Engine.Events.STATEMENT_IN_FLIGHT, function() {
@@ -201,10 +220,12 @@ module.exports = {
test.ok(response);
test.ok(success, 'Failed');
test.done();
+ server.close();
}
}
}
);
+ });
},
'define': function(test) {
View
9 modules/engine/test/mock/eng-emit2.json
@@ -0,0 +1,9 @@
+{
+
+ "itemId": "280849783720",
+ "title": "New Apple iPad 3rd Generation 64GB, Wi-Fi + 4G (Unlocked) - Black (Latest Model)",
+ "primaryCategory": {
+ "categoryId": "171485",
+ "categoryName": "iPads, Tablets&eReaders"
+ }
+}
View
3 modules/engine/test/mock/eng-emit2.ql
@@ -0,0 +1,3 @@
+
+create table first on select get from "http://localhost:3000/eng-emit2.json"
+select * from first where keyword = "ipad";

0 comments on commit f30a9be

Please sign in to comment.
Something went wrong with that request. Please try again.