-
Notifications
You must be signed in to change notification settings - Fork 4
/
model_test.go
74 lines (62 loc) · 1.45 KB
/
model_test.go
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
package firestoreadapter
import (
"fmt"
"sort"
"strings"
"testing"
"github.com/casbin/casbin/v2/model"
)
func TestSaveAndLoadModel(t *testing.T) {
original, err := model.NewModelFromFile("examples/rbac_model.conf")
if err != nil {
t.Fatal(err)
}
client := getClient()
config := Config{Collection: "firestoreadapter-unittest"}
if err = SaveModelWithConfig(client, "examples/rbac_model.conf", config); err != nil {
t.Errorf("got %v, wants no error", err)
return
}
actual, err := LoadModelWithConfig(client, config)
if err != nil {
t.Errorf("got %v, wants no error", err)
return
}
s1 := modelToText(original)
s2 := modelToText(actual)
if s1 != s2 {
t.Errorf("Loaded model is different")
}
}
func TestSaveInvalidFile(t *testing.T) {
db := getClient()
config := Config{
Collection: "firestoreadapter-unittest",
}
err := SaveModelWithConfig(db, "examples/rbac_policy.csv", config)
if err == nil {
t.Errorf("got no error, wants an error")
return
}
}
func TestLoadModelFail(t *testing.T) {
db := getClient()
config := Config{
Collection: "firestoreadapter-notexist",
}
_, err := LoadModelWithConfig(db, config)
if err == nil {
t.Errorf("got no error, wants an error")
return
}
}
func modelToText(model model.Model) string {
var lines []string
for k, v := range model {
for i, j := range v {
lines = append(lines, fmt.Sprintf("%s.%s: %s", k, i, j.Value))
}
}
sort.Strings(lines)
return strings.Join(lines, "\n")
}