This repository has been archived by the owner on Jan 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtest.js
151 lines (128 loc) · 5.49 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
const test = require('tape');
const Joi = require('joi');
const Mongoose = require('mongoose');
const getItReady = require('./index');
const automaticSchemaDefinition = {
name: { type: String, required: false, joi: Joi.string() },
firstName: { type: String, required: true, joi: Joi.string() },
lastName: { type: String, required: true, joi: Joi.string() },
status: { type: String, required: false, joi: Joi.string() },
photo: { type: String, required: false, joi: Joi.string() },
createdOn: { type: Date, required: false, default: Date.now, joi: Joi.date() }
};
const manualSchemaDefinition = {
firstName: { type: String, required: true, joi: Joi.string() },
lastName: { type: String, required: true, joi: Joi.string() },
createdOn: { type: Date, required: false, default: Date.now, joi: Joi.date() }
};
let result = null;
let manual = {};
function teardown(){
Mongoose.connection.close()
}
function testWrapper (description, fn) {
test(description, function (t) {
// setup(); //Will be needed later
fn(t);
teardown();
});
}
testWrapper('getItReady', function (t) {
t.doesNotThrow(function () {
result = getItReady(automaticSchemaDefinition, 'persons', 'Person', 'person')
}, 'should not throw error');
t.end();
});
testWrapper('results', function (t) {
t.equal(typeof result, 'object', 'Result should be an object')
t.deepEqual(
Object.keys(result),
['validations', 'schema', 'model', 'controllers', 'routes'],
'Result should have keys: schema, model, controllers, results'
)
t.end()
})
testWrapper('results.schema', function (t) {
t.equal(typeof result.schema, 'object', 'should be an object')
t.ok(result.schema instanceof Mongoose.Schema, 'should be the instanceof Mongoose.Schema')
t.end()
})
testWrapper('results.model', function (t) {
t.ok(result.model, 'should exist')
t.equal(result.model.modelName, 'Person', 'should have the supplied model name')
t.end()
})
testWrapper('results.controllers', function (t) {
// console.log( Object.keys( result.controllers ) )
t.equal(typeof result.controllers, 'object', 'should be an object')
t.deepEqual(Object.keys(result.controllers), [ 'getAll', 'getOne', 'create', 'update', 'remove' ])
t.end()
})
testWrapper('results.routes', function (t) {
t.equal(typeof result.routes, 'object')
t.end()
})
testWrapper('manual control', function (t) {
t.ok(getItReady.separateJoiValidationObject, 'should have separateJoiValidationObject method')
t.ok(getItReady.getControllers, 'should have getControllers method')
t.ok(getItReady.getRoutes, 'should have getRoutes method')
t.ok(getItReady.getModel, 'should have getModel method')
t.ok(getItReady.getSchema, 'should have getSchema method')
t.end();
});
testWrapper('manual: separateJoiValidationObject ', function (t) {
manual.separateJoiValidationObject = getItReady.separateJoiValidationObject(manualSchemaDefinition);
var keys = Object.keys(manualSchemaDefinition);
t.ok(manual.separateJoiValidationObject.schema, 'should have a schema object');
t.deepEqual(Object.keys(manual.separateJoiValidationObject.schema), keys, 'new schema should have same key as input')
t.ok(manual.separateJoiValidationObject.post, 'should have a post obejct')
t.deepEqual(Object.keys(manual.separateJoiValidationObject.post), keys, 'post should have same key as input')
t.ok(manual.separateJoiValidationObject.put, 'should have a put object')
t.deepEqual(Object.keys(manual.separateJoiValidationObject.put), keys, 'put schema should have same key as input')
t.end();
});
testWrapper('manual: getSchema ', function (t) {
t.doesNotThrow(function () {
manual.getSchema = getItReady.getSchema(manual.separateJoiValidationObject.schema);
} , 'should not throw error');
t.ok(manual.getSchema instanceof Mongoose.Schema, 'should return a valid Schema object');
t.end();
});
testWrapper('manual: getModel ', function (t) {
t.doesNotThrow(function () {
manual.getModel = getItReady.getModel( 'SchemaName', manual.getSchema);
} , 'should not throw error');
t.ok(manual.getModel, 'should return a valid Model object');
t.equal(manual.getModel.modelName, 'SchemaName', 'should have model name as specified');
t.end();
});
testWrapper('manual: getControllers ', function (t) {
t.doesNotThrow(function () {
manual.getControllers = getItReady.getControllers(manual.getModel, manual.separateJoiValidationObject);
} , 'should not throw error');
t.ok(manual.getControllers, 'should return controllers collection');
t.deepEqual(
Object.keys(manual.getControllers),
['getAll', 'getOne', 'create', 'update', 'remove'],
'should have required controller methods'
);
t.end();
});
testWrapper('manual: getRoutes ', function (t) {
t.doesNotThrow(function () {
manual.getRoutes = getItReady.getRoutes(manual.getControllers, 'persons', 'person');
} , 'should not throw error');
t.ok(manual.getRoutes, 'should return routes collection');
t.equal( manual.getRoutes.length, 5, 'should have desired number of routes');
var methods = ['GET', 'POST', 'PUT', 'DELETE']
for (var i = manual.getRoutes.length - 1; i >= 0; i--) {
var index = methods.indexOf(manual.getRoutes[i].method);
t.ok( index >= 0, 'should have a valid HTTP method');
t.ok(manual.getRoutes[i].path.match('/persons'), 'should have a valid HTTP method');
//vrey brittle and crude way to test path, may be enhanced later
if( i < 4 && i > 0 ){
t.ok(manual.getRoutes[i].path.match('/persons/{id}'), 'should have good path for '+methods[index]);
}
}
t.end();
});