Skip to content

Commit b234987

Browse files
committed
Small improvements to tests for JunctionRepository
1 parent ab115a9 commit b234987

File tree

1 file changed

+39
-38
lines changed

1 file changed

+39
-38
lines changed
Lines changed: 39 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package org.virtuslab.unicorn.repositories
22

33
import org.virtuslab.unicorn.{ LongTestUnicorn, TestUnicorn, BaseTest }
4-
import TestUnicorn._
54
import TestUnicorn.driver.simple._
65

76
class JunctionRepositoryTest extends BaseTest[Long] with LongTestUnicorn {
@@ -18,108 +17,110 @@ class JunctionRepositoryTest extends BaseTest[Long] with LongTestUnicorn {
1817

1918
object CustomerId extends IdCompanion[CustomerId]
2019

21-
class OrderCustomerJunctionTable(tag: Tag) extends JunctionTable[OrderId, CustomerId](tag, "order_customer") {
20+
class OrderCustomer(tag: Tag) extends JunctionTable[OrderId, CustomerId](tag, "order_customer") {
2221
def orderId = column[OrderId]("ORDER_ID")
2322
def customerId = column[CustomerId]("CUSTOMER_ID")
2423

2524
def columns = orderId -> customerId
2625
}
26+
object OrderCustomer {
27+
val tableQuery = TableQuery[OrderCustomer]
28+
}
2729

28-
val junctionQueries = TableQuery[OrderCustomerJunctionTable]
29-
30-
object exampleJunctionRepository extends JunctionRepository[OrderId, CustomerId, OrderCustomerJunctionTable](junctionQueries)
30+
object OrderCustomerRepository
31+
extends JunctionRepository[OrderId, CustomerId, OrderCustomer](OrderCustomer.tableQuery)
3132

3233
def createTables(implicit session: Session) = {
33-
junctionQueries.ddl.create
34+
OrderCustomerRepository.create
3435
}
3536

3637
it should "save pairs" in rollback { implicit session =>
3738
createTables
3839

39-
exampleJunctionRepository.save(OrderId(100), CustomerId(200))
40+
OrderCustomerRepository.save(OrderId(100), CustomerId(200))
4041

41-
junctionQueries.run should have size 1
42+
OrderCustomerRepository.findAll() should have size 1
4243
}
4344

4445
it should "save pair only once" in rollback { implicit session =>
4546
createTables
4647

47-
exampleJunctionRepository.save(OrderId(100), CustomerId(200))
48-
exampleJunctionRepository.save(OrderId(100), CustomerId(200))
48+
OrderCustomerRepository.save(OrderId(100), CustomerId(200))
49+
OrderCustomerRepository.save(OrderId(100), CustomerId(200))
4950

50-
junctionQueries.run should have size 1
51+
OrderCustomerRepository.findAll() should have size 1
5152
}
5253

5354
it should "find all pairs" in rollback { implicit session =>
5455
createTables
5556

56-
junctionQueries += ((OrderId(100), CustomerId(200)))
57-
junctionQueries += ((OrderId(101), CustomerId(200)))
57+
OrderCustomerRepository.save(OrderId(100), CustomerId(200))
58+
OrderCustomerRepository.save(OrderId(101), CustomerId(200))
5859

59-
exampleJunctionRepository.findAll should have size 2
60+
OrderCustomerRepository.findAll should have size 2
6061
}
6162

6263
it should "find by first" in rollback { implicit session =>
6364
createTables
6465
val orderId = OrderId(100)
65-
exampleJunctionRepository.save(orderId, CustomerId(200))
66-
exampleJunctionRepository.save(orderId, CustomerId(201))
67-
exampleJunctionRepository.save(OrderId(101), CustomerId(201))
66+
OrderCustomerRepository.save(orderId, CustomerId(200))
67+
OrderCustomerRepository.save(orderId, CustomerId(201))
68+
OrderCustomerRepository.save(OrderId(101), CustomerId(201))
6869

69-
exampleJunctionRepository.forA(orderId) should have size 2
70+
OrderCustomerRepository.forA(orderId) should have size 2
7071
}
7172

7273
it should "find by second" in rollback { implicit session =>
7374
createTables
7475
val customerId = CustomerId(200)
75-
exampleJunctionRepository.save(OrderId(100), customerId)
76-
exampleJunctionRepository.save(OrderId(101), customerId)
77-
exampleJunctionRepository.save(OrderId(101), CustomerId(100))
76+
OrderCustomerRepository.save(OrderId(100), customerId)
77+
OrderCustomerRepository.save(OrderId(101), customerId)
78+
OrderCustomerRepository.save(OrderId(101), CustomerId(100))
7879

79-
exampleJunctionRepository.forB(customerId) should have size 2
80+
OrderCustomerRepository.forB(customerId) should have size 2
8081
}
8182

8283
it should "delete by first" in rollback { implicit session =>
8384
createTables
8485
val orderId = OrderId(100)
85-
exampleJunctionRepository.save(orderId, CustomerId(200))
86-
exampleJunctionRepository.save(orderId, CustomerId(201))
86+
OrderCustomerRepository.save(orderId, CustomerId(200))
87+
OrderCustomerRepository.save(orderId, CustomerId(201))
8788

88-
exampleJunctionRepository.delete(orderId, CustomerId(200))
89+
OrderCustomerRepository.delete(orderId, CustomerId(200))
8990

90-
junctionQueries.run should have size 1
91+
OrderCustomerRepository.findAll() should have size 1
9192
}
9293

9394
it should "delete all items with given first" in rollback { implicit session =>
9495
createTables
9596
val orderId = OrderId(100)
96-
exampleJunctionRepository.save(orderId, CustomerId(200))
97-
exampleJunctionRepository.save(orderId, CustomerId(201))
97+
OrderCustomerRepository.save(orderId, CustomerId(200))
98+
OrderCustomerRepository.save(orderId, CustomerId(201))
9899

99-
exampleJunctionRepository.deleteForA(orderId)
100+
OrderCustomerRepository.deleteForA(orderId)
100101

101-
junctionQueries.run shouldBe empty
102+
OrderCustomerRepository.findAll() shouldBe empty
102103
}
103104

104105
it should "delete all items with given second" in rollback { implicit session =>
105106
createTables
106107
val customerId = CustomerId(200)
107-
exampleJunctionRepository.save(OrderId(100), customerId)
108-
exampleJunctionRepository.save(OrderId(101), customerId)
108+
OrderCustomerRepository.save(OrderId(100), customerId)
109+
OrderCustomerRepository.save(OrderId(101), customerId)
109110

110-
exampleJunctionRepository.deleteForB(customerId)
111+
OrderCustomerRepository.deleteForB(customerId)
111112

112-
junctionQueries.run shouldBe empty
113+
OrderCustomerRepository.findAll() shouldBe empty
113114
}
114115

115116
it should "check that one pair exists" in rollback { implicit session =>
116117
createTables
117118
val customerId = CustomerId(200)
118-
exampleJunctionRepository.save(OrderId(100), customerId)
119-
exampleJunctionRepository.save(OrderId(101), customerId)
119+
OrderCustomerRepository.save(OrderId(100), customerId)
120+
OrderCustomerRepository.save(OrderId(101), customerId)
120121

121-
exampleJunctionRepository.exists(OrderId(200), customerId)(session) shouldBe false
122-
exampleJunctionRepository.exists(OrderId(101), customerId)(session) shouldBe true
122+
OrderCustomerRepository.exists(OrderId(200), customerId)(session) shouldBe false
123+
OrderCustomerRepository.exists(OrderId(101), customerId)(session) shouldBe true
123124
}
124125

125126
}

0 commit comments

Comments
 (0)