~samwhited/migration

ref: refs/heads/master migration/migration_test.go -rw-r--r-- 2.5 KiB View raw
7a307fa1Sam Whited .github: auto close PRs and mirror from SourceHut 8 months ago
                                                                                
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
package migration_test

import (
	"context"
	"io/ioutil"
	"os"
	"path"
	"strconv"
	"strings"
	"testing"

	"code.soquee.net/migration"
	"golang.org/x/tools/godoc/vfs"
	"golang.org/x/tools/godoc/vfs/mapfs"
)

const migrationsTable = "__migrations"

var genTestCases = [...]struct {
	name string
	err  error
	dir  string
}{
	0: {
		name: "test",
		err:  nil,
		dir:  "test",
	},
	1: {
		name: "test me'\"\tagain",
		err:  nil,
		dir:  "test_me_again",
	},
}

func TestLastRun(t *testing.T) {
	for i, tc := range genTestCases {
		t.Run(strconv.Itoa(i), func(t *testing.T) {
			migrationDir := "001_" + tc.dir
			fs := mapfs.New(map[string]string{
				path.Join("/", migrationDir, "up.sql"): "-- up.sql",
			})
			ident, name, err := migration.LastRun(context.Background(), migrationsTable, fs, nil)
			if ident != "" {
				t.Errorf("Wrong ident: %q", ident)
			}
			if name != migrationDir {
				t.Errorf("Wrong name: want=%q, got=%q", migrationDir, name)
			}
			if err != nil {
				t.Errorf("Unexpected error: %q", err)
			}
		})
	}
}

func TestGenerate(t *testing.T) {
	for i, tc := range genTestCases {
		t.Run(strconv.Itoa(i), func(t *testing.T) {
			dir, err := ioutil.TempDir("", "migration_test")
			if err != nil {
				t.Fatalf("Error creating temp directory for tests: %q", err)
			}
			defer func() {
				err = os.RemoveAll(dir)
				if err != nil {
					t.Logf("Error cleaning up temp dir %q: %q", dir, err)
				}
			}()

			err = migration.Generator(dir)(tc.name)
			if err != tc.err {
				t.Errorf("Unexpected error: want=%q, got=%q", tc.err, err)
			}

			walked := 0
			walker, err := migration.NewWalker(context.Background(), migrationsTable, nil)
			if err != nil {
				t.Fatalf("error creating walker: %q", err)
			}
			err = walker(vfs.OS(dir), func(name string, info os.FileInfo, status migration.RunStatus) error {
				dirName := info.Name()
				if dirName == path.Base(dir) {
					t.Fatalf("Walk included top level directory but should only hit migrations")
				}
				walked++
				if walked > 1 {
					t.Fatalf("Too many files created in temp dir %q, is cleanup working?", dir)
				}

				// TODO: test name generation
				if status != migration.StatusUnknown {
					t.Errorf("Unexpected status: want=%d, got=%d", migration.StatusUnknown, status)
				}

				idx := strings.Index(dirName, "_")
				if idx < 0 {
					idx = 0
				}
				dirName = dirName[idx+1:]
				if dirName != tc.dir {
					t.Errorf("Unexpected migration dir: want=%q, got=%q", tc.dir, dirName)
				}
				return nil
			})
			if err != nil {
				t.Errorf("Unexpected error walking test output: %q", err)
			}
		})
	}
}