-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathUpdateStatementSpec.kt
109 lines (97 loc) · 4.1 KB
/
UpdateStatementSpec.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package io.zeko.db.sql
import io.zeko.db.sql.dsl.*
import io.zeko.model.entities.Address
import io.zeko.model.entities.User
import io.zeko.model.entities.UserRole
import org.spekframework.spek2.Spek
import org.spekframework.spek2.style.specification.describe
import kotlin.test.assertEquals
class UpdateStatementSpec : Spek({
fun debug(msg: Any) {
if (false) println(msg.toString())
}
describe("Query Builder Update Statement test") {
context("Given a set of Entities User, UserRole and Address") {
it("should match update sql when passing entity with properties set with Map") {
val sql = Update(User(
mapOf(
"id" to 1,
"name" to "O'Connor"
)
)).toSql()
debug(sql)
assertEquals("UPDATE users SET id = 1, name = 'O''Connor'", sql)
}
it("should match update parameterized sql when passing entity with properties set with Map") {
val sql = Update(User(
mapOf(
"id" to 1,
"name" to "O'Connor"
)
), true).toSql()
debug(sql)
assertEquals("UPDATE users SET id = ?, name = ?", sql)
}
it("should match update sql when passing pairs") {
val sql = Update(User(
"id" to 1,
"name" to "O'Connor"
)).toSql()
debug(sql)
assertEquals("UPDATE users SET id = 1, name = 'O''Connor'", sql)
}
it("should match update sql using where by passing Query object") {
val sql = Update(User().apply {
name = "Leng"
}).where(
Query().where(
"id" greater 100,
"age" eq 60
)
).toSql()
debug(sql)
assertEquals("UPDATE users SET name = 'Leng' WHERE id > 100 AND age = 60", sql)
}
it("should match update sql using where by passing multiple conditions") {
val sql = Update(User().apply {
name = "Leng"
}).where(
"id" greater 100,
"age" eq 60
).toSql()
debug(sql)
assertEquals("UPDATE users SET name = 'Leng' WHERE id > 100 AND age = 60", sql)
}
it("should match update sql using primitive typed properties only") {
val user = User().apply {
age = 60
id = 12
roles = listOf(UserRole().apply { role_name = "admin" })
addresses = listOf(Address().apply { street1 = "jalan 123" })
}
val sql = Update(user).toSql()
debug(sql)
assertEquals("UPDATE users SET age = 60, id = 12", sql)
}
it("should match update sql when using escape table name") {
val sql = Update(User(
"id" to 1,
"name" to "Leng"
))
.escapeTable(true)
.toSql()
debug(sql)
assertEquals("UPDATE \"users\" SET id = 1, name = 'Leng'", sql)
}
it("should match update sql when using entity class name as table") {
val sql = Update(Address().apply {
user_id = 1
street1 = "Jalan 123"
street2 = "Taman OUG"
}).toSql()
debug(sql)
assertEquals("UPDATE address SET user_id = 1, street1 = 'Jalan 123', street2 = 'Taman OUG'", sql)
}
}
}
})