11package com .baeldung .jooq .introduction ;
22
3- import com .baeldung .jooq .introduction .db .public_ .tables .Author ;
4- import com .baeldung .jooq .introduction .db .public_ .tables .AuthorBook ;
5- import com .baeldung .jooq .introduction .db .public_ .tables .Book ;
63import org .jooq .DSLContext ;
74import org .jooq .Record3 ;
85import org .jooq .Result ;
1512import org .springframework .test .context .junit4 .SpringJUnit4ClassRunner ;
1613import org .springframework .transaction .annotation .Transactional ;
1714
15+ import static com .baeldung .jooq .introduction .db .public_ .tables .Author .AUTHOR ;
16+ import static com .baeldung .jooq .introduction .db .public_ .tables .AuthorBook .AUTHOR_BOOK ;
17+ import static com .baeldung .jooq .introduction .db .public_ .tables .Book .BOOK ;
1818import static org .junit .Assert .assertEquals ;
1919
2020@ ContextConfiguration (classes = PersistenceContext .class )
@@ -25,61 +25,98 @@ public class QueryTest {
2525 @ Autowired
2626 private DSLContext dsl ;
2727
28- Author author = Author .AUTHOR ;
29- Book book = Book .BOOK ;
30- AuthorBook authorBook = AuthorBook .AUTHOR_BOOK ;
31-
3228 @ Test
3329 public void givenValidData_whenInserting_thenSucceed () {
34- dsl .insertInto (author ).set (author .ID , 4 ).set (author .FIRST_NAME , "Herbert" ).set (author .LAST_NAME , "Schildt" ).execute ();
35- dsl .insertInto (book ).set (book .ID , 4 ).set (book .TITLE , "A Beginner's Guide" ).execute ();
36- dsl .insertInto (authorBook ).set (authorBook .AUTHOR_ID , 4 ).set (authorBook .BOOK_ID , 4 ).execute ();
37- Result <Record3 <Integer , String , Integer >> result = dsl .select (author .ID , author .LAST_NAME , DSL .count ()).from (author ).join (authorBook ).on (author .ID .equal (authorBook .AUTHOR_ID )).join (book ).on (authorBook .BOOK_ID .equal (book .ID ))
38- .groupBy (author .LAST_NAME ).fetch ();
30+ dsl .insertInto (AUTHOR )
31+ .set (AUTHOR .ID , 4 )
32+ .set (AUTHOR .FIRST_NAME , "Herbert" )
33+ .set (AUTHOR .LAST_NAME , "Schildt" )
34+ .execute ();
35+
36+ dsl .insertInto (BOOK )
37+ .set (BOOK .ID , 4 )
38+ .set (BOOK .TITLE , "A Beginner's Guide" )
39+ .execute ();
40+
41+ dsl .insertInto (AUTHOR_BOOK )
42+ .set (AUTHOR_BOOK .AUTHOR_ID , 4 )
43+ .set (AUTHOR_BOOK .BOOK_ID , 4 )
44+ .execute ();
45+
46+ final Result <Record3 <Integer , String , Integer >> result = dsl .select (AUTHOR .ID , AUTHOR .LAST_NAME , DSL .count ())
47+ .from (AUTHOR )
48+ .join (AUTHOR_BOOK ).on (AUTHOR .ID .equal (AUTHOR_BOOK .AUTHOR_ID ))
49+ .join (BOOK ).on (AUTHOR_BOOK .BOOK_ID .equal (BOOK .ID ))
50+ .groupBy (AUTHOR .LAST_NAME ).fetch ();
3951
4052 assertEquals (3 , result .size ());
41- assertEquals ("Sierra" , result .getValue (0 , author .LAST_NAME ));
53+ assertEquals ("Sierra" , result .getValue (0 , AUTHOR .LAST_NAME ));
4254 assertEquals (Integer .valueOf (2 ), result .getValue (0 , DSL .count ()));
43- assertEquals ("Schildt" , result .getValue (2 , author .LAST_NAME ));
55+ assertEquals ("Schildt" , result .getValue (2 , AUTHOR .LAST_NAME ));
4456 assertEquals (Integer .valueOf (1 ), result .getValue (2 , DSL .count ()));
4557 }
4658
4759 @ Test (expected = DataAccessException .class )
4860 public void givenInvalidData_whenInserting_thenFail () {
49- dsl .insertInto (authorBook ).set (authorBook .AUTHOR_ID , 4 ).set (authorBook .BOOK_ID , 5 ).execute ();
61+ dsl .insertInto (AUTHOR_BOOK ).set (AUTHOR_BOOK .AUTHOR_ID , 4 ).set (AUTHOR_BOOK .BOOK_ID , 5 ).execute ();
5062 }
5163
5264 @ Test
5365 public void givenValidData_whenUpdating_thenSucceed () {
54- dsl .update (author ).set (author .LAST_NAME , "Baeldung" ).where (author .ID .equal (3 )).execute ();
55- dsl .update (book ).set (book .TITLE , "Building your REST API with Spring" ).where (book .ID .equal (3 )).execute ();
56- dsl .insertInto (authorBook ).set (authorBook .AUTHOR_ID , 3 ).set (authorBook .BOOK_ID , 3 ).execute ();
57- Result <Record3 <Integer , String , String >> result = dsl .select (author .ID , author .LAST_NAME , book .TITLE ).from (author ).join (authorBook ).on (author .ID .equal (authorBook .AUTHOR_ID )).join (book ).on (authorBook .BOOK_ID .equal (book .ID )).where (author .ID .equal (3 ))
66+ dsl .update (AUTHOR )
67+ .set (AUTHOR .LAST_NAME , "Baeldung" )
68+ .where (AUTHOR .ID .equal (3 ))
69+ .execute ();
70+
71+ dsl .update (BOOK )
72+ .set (BOOK .TITLE , "Building your REST API with Spring" )
73+ .where (BOOK .ID .equal (3 )).execute ();
74+
75+ dsl .insertInto (AUTHOR_BOOK )
76+ .set (AUTHOR_BOOK .AUTHOR_ID , 3 )
77+ .set (AUTHOR_BOOK .BOOK_ID , 3 )
78+ .execute ();
79+
80+ final Result <Record3 <Integer , String , String >> result = dsl .select (AUTHOR .ID , AUTHOR .LAST_NAME , BOOK .TITLE )
81+ .from (AUTHOR )
82+ .join (AUTHOR_BOOK ).on (AUTHOR .ID .equal (AUTHOR_BOOK .AUTHOR_ID ))
83+ .join (BOOK ).on (AUTHOR_BOOK .BOOK_ID .equal (BOOK .ID ))
84+ .where (AUTHOR .ID .equal (3 ))
5885 .fetch ();
5986
6087 assertEquals (1 , result .size ());
61- assertEquals (Integer .valueOf (3 ), result .getValue (0 , author .ID ));
62- assertEquals ("Baeldung" , result .getValue (0 , author .LAST_NAME ));
63- assertEquals ("Building your REST API with Spring" , result .getValue (0 , book .TITLE ));
88+ assertEquals (Integer .valueOf (3 ), result .getValue (0 , AUTHOR .ID ));
89+ assertEquals ("Baeldung" , result .getValue (0 , AUTHOR .LAST_NAME ));
90+ assertEquals ("Building your REST API with Spring" , result .getValue (0 , BOOK .TITLE ));
6491 }
6592
6693 @ Test (expected = DataAccessException .class )
6794 public void givenInvalidData_whenUpdating_thenFail () {
68- dsl .update (authorBook ).set (authorBook .AUTHOR_ID , 4 ).set (authorBook .BOOK_ID , 5 ).execute ();
95+ dsl .update (AUTHOR_BOOK )
96+ .set (AUTHOR_BOOK .AUTHOR_ID , 4 )
97+ .set (AUTHOR_BOOK .BOOK_ID , 5 )
98+ .execute ();
6999 }
70100
71101 @ Test
72102 public void givenValidData_whenDeleting_thenSucceed () {
73- dsl .delete (author ).where (author .ID .lt (3 )).execute ();
74- Result <Record3 <Integer , String , String >> result = dsl .select (author .ID , author .FIRST_NAME , author .LAST_NAME ).from (author ).fetch ();
103+ dsl .delete (AUTHOR )
104+ .where (AUTHOR .ID .lt (3 ))
105+ .execute ();
106+
107+ final Result <Record3 <Integer , String , String >> result = dsl .select (AUTHOR .ID , AUTHOR .FIRST_NAME , AUTHOR .LAST_NAME )
108+ .from (AUTHOR )
109+ .fetch ();
75110
76111 assertEquals (1 , result .size ());
77- assertEquals ("Bryan" , result .getValue (0 , author .FIRST_NAME ));
78- assertEquals ("Basham" , result .getValue (0 , author .LAST_NAME ));
112+ assertEquals ("Bryan" , result .getValue (0 , AUTHOR .FIRST_NAME ));
113+ assertEquals ("Basham" , result .getValue (0 , AUTHOR .LAST_NAME ));
79114 }
80115
81116 @ Test (expected = DataAccessException .class )
82117 public void givenInvalidData_whenDeleting_thenFail () {
83- dsl .delete (book ).where (book .ID .equal (1 )).execute ();
118+ dsl .delete (BOOK )
119+ .where (BOOK .ID .equal (1 ))
120+ .execute ();
84121 }
85122}
0 commit comments