migrate/source/file/file_test.go

157 lines
3.1 KiB
Go

package file
import (
"fmt"
"io/ioutil"
"os"
"path"
"testing"
st "github.com/mattes/migrate/source/testing"
)
func Test(t *testing.T) {
tmpDir, err := ioutil.TempDir("", "")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmpDir)
// write files that meet driver test requirements
mustWriteFile(t, tmpDir, "1_foobar.up.sql", "1 up")
mustWriteFile(t, tmpDir, "1_foobar.down.sql", "1 down")
mustWriteFile(t, tmpDir, "3_foobar.up.sql", "3 up")
mustWriteFile(t, tmpDir, "4_foobar.up.sql", "4 up")
mustWriteFile(t, tmpDir, "4_foobar.down.sql", "4 down")
mustWriteFile(t, tmpDir, "5_foobar.down.sql", "5 down")
mustWriteFile(t, tmpDir, "7_foobar.up.sql", "7 up")
mustWriteFile(t, tmpDir, "7_foobar.down.sql", "7 down")
f := &File{}
d, err := f.Open("file://" + tmpDir)
if err != nil {
t.Fatal(err)
}
st.Test(t, d)
}
func TestOpen(t *testing.T) {
tmpDir, err := ioutil.TempDir("", "TestOpen")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmpDir)
mustWriteFile(t, tmpDir, "1_foobar.up.sql", "")
mustWriteFile(t, tmpDir, "1_foobar.down.sql", "")
f := &File{}
_, err = f.Open("file://" + tmpDir)
if err != nil {
t.Fatal(err)
}
}
func TestOpenDefaultsToCurrentDirectory(t *testing.T) {
wd, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
f := &File{}
d, err := f.Open("file://")
if err != nil {
t.Fatal(err)
}
if d.(*File).path != wd {
t.Fatal("expected driver to default to current directory")
}
}
func TestOpenWithDuplicateVersion(t *testing.T) {
tmpDir, err := ioutil.TempDir("", "TestOpenWithDuplicateVersion")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmpDir)
mustWriteFile(t, tmpDir, "1_foo.up.sql", "") // 1 up
mustWriteFile(t, tmpDir, "1_bar.up.sql", "") // 1 up
f := &File{}
_, err = f.Open("file://" + tmpDir)
if err == nil {
t.Fatal("expected err")
}
}
func TestClose(t *testing.T) {
tmpDir, err := ioutil.TempDir("", "TestOpen")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmpDir)
f := &File{}
d, err := f.Open("file://" + tmpDir)
if err != nil {
t.Fatal(err)
}
if d.Close() != nil {
t.Fatal("expected nil")
}
}
func mustWriteFile(t testing.TB, dir, file string, body string) {
if err := ioutil.WriteFile(path.Join(dir, file), []byte(body), 06444); err != nil {
t.Fatal(err)
}
}
func mustCreateBenchmarkDir(t *testing.B) (dir string) {
tmpDir, err := ioutil.TempDir("", "Benchmark")
if err != nil {
t.Fatal(err)
}
for i := 0; i < 1000; i++ {
mustWriteFile(t, tmpDir, fmt.Sprintf("%v_foobar.up.sql", i), "")
mustWriteFile(t, tmpDir, fmt.Sprintf("%v_foobar.down.sql", i), "")
}
return tmpDir
}
func BenchmarkOpen(b *testing.B) {
dir := mustCreateBenchmarkDir(b)
defer os.RemoveAll(dir)
b.ResetTimer()
for n := 0; n < b.N; n++ {
f := &File{}
f.Open("file://" + dir)
}
b.StopTimer()
}
func BenchmarkNext(b *testing.B) {
dir := mustCreateBenchmarkDir(b)
defer os.RemoveAll(dir)
f := &File{}
d, _ := f.Open("file://" + dir)
b.ResetTimer()
v, err := d.First()
for n := 0; n < b.N; n++ {
for !os.IsNotExist(err) {
v, err = d.Next(v)
}
}
b.StopTimer()
}