Skip to content

Commit 9bfef0e

Browse files
committed
Added tests for smart_associations
Some preliminary tests to ensure that everything functions as it should. Covers extendsTo, hasOne and hasMany.
1 parent 030159c commit 9bfef0e

File tree

1 file changed

+135
-0
lines changed

1 file changed

+135
-0
lines changed

test/integration/smart-types.js

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
var should = require('should');
2+
var helper = require('../support/spec_helper');
3+
var ORM = require('../../');
4+
5+
describe("Smart types", function () {
6+
this.timeout(0);
7+
var db = null;
8+
var User = null;
9+
var Profile = null;
10+
var Post = null;
11+
var Group = null;
12+
13+
var setup = function () {
14+
return function (done) {
15+
User = db.define("user", {
16+
username: { type: 'text', size: 64 },
17+
password: { type: 'text', size: 128 }
18+
}, {
19+
id: 'username'
20+
});
21+
22+
Profile = User.extendsTo("profile", {
23+
firstname: String,
24+
lastname: String
25+
}, {
26+
reverse: 'user',
27+
required: true
28+
});
29+
30+
Group = db.define("group", {
31+
name: { type: 'text', size: 64 }
32+
}, {
33+
id: 'name'
34+
});
35+
Group.hasMany('users', User, {}, {
36+
reverse: 'groups'
37+
});
38+
39+
Post = db.define("post", {
40+
content: String
41+
}, {
42+
43+
});
44+
Post.hasOne('user', User, {
45+
reverse: 'posts'
46+
});
47+
48+
ORM.singleton.clear();
49+
return helper.dropSync([ User, Profile, Group, Post ], function () {
50+
User.create({
51+
username: 'billy',
52+
password: 'hashed password'
53+
}, function (err, billy) {
54+
should.not.exist(err);
55+
billy.setProfile(new Profile({ firstname: 'William', lastname: 'Franklin' }), function (err, profile) {
56+
should.not.exist(err);
57+
billy.addGroups([new Group({ name: 'admins' }), new Group({ name: 'developers' })], function (err, groups) {
58+
should.not.exist(err);
59+
billy.setPosts(new Post({content: 'Hello world!'}), function(err, posts) {
60+
should.not.exist(err);
61+
done();
62+
});
63+
});
64+
});
65+
});
66+
});
67+
};
68+
};
69+
70+
before(function (done) {
71+
helper.connect(function (connection) {
72+
db = connection;
73+
74+
return done();
75+
});
76+
});
77+
78+
after(function () {
79+
return db.close();
80+
});
81+
82+
describe("extends", function () {
83+
before(setup());
84+
85+
it("should be able to get extendsTo with custom id", function (done) {
86+
User.get('billy', function (err, billy) {
87+
should.not.exist(err);
88+
should.exist(billy);
89+
90+
billy.getProfile(function (err, profile) {
91+
should.not.exist(err);
92+
should.exist(profile);
93+
should.equal(profile.firstname, 'William');
94+
should.equal(profile.lastname, 'Franklin');
95+
96+
done();
97+
});
98+
});
99+
});
100+
101+
it("should be able to get hasOne with custom id", function (done) {
102+
User.get('billy', function (err, billy) {
103+
should.not.exist(err);
104+
should.exist(billy);
105+
106+
billy.getPosts(function (err, posts) {
107+
should.not.exist(err);
108+
should.exist(posts);
109+
should.equal(posts.length, 1);
110+
should.equal(posts[0].content, 'Hello world!');
111+
112+
done();
113+
});
114+
});
115+
});
116+
117+
it("should be able to get hasMany with custom id", function (done) {
118+
User.get('billy', function (err, billy) {
119+
should.not.exist(err);
120+
should.exist(billy);
121+
122+
billy.getGroups(function (err, groups) {
123+
should.not.exist(err);
124+
should.exist(groups);
125+
should.equal(groups.length, 2);
126+
should.equal(groups[0].name, 'developers');
127+
should.equal(groups[1].name, 'admins');
128+
129+
done();
130+
});
131+
});
132+
});
133+
134+
});
135+
});

0 commit comments

Comments
 (0)