-
Notifications
You must be signed in to change notification settings - Fork 0
/
DbTest.kt
89 lines (70 loc) · 2.43 KB
/
DbTest.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package ca.allanwang.mcgill.db
import ca.allanwang.kit.logger.WithLogging
import ca.allanwang.mcgill.db.bindings.getMap
import ca.allanwang.mcgill.db.bindings.stdlog
import ca.allanwang.mcgill.db.bindings.toMap
import ca.allanwang.mcgill.db.internal.DbSetup
import ca.allanwang.mcgill.db.internal.testTransaction
import ca.allanwang.mcgill.db.tables.*
import org.jetbrains.exposed.sql.SchemaUtils.create
import org.jetbrains.exposed.sql.SchemaUtils.drop
import org.jetbrains.exposed.sql.Table
import org.jetbrains.exposed.sql.select
import org.jetbrains.exposed.sql.selectAll
import org.jetbrains.exposed.sql.transactions.transaction
import org.junit.BeforeClass
import org.junit.Test
import kotlin.test.assertEquals
class DbTest {
companion object : WithLogging() {
@BeforeClass
@JvmStatic
fun before() = DbSetup.connect()
}
private fun Table.assertCount(count: Int, message: String? = null) =
assertEquals(count, selectAll().count(),
message ?: "$tableName count mismatch\n\n${getMap(limit = 20)}")
/**
* Assert that all related tables have the appropriate counts
*/
private fun assertUserCount(userCount: Int) {
TestUsers.assertCount(userCount)
TestGroups.assertCount(if (userCount == 0) 0 else userCount + 1) // one shared group
TestUserGroups.assertCount(userCount * 2)
}
@Test
fun batchInsert() {
testTransaction {
testUser(19).save()
}
}
@Test
fun test() {
testTransaction {
assertUserCount(0)
val test1 = testUser(1)
// first save
test1.save()
assertUserCount(1)
test1.assertMatch()
test1.name = test1.name + "$2"
// updating attribute; table size should not change
test1.save()
assertUserCount(1)
test1.assertMatch()
val test2 = testUser(2)
// no change should occur
test2.delete()
assertUserCount(1)
// saving new user
test2.save()
assertUserCount(2)
test2.assertMatch()
// deleting valid user
test1.delete()
TestUsers.assertCount(1) // only user2 remains
TestGroups.assertCount(3) // user1's group still exists
TestUserGroups.assertCount(2) // only mappings for user2
}
}
}