Browse Source

[temp]

temp-branch
Muthu Kumar 6 years ago
parent
commit
81c541a8d2
  1. 30
      lib/compiler.js
  2. 4
      lib/createAssertions.js
  3. 16
      lib/resources/db.js
  4. 2
      lib/resources/index.js
  5. 15
      lib/resources/request.js
  6. 56
      lib/resources/seed.js
  7. 4
      lib/units/convertTest.js
  8. 41
      lib/units/processUnit.js
  9. 5
      package.json
  10. 1338
      shrinkwrap.yaml
  11. 1
      temp.js

30
lib/compiler.js

@ -1,4 +1,4 @@
const Gunner = require('@klenty/gunner'); const { Gunner, expect } = require('@klenty/gunner');
const convertHook = require('./units/convertHook'); const convertHook = require('./units/convertHook');
const convertTest = require('./units/convertTest'); const convertTest = require('./units/convertTest');
@ -7,27 +7,31 @@ module.exports = strategy => plan => {
const gunner = new Gunner(plan.name); const gunner = new Gunner(plan.name);
gunner.before(Gunner.Start, () => { gunner.before(
return strategy.createResource( Gunner.Start,
() => strategy.createResource(
'request', 'request',
plan.resources.request, plan.resources.request,
); ),
}); 'request',
);
gunner.before(Gunner.Start, () => { gunner.before(
return strategy.createResource( Gunner.Start,
() => strategy.createResource(
'db', 'db',
plan.resources.db, plan.resources.db,
); ),
}); 'db',
);
const addHook = convertHook(gunner); const addHook = convertHook(gunner, expect);
// Convert 'before' hooks // Convert 'before' hooks
(plan.before || []).forEach(addHook('before')); (plan.before || []).forEach(addHook('before'));
// Convert tests // Convert tests
(plan.tests || []).forEach(convertTest(gunner)); (plan.tests || []).forEach(convertTest(gunner, expect));
// Convert 'after' hooks // Convert 'after' hooks
(plan.after || []).forEach(addHook('after')); (plan.after || []).forEach(addHook('after'));
@ -37,8 +41,8 @@ module.exports = strategy => plan => {
&& db.__mongoose__.connection.close(); && db.__mongoose__.connection.close();
}; };
gunner.after(Gunner.End, state => { gunner.before(Gunner.End, state => {
const [ , db ] = state['@start']; const { db } = state['@start'];
return closeConnections(db); return closeConnections(db);
}); });

4
lib/createAssertions.js

