Repository navigation
Expand file tree
/
Copy pathmodel-createAsync.js
More file actions
113 lines (93 loc) · 2.87 KB
/
Copy pathmodel-createAsync.js
File metadata and controls
113 lines (93 loc) · 2.87 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
var should = require('should');
var helper = require('../support/spec_helper');
describe("Model.createAsync()", 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 () {
return Person.createAsync({
name : "John Doe"
})
.then(function (John) {
John.should.have.property("name", "John Doe");
})
});
});
describe("if passing an array", function () {
before(setup());
it("should accept it as a list of items to create", function () {
return Person.createAsync([{
name : "John Doe"
}, {
name : "Jane Doe"
}])
.then(function (people) {
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");
})
});
});
describe("if element has an association", function () {
before(setup());
it("should also create it or save it", function () {
return Person.createAsync({
name : "John Doe",
pets : [ new Pet({ name: "Deco" }) ]
})
.then(function (John) {
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);
should.equal(John.pets[0].saved(), true);
});
});
it("should also create it or save it even if it's an object and not an instance", function () {
return Person.createAsync({
name : "John Doe",
pets : [ { name: "Deco" } ]
})
.then(function (John) {
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);
should.equal(John.pets[0].saved(), true);
});
});
});
describe("when not passing a property", function () {
before(setup());
it("should use defaultValue if defined", function () {
return Pet.createAsync({})
.then(function (Mutt) {
Mutt.should.have.property("name", "Mutt");
});
});
});
});