1515package xormadapter
1616
1717import (
18- "github.com/casbin/casbin/v2/util"
1918 "log"
2019 "strings"
2120 "testing"
2221
2322 "github.com/casbin/casbin/v2"
23+ "github.com/casbin/casbin/v2/util"
2424 _ "github.com/go-sql-driver/mysql"
2525 _ "github.com/lib/pq"
2626)
@@ -45,20 +45,15 @@ func testGetPolicy(t *testing.T, e *casbin.Enforcer, res [][]string) {
4545 }
4646}
4747
48- func initPolicy (t * testing.T , driverName string , dataSourceName string , dbSpecified ... bool ) {
48+ func initPolicy (t * testing.T , a * Adapter ) {
4949 // Because the DB is empty at first,
5050 // so we need to load the policy from the file adapter (.CSV) first.
5151 e , _ := casbin .NewEnforcer ("examples/rbac_model.conf" , "examples/rbac_policy.csv" )
5252
53- a , err := NewAdapter (driverName , dataSourceName , dbSpecified ... )
54- if err != nil {
55- panic (err )
56- }
57-
5853 // This is a trick to save the current policy to the DB.
5954 // We can't call e.SavePolicy() because the adapter in the enforcer is still the file adapter.
6055 // The current policy means the policy in the Casbin enforcer (aka in memory).
61- err = a .SavePolicy (e .GetModel ())
56+ err : = a .SavePolicy (e .GetModel ())
6257 if err != nil {
6358 panic (err )
6459 }
@@ -75,30 +70,28 @@ func initPolicy(t *testing.T, driverName string, dataSourceName string, dbSpecif
7570 testGetPolicy (t , e , [][]string {{"alice" , "data1" , "read" }, {"bob" , "data2" , "write" }, {"data2_admin" , "data2" , "read" }, {"data2_admin" , "data2" , "write" }})
7671}
7772
78- func testSaveLoad (t * testing.T , driverName string , dataSourceName string , dbSpecified ... bool ) {
73+ func testSaveLoad (t * testing.T , a * Adapter ) {
7974 // Initialize some policy in DB.
80- initPolicy (t , driverName , dataSourceName , dbSpecified ... )
75+ initPolicy (t , a )
8176 // Note: you don't need to look at the above code
8277 // if you already have a working DB with policy inside.
8378
8479 // Now the DB has policy, so we can provide a normal use case.
8580 // Create an adapter and an enforcer.
8681 // NewEnforcer() will load the policy automatically.
87- a , _ := NewAdapter (driverName , dataSourceName , dbSpecified ... )
8882 e , _ := casbin .NewEnforcer ("examples/rbac_model.conf" , a )
8983 testGetPolicy (t , e , [][]string {{"alice" , "data1" , "read" }, {"bob" , "data2" , "write" }, {"data2_admin" , "data2" , "read" }, {"data2_admin" , "data2" , "write" }})
9084}
9185
92- func testAutoSave (t * testing.T , driverName string , dataSourceName string , dbSpecified ... bool ) {
86+ func testAutoSave (t * testing.T , a * Adapter ) {
9387 // Initialize some policy in DB.
94- initPolicy (t , driverName , dataSourceName , dbSpecified ... )
88+ initPolicy (t , a )
9589 // Note: you don't need to look at the above code
9690 // if you already have a working DB with policy inside.
9791
9892 // Now the DB has policy, so we can provide a normal use case.
9993 // Create an adapter and an enforcer.
10094 // NewEnforcer() will load the policy automatically.
101- a , _ := NewAdapter (driverName , dataSourceName , dbSpecified ... )
10295 e , _ := casbin .NewEnforcer ("examples/rbac_model.conf" , a )
10396
10497 // AutoSave is enabled by default.
@@ -152,16 +145,15 @@ func testAutoSave(t *testing.T, driverName string, dataSourceName string, dbSpec
152145 testGetPolicy (t , e , [][]string {{"alice" , "data1" , "read" }, {"bob" , "data2" , "write" }})
153146}
154147
155- func testFilteredPolicy (t * testing.T , driverName string , dataSourceName string , dbSpecified ... bool ) {
148+ func testFilteredPolicy (t * testing.T , a * Adapter ) {
156149 // Initialize some policy in DB.
157- initPolicy (t , driverName , dataSourceName , dbSpecified ... )
150+ initPolicy (t , a )
158151 // Note: you don't need to look at the above code
159152 // if you already have a working DB with policy inside.
160153
161154 // Now the DB has policy, so we can provide a normal use case.
162155 // Create an adapter and an enforcer.
163156 // NewEnforcer() will load the policy automatically.
164- a , _ := NewAdapter (driverName , dataSourceName , dbSpecified ... )
165157 e , _ := casbin .NewEnforcer ("examples/rbac_model.conf" )
166158 // Now set the adapter
167159 e .SetAdapter (a )
@@ -194,16 +186,15 @@ func testFilteredPolicy(t *testing.T, driverName string, dataSourceName string,
194186 testGetPolicy (t , e , [][]string {{"alice" , "data1" , "read" }, {"bob" , "data2" , "write" }})
195187}
196188
197- func testRemovePolicies (t * testing.T , driverName string , dataSourceName string , dbSpecified ... bool ) {
189+ func testRemovePolicies (t * testing.T , a * Adapter ) {
198190 // Initialize some policy in DB.
199- initPolicy (t , driverName , dataSourceName , dbSpecified ... )
191+ initPolicy (t , a )
200192 // Note: you don't need to look at the above code
201193 // if you already have a working DB with policy inside.
202194
203195 // Now the DB has policy, so we can provide a normal use case.
204196 // Create an adapter and an enforcer.
205197 // NewEnforcer() will load the policy automatically.
206- a , _ := NewAdapter (driverName , dataSourceName , dbSpecified ... )
207198 e , _ := casbin .NewEnforcer ("examples/rbac_model.conf" )
208199
209200 // Now set the adapter
@@ -236,16 +227,15 @@ func testRemovePolicies(t *testing.T, driverName string, dataSourceName string,
236227 testGetPolicy (t , e , [][]string {{"max" , "data1" , "delete" }})
237228}
238229
239- func testAddPolicies (t * testing.T , driverName string , dataSourceName string , dbSpecified ... bool ) {
230+ func testAddPolicies (t * testing.T , a * Adapter ) {
240231 // Initialize some policy in DB.
241- initPolicy (t , driverName , dataSourceName , dbSpecified ... )
232+ initPolicy (t , a )
242233 // Note: you don't need to look at the above code
243234 // if you already have a working DB with policy inside.
244235
245236 // Now the DB has policy, so we can provide a normal use case.
246237 // Create an adapter and an enforcer.
247238 // NewEnforcer() will load the policy automatically.
248- a , _ := NewAdapter (driverName , dataSourceName , dbSpecified ... )
249239 e , _ := casbin .NewEnforcer ("examples/rbac_model.conf" )
250240
251241 // Now set the adapter
@@ -268,16 +258,15 @@ func testAddPolicies(t *testing.T, driverName string, dataSourceName string, dbS
268258 testGetPolicy (t , e , [][]string {{"max" , "data2" , "read" }, {"max" , "data1" , "write" }})
269259}
270260
271- func testUpdatePolicies (t * testing.T , driverName string , dataSourceName string , dbSpecified ... bool ) {
261+ func testUpdatePolicies (t * testing.T , a * Adapter ) {
272262 // Initialize some policy in DB.
273- initPolicy (t , driverName , dataSourceName , dbSpecified ... )
263+ initPolicy (t , a )
274264 // Note: you don't need to look at the above code
275265 // if you already have a working DB with policy inside.
276266
277267 // Now the DB has policy, so we can provide a normal use case.
278268 // Create an adapter and an enforcer.
279269 // NewEnforcer() will load the policy automatically.
280- a , _ := NewAdapter (driverName , dataSourceName , dbSpecified ... )
281270 e , _ := casbin .NewEnforcer ("examples/rbac_model.conf" )
282271
283272 // Now set the adapter
@@ -301,16 +290,15 @@ func testUpdatePolicies(t *testing.T, driverName string, dataSourceName string,
301290 testGetPolicy (t , e , [][]string {{"bob" , "data1" , "read" }, {"bob" , "data2" , "write" }, {"data2_admin" , "data2" , "read" }, {"data2_admin" , "data2" , "write" }})
302291}
303292
304- func testUpdateFilteredPolicies (t * testing.T , driverName string , dataSourceName string , dbSpecified ... bool ) {
293+ func testUpdateFilteredPolicies (t * testing.T , a * Adapter ) {
305294 // Initialize some policy in DB.
306- initPolicy (t , driverName , dataSourceName , dbSpecified ... )
295+ initPolicy (t , a )
307296 // Note: you don't need to look at the above code
308297 // if you already have a working DB with policy inside.
309298
310299 // Now the DB has policy, so we can provide a normal use case.
311300 // Create an adapter and an enforcer.
312301 // NewEnforcer() will load the policy automatically.
313- a , _ := NewAdapter (driverName , dataSourceName , dbSpecified ... )
314302 e , _ := casbin .NewEnforcer ("examples/rbac_model.conf" )
315303
316304 // Now set the adapter
@@ -370,23 +358,30 @@ func TestAdapters(t *testing.T) {
370358 // You can also use the following way to use an existing DB "abc":
371359 // testSaveLoad(t, "mysql", "root:@tcp(127.0.0.1:3306)/abc", true)
372360
373- testSaveLoad (t , "mysql" , "root:@tcp(127.0.0.1:3306)/" )
374- testSaveLoad (t , "postgres" , "user=postgres password=postgres host=127.0.0.1 port=5432 sslmode=disable" )
375-
376- testAutoSave (t , "mysql" , "root:@tcp(127.0.0.1:3306)/" )
377- testAutoSave (t , "postgres" , "user=postgres password=postgres host=127.0.0.1 port=5432 sslmode=disable" )
378-
379- testFilteredPolicy (t , "mysql" , "root:@tcp(127.0.0.1:3306)/" )
380-
381- testAddPolicies (t , "mysql" , "root:@tcp(127.0.0.1:3306)/" )
382- testAddPolicies (t , "postgres" , "user=postgres password=postgres host=127.0.0.1 port=5432 sslmode=disable" )
383-
384- testRemovePolicies (t , "mysql" , "root:@tcp(127.0.0.1:3306)/" )
385- testRemovePolicies (t , "postgres" , "user=postgres password=postgres host=127.0.0.1 port=5432 sslmode=disable" )
386-
387- testUpdatePolicies (t , "mysql" , "root:@tcp(127.0.0.1:3306)/" )
388- testUpdatePolicies (t , "postgres" , "user=postgres password=postgres host=127.0.0.1 port=5432 sslmode=disable" )
389-
390- testUpdateFilteredPolicies (t , "mysql" , "root:@tcp(127.0.0.1:3306)/" )
391- testUpdateFilteredPolicies (t , "postgres" , "user=postgres password=postgres host=127.0.0.1 port=5432 sslmode=disable" )
361+ a , _ := NewAdapter ("mysql" , "root:@tcp(127.0.0.1:3306)/" )
362+ testSaveLoad (t , a )
363+ testAutoSave (t , a )
364+ testFilteredPolicy (t , a )
365+ testAddPolicies (t , a )
366+ testRemovePolicies (t , a )
367+ testUpdatePolicies (t , a )
368+ testUpdateFilteredPolicies (t , a )
369+
370+ a , _ = NewAdapter ("postgres" , "user=postgres password=postgres host=127.0.0.1 port=5432 sslmode=disable" )
371+ testSaveLoad (t , a )
372+ testAutoSave (t , a )
373+ testFilteredPolicy (t , a )
374+ testAddPolicies (t , a )
375+ testRemovePolicies (t , a )
376+ testUpdatePolicies (t , a )
377+ testUpdateFilteredPolicies (t , a )
378+
379+ a , _ = NewAdapterWithTableName ("mysql" , "root:@tcp(127.0.0.1:3306)/" , "test" , "abc" )
380+ testSaveLoad (t , a )
381+ testAutoSave (t , a )
382+ testFilteredPolicy (t , a )
383+ testAddPolicies (t , a )
384+ testRemovePolicies (t , a )
385+ testUpdatePolicies (t , a )
386+ testUpdateFilteredPolicies (t , a )
392387}
0 commit comments