github.com/scraniel/migrate@v0.0.0-20230320185700-339088f36cee/database/neo4j/neo4j_test.go (about) 1 package neo4j 2 3 import ( 4 "bytes" 5 "context" 6 "fmt" 7 "log" 8 "testing" 9 10 "github.com/dhui/dktest" 11 "github.com/neo4j/neo4j-go-driver/neo4j" 12 13 "github.com/golang-migrate/migrate/v4" 14 dt "github.com/golang-migrate/migrate/v4/database/testing" 15 "github.com/golang-migrate/migrate/v4/dktesting" 16 _ "github.com/golang-migrate/migrate/v4/source/file" 17 ) 18 19 var ( 20 opts = dktest.Options{PortRequired: true, ReadyFunc: isReady, 21 Env: map[string]string{"NEO4J_AUTH": "neo4j/migratetest", "NEO4J_ACCEPT_LICENSE_AGREEMENT": "yes"}} 22 specs = []dktesting.ContainerSpec{ 23 {ImageName: "neo4j:4.0", Options: opts}, 24 {ImageName: "neo4j:4.0-enterprise", Options: opts}, 25 {ImageName: "neo4j:3.5", Options: opts}, 26 {ImageName: "neo4j:3.5-enterprise", Options: opts}, 27 } 28 ) 29 30 func neoConnectionString(host, port string) string { 31 return fmt.Sprintf("bolt://neo4j:migratetest@%s:%s", host, port) 32 } 33 34 func isReady(ctx context.Context, c dktest.ContainerInfo) bool { 35 ip, port, err := c.Port(7687) 36 if err != nil { 37 return false 38 } 39 40 driver, err := neo4j.NewDriver( 41 neoConnectionString(ip, port), 42 neo4j.BasicAuth("neo4j", "migratetest", ""), 43 func(config *neo4j.Config) { 44 config.Encrypted = false 45 }) 46 if err != nil { 47 return false 48 } 49 defer func() { 50 if err := driver.Close(); err != nil { 51 log.Println("close error:", err) 52 } 53 }() 54 session, err := driver.Session(neo4j.AccessModeRead) 55 if err != nil { 56 return false 57 } 58 result, err := session.Run("RETURN 1", nil) 59 if err != nil { 60 return false 61 } else if result.Err() != nil { 62 return false 63 } 64 65 return true 66 } 67 68 func Test(t *testing.T) { 69 dktesting.ParallelTest(t, specs, func(t *testing.T, c dktest.ContainerInfo) { 70 ip, port, err := c.Port(7687) 71 if err != nil { 72 t.Fatal(err) 73 } 74 75 n := &Neo4j{} 76 d, err := n.Open(neoConnectionString(ip, port)) 77 if err != nil { 78 t.Fatal(err) 79 } 80 defer func() { 81 if err := d.Close(); err != nil { 82 t.Error(err) 83 } 84 }() 85 dt.Test(t, d, []byte("MATCH (a) RETURN a")) 86 }) 87 } 88 89 func TestMigrate(t *testing.T) { 90 dktesting.ParallelTest(t, specs, func(t *testing.T, c dktest.ContainerInfo) { 91 ip, port, err := c.Port(7687) 92 if err != nil { 93 t.Fatal(err) 94 } 95 96 n := &Neo4j{} 97 neoUrl := neoConnectionString(ip, port) + "/?x-multi-statement=true" 98 d, err := n.Open(neoUrl) 99 if err != nil { 100 t.Fatal(err) 101 } 102 defer func() { 103 if err := d.Close(); err != nil { 104 t.Error(err) 105 } 106 }() 107 m, err := migrate.NewWithDatabaseInstance("file://./examples/migrations", "neo4j", d) 108 if err != nil { 109 t.Fatal(err) 110 } 111 dt.TestMigrate(t, m) 112 }) 113 } 114 115 func TestMalformed(t *testing.T) { 116 dktesting.ParallelTest(t, specs, func(t *testing.T, c dktest.ContainerInfo) { 117 ip, port, err := c.Port(7687) 118 if err != nil { 119 t.Fatal(err) 120 } 121 122 n := &Neo4j{} 123 d, err := n.Open(neoConnectionString(ip, port)) 124 if err != nil { 125 t.Fatal(err) 126 } 127 defer func() { 128 if err := d.Close(); err != nil { 129 t.Error(err) 130 } 131 }() 132 133 migration := bytes.NewReader([]byte("CREATE (a {qid: 1) RETURN a")) 134 if err := d.Run(migration); err == nil { 135 t.Fatal("expected failure for malformed migration") 136 } 137 }) 138 }