-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathdatabase.products.js
More file actions
164 lines (129 loc) · 6.2 KB
/
Copy pathdatabase.products.js
File metadata and controls
164 lines (129 loc) · 6.2 KB
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
152
153
154
155
156
157
158
159
160
161
162
163
164
const should = require('should');
const flow = require('nimble');
const requireNew = require('require-new');
const wipe = require('./fixture-helpers/wipe');
const setup = require('./fixture-helpers/setup');
const shallowClone = require('./fixture-helpers/test-utils').shallowClone;
const DuplicateRecordError = require('../lib/errors').DuplicateRecordError;
const ValidationError = require('../lib/errors').ValidationError;
describe("Database", function() {
const user1 = requireNew('./fixtures/user1.json');
before(function(initDone) {
flow.series(
[
wipe.wipeAllData,
function(done) {
setup.createUser(user1, done);
}
],
initDone
);
});
describe("Products", function() {
const product4 = requireNew('./fixtures/product4.json');
const product5 = requireNew('./fixtures/product5.json');
describe("Create", function() {
it("Should be able to create a product with a null creator", function(done) {
global.db.products.create(product4, null, function(err, product) {
should.not.exist(err);
should.exist(product);
product.should.have.property('insertId');
product.should.have.property('name', product4['name']);
// remember the insert ID
product4['id'] = product.insertId;
done();
});
});
it("Should fail to create a product with a name that doesn't contain at least one letter", function(done) {
const invalidProduct = shallowClone(product4);
invalidProduct['name'] = "4242";
global.db.products.create(invalidProduct, null, function(err, product) {
should.exist(err);
should.not.exist(product);
err.should.be.an.instanceOf(ValidationError);
err.should.have.property('data');
err.data.errors.should.have.length(1);
err.data.errors[0].should.have.properties({
keyword : 'pattern',
dataPath : '.name',
schemaPath : '#/properties/name/pattern'
});
done();
});
});
it("Should be able to create a product with a non-null creator", function(done) {
global.db.products.create(product5, user1['id'], function(err, product) {
should.not.exist(err);
should.exist(product);
product.should.have.property('insertId');
product.should.have.property('name', product5['name']);
// remember the insert ID
product5['id'] = product.insertId;
done();
});
});
it("Should fail to create a duplicate product", function(done) {
global.db.products.create(product4, user1['id'], function(err, product) {
should.exist(err);
should.not.exist(product);
err.should.be.an.instanceOf(DuplicateRecordError);
err.should.have.property("data");
err.data.should.have.property("code", "ER_DUP_ENTRY");
done();
});
});
}); // End Create
describe("Find", function() {
it("Should be able to find a product by name", function(done) {
global.db.products.findByName(product4['name'], null, function(err, product) {
should.not.exist(err);
should.exist(product);
product.should.have.properties({
id : product4['id'],
name : product4['name'],
prettyName : product4['prettyName'],
vendor : product4['vendor'],
description : product4['description'],
creatorUserId : null
});
product.should.have.properties('created', 'modified');
// do a deep equal
should(JSON.parse(product['defaultChannelSpecs'])).eql(product4['defaultChannelSpecs']);
done();
});
});
it("Should be able to find a product by ID", function(done) {
global.db.products.findById(product5['id'], null, function(err, product) {
should.not.exist(err);
should.exist(product);
product.should.have.properties({
id : product5['id'],
name : product5['name'],
prettyName : product5['prettyName'],
vendor : product5['vendor'],
description : product5['description'],
creatorUserId : user1['id']
});
product.should.have.properties('created', 'modified');
// do a deep equal
should(JSON.parse(product['defaultChannelSpecs'])).eql(product5['defaultChannelSpecs']);
done();
});
});
it("Should not be able to find a product by a non-existent name", function(done) {
global.db.products.findByName("bogus", null, function(err, product) {
should.not.exist(err);
should.not.exist(product);
done();
});
});
it("Should not be able to find a product by a non-existent ID", function(done) {
global.db.products.findById(-1, null, function(err, product) {
should.not.exist(err);
should.not.exist(product);
done();
});
});
}); // End Find
}); // End Products
}); // End Database