Refactor: 封装获取neo4j的连接函数
This commit is contained in:
@@ -2,14 +2,13 @@ package neo4j
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"github.com/neo4j/neo4j-go-driver/v5/neo4j"
|
"github.com/neo4j/neo4j-go-driver/v5/neo4j"
|
||||||
|
|
||||||
"knowledge-graph-backend/config"
|
"knowledge-graph-backend/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
func DoConnect(cfg config.Neo4jConfig) {
|
func NewDriver(cfg config.Neo4jConfig) neo4j.DriverWithContext {
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
driver, err := neo4j.NewDriverWithContext(
|
driver, err := neo4j.NewDriverWithContext(
|
||||||
cfg.URI,
|
cfg.URI,
|
||||||
@@ -17,11 +16,11 @@ func DoConnect(cfg config.Neo4jConfig) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
defer driver.Close(ctx)
|
|
||||||
|
|
||||||
err = driver.VerifyConnectivity(ctx)
|
err = driver.VerifyConnectivity(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
fmt.Println("Connection established.")
|
|
||||||
|
return driver
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,12 +1,13 @@
|
|||||||
package neo4j
|
package neo4j
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"knowledge-graph-backend/config"
|
"knowledge-graph-backend/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestDoConnect(t *testing.T) {
|
func TestNewDriver(t *testing.T) {
|
||||||
cnf, err := config.Load()
|
cnf, err := config.Load()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("[cnf]: %v", err)
|
t.Errorf("[cnf]: %v", err)
|
||||||
@@ -16,5 +17,6 @@ func TestDoConnect(t *testing.T) {
|
|||||||
Username: cnf.Neo4j.Username,
|
Username: cnf.Neo4j.Username,
|
||||||
Password: cnf.Neo4j.Password,
|
Password: cnf.Neo4j.Password,
|
||||||
}
|
}
|
||||||
DoConnect(cfg)
|
driver := NewDriver(cfg)
|
||||||
|
defer driver.Close(context.Background())
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user