github.com/Systemnick/migrate@v3.5.4+incompatible/database/testing/testing.go (about)

     1  // Package testing has the database tests.
     2  // All database 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  	"bytes"
     8  	"fmt"
     9  	"io"
    10  	"testing"
    11  	"time"
    12  
    13  	"github.com/golang-migrate/migrate/database"
    14  )
    15  
    16  // Test runs tests against database implementations.
    17  func Test(t *testing.T, d database.Driver, migration []byte) {
    18  	if migration == nil {
    19  		panic("test must provide migration reader")
    20  	}
    21  
    22  	TestNilVersion(t, d) // test first
    23  	TestLockAndUnlock(t, d)
    24  	TestRun(t, d, bytes.NewReader(migration))
    25  	TestDrop(t, d)
    26  	TestSetVersion(t, d) // also tests Version()
    27  }
    28  
    29  func TestNilVersion(t *testing.T, d database.Driver) {
    30  	v, _, err := d.Version()
    31  	if err != nil {
    32  		t.Fatal(err)
    33  	}
    34  	if v != database.NilVersion {
    35  		t.Fatalf("Version: expected version to be NilVersion (-1), got %v", v)
    36  	}
    37  }
    38  
    39  func TestLockAndUnlock(t *testing.T, d database.Driver) {
    40  	// add a timeout, in case there is a deadlock
    41  	done := make(chan bool, 1)
    42  	go func() {
    43  		timeout := time.After(15 * time.Second)
    44  		for {
    45  			select {
    46  			case <-done:
    47  				return
    48  			case <-timeout:
    49  				panic(fmt.Sprintf("Timeout after 15 seconds. Looks like a deadlock in Lock/UnLock.\n%#v", d))
    50  			}
    51  		}
    52  	}()
    53  	defer func() {
    54  		done <- true
    55  	}()
    56  
    57  	// run the locking test ...
    58  
    59  	if err := d.Lock(); err != nil {
    60  		t.Fatal(err)
    61  	}
    62  
    63  	// try to acquire lock again
    64  	if err := d.Lock(); err == nil {
    65  		t.Fatal("Lock: expected err not to be nil")
    66  	}
    67  
    68  	// unlock
    69  	if err := d.Unlock(); err != nil {
    70  		t.Fatal(err)
    71  	}
    72  
    73  	// try to lock
    74  	if err := d.Lock(); err != nil {
    75  		t.Fatal(err)
    76  	}
    77  	if err := d.Unlock(); err != nil {
    78  		t.Fatal(err)
    79  	}
    80  }
    81  
    82  func TestRun(t *testing.T, d database.Driver, migration io.Reader) {
    83  	if migration == nil {
    84  		panic("migration can't be nil")
    85  	}
    86  
    87  	if err := d.Run(migration); err != nil {
    88  		t.Fatal(err)
    89  	}
    90  }
    91  
    92  func TestDrop(t *testing.T, d database.Driver) {
    93  	if err := d.Drop(); err != nil {
    94  		t.Fatal(err)
    95  	}
    96  }
    97  
    98  func TestSetVersion(t *testing.T, d database.Driver) {
    99  	if err := d.SetVersion(1, true); err != nil {
   100  		t.Fatal(err)
   101  	}
   102  
   103  	// call again
   104  	if err := d.SetVersion(1, true); err != nil {
   105  		t.Fatal(err)
   106  	}
   107  
   108  	v, dirty, err := d.Version()
   109  	if err != nil {
   110  		t.Fatal(err)
   111  	}
   112  	if !dirty {
   113  		t.Fatal("expected dirty")
   114  	}
   115  	if v != 1 {
   116  		t.Fatal("expected version to be 1")
   117  	}
   118  
   119  	if err := d.SetVersion(2, false); err != nil {
   120  		t.Fatal(err)
   121  	}
   122  
   123  	// call again
   124  	if err := d.SetVersion(2, false); err != nil {
   125  		t.Fatal(err)
   126  	}
   127  
   128  	v, dirty, err = d.Version()
   129  	if err != nil {
   130  		t.Fatal(err)
   131  	}
   132  	if dirty {
   133  		t.Fatal("expected not dirty")
   134  	}
   135  	if v != 2 {
   136  		t.Fatal("expected version to be 2")
   137  	}
   138  }