/
insert_spec.coffee
156 lines (138 loc) · 5.41 KB
/
insert_spec.coffee
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
{ assert, vows, connect, setup, Model } = require("../helpers")
class Post extends Model
@collection "posts"
@field "title", String
@field "author_id"
@field "category", String
@field "created_at", Date
vows.describe("Model insert").addBatch
# -- Model.insert --
"Model.insert":
topic: ->
setup @callback
"POJO":
"single":
"no callback":
topic: ->
result = Post.insert(title: "Insert 3.1")
return result || "nothing"
"should return nothing": (result)->
assert.equal result, "nothing"
"new document":
topic: ->
Post.find title: "Insert 3.1", @callback
"should exist in database": (posts)->
assert.equal posts[0].title, "Insert 3.1"
"with callback":
topic: ->
Post.insert title: "Insert 3.2", @callback
"should pass document to callback": (post)->
assert.equal post.title, "Insert 3.2"
"should pass a POJO to callback": (post)->
assert !(post instanceof Post)
"should set document ID": (post)->
assert post._id
"new document":
topic: (post)->
Post.find post._id, @callback
"should exist in database": (post)->
assert.equal post.title, "Insert 3.2"
"multiple":
"no callback":
topic: ->
result = Post.insert([{ title: "Insert 3.3", category: "foo" }, { title: "Insert 3.3", category: "bar" }])
return result || "nothing"
"should return nothing": (result)->
assert.equal result, "nothing"
"new documents":
topic: ->
Post.find title: "Insert 3.3", @callback
"should exist in database": (posts)->
assert.lengthOf posts, 2
categories = (post.category for post in posts).join(" ")
assert.equal categories, "foo bar"
"with callback":
topic: ->
Post.insert [{ title: "Insert 3.4", category: "foo" }, { title: "Insert 3.4", category: "bar" }], @callback
"should pass documents to callback": (posts)->
for post in posts
assert.equal post.title, "Insert 3.4"
"should pass POJOs to callback": (posts)->
for post in posts
assert !(post instanceof Post)
"should set document ID": (posts)->
for post in posts
assert post._id
"new documents":
topic: (posts)->
ids = (post._id for post in posts)
Post.find ids, @callback
"should exist in database": (posts)->
assert.lengthOf posts, 2
categories = (post.category for post in posts).join(" ")
assert.equal categories, "foo bar"
"Model":
"no callback":
topic: ->
post = new Post(title: "Insert 3.5")
result = Post.insert(post)
return result || "nothing"
"should return nothing": (result)->
assert.equal result, "nothing"
"new document":
topic: ->
Post.find title: "Insert 3.5", @callback
"should exist in database": (posts)->
assert.equal posts[0].title, "Insert 3.5"
"with callback":
topic: (post)->
post = new Post(title: "Insert 3.6")
Post.insert post, @callback
"should pass document to callback": (post)->
assert.equal post.title, "Insert 3.6"
"should pass a model to callback": (post)->
assert.instanceOf post, Post
"should set document ID": (post)->
assert post._id
"new document":
topic: (post)->
Post.find post._id, @callback
"should exist in database": (post)->
assert.equal post.title, "Insert 3.6"
"multiple":
"no callback":
topic: ->
posts = [new Post( title: "Insert 3.7", category: "foo" ), new Post( title: "Insert 3.7", category: "bar" )]
result = Post.insert(posts)
return result || "nothing"
"should return nothing": (result)->
assert.equal result, "nothing"
"new documents":
topic: ->
Post.find title: "Insert 3.7", @callback
"should exist in database": (posts)->
assert.lengthOf posts, 2
categories = (post.category for post in posts).join(" ")
assert.equal categories, "foo bar"
"with callback":
topic: ->
posts = [new Post( title: "Insert 3.8", category: "foo" ), new Post( title: "Insert 3.8", category: "bar" )]
Post.insert posts, @callback
"should pass documents to callback": (posts)->
for post in posts
assert.equal post.title, "Insert 3.8"
"should pass models to callback": (posts)->
for post in posts
assert.instanceOf post, Post
"should set document ID": (posts)->
for post in posts
assert post._id
"new documents":
topic: (posts)->
ids = (post._id for post in posts)
Post.find ids, @callback
"should exist in database": (posts)->
assert.lengthOf posts, 2
categories = (post.category for post in posts).join(" ")
assert.equal categories, "foo bar"
.export(module)