UNPKG

2.43 kBJavaScriptView Raw
1var describe = require('Jody').describe,
2 assert = require('assert'),
3 cradle = require('cradle'),
4 Model = require('../lib/index'),
5 db = require('./spec_helper').db,
6 BlogPost, Comment;
7
8describe("Saving multiple embedded docs").
9 beforeAll(function (done) {
10
11 Comment = Model.define("Comment", {
12 name: String,
13 text: String
14 });
15
16 BlogPost = Model.define("BlogPost", {
17 title: String,
18 comments: {has_many: Comment}
19 });
20
21 Model.load();
22 done();
23
24 }).
25 it("Should save all embedded docs in doc as array", function (async) {
26 var blog_post = BlogPost.create({title: "Cool blog Post", comments: []});
27
28 blog_post.comments.push(Comment.create({name:"Garren", text: "Nicely done"}));
29 blog_post.comments.push(Comment.create({name:"Billy", text: "What the hell? this doesn't make sense"}));
30
31 blog_post.save(async(function (err, res) {
32 db.get(res.id, async( function (err, doc) {
33 doc.comments.length.should().beEqual(2);
34 }));
35 }));
36 }).
37 it("Should serialise each each embedded doc", function (async) {
38 var blog_post = BlogPost.create({title: "Another blog Post", comments: []});
39
40 blog_post.comments.push(Comment.create({name:"Kirsty", text: "Wow"}));
41 blog_post.comments.push(Comment.create({name:"James", text: "Here is a random comment"}));
42
43 blog_post.save(async(function (err, res) {
44 db.get(res.id, async( function (err, doc) {
45 assert.equal(doc.comments[0].schema, null);
46 }));
47 }));
48 }).
49 it("Should load embedded docs", function (async) {
50 var blog_post = BlogPost.create({title: "A third blog Post", comments: []});
51
52 blog_post.comments.push(Comment.create({name:"Henry", text: "Cool"}));
53 blog_post.comments.push(Comment.create({name:"Wallace", text: "Awesome"}));
54
55 blog_post.save(async(function (err, res) {
56 BlogPost.find(res.id, async(function (err, loaded) {
57 loaded.comments.length.should().beEqual(2);
58 assert.notEqual(loaded.comments[0].schema, null);
59 }));
60 }));
61 }).
62 it("Should save embedded doc as empty array if not supplied", function (async) {
63 var blog_post = BlogPost.create({title: "A third blog Post"});
64
65 blog_post.save(async(function (err, res) {
66 BlogPost.find(res.id, async(function (err, loaded) {
67 loaded.comments.length.should().beEqual(0);
68 }));
69 }));
70 });
71
72
73
74
75