Skip to content

Commit d99d7cf

Browse files
committed
Continued work on one-to-many tests
1 parent 16059c0 commit d99d7cf

File tree

1 file changed

+101
-14
lines changed
  • bee.persistent.test/src/test/kotlin/com/beeproduced/bee/persistent/test/base

1 file changed

+101
-14
lines changed

bee.persistent.test/src/test/kotlin/com/beeproduced/bee/persistent/test/base/OneToManyTest.kt

+101-14
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package com.beeproduced.bee.persistent.test.base
22

33
import com.beeproduced.bee.persistent.blaze.selection.BeeSelection
44
import com.beeproduced.datasource.test.dsl.WorkCollectionDSL
5+
import com.beeproduced.datasource.test.dsl.WorkDSL
56
import com.beeproduced.datasource.test.onetomany.*
67
import jakarta.persistence.EntityManager
78
import org.junit.jupiter.api.AfterEach
@@ -81,7 +82,7 @@ class OneToManyTest(
8182
}.firstOrNull()
8283

8384
assertNotNull(collection)
84-
assertWorkCollection(collection, collectionId, setOf(workId1, workId2), 2)
85+
assertWorkCollection(collection, collectionId, setOf(workId1, workId2), 3)
8586
}
8687
}
8788

@@ -142,7 +143,7 @@ class OneToManyTest(
142143
}.firstOrNull()
143144

144145
assertNotNull(collection)
145-
assertWorkCollection(collection, collectionId2, setOf(workId2), 2)
146+
assertWorkCollection(collection, collectionId2, setOf(workId2), 3)
146147
}
147148
}
148149

@@ -180,11 +181,82 @@ class OneToManyTest(
180181
assertEquals(2, collections.count())
181182
val collection1 = collections.first { it.id == collectionId1 }
182183
val collection2 = collections.first { it.id == collectionId2 }
183-
assertWorkCollection(collection1, collectionId1, setOf(workId1), 2)
184-
assertWorkCollection(collection2, collectionId2, setOf(workId2), 2)
184+
assertWorkCollection(collection1, collectionId1, setOf(workId1), 3)
185+
assertWorkCollection(collection2, collectionId2, setOf(workId2), 3)
185186
}
186187
}
187188

189+
@Test
190+
fun `persist all and select with relation loaded`() {
191+
// Note: `persistAllAndSelect` will not be implemented for `bee.persistent.blaze`
192+
var collectionId1: Long = -1
193+
var collectionId2: Long = -1
194+
var workId1 = WorkId()
195+
var workId2 = WorkId()
196+
197+
transaction.executeWithoutResult {
198+
val collection1 = collectionRepo.persist(WorkCollection())
199+
collectionId1 = collection1.id
200+
val collection2 = collectionRepo.persist(WorkCollection())
201+
collectionId2 = collection2.id
202+
val work1 = workRepo.persist(Work(WorkId(++workIdCount, collectionId1), "Hey!"))
203+
workId1 = work1.id
204+
val work2 = workRepo.persist(Work(WorkId(++workIdCount, collectionId2), "Moin!"))
205+
workId2 = work2.id
206+
207+
val selection = WorkDSL.select {
208+
this.workCollection { this.works { this.workCollection { } } }
209+
}
210+
211+
val pstWorks = listOf(work1, work2).map {
212+
workRepo.select(selection) {
213+
where(WorkDSL.id.eq(it.id))
214+
}.first()
215+
}
216+
217+
val pstWork1 = pstWorks.first { it.id == workId1 }
218+
val pstWork2 = pstWorks.first { it.id == workId2 }
219+
assertWork(pstWork1, collectionId1, setOf(workId1), 3)
220+
assertWork(pstWork2, collectionId2, setOf(workId2), 3)
221+
}
222+
}
223+
224+
@Test
225+
// Note: This test cases does not work with `bee.persistent.jpa`!
226+
// Single relations are loaded eagerly since Hibernate 6
227+
fun `persist all and select with relation not loaded`() {
228+
// Note: `persistAllAndSelect` will not be implemented for `bee.persistent.blaze`
229+
var collectionId1: Long = -1
230+
var collectionId2: Long = -1
231+
var workId1 = WorkId()
232+
var workId2 = WorkId()
233+
234+
transaction.executeWithoutResult {
235+
val collection1 = collectionRepo.persist(WorkCollection())
236+
collectionId1 = collection1.id
237+
val collection2 = collectionRepo.persist(WorkCollection())
238+
collectionId2 = collection2.id
239+
val work1 = workRepo.persist(Work(WorkId(++workIdCount, collectionId1), "Hey!"))
240+
workId1 = work1.id
241+
val work2 = workRepo.persist(Work(WorkId(++workIdCount, collectionId2), "Moin!"))
242+
workId2 = work2.id
243+
244+
val selection = BeeSelection.empty()
245+
246+
val pstWorks = listOf(work1, work2).map {
247+
workRepo.select(selection) {
248+
where(WorkDSL.id.eq(it.id))
249+
}.first()
250+
}
251+
252+
val pstWork1 = pstWorks.first { it.id == workId1 }
253+
val pstWork2 = pstWorks.first { it.id == workId2 }
254+
assertWork(pstWork1, collectionId1, setOf(workId1), 0)
255+
assertWork(pstWork2, collectionId2, setOf(workId2), 0)
256+
}
257+
}
258+
259+
188260
private fun assertWorkCollection(
189261
workCollection: WorkCollection,
190262
collectionId: Long, workIds: Set<WorkId>, depth: Int
@@ -200,18 +272,33 @@ class OneToManyTest(
200272
assertNotNull(works)
201273
assertEquals(workIds.count(), works.count())
202274
for (work in works) {
203-
workIds.contains(work.id)
204-
val newDepth = depth - 1
205-
val newWorkCollection = work.workCollection
206-
if (newDepth != 0) {
207-
assertNotNull(newWorkCollection)
208-
assertWorkCollection(newWorkCollection, collectionId, workIds, newDepth)
209-
} else {
210-
assertNull(newWorkCollection)
211-
}
212-
275+
// assertTrue { workIds.contains(work.id) }
276+
assertWork(work, collectionId, workIds, depth - 1)
277+
278+
// val newDepth = depth - 1
279+
// val newWorkCollection = work.workCollection
280+
// if (newDepth != 0) {
281+
// assertNotNull(newWorkCollection)
282+
// assertWorkCollection(newWorkCollection, collectionId, workIds, newDepth)
283+
// } else {
284+
// assertNull(newWorkCollection)
285+
// }
213286
}
214287
}
215288

289+
private fun assertWork(
290+
work: Work,
291+
collectionId: Long, workIds: Set<WorkId>, depth: Int
292+
) {
293+
assertTrue { workIds.contains(work.id) }
294+
val collection = work.workCollection
295+
if (depth == 0) {
296+
assertNull(collection)
297+
return
298+
}
299+
300+
assertNotNull(collection)
301+
assertWorkCollection(collection, collectionId, workIds, depth - 1)
302+
}
216303

217304
}

0 commit comments

Comments
 (0)