github.com/bishtawi/migrate/v4@v4.8.11/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/bishtawi/migrate/v4" 14 dt "github.com/bishtawi/migrate/v4/database/testing" 15 "github.com/bishtawi/migrate/v4/dktesting" 16 _ "github.com/bishtawi/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:3.5", Options: opts}, 24 {ImageName: "neo4j:3.5-enterprise", Options: opts}, 25 {ImageName: "neo4j:3.4", Options: opts}, 26 {ImageName: "neo4j:3.4-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(neoConnectionString(ip, port), neo4j.BasicAuth("neo4j", "migratetest", "")) 41 if err != nil { 42 return false 43 } 44 defer func() { 45 if err := driver.Close(); err != nil { 46 log.Println("close error:", err) 47 } 48 }() 49 session, err := driver.Session(neo4j.AccessModeRead) 50 if err != nil { 51 return false 52 } 53 result, err := session.Run("RETURN 1", nil) 54 if err != nil { 55 return false 56 } else if result.Err() != nil { 57 return false 58 } 59 60 return true 61 } 62 63 func Test(t *testing.T) { 64 dktesting.ParallelTest(t, specs, func(t *testing.T, c dktest.ContainerInfo) { 65 ip, port, err := c.Port(7687) 66 if err != nil { 67 t.Fatal(err) 68 } 69 70 n := &Neo4j{} 71 d, err := n.Open(neoConnectionString(ip, port)) 72 if err != nil { 73 t.Fatal(err) 74 } 75 defer func() { 76 if err := d.Close(); err != nil { 77 t.Error(err) 78 } 79 }() 80 dt.Test(t, d, []byte("MATCH (a) RETURN a")) 81 }) 82 } 83 84 func TestMigrate(t *testing.T) { 85 dktesting.ParallelTest(t, specs, func(t *testing.T, c dktest.ContainerInfo) { 86 ip, port, err := c.Port(7687) 87 if err != nil { 88 t.Fatal(err) 89 } 90 91 n := &Neo4j{} 92 neoUrl := neoConnectionString(ip, port) + "/?x-multi-statement=true" 93 d, err := n.Open(neoUrl) 94 if err != nil { 95 t.Fatal(err) 96 } 97 defer func() { 98 if err := d.Close(); err != nil { 99 t.Error(err) 100 } 101 }() 102 m, err := migrate.NewWithDatabaseInstance("file://./examples/migrations", "neo4j", d) 103 if err != nil { 104 t.Fatal(err) 105 } 106 dt.TestMigrate(t, m) 107 }) 108 } 109 110 func TestMalformed(t *testing.T) { 111 dktesting.ParallelTest(t, specs, func(t *testing.T, c dktest.ContainerInfo) { 112 ip, port, err := c.Port(7687) 113 if err != nil { 114 t.Fatal(err) 115 } 116 117 n := &Neo4j{} 118 d, err := n.Open(neoConnectionString(ip, port)) 119 if err != nil { 120 t.Fatal(err) 121 } 122 defer func() { 123 if err := d.Close(); err != nil { 124 t.Error(err) 125 } 126 }() 127 128 migration := bytes.NewReader([]byte("CREATE (a {qid: 1) RETURN a")) 129 if err := d.Run(migration); err == nil { 130 t.Fatal("expected failure for malformed migration") 131 } 132 }) 133 }