Repository navigation
Expand file tree
/
Copy pathmodel-create.js
More file actions
127 lines (99 loc) · 2.79 KB
/
Copy pathmodel-create.js
File metadata and controls
127 lines (99 loc) · 2.79 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
var should = require('should');
var helper = require('../support/spec_helper');
var ORM = require('../../');
describe("Model.create()", function() {
var db = null;
var Pet = null;
var Person = null;
var setup = function () {
return function (done) {
Person = db.define("person", {
name : String
});
Pet = db.define("pet", {
name : { type: "text", defaultValue: "Mutt" }
});
Person.hasMany("pets", Pet);
return helper.dropSync([ Person, Pet ], done);
};
};
before(function (done) {
helper.connect(function (connection) {
db = connection;
return done();
});
});
after(function () {
return db.close();
});
describe("if passing an object", function () {
before(setup());
it("should accept it as the only item to create", function (done) {
Person.create({
name : "John Doe"
}, function (err, John) {
should.equal(err, null);
John.should.have.property("name", "John Doe");
return done();
});
});
});
describe("if passing an array", function () {
before(setup());
it("should accept it as a list of items to create", function (done) {
Person.create([{
name : "John Doe"
}, {
name : "Jane Doe"
}], function (err, people) {
should.equal(err, null);
should(Array.isArray(people));
people.should.have.property("length", 2);
people[0].should.have.property("name", "John Doe");
people[1].should.have.property("name", "Jane Doe");
return done();
});
});
});
describe("if element has an association", function () {
before(setup());
it("should also create it or save it", function (done) {
Person.create({
name : "John Doe",
pets : [ new Pet({ name: "Deco" }) ]
}, function (err, John) {
should.equal(err, null);
John.should.have.property("name", "John Doe");
should(Array.isArray(John.pets));
John.pets[0].should.have.property("name", "Deco");
John.pets[0].should.have.property(Pet.id);
John.pets[0].saved().should.be.true;
return done();
});
});
it("should also create it or save it even if it's an object and not an instance", function (done) {
Person.create({
name : "John Doe",
pets : [ { name: "Deco" } ]
}, function (err, John) {
should.equal(err, null);
John.should.have.property("name", "John Doe");
should(Array.isArray(John.pets));
John.pets[0].should.have.property("name", "Deco");
John.pets[0].should.have.property(Pet.id);
John.pets[0].saved().should.be.true;
return done();
});
});
});
describe("when not passing a property", function () {
before(setup());
it("should use defaultValue if defined", function (done) {
Pet.create({}, function (err, Mutt) {
should.equal(err, null);
Mutt.should.have.property("name", "Mutt");
return done();
});
});
});
});