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