-
Notifications
You must be signed in to change notification settings - Fork 0
/
read_test.go
115 lines (102 loc) · 2.73 KB
/
read_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
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
package neo4j
import (
"fmt"
"github.com/aliworkshop/configer"
"github.com/aliworkshop/dbcore"
"github.com/stretchr/testify/assert"
"os"
"testing"
)
func TestNeo_Get(t *testing.T) {
registry := configer.New()
registry.SetConfigType("yaml")
f, err := os.Open("./config.sample.yaml")
if err != nil {
panic("cannot read config: " + err.Error())
}
err = registry.ReadConfig(f)
if err != nil {
panic("cannot read config" + err.Error())
}
db := NewNeo4jRepository(registry)
err = db.Connect()
assert.Nil(t, err)
defer db.Close()
item, err := db.Get(NewQuery().WithModelFunc(func() dbcore.Modeler {
return new(User)
}))
assert.Nil(t, err)
assert.NotNil(t, item)
}
func TestNeo_Count(t *testing.T) {
registry := configer.New()
registry.SetConfigType("yaml")
f, err := os.Open("./config.sample.yaml")
if err != nil {
panic("cannot read config: " + err.Error())
}
err = registry.ReadConfig(f)
if err != nil {
panic("cannot read config" + err.Error())
}
db := NewNeo4jRepository(registry)
err = db.Connect()
assert.Nil(t, err)
defer db.Close()
count, err := db.Count(NewQuery().WithModelFunc(func() dbcore.Modeler {
return new(User)
}))
assert.Nil(t, err)
assert.NotNil(t, count)
}
func TestNeo_Followings(t *testing.T) {
registry := configer.New()
registry.SetConfigType("yaml")
f, err := os.Open("./config.sample.yaml")
if err != nil {
panic("cannot read config: " + err.Error())
}
err = registry.ReadConfig(f)
if err != nil {
panic("cannot read config" + err.Error())
}
db := NewNeo4jRepository(registry)
err = db.Connect()
assert.Nil(t, err)
defer db.Close()
items, err := db.List(NewQuery().WithModelFunc(func() dbcore.Modeler {
return new(User)
}).WithQuery(`MATCH(u:User)-[:FOLLOWS]->(n:User) WHERE u.Name=$Name RETURN n`).
WithCountQuery(`MATCH(u:User)-[:FOLLOWS]->(n:User) WHERE u.Name=$Name RETURN COUNT(n)`).
WithParams("Name", "ali").
WithSorts(dbcore.SortItem{Field: "id", Order: dbcore.DESC}).
WithPage(1).WithPageSize(15))
assert.Nil(t, err)
for _, item := range items.([]*User) {
fmt.Println(item)
}
}
func TestNeo_Followers(t *testing.T) {
registry := configer.New()
registry.SetConfigType("yaml")
f, err := os.Open("./config.sample.yaml")
if err != nil {
panic("cannot read config: " + err.Error())
}
err = registry.ReadConfig(f)
if err != nil {
panic("cannot read config" + err.Error())
}
db := NewNeo4jRepository(registry)
err = db.Connect()
assert.Nil(t, err)
defer db.Close()
items, err := db.List(NewQuery().WithModelFunc(func() dbcore.Modeler {
return new(User)
}).WithQuery(`
MATCH(u:User)<-[:FOLLOWS]-(n:User) WHERE u.first_name=$Name RETURN n`).WithParams("Name", "John"))
assert.Nil(t, err)
for _, item := range items.([]*User) {
fmt.Println(item)
}
}