github.com/svrana/migrate@v3.5.4+incompatible/source/testing/testing.go (about) 1 // Package testing has the source tests. 2 // All source drivers must pass the Test function. 3 // This lives in it's own package so it stays a test dependency. 4 package testing 5 6 import ( 7 "os" 8 "testing" 9 10 "github.com/golang-migrate/migrate/source" 11 ) 12 13 // Test runs tests against source implementations. 14 // It assumes that the driver tests has access to the following migrations: 15 // 16 // u = up migration, d = down migration, n = version 17 // | 1 | - | 3 | 4 | 5 | - | 7 | 18 // | u d | - | u | u d | d | - | u d | 19 // 20 // See source/stub/stub_test.go or source/file/file_test.go for an example. 21 func Test(t *testing.T, d source.Driver) { 22 TestFirst(t, d) 23 TestPrev(t, d) 24 TestNext(t, d) 25 TestReadUp(t, d) 26 TestReadDown(t, d) 27 } 28 29 func TestFirst(t *testing.T, d source.Driver) { 30 version, err := d.First() 31 if err != nil { 32 t.Fatalf("First: expected err to be nil, got %v", err) 33 } 34 if version != 1 { 35 t.Errorf("First: expected 1, got %v", version) 36 } 37 } 38 39 func TestPrev(t *testing.T, d source.Driver) { 40 tt := []struct { 41 version uint 42 expectErr error 43 expectPrevVersion uint 44 }{ 45 {version: 0, expectErr: os.ErrNotExist}, 46 {version: 1, expectErr: os.ErrNotExist}, 47 {version: 2, expectErr: os.ErrNotExist}, 48 {version: 3, expectErr: nil, expectPrevVersion: 1}, 49 {version: 4, expectErr: nil, expectPrevVersion: 3}, 50 {version: 5, expectErr: nil, expectPrevVersion: 4}, 51 {version: 6, expectErr: os.ErrNotExist}, 52 {version: 7, expectErr: nil, expectPrevVersion: 5}, 53 {version: 8, expectErr: os.ErrNotExist}, 54 {version: 9, expectErr: os.ErrNotExist}, 55 } 56 57 for i, v := range tt { 58 pv, err := d.Prev(v.version) 59 if (v.expectErr == os.ErrNotExist && !os.IsNotExist(err)) && v.expectErr != err { 60 t.Errorf("Prev: expected %v, got %v, in %v", v.expectErr, err, i) 61 } 62 if err == nil && v.expectPrevVersion != pv { 63 t.Errorf("Prev: expected %v, got %v, in %v", v.expectPrevVersion, pv, i) 64 } 65 } 66 } 67 68 func TestNext(t *testing.T, d source.Driver) { 69 tt := []struct { 70 version uint 71 expectErr error 72 expectNextVersion uint 73 }{ 74 {version: 0, expectErr: os.ErrNotExist}, 75 {version: 1, expectErr: nil, expectNextVersion: 3}, 76 {version: 2, expectErr: os.ErrNotExist}, 77 {version: 3, expectErr: nil, expectNextVersion: 4}, 78 {version: 4, expectErr: nil, expectNextVersion: 5}, 79 {version: 5, expectErr: nil, expectNextVersion: 7}, 80 {version: 6, expectErr: os.ErrNotExist}, 81 {version: 7, expectErr: os.ErrNotExist}, 82 {version: 8, expectErr: os.ErrNotExist}, 83 {version: 9, expectErr: os.ErrNotExist}, 84 } 85 86 for i, v := range tt { 87 nv, err := d.Next(v.version) 88 if (v.expectErr == os.ErrNotExist && !os.IsNotExist(err)) && v.expectErr != err { 89 t.Errorf("Next: expected %v, got %v, in %v", v.expectErr, err, i) 90 } 91 if err == nil && v.expectNextVersion != nv { 92 t.Errorf("Next: expected %v, got %v, in %v", v.expectNextVersion, nv, i) 93 } 94 } 95 } 96 97 func TestReadUp(t *testing.T, d source.Driver) { 98 tt := []struct { 99 version uint 100 expectErr error 101 expectUp bool 102 }{ 103 {version: 0, expectErr: os.ErrNotExist}, 104 {version: 1, expectErr: nil, expectUp: true}, 105 {version: 2, expectErr: os.ErrNotExist}, 106 {version: 3, expectErr: nil, expectUp: true}, 107 {version: 4, expectErr: nil, expectUp: true}, 108 {version: 5, expectErr: os.ErrNotExist}, 109 {version: 6, expectErr: os.ErrNotExist}, 110 {version: 7, expectErr: nil, expectUp: true}, 111 {version: 8, expectErr: os.ErrNotExist}, 112 } 113 114 for i, v := range tt { 115 up, identifier, err := d.ReadUp(v.version) 116 if (v.expectErr == os.ErrNotExist && !os.IsNotExist(err)) || 117 (v.expectErr != os.ErrNotExist && err != v.expectErr) { 118 t.Errorf("expected %v, got %v, in %v", v.expectErr, err, i) 119 120 } else if err == nil { 121 if len(identifier) == 0 { 122 t.Errorf("expected identifier not to be empty, in %v", i) 123 } 124 125 if v.expectUp == true && up == nil { 126 t.Errorf("expected up not to be nil, in %v", i) 127 } else if v.expectUp == false && up != nil { 128 t.Errorf("expected up to be nil, got %v, in %v", up, i) 129 } 130 } 131 } 132 } 133 134 func TestReadDown(t *testing.T, d source.Driver) { 135 tt := []struct { 136 version uint 137 expectErr error 138 expectDown bool 139 }{ 140 {version: 0, expectErr: os.ErrNotExist}, 141 {version: 1, expectErr: nil, expectDown: true}, 142 {version: 2, expectErr: os.ErrNotExist}, 143 {version: 3, expectErr: os.ErrNotExist}, 144 {version: 4, expectErr: nil, expectDown: true}, 145 {version: 5, expectErr: nil, expectDown: true}, 146 {version: 6, expectErr: os.ErrNotExist}, 147 {version: 7, expectErr: nil, expectDown: true}, 148 {version: 8, expectErr: os.ErrNotExist}, 149 } 150 151 for i, v := range tt { 152 down, identifier, err := d.ReadDown(v.version) 153 if (v.expectErr == os.ErrNotExist && !os.IsNotExist(err)) || 154 (v.expectErr != os.ErrNotExist && err != v.expectErr) { 155 t.Errorf("expected %v, got %v, in %v", v.expectErr, err, i) 156 157 } else if err == nil { 158 if len(identifier) == 0 { 159 t.Errorf("expected identifier not to be empty, in %v", i) 160 } 161 162 if v.expectDown == true && down == nil { 163 t.Errorf("expected down not to be nil, in %v", i) 164 } else if v.expectDown == false && down != nil { 165 t.Errorf("expected down to be nil, got %v, in %v", down, i) 166 } 167 } 168 } 169 }