@ -1,6 +1,7 @@
const Shotgun = require('shotgun-query'); const Shotgun = require('shotgun-query');
module.exports = (expectable, expect, docs) => { module.exports = (expectable, expect, docs) => {
const unitExpects = []; const unitExpects = [];
if (expectable.checks) { if (expectable.checks) {
@ -16,5 +17,6 @@ module.exports = (expectable, expect, docs) => {
} }
return Promise.all(unitExpects); return expect.expectMany(unitExpects);
}; };

16
lib/resources/db.js

@ -1,6 +1,8 @@
const Mongoose = require("mongoose"); const Mongoose = require("mongoose");
/* Resource creator */
module.exports = context => { module.exports = context => {
const mongoose = new Mongoose.Mongoose(); const mongoose = new Mongoose.Mongoose();
mongoose.connect( mongoose.connect(
context.connectionString, context.connectionString,
@ -17,3 +19,17 @@ module.exports = context => {
return db; return db;
}; };
/* Process resource instance */
module.exports.do = async (unit, { db }) => {
if (!unit.hasOwnProperty('method')) unit.method = 'findOne';
const exec = [ 'create', 'insertMany' ].indexOf(unit.method) === -1;
const action = db[unit.collection][unit.method](
unit.query || unit.insert,
unit.update || unit.queryList,
);
return !exec ? await action : action.exec();
};

2
lib/resources/index.js

@ -1,7 +1,9 @@
const request = require('./request'); const request = require('./request');
const db = require('./db'); const db = require('./db');
const seed = require('./seed');
module.exports = { module.exports = {
request, request,
db, db,
seed,
}; };

15
lib/resources/request.js

@ -1,3 +1,4 @@
/* Resource Creator */
module.exports = context => { module.exports = context => {
const chai = require('chai'); const chai = require('chai');
@ -10,3 +11,17 @@ module.exports = context => {
return agent; return agent;
}; };
/* Process resource instance */
module.exports.do = async (unit, { request }) => {
switch (unit.method) {
case 'get':
return await request[unit.method](unit.path);
case 'post':
return await (request[unit.method](unit.path)
.type(unit.reqType || 'json')
.send(unit.body));
}
};

56
lib/resources/seed.js

@ -0,0 +1,56 @@
const fs = require('fs-extra');
const requireDirectory = require('require-directory');
const JSONT = require('@codefeathers/jsont');
/**
* Resource creator for 'seed' resource.
* @param {object} context
* @param {string} context.templates
* @param {object} options
* @param {string} options.basePath
*/
module.exports = (context, options) => {
if (typeof context.templates === 'object')
return context.templates;
if (typeof context.templates !== 'string')
throw new Error(
`${context.templates} is not a string!`
+ `\n^^^`
)
if (context.templates.slice(0, 3) !== '#!/')
throw new Error(
`template path does not start with '#!/'\n`
+ context.templates.slice(0, 10)
+ `\n^^^`
);
const templatesPath =
options.basePath + context.templates.slice(2);
return requireDirectory(
module,
options.basePath,
{ include: /.*template.json$/ },
);
};
const range = n => Array(n).fill(undefined).map((x, i) => i);
module.exports.do = (unit, { db }) => {
switch (unit.flow) {
case 'createUser':
return [range(unit.count).map(() => {
})].map(x => db['userdetails'].insertMany(x));
// case 'createProspect':
}
};

4
lib/units/convertTest.js

@ -1,8 +1,8 @@
const createAssertions = require('../createAssertions'); const createAssertions = require('../createAssertions');
const processUnit = require('./processUnit'); const processUnit = require('./processUnit');
module.exports = instance => test => { module.exports = (instance, expect) => test => {
instance.test(test.description, async (expect, state) => { instance.test(test.description, async state => {
const response = await processUnit(test, state); const response = await processUnit(test, state);
let expects = []; let expects = [];

41
lib/units/processUnit.js

@ -1,39 +1,12 @@
const requestor = async (unit, request) => { const resources = require('../resources');
switch (unit.method) {
case 'get':
return await request[unit.method](unit.path);
case 'post':
return await (request[unit.method](unit.path)
.type(unit.reqType || 'json')
.send(unit.body));
}
};
const dbAction = async (unit, db) => {
if (!unit.hasOwnProperty('method')) unit.method = 'findOne';
const exec = [ 'create', 'insertMany' ].indexOf(unit.method) === -1;
const action = db[unit.collection][unit.method](
unit.query || unit.insert,
unit.update || unit.queryList,
);
return !exec ? await action : action.exec();
};
module.exports = async (unit, state) => { module.exports = async (unit, state) => {
const [ request, db ] = state['@start'];
switch (unit.type) {
case 'request': const instances = {
return await requestor(unit, request); request: state['@start'].request,
case 'db': db: state['@start'].db,
return await dbAction(unit, db); };
default:
throw new Error( return resources[unit.type].do(unit, instances);
`Unknown before hook type: ${unit.type}`
);
}
}; };

5
package.json

@ -24,10 +24,13 @@
}, },
"homepage": "https://github.com/klenty/gunner-strategy-endpoint#readme", "homepage": "https://github.com/klenty/gunner-strategy-endpoint#readme",
"dependencies": { "dependencies": {
"@klenty/gunner": "^0.6.7", "@codefeathers/jsont": "^0.0.4",
"@klenty/gunner": "^0.9.0",
"chai": "^4.1.2", "chai": "^4.1.2",
"chai-http": "^4.0.0", "chai-http": "^4.0.0",
"fs-extra": "^7.0.0",
"mongoose": "^5.2.6", "mongoose": "^5.2.6",
"require-directory": "^2.1.1",
"shotgun-query": "^0.1.0" "shotgun-query": "^0.1.0"
} }
} }

1338
shrinkwrap.yaml

File diff suppressed because it is too large

1
temp.js

@ -0,0 +1 @@
new Promise(() => {}).then(console.log)
Loading…
Cancel
Save