1
- import { Schema } from '@orbit/data' ;
1
+ import { Schema , RecordNotFoundException } from '@orbit/data' ;
2
2
3
3
import SQLSource from '../src' ;
4
4
5
5
QUnit . module ( 'SQLSource' , function ( hooks ) {
6
6
let schema : Schema ;
7
7
let source : SQLSource ;
8
8
9
+ const author1 = {
10
+ type : 'author' ,
11
+ id : '1' ,
12
+ attributes : {
13
+ firstName : 'Paul' ,
14
+ lastName : 'Chavard'
15
+ }
16
+ } ;
17
+ const article1 = {
18
+ type : 'article' ,
19
+ id : '1' ,
20
+ attributes : {
21
+ title : 'Article 1'
22
+ } ,
23
+ relationships : {
24
+ author : {
25
+ data : { type : 'author' , id : '1' }
26
+ }
27
+ }
28
+ } ;
29
+ const article2 = {
30
+ type : 'article' ,
31
+ id : '2' ,
32
+ attributes : {
33
+ title : 'Article 2'
34
+ }
35
+ } ;
36
+
9
37
hooks . beforeEach ( async function ( ) {
10
38
schema = new Schema ( {
11
39
models : {
12
- user : {
40
+ author : {
13
41
attributes : {
14
- name : { type : 'string' }
42
+ firstName : { type : 'string' } ,
43
+ lastName : { type : 'string' }
15
44
} ,
16
45
relationships : {
17
- blogs : {
46
+ articles : {
18
47
type : 'hasMany' ,
19
- model : 'blog ' ,
20
- inverse : 'user '
48
+ model : 'article ' ,
49
+ inverse : 'author '
21
50
}
22
51
}
23
52
} ,
24
- blog : {
53
+ article : {
25
54
attributes : {
26
- title : { type : 'string' }
55
+ title : { type : 'string' } ,
56
+ publishedOn : { type : 'date' } ,
57
+ createdAt : { type : 'datetime' } ,
58
+ updatedAt : { type : 'datetime' }
27
59
} ,
28
60
relationships : {
29
- user : {
61
+ author : {
30
62
type : 'hasOne' ,
31
- model : 'user ' ,
32
- inverse : 'blogs '
63
+ model : 'author ' ,
64
+ inverse : 'articles '
33
65
} ,
34
66
tags : {
35
67
type : 'hasMany' ,
36
68
model : 'tag' ,
37
- inverse : 'blogs '
69
+ inverse : 'articles '
38
70
}
39
71
}
40
72
} ,
@@ -43,9 +75,9 @@ QUnit.module('SQLSource', function(hooks) {
43
75
name : { type : 'string' }
44
76
} ,
45
77
relationships : {
46
- blogs : {
78
+ articles : {
47
79
type : 'hasMany' ,
48
- model : 'blog ' ,
80
+ model : 'article ' ,
49
81
inverse : 'tags'
50
82
}
51
83
}
@@ -68,36 +100,161 @@ QUnit.module('SQLSource', function(hooks) {
68
100
await source . deactivate ( ) ;
69
101
} ) ;
70
102
71
- QUnit . test ( 'relationships' , async function ( assert ) {
72
- let users = await source . query ( q => q . findRecords ( 'user' ) ) ;
73
- assert . deepEqual ( users , [ ] ) ;
74
-
75
- let user : any = {
76
- type : 'user' ,
77
- attributes : { name : 'Paul' } ,
78
- relationships : { }
79
- } ;
80
- let blog : any = { type : 'blog' , attributes : { title : 'Hello World' } } ;
81
- let tag : any = { type : 'tag' , attributes : { name : 'js' } } ;
82
-
83
- blog = await source . update ( t => t . addRecord ( blog ) ) ;
84
- tag = await source . update ( t => t . addRecord ( tag ) ) ;
85
- user . relationships . blogs = { data : [ blog ] } ;
86
- user = await source . update ( t => t . addRecord ( user ) ) ;
87
- assert . equal ( user . attributes . name , 'Paul' ) ;
88
- users = await source . query ( q => q . findRecords ( 'user' ) ) ;
89
- assert . equal ( users [ 0 ] . attributes . name , 'Paul' ) ;
90
- let blogs = await source . query ( q => q . findRelatedRecords ( user , 'blogs' ) ) ;
91
- user = await source . query ( q => q . findRelatedRecord ( blog , 'user' ) ) ;
92
- assert . equal ( user . attributes . name , 'Paul' ) ;
93
- assert . equal ( blogs [ 0 ] . attributes . title , 'Hello World' ) ;
94
-
95
- blog . relationships = { tags : { data : [ tag ] } } ;
96
- await source . update ( t => t . updateRecord ( blog ) ) ;
97
- let tags = await source . query ( q => q . findRelatedRecords ( blog , 'tags' ) ) ;
98
- blogs = await source . query ( q => q . findRelatedRecords ( tag , 'blogs' ) ) ;
99
-
100
- assert . equal ( tags [ 0 ] . attributes . name , 'js' ) ;
101
- assert . equal ( blogs [ 0 ] . attributes . title , 'Hello World' ) ;
103
+ QUnit . module ( 'base' , function ( ) {
104
+ QUnit . test ( 'it exists' , function ( assert ) {
105
+ assert . ok ( source instanceof SQLSource ) ;
106
+ } ) ;
107
+ } ) ;
108
+
109
+ QUnit . module ( 'findRecord' , function ( ) {
110
+ QUnit . test ( 'not found' , async function ( assert ) {
111
+ try {
112
+ await source . query ( q => q . findRecord ( { type : 'author' , id : '1' } ) ) ;
113
+ } catch ( error ) {
114
+ assert . equal ( error . message , 'Record not found: author:1' ) ;
115
+ assert . throws ( ( ) => {
116
+ throw error ;
117
+ } , RecordNotFoundException ) ;
118
+ }
119
+ } ) ;
120
+
121
+ QUnit . module ( 'with records' , function ( hooks ) {
122
+ hooks . beforeEach ( async function ( ) {
123
+ await source . update ( t => [ t . addRecord ( author1 ) , t . addRecord ( article1 ) ] ) ;
124
+ } ) ;
125
+
126
+ QUnit . test ( 'find' , async function ( assert ) {
127
+ let record = await source . query ( q =>
128
+ q . findRecord ( { type : 'author' , id : '1' } )
129
+ ) ;
130
+ assert . deepEqual ( record , author1 , 'should find the record' ) ;
131
+ } ) ;
132
+ } ) ;
133
+ } ) ;
134
+
135
+ QUnit . module ( 'findRecords' , function ( ) {
136
+ QUnit . test ( 'empty' , async function ( assert ) {
137
+ let records = await source . query ( q => q . findRecords ( 'author' ) ) ;
138
+ assert . deepEqual ( records , [ ] , 'should be empty' ) ;
139
+ } ) ;
140
+
141
+ QUnit . module ( 'with records' , function ( hooks ) {
142
+ hooks . beforeEach ( async function ( ) {
143
+ await source . update ( t => [ t . addRecord ( author1 ) , t . addRecord ( article1 ) ] ) ;
144
+ } ) ;
145
+
146
+ QUnit . test ( 'find' , async function ( assert ) {
147
+ let records = await source . query ( q => q . findRecords ( 'author' ) ) ;
148
+ assert . deepEqual ( records , [ author1 ] , 'should find records' ) ;
149
+ } ) ;
150
+ } ) ;
151
+ } ) ;
152
+
153
+ QUnit . module ( 'findRelatedRecord' , function ( ) {
154
+ QUnit . test ( 'not found' , async function ( assert ) {
155
+ try {
156
+ await source . query ( q =>
157
+ q . findRelatedRecord ( { type : 'article' , id : '1' } , 'author' )
158
+ ) ;
159
+ } catch ( error ) {
160
+ assert . equal ( error . message , 'Record not found: article:1' ) ;
161
+ assert . throws ( ( ) => {
162
+ throw error ;
163
+ } , RecordNotFoundException ) ;
164
+ }
165
+ } ) ;
166
+
167
+ QUnit . test ( 'empty' , async function ( assert ) {
168
+ await source . update ( t => t . addRecord ( article2 ) ) ;
169
+ let record = await source . query ( q =>
170
+ q . findRelatedRecord ( { type : 'article' , id : '2' } , 'author' )
171
+ ) ;
172
+ assert . deepEqual ( record , null , 'should be empty' ) ;
173
+ } ) ;
174
+ } ) ;
175
+
176
+ QUnit . module ( 'findRelatedRecords' , function ( ) {
177
+ QUnit . test ( 'not found' , async function ( assert ) {
178
+ try {
179
+ await source . query ( q =>
180
+ q . findRelatedRecords ( { type : 'author' , id : '1' } , 'articles' )
181
+ ) ;
182
+ } catch ( error ) {
183
+ assert . equal ( error . message , 'Record not found: author:1' ) ;
184
+ assert . throws ( ( ) => {
185
+ throw error ;
186
+ } , RecordNotFoundException ) ;
187
+ }
188
+ } ) ;
189
+
190
+ QUnit . module ( '1-n' , function ( ) {
191
+ QUnit . test ( 'empty' , async function ( assert ) {
192
+ await source . update ( t => t . addRecord ( author1 ) ) ;
193
+ let records = await source . query ( q =>
194
+ q . findRelatedRecords ( { type : 'author' , id : '1' } , 'articles' )
195
+ ) ;
196
+ assert . deepEqual ( records , [ ] , 'should be empty' ) ;
197
+ } ) ;
198
+ } ) ;
199
+
200
+ QUnit . module ( 'n-n' , function ( ) {
201
+ QUnit . test ( 'empty' , async function ( assert ) {
202
+ await source . update ( t => t . addRecord ( article2 ) ) ;
203
+ let records = await source . query ( q =>
204
+ q . findRelatedRecords ( { type : 'article' , id : '2' } , 'tags' )
205
+ ) ;
206
+ assert . deepEqual ( records , [ ] , 'should be empty' ) ;
207
+ } ) ;
208
+ } ) ;
209
+ } ) ;
210
+
211
+ QUnit . module ( 'addRecord' , function ( ) {
212
+ QUnit . test ( 'with attribute' , async function ( assert ) {
213
+ const record = await source . update ( t => t . addRecord ( article2 ) ) ;
214
+ assert . equal ( record . type , article2 . type ) ;
215
+ assert . equal ( record . id , article2 . id ) ;
216
+ assert . equal ( record . attributes . title , article2 . attributes . title ) ;
217
+ } ) ;
218
+ } ) ;
219
+
220
+ QUnit . module ( 'updateRecord' , function ( ) {
221
+ QUnit . test ( 'not found' , async function ( assert ) {
222
+ try {
223
+ await source . update ( t => t . updateRecord ( { type : 'author' , id : '1' } ) ) ;
224
+ } catch ( error ) {
225
+ assert . equal ( error . message , 'Record not found: author:1' ) ;
226
+ assert . throws ( ( ) => {
227
+ throw error ;
228
+ } , RecordNotFoundException ) ;
229
+ }
230
+ } ) ;
231
+ } ) ;
232
+
233
+ QUnit . module ( 'removeRecord' , function ( ) {
234
+ QUnit . test ( 'not found' , async function ( assert ) {
235
+ try {
236
+ await source . update ( t => t . removeRecord ( { type : 'author' , id : '1' } ) ) ;
237
+ } catch ( error ) {
238
+ assert . equal ( error . message , 'Record not found: author:1' ) ;
239
+ assert . throws ( ( ) => {
240
+ throw error ;
241
+ } , RecordNotFoundException ) ;
242
+ }
243
+ } ) ;
244
+ } ) ;
245
+
246
+ QUnit . module ( 'replaceAttribute' , function ( ) {
247
+ QUnit . test ( 'not found' , async function ( assert ) {
248
+ try {
249
+ await source . update ( t =>
250
+ t . replaceAttribute ( { type : 'author' , id : '1' } , 'firstName' , 'Paul' )
251
+ ) ;
252
+ } catch ( error ) {
253
+ assert . equal ( error . message , 'Record not found: author:1' ) ;
254
+ assert . throws ( ( ) => {
255
+ throw error ;
256
+ } , RecordNotFoundException ) ;
257
+ }
258
+ } ) ;
102
259
} ) ;
103
260
} ) ;
0 commit comments