Browse Source

[temp]

temp-branch
Muthu Kumar 6 years ago
parent
commit
81c541a8d2
  1. 26
      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. 39
      lib/units/processUnit.js
  9. 5
      package.json
  10. 1336
      shrinkwrap.yaml
  11. 1
      temp.js

26
lib/compiler.js

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

4
lib/createAssertions.js

@ -1,6 +1,7 @@
const Shotgun = require('shotgun-query');
module.exports = (expectable, expect, docs) => {
const unitExpects = [];
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");
/* Resource creator */
module.exports = context => {
const mongoose = new Mongoose.Mongoose();
mongoose.connect(
context.connectionString,
@ -17,3 +19,17 @@ module.exports = context => {
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 db = require('./db');
const seed = require('./seed');
module.exports = {
request,
db,
seed,
};

15
lib/resources/request.js

@ -1,3 +1,4 @@
/* Resource Creator */
module.exports = context => {
const chai = require('chai');
@ -10,3 +11,17 @@ module.exports = context => {
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 processUnit = require('./processUnit');
module.exports = instance => test => {
instance.test(test.description, async (expect, state) => {
module.exports = (instance, expect) => test => {
instance.test(test.description, async state => {
const response = await processUnit(test, state);
let expects = [];

39
lib/units/processUnit.js

@ -1,39 +1,12 @@
const requestor = async (unit, request) => {
switch (unit.method) {
const resources = require('../resources');
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,
);
module.exports = async (unit, state) => {
return !exec ? await action : action.exec();
const instances = {
request: state['@start'].request,
db: state['@start'].db,
};
module.exports = async (unit, state) => {
const [ request, db ] = state['@start'];
switch (unit.type) {
case 'request':
return await requestor(unit, request);
case 'db':
return await dbAction(unit, db);
default:
throw new Error(
`Unknown before hook type: ${unit.type}`
);
return resources[unit.type].do(unit, instances);
}
};

5
package.json

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

1336
shrinkwrap.yaml

File diff suppressed because it is too large

1
temp.js

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