-
Notifications
You must be signed in to change notification settings - Fork 6
/
user-has-type-proxy.test.js
186 lines (164 loc) · 6.31 KB
/
user-has-type-proxy.test.js
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import db, { User, UserType, UserHasType } from '../'
import GetProxy from '../proxies'
const { assert } = require('chai')
const { UserProxy, UserTypeProxy, UserHasTypeProxy, Events } = GetProxy({ User, UserHasType, UserType })
const USER_EMAIL = 'abc@mailinator.com', USER_PASSWORD = 'password', USER_PASSWORD2 = 'password2', USER_GENDER = 'male'
const ROLE_NAME = 'student', ROLE_NAME2 = 'staff'
const { isDataProxy } = require('./helpers')
describe('UserHasTypeProxy', () => {
it('should be a valid Data Proxy', () => {
/**
* First, we check that the proxy object provided has all proxy functions
*/
isDataProxy(UserProxy)
isDataProxy(UserTypeProxy)
return isDataProxy(UserHasTypeProxy)
})
describe('Create UserType and User then Inserts', () => {
let createdUserType = null, createdUser = null
before(() => {
/**
* create the user that has the phone-number
*/
return UserTypeProxy.insert({
name: ROLE_NAME
})
.then(userType => {
createdUserType = userType
return UserProxy.insert({
email: USER_EMAIL,
pwd: USER_PASSWORD,
gender: USER_GENDER
})
.then(user => {
createdUser = user
})
.catch(err => {
throw err
})
})
.catch(err => {
throw err
})
})
after(() => {
/**
* delete the userType that was created before all tests
*/
return UserTypeProxy.destroy(createdUserType.id).then(success => {
return UserProxy.destroy(createdUser.id).then(success => {
}).catch(err => {
throw err
})
}).catch(err => {
throw err
})
})
/**
* We check that all INSERT constraints are obeyed
*/
it('insert(userHasType) without userTypeId should throw NullParamError', () => {
return UserHasTypeProxy.insert({
userId: createdUser.id
}).then(userHasType => {
assert.isNotNull(userHasType)
}).catch(err => {
assert.equal(err.name, 'NullParamError')
})
})
it('insert(userHasType) without userId should throw NullParamError', () => {
return UserHasTypeProxy.insert({
userTypeId: createdUserType.id
}).then(userHasType => {
assert.isNotNull(userHasType)
}).catch(err => {
assert.equal(err.name, 'NullParamError')
})
})
it('insert(userHasType) with userTypeId and userId should work', () => {
return UserHasTypeProxy.insert({
userTypeId: createdUserType.id,
userId: createdUser.id
}).then(userHasType => {
assert.isNotNull(userHasType)
}).catch(err => {
throw err
})
})
describe('Selects and Updates', () => {
const getLastUserHasType = () => {
/**
* retrieve the last added action
*/
return UserHasTypeProxy.getAll({
limit: 1,
order: [ [ 'createdAt', 'DESC' ]]
}).then(userHasTypes => {
const lastUserHasType = userHasTypes[0]
assert.isDefined(lastUserHasType)
return lastUserHasType
})
}
/**
* We check that all SELECT and UPDATE operations work
*/
it('getById(-1) should return NULL', () => {
return UserHasTypeProxy.getById(-1).then((userHasType) => {
assert.isNull(userHasType)
}).catch(err => {
assert.isNull(err)
})
})
it(`getById(id) should return an user "${ROLE_NAME}"`, () => {
return getLastUserHasType().then(lastUserHasType => {
return UserHasTypeProxy.getById(lastUserHasType.id).then(userHasType => {
if (userHasType) {
assert.equal(userHasType.userId, lastUserHasType.userId)
assert.equal(userHasType.userTypeId, lastUserHasType.userTypeId)
}
}).catch(err => {
throw err
})
})
})
it('getAll() should return items', () => {
return UserHasTypeProxy.getAll({}).then((userHasTypes) => {
assert.isArray(userHasTypes)
userHasTypes.forEach(userHasType => {
assert.isDefined(userHasType)
})
}).catch(err => {
assert.isNull(err)
})
})
it('update(userHasType) should work', () => {
return getLastUserHasType().then(lastRole => {
return UserHasTypeProxy.update({
id: lastRole.id,
name: `${ROLE_NAME}`
}).then(rows => {
assert.isArray(rows)
}).catch(err => {
assert.fail()
})
}).catch(err => {
assert.isNull(err, 'update(userHasType)')
})
})
describe('Deletes', () => {
/**
* Last, we delete the items created
*/
it(`destroy(id) should return a boolean`, () => {
return getLastUserHasType().then(lastRole => {
return UserHasTypeProxy.destroy(lastRole.id).then((success) => {
assert.equal(success, 1)
}).catch(err => {
assert.isNull(err)
})
})
})
})
})
})
})