1
+ package com.beeproduced.bee.persistent.test.select
2
+
3
+ import com.beeproduced.bee.persistent.blaze.dsl.expression.builder.lower
4
+ import com.beeproduced.bee.persistent.blaze.dsl.predicate.builder.and
5
+ import com.beeproduced.bee.persistent.blaze.dsl.predicate.builder.or
6
+ import com.beeproduced.bee.persistent.test.config.ATestConfig
7
+ import com.beeproduced.datasource.a.*
8
+ import com.beeproduced.datasource.a.dsl.CircularDSL
9
+ import com.beeproduced.datasource.a.dsl.WeirdClassDSL
10
+ import jakarta.persistence.EntityManager
11
+ import org.junit.jupiter.api.AfterEach
12
+ import org.junit.jupiter.api.BeforeAll
13
+ import org.junit.jupiter.api.Test
14
+ import org.junit.jupiter.api.TestInstance
15
+ import org.junit.jupiter.api.extension.ExtendWith
16
+ import org.springframework.beans.factory.annotation.Autowired
17
+ import org.springframework.beans.factory.annotation.Qualifier
18
+ import org.springframework.boot.test.context.SpringBootTest
19
+ import org.springframework.test.context.TestPropertySource
20
+ import org.springframework.test.context.junit.jupiter.SpringExtension
21
+ import org.springframework.transaction.PlatformTransactionManager
22
+ import org.springframework.transaction.support.TransactionTemplate
23
+ import java.util.*
24
+ import kotlin.test.assertEquals
25
+ import kotlin.test.assertNotNull
26
+
27
+ /* *
28
+ *
29
+ *
30
+ * @author Kacper Urbaniec
31
+ * @version 2024-01-15
32
+ */
33
+ @ExtendWith(SpringExtension ::class )
34
+ @SpringBootTest(classes = [ATestConfig ::class ])
35
+ @TestPropertySource(" classpath:application.properties" )
36
+ @TestInstance(TestInstance .Lifecycle .PER_CLASS )
37
+ class SelectWhereATest (
38
+ @Qualifier(" aEM" )
39
+ val em : EntityManager ,
40
+ @Qualifier(" aTM" )
41
+ transactionManager : PlatformTransactionManager ,
42
+ @Autowired
43
+ val circularRepo : CircularRepository ,
44
+ @Autowired
45
+ val weirdRepo : WeirdClassRepository
46
+ ) {
47
+ private val transaction = TransactionTemplate (transactionManager)
48
+
49
+ @BeforeAll
50
+ fun beforeAll () = clear()
51
+
52
+ @AfterEach
53
+ fun afterEach () = clear()
54
+
55
+ fun clear () {
56
+ transaction.executeWithoutResult {
57
+ circularRepo.cbf.delete(em, Circular ::class .java).executeUpdate()
58
+ weirdRepo.cbf.delete(em, WeirdClass ::class .java).executeUpdate()
59
+ }
60
+ }
61
+
62
+ @Test
63
+ fun `where` () {
64
+ transaction.executeWithoutResult {
65
+ val id1 = UUID .randomUUID()
66
+ val id2 = UUID .randomUUID()
67
+ circularRepo.persist(Circular (id1, null , null ))
68
+ circularRepo.persist(Circular (id2, id1, null ))
69
+ circularRepo.cbf.update(em, Circular ::class .java)
70
+ .set(" cId" , id2)
71
+ .where(" id" ).eq(id1)
72
+ .executeUpdate()
73
+
74
+ val selection = CircularDSL .select { this .circular { this .circular { } } }
75
+ val circles = circularRepo.select(selection) {
76
+ where(CircularDSL .id.eq(id2))
77
+ }
78
+
79
+ assertEquals(1 , circles.count())
80
+ val circle = circles.firstOrNull()
81
+ assertNotNull(circle)
82
+ assertEquals(id2, circle.id)
83
+ }
84
+ }
85
+
86
+ @Test
87
+ fun whereAnd () {
88
+ transaction.executeWithoutResult {
89
+ val id1 = UUID .randomUUID()
90
+ val id2 = UUID .randomUUID()
91
+ circularRepo.persist(Circular (id1, null , null ))
92
+ circularRepo.persist(Circular (id2, id1, null ))
93
+ circularRepo.cbf.update(em, Circular ::class .java)
94
+ .set(" cId" , id2)
95
+ .where(" id" ).eq(id1)
96
+ .executeUpdate()
97
+
98
+ val selection = CircularDSL .select { this .circular { this .circular { } } }
99
+ val circles = circularRepo.select(selection) {
100
+ whereAnd(
101
+ CircularDSL .id.eq(id1),
102
+ CircularDSL .id.eq(id2),
103
+ )
104
+ }
105
+
106
+ assertEquals(0 , circles.count())
107
+ }
108
+ }
109
+
110
+ @Test
111
+ fun `whereOr with inner whereAnd` () {
112
+ transaction.executeWithoutResult {
113
+ val id1 = UUID .randomUUID()
114
+ val id2 = UUID .randomUUID()
115
+ circularRepo.persist(Circular (id1, null , null ))
116
+ circularRepo.persist(Circular (id2, id1, null ))
117
+ circularRepo.cbf.update(em, Circular ::class .java)
118
+ .set(" cId" , id2)
119
+ .where(" id" ).eq(id1)
120
+ .executeUpdate()
121
+
122
+ val selection = CircularDSL .select { this .circular { this .circular { } } }
123
+ val circles = circularRepo.select(selection) {
124
+ whereOr(
125
+ CircularDSL .id.eq(id1),
126
+ and (
127
+ CircularDSL .id.eq(id2),
128
+ CircularDSL .id.eq(UUID .randomUUID())
129
+ )
130
+ )
131
+ }
132
+
133
+ assertEquals(1 , circles.count())
134
+ val circle = circles.firstOrNull()
135
+ assertNotNull(circle)
136
+ assertEquals(id1, circle.id)
137
+ }
138
+ }
139
+
140
+ @Test
141
+ fun `whereOr with inner whereOr` () {
142
+ transaction.executeWithoutResult {
143
+ val id1 = UUID .randomUUID()
144
+ val id2 = UUID .randomUUID()
145
+ circularRepo.persist(Circular (id1, null , null ))
146
+ circularRepo.persist(Circular (id2, id1, null ))
147
+ circularRepo.cbf.update(em, Circular ::class .java)
148
+ .set(" cId" , id2)
149
+ .where(" id" ).eq(id1)
150
+ .executeUpdate()
151
+
152
+ val selection = CircularDSL .select { this .circular { this .circular { } } }
153
+ val circles = circularRepo.select(selection) {
154
+ whereOr(
155
+ CircularDSL .id.eq(id1),
156
+ or (
157
+ CircularDSL .id.eq(id2),
158
+ CircularDSL .id.eq(UUID .randomUUID())
159
+ )
160
+ )
161
+ }
162
+
163
+ assertEquals(2 , circles.count())
164
+ val circle1 = circles.firstOrNull { it.id == id1 }
165
+ assertNotNull(circle1)
166
+ val circle2 = circles.firstOrNull { it.id == id2 }
167
+ assertNotNull(circle2)
168
+ }
169
+ }
170
+
171
+ @Test
172
+ fun `whereAnd with inner whereAnd` () {
173
+ transaction.executeWithoutResult {
174
+ val id1 = UUID .randomUUID()
175
+ val id2 = UUID .randomUUID()
176
+ circularRepo.persist(Circular (id1, null , null ))
177
+ circularRepo.persist(Circular (id2, id1, null ))
178
+ circularRepo.cbf.update(em, Circular ::class .java)
179
+ .set(" cId" , id2)
180
+ .where(" id" ).eq(id1)
181
+ .executeUpdate()
182
+
183
+ val selection = CircularDSL .select { this .circular { this .circular { } } }
184
+ val circles = circularRepo.select(selection) {
185
+ whereAnd(
186
+ CircularDSL .id.eq(id1),
187
+ and (
188
+ CircularDSL .id.eq(id2),
189
+ CircularDSL .id.eq(UUID .randomUUID())
190
+ )
191
+ )
192
+ }
193
+
194
+ assertEquals(0 , circles.count())
195
+ }
196
+ }
197
+
198
+ @Test
199
+ fun `whereAnd with inner whereOr` () {
200
+ transaction.executeWithoutResult {
201
+ val id1 = UUID .randomUUID()
202
+ val id2 = UUID .randomUUID()
203
+ circularRepo.persist(Circular (id1, null , null ))
204
+ circularRepo.persist(Circular (id2, id1, null ))
205
+ circularRepo.cbf.update(em, Circular ::class .java)
206
+ .set(" cId" , id2)
207
+ .where(" id" ).eq(id1)
208
+ .executeUpdate()
209
+
210
+ val selection = CircularDSL .select { this .circular { this .circular { } } }
211
+ val circles = circularRepo.select(selection) {
212
+ whereAnd(
213
+ CircularDSL .id.eq(id1),
214
+ or (
215
+ CircularDSL .id.eq(id2),
216
+ CircularDSL .id.eq(UUID .randomUUID())
217
+ )
218
+ )
219
+ }
220
+
221
+ assertEquals(0 , circles.count())
222
+ }
223
+ }
224
+
225
+ @Test
226
+ fun `where with inline value class` () {
227
+ transaction.executeWithoutResult {
228
+ val id = UUID .randomUUID()
229
+ val fooBar = FooBar (" foo" , " bar" )
230
+ val foxtrot = Foxtrot (" Foxtrot" )
231
+ weirdRepo.persist(WeirdClass (id, fooBar, foxtrot))
232
+ val id2 = UUID .randomUUID()
233
+ val fooBar2 = FooBar (" foo2" , " bar2" )
234
+ val foxtrot2 = Foxtrot (" foxtrot2" )
235
+ weirdRepo.persist(WeirdClass (id2, fooBar2, foxtrot2))
236
+
237
+ val ws = weirdRepo.select {
238
+ where(WeirdClassDSL .foxtrot.eq(" Foxtrot" ))
239
+ }
240
+ assertEquals(1 , ws.count())
241
+ val w = ws.first()
242
+ assertEquals(id, w.id)
243
+
244
+ val wsInline = weirdRepo.select {
245
+ where(WeirdClassDSL .foxtrot.eqInline(Foxtrot (" Foxtrot" )))
246
+ }
247
+ assertEquals(1 , wsInline.count())
248
+ val wInline = wsInline.first()
249
+ assertEquals(id, wInline.id)
250
+
251
+ assertEquals(w, wInline)
252
+ }
253
+ }
254
+
255
+ @Test
256
+ fun `lower expression` () {
257
+ transaction.executeWithoutResult {
258
+ val id = UUID .randomUUID()
259
+ val fooBar = FooBar (" foo" , " bar" )
260
+ val foxtrot = Foxtrot (" Foxtrot" )
261
+ weirdRepo.persist(WeirdClass (id, fooBar, foxtrot))
262
+ val id2 = UUID .randomUUID()
263
+ val fooBar2 = FooBar (" foo2" , " bar2" )
264
+ val foxtrot2 = Foxtrot (" foxtrot2" )
265
+ weirdRepo.persist(WeirdClass (id2, fooBar2, foxtrot2))
266
+
267
+ val ws = weirdRepo.select {
268
+ where(lower(WeirdClassDSL .foxtrot).eq(" Foxtrot" .lowercase()))
269
+ }
270
+ assertEquals(1 , ws.count())
271
+ val w = ws.first()
272
+ assertEquals(id, w.id)
273
+ }
274
+ }
275
+
276
+ }
0 commit comments