-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUserRoleIntegrationSpec.groovy
118 lines (100 loc) · 3.72 KB
/
UserRoleIntegrationSpec.groovy
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
110
111
112
113
114
115
116
117
118
import com.example.Role
import com.example.User
import com.example.UserRole
import grails.test.mixin.Mock
import grails.test.mixin.integration.Integration
import grails.transaction.Transactional
import org.hibernate.QueryException
import spock.lang.Specification
@Integration
@Transactional
class UserRoleIntegrationSpec extends Specification {
UserRole adminAdminRole
UserRole staffStaffRole
def setup() {
User admin = new User(username: 'admin', password: 'password').save()
User staff = new User(username: 'staff', password: 'password').save()
Role adminRole = new Role(authority: 'ROLE_ADMIN').save()
Role staffRole = new Role(authority: 'ROLE_STAFF').save()
adminAdminRole = UserRole.create admin, adminRole
staffStaffRole = UserRole.create staff, staffRole
}
void "Test sorting by role.authority"() {
when: "we execute a query that orders by role.authority"
List<UserRole> l = UserRole.where {
role.authority in ['ROLE_ADMIN', 'ROLE_STAFF']
order('role.authority', 'desc')
}.list()
then: "we have a sorted list"
l.size() == 2
l[0].id == staffStaffRole.id
}
void "Test sorting by user.username"() {
when: "we execute a query that orders by user.username"
List<UserRole> l = UserRole.where {
role.authority in ['ROLE_ADMIN', 'ROLE_STAFF']
order('user.username', 'desc')
}.list()
then: "we have a sorted list"
l.size() == 2
l[0].id == staffStaffRole.id
}
void "Test querying by username first"() {
when: "we execute a query by username and sort by role"
List<UserRole> l = UserRole.where {
user.username in ['admin', 'staff']
order('role.authority', 'desc')
}.list()
then: "we have a sorted list"
l.size() == 2
l[0].id == staffStaffRole.id
}
void "Test querying and sorting by username"() {
when: "we execute a query by username and sort by role"
List<UserRole> l = UserRole.where {
user.username in ['admin', 'staff']
order('user.username', 'desc')
}.list()
then: "we have a sorted list"
l.size() == 2
l[0].id == staffStaffRole.id
}
void "Test using aliases"() {
when: "we include an alias for each table"
List<UserRole> l = UserRole.where {
createAlias('role', 'role')
createAlias('user', 'user')
role.authority in ['ROLE_ADMIN', 'ROLE_STAFF']
order('role.authority', 'desc')
}.list()
then: "we have a sorted list"
l.size() == 2
l[0].id == staffStaffRole.id
}
void "Test using aliases with inList"() {
when: "we include an alias for each table and use inList"
List<UserRole> l = UserRole.where {
createAlias('role', 'role')
createAlias('user', 'user')
inList('role.authority', ['ROLE_ADMIN', 'ROLE_STAFF'])
order('role.authority', 'desc')
}.list()
then: "we have a sorted list"
l.size() == 2
l[0].id == staffStaffRole.id
}
// This test illustrates another possible work-around but the approach
// requires additional logic when the sort column is passed in dynamically
void "Test querying and sorting by username with closure"() {
when: "we execute a query by username and sort by role"
List<UserRole> l = UserRole.where {
user {
username in ['admin', 'staff']
order('username', 'desc')
}
}.list()
then: "we have a sorted list"
l.size() == 2
l[0].id == staffStaffRole.id
}
}