Repository navigation
Expand file tree
/
Copy pathdb.js
More file actions
178 lines (135 loc) · 3.54 KB
/
Copy pathdb.js
File metadata and controls
178 lines (135 loc) · 3.54 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
var should = require('should');
var helper = require('../support/spec_helper');
var ORM = require('../../');
describe("db.use()", function () {
var db = null;
before(function (done) {
helper.connect(function (connection) {
db = connection;
return done();
});
});
after(function () {
return db.close();
});
it("should be able to register a plugin", function (done) {
var MyPlugin = require("../support/my_plugin");
var opts = {
option : true,
calledDefine : false
};
db.use(MyPlugin, opts);
var MyModel = db.define("my_model", { // db.define should call plugin.define method
property: String
});
opts.calledDefine.should.be.true;
return done();
});
it("a plugin should be able to catch models before defining them", function (done) {
var MyPlugin = require("../support/my_plugin");
var opts = {
option : true,
calledDefine : false,
beforeDefine : function (name, props, opts) {
props.otherprop = Number;
}
};
db.use(MyPlugin, opts);
var MyModel = db.define("my_model", { // db.define should call plugin.define method
property: String
});
opts.calledDefine.should.be.true;
MyModel.properties.should.have.property("otherprop");
return done();
});
it("should be able to register a plugin as string", function (done) {
var opts = {
option : true,
calledDefine : false
};
db.use("../support/my_plugin", opts);
var MyModel = db.define("my_model", { // db.define should call plugin.define method
property: String
});
opts.calledDefine.should.be.true;
return done();
});
});
describe("db.define()", function() {
var db = null;
before(function (done) {
helper.connect(function (connection) {
db = connection;
return done();
});
});
after(function () {
return db.close();
});
it("should use setting model.namePrefix as table prefix if defined", function (done) {
db.settings.set("model.namePrefix", "orm_");
var Person = db.define("person", {
name: String
});
Person.table.should.equal("orm_person");
return done();
});
});
describe("db.load()", function () {
var db = null;
before(function (done) {
helper.connect(function (connection) {
db = connection;
return done();
});
});
after(function () {
return db.close();
});
it("should require a file based on relative path", function (done) {
db.load("../support/spec_load", function () {
db.models.should.have.property("person");
db.models.should.have.property("pet");
return done();
});
});
});
describe("db.serial()", function () {
var db = null;
before(function (done) {
helper.connect(function (connection) {
db = connection;
return done();
});
});
after(function () {
return db.close();
});
it("should be able to execute chains in serial", function (done) {
var Person = db.define("person", {
name : String,
surname : String
});
helper.dropSync(Person, function () {
Person.create([
{ name : "John", surname : "Doe" },
{ name : "Jane", surname : "Doe" }
], function () {
db.serial(
Person.find({ surname : "Doe" }),
Person.find({ name : "John" })
).get(function (err, DoeFamily, JohnDoe) {
should.equal(err, null);
should(Array.isArray(DoeFamily));
should(Array.isArray(JohnDoe));
DoeFamily.length.should.equal(2);
JohnDoe.length.should.equal(1);
DoeFamily[0].surname.should.equal("Doe");
DoeFamily[1].surname.should.equal("Doe");
JohnDoe[0].name.should.equal("John");
return done();
});
});
});
});
});