@@ -22,6 +22,44 @@ describe("SQLite", async () => {
22
22
const crud = new Crud ( database , mapper , false ) ;
23
23
const ddl = new Ddl ( database , mapper , false ) ;
24
24
25
+ const insertUf = async ( ) => {
26
+ await ddl . create ( Uf ) . execute ( ) . toPromise ( ) ;
27
+ const itemExist = await crud . query ( Uf )
28
+ . where ( w => w . equal ( x => x . codeImport , ObjectToTest . uf . codeImport ) )
29
+ . firstOrDefault ( ) . toPromise ( ) ;
30
+ if ( ! itemExist ) {
31
+ const insert = crud . insert ( Uf , ObjectToTest . uf ) ;
32
+ const insertedResult = await insert . execute ( ) . toPromise ( ) ;
33
+ expect ( insertedResult [ 0 ] . rowsAffected ) . to . equal ( 1 ) ;
34
+ }
35
+ } ;
36
+
37
+ const insertSubRegiao = async ( ) => {
38
+ await ddl . create ( SubRegiao ) . execute ( ) . toPromise ( ) ;
39
+ const itemExist = await crud . query ( SubRegiao )
40
+ . where ( w => w . equal ( x => x . codeImport , ObjectToTest . subRegiao . codeImport ) )
41
+ . firstOrDefault ( ) . toPromise ( ) ;
42
+ if ( ! itemExist ) {
43
+ console . log ( `insert subregião: ${ ObjectToTest . subRegiao } ` ) ;
44
+ const insert = crud . insert ( SubRegiao , ObjectToTest . subRegiao ) ;
45
+ const insertedResult = await insert . execute ( ) . toPromise ( ) ;
46
+ console . log ( insertedResult ) ;
47
+ expect ( insertedResult [ 0 ] . rowsAffected ) . to . equal ( 1 ) ;
48
+ }
49
+ } ;
50
+
51
+ const insertRegiao = async ( ) => {
52
+ await ddl . create ( Regiao ) . execute ( ) . toPromise ( ) ;
53
+ const itemExist = await crud . query ( Regiao )
54
+ . where ( w => w . equal ( x => x . codeImport , ObjectToTest . regiao . codeImport ) )
55
+ . firstOrDefault ( ) . toPromise ( ) ;
56
+ if ( ! itemExist ) {
57
+ const insert = crud . insert ( Regiao , ObjectToTest . regiao ) ;
58
+ const insertedResult = await insert . execute ( ) . toPromise ( ) ;
59
+ expect ( insertedResult [ 0 ] . rowsAffected ) . to . equal ( 1 ) ;
60
+ }
61
+ } ;
62
+
25
63
it ( "GuidClazz" , async ( ) => {
26
64
27
65
await ddl . create ( GuidClazz ) . execute ( ) . toPromise ( ) ;
@@ -111,20 +149,76 @@ describe("SQLite", async () => {
111
149
expect ( queryResultNull . length ) . to . equal ( 1 ) ;
112
150
} ) ;
113
151
152
+ it ( "Cidade read reference without join" , async ( ) => {
153
+
154
+ await insertUf ( ) ;
155
+
156
+ await insertSubRegiao ( ) ;
157
+
158
+ await insertRegiao ( ) ;
159
+
160
+ const model = {
161
+ codeImport : 120 ,
162
+ nome : "Teste 120" ,
163
+ population : 12 ,
164
+ uf : ObjectToTest . uf ,
165
+ subRegiao : ObjectToTest . subRegiao ,
166
+ } as Cidade ;
167
+
168
+ const insert = crud . insert ( Cidade , model ) ;
169
+ const insertResult4 = await insert . execute ( ) . toPromise ( ) ;
170
+ expect ( insertResult4 [ 0 ] . rowsAffected ) . to . equal ( 1 ) ;
171
+
172
+ const query = await crud . query ( Cidade )
173
+ . projection ( p => p . all ( ) )
174
+ . where ( where => where . equal ( x => x . codeImport , model . codeImport ) ) ;
175
+ let joinSubRegiao : JoinQueryBuilder < SubRegiao > ;
176
+ query . join (
177
+ SubRegiao ,
178
+ on => on . equal ( x => x . codeImport , query . ref ( x => x . subRegiao . codeImport ) ) ,
179
+ join => {
180
+ join . projection ( projection => {
181
+ projection . add ( x => x . nome ) ;
182
+ } ) ;
183
+ joinSubRegiao = join ;
184
+ }
185
+ ) ;
186
+ query . join (
187
+ Regiao ,
188
+ on => on . equal ( x => x . codeImport , joinSubRegiao . ref ( x => x . regiao . codeImport ) ) ,
189
+ join => {
190
+ join . projection ( projection => {
191
+ projection . all ( ) ;
192
+ } ) ;
193
+ }
194
+ ) ;
195
+ const queryResult = await query . mapper < Cidade > ( row => {
196
+ const result = row
197
+ . map ( )
198
+ . map ( Uf , x => x . uf , "uf" )
199
+ . map ( SubRegiao , x => x . subRegiao )
200
+ . map ( Regiao , x => x . subRegiao . regiao )
201
+ . result ( ) ;
202
+ return result ;
203
+ } ) . toPromise ( ) ;
204
+ expect ( queryResult . length ) . to . equal ( 1 ) ;
205
+ expect ( queryResult [ 0 ] . codeImport ) . to . equal ( model . codeImport ) ;
206
+ expect ( queryResult [ 0 ] . nome ) . to . equal ( model . nome ) ;
207
+ expect ( queryResult [ 0 ] . population ) . to . equal ( model . population ) ;
208
+ expect ( queryResult [ 0 ] . uf . codeImport ) . to . equal ( model . uf . codeImport ) ;
209
+ expect ( queryResult [ 0 ] . subRegiao . codeImport ) . to . equal ( model . subRegiao . codeImport ) ;
210
+ expect ( queryResult [ 0 ] . subRegiao . nome ) . to . equal ( model . subRegiao . nome ) ;
211
+ expect ( queryResult [ 0 ] . subRegiao . regiao . codeImport ) . to . equal ( model . subRegiao . regiao . codeImport ) ;
212
+ expect ( queryResult [ 0 ] . subRegiao . regiao . nome ) . to . equal ( model . subRegiao . regiao . nome ) ;
213
+ } ) ;
214
+
114
215
it ( "Cidade join to mappper" , async ( ) => {
115
216
116
- await ddl . create ( Uf ) . execute ( ) . toPromise ( ) ;
117
- const insertUf = crud . insert ( Uf , ObjectToTest . uf ) ;
118
- const insertResultUf = await insertUf . execute ( ) . toPromise ( ) ;
119
- expect ( insertResultUf [ 0 ] . rowsAffected ) . to . equal ( 1 ) ;
217
+ await insertUf ( ) ;
120
218
121
- await ddl . create ( SubRegiao ) . execute ( ) . toPromise ( ) ;
122
- const insertResulSubRegiao = await crud . insert ( SubRegiao , ObjectToTest . subRegiao ) . execute ( ) . toPromise ( ) ;
123
- expect ( insertResulSubRegiao [ 0 ] . rowsAffected ) . to . equal ( 1 ) ;
219
+ await insertSubRegiao ( ) ;
124
220
125
- await ddl . create ( Regiao ) . execute ( ) . toPromise ( ) ;
126
- const insertResultRegiao = await crud . insert ( Regiao , ObjectToTest . regiao ) . execute ( ) . toPromise ( ) ;
127
- expect ( insertResultRegiao [ 0 ] . rowsAffected ) . to . equal ( 1 ) ;
221
+ await insertRegiao ( ) ;
128
222
129
223
const model = {
130
224
codeImport : 101 ,
0 commit comments