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