Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

added some more tests

  • Loading branch information...
commit 36e11697f1269b1a9f46e577c4c683fcd757456a 1 parent fb1947a
@garrensmith authored
View
21 examples/blog.js
@@ -6,23 +6,24 @@ logger.setLogLevel(7);
Model.create_connection('lazyboy_tests');
-var Author = Model.define('Author', {name: String,
+var Author = Model.define('Author', {
+ name: String,
email: String
});
-var Comment = Model.define('Comment',{user: String,
+var Comment = Model.define('Comment',{
+ user: String,
msg: String
});
-var Post = Model.define('Post',{title: String,
- text: String,
- date: Date,
- comments: {hasmany: Comment ,
- author: {hasone: Author }
- });
+var Post = Model.define('Post',{
+ title: String,
+ text: String,
+ date: Date,
+ comments: {has_many: Comment ,
+ author: {has_one: Author }
+ });
-// to understand it -- Storing inside document
-//
View
18 lib/model.js
@@ -93,6 +93,8 @@ var ModelDocument = function (model_type, schema, views) {
model.schema = self.schema;
model.model_type = self.model_type;
+ if (!model_data) return model;
+
// TODO convert to correct type if needed
Object.keys(self.schema).forEach(function (key) {
@@ -102,9 +104,14 @@ var ModelDocument = function (model_type, schema, views) {
} else if (self.schema[key].type.has_many) {
var created_docs = [];
var key_store = key;
- model_data[key].forEach(function (item) {
- created_docs.push(Model(item.model_type).create(item));
- });
+
+ if(model_data[key]) {
+
+ model_data[key].forEach(function (item) {
+ created_docs.push(Model(item.model_type).create(item));
+ });
+ }
+
model[key] = created_docs;
} else if (model_data[key]) {
@@ -117,10 +124,7 @@ var ModelDocument = function (model_type, schema, views) {
if (model_data._id) {
model.id = model_data._id;
- }
-
- model.model_type = self.model_type;
-
+ }
self.beforeCreateFn && self.beforeCreateFn(model);
return model;
View
20 specs/simple_embedd_multiple_docs_specs.js
@@ -24,8 +24,7 @@ describe("Saving multiple embedded docs").
}).
it("Should save all embedded docs in doc as array", function (async) {
var blog_post = BlogPost.create({title: "Cool blog Post", comments: []});
-
-
+
blog_post.comments.push(Comment.create({name:"Garren", text: "Nicely done"}));
blog_post.comments.push(Comment.create({name:"Billy", text: "What the hell? this doesn't make sense"}));
@@ -37,7 +36,6 @@ describe("Saving multiple embedded docs").
}).
it("Should serialise each each embedded doc", function (async) {
var blog_post = BlogPost.create({title: "Another blog Post", comments: []});
-
blog_post.comments.push(Comment.create({name:"Kirsty", text: "Wow"}));
blog_post.comments.push(Comment.create({name:"James", text: "Here is a random comment"}));
@@ -50,24 +48,28 @@ describe("Saving multiple embedded docs").
}).
it("Should load embedded docs", function (async) {
var blog_post = BlogPost.create({title: "A third blog Post", comments: []});
-
blog_post.comments.push(Comment.create({name:"Henry", text: "Cool"}));
blog_post.comments.push(Comment.create({name:"Wallace", text: "Awesome"}));
blog_post.save(async(function (err, res) {
BlogPost.find(res.id, async(function (err, loaded) {
- //console.log("loaded");
- //console.dir(loaded);
loaded.comments.length.should().beEqual(2);
assert.notEqual(loaded.comments[0].schema, null);
-
}));
-
}));
+ }).
+ it("Should save embedded doc as empty array if not supplied", function (async) {
+ var blog_post = BlogPost.create({title: "A third blog Post"});
+
+ blog_post.save(async(function (err, res) {
+ BlogPost.find(res.id, async(function (err, loaded) {
+ loaded.comments.length.should().beEqual(0);
+ }));
+ }));
+ });
- });
View
14 specs/simple_embedded_specs.js
@@ -56,4 +56,18 @@ describe("Simple Embedding of Single Doc").
+ }).
+ it("Should save doc with null if no embedded doc and load with empty object", function (async) {
+ var address_book = AddressBook.create({name: "Address Book with no owner"});
+
+ address_book.save(async(function (err, res) {
+
+ AddressBook.find(res.id,async(function (err, loaded) {
+ console.dir(loaded.owner);
+ loaded.owner.model_type.should().beEqual('Owner');
+ assert.equal(loaded.owner.name, null);
+ }));
+
+ }));
+
});
Please sign in to comment.
Something went wrong with that request. Please try again.