Repository navigation
Expand file tree
/
Copy pathsmart-types.js
More file actions
133 lines (112 loc) · 3.41 KB
/
Copy pathsmart-types.js
File metadata and controls
133 lines (112 loc) · 3.41 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
var should = require('should');
var helper = require('../support/spec_helper');
var ORM = require('../../');
describe("Smart types", function () {
this.timeout(0);
var db = null;
var User = null;
var Profile = null;
var Post = null;
var Group = null;
var setup = function () {
return function (done) {
User = db.define("user", {
username: { type: 'text', size: 64 },
password: { type: 'text', size: 128 }
}, {
id: 'username'
});
Profile = User.extendsTo("profile", {
firstname: String,
lastname: String
}, {
reverse: 'user',
required: true
});
Group = db.define("group", {
name: { type: 'text', size: 64 }
}, {
id: 'name'
});
Group.hasMany('users', User, {}, {
reverse: 'groups'
});
Post = db.define("post", {
content: String
}, {
});
Post.hasOne('user', User, {
reverse: 'posts'
});
ORM.singleton.clear();
return helper.dropSync([ User, Profile, Group, Post ], function () {
User.create({
username: 'billy',
password: 'hashed password'
}, function (err, billy) {
should.not.exist(err);
billy.setProfile(new Profile({ firstname: 'William', lastname: 'Franklin' }), function (err, profile) {
should.not.exist(err);
billy.addGroups([new Group({ name: 'admins' }), new Group({ name: 'developers' })], function (err, groups) {
should.not.exist(err);
billy.setPosts(new Post({content: 'Hello world!'}), function(err, posts) {
should.not.exist(err);
done();
});
});
});
});
});
};
};
before(function (done) {
helper.connect(function (connection) {
db = connection;
return done();
});
});
after(function () {
return db.close();
});
describe("extends", function () {
before(setup());
it("should be able to get extendsTo with custom id", function (done) {
User.get('billy', function (err, billy) {
should.not.exist(err);
should.exist(billy);
billy.getProfile(function (err, profile) {
should.not.exist(err);
should.exist(profile);
should.equal(profile.firstname, 'William');
should.equal(profile.lastname, 'Franklin');
done();
});
});
});
it("should be able to get hasOne with custom id", function (done) {
User.get('billy', function (err, billy) {
should.not.exist(err);
should.exist(billy);
billy.getPosts(function (err, posts) {
should.not.exist(err);
should.exist(posts);
should.equal(posts.length, 1);
should.equal(posts[0].content, 'Hello world!');
done();
});
});
});
it("should be able to get hasMany with custom id", function (done) {
User.get('billy', function (err, billy) {
should.not.exist(err);
should.exist(billy);
billy.getGroups(function (err, groups) {
should.not.exist(err);
should.exist(groups);
should.equal(groups.length, 2);
done();
});
});
});
});
});