github.com/scraniel/migrate@v0.0.0-20230320185700-339088f36cee/database/driver_test.go (about)

     1  package database
     2  
     3  import (
     4  	"io"
     5  	"testing"
     6  )
     7  
     8  func ExampleDriver() {
     9  	// see database/stub for an example
    10  
    11  	// database/stub/stub.go has the driver implementation
    12  	// database/stub/stub_test.go runs database/testing/test.go:Test
    13  }
    14  
    15  // Using database/stub here is not possible as it
    16  // results in an import cycle.
    17  type mockDriver struct {
    18  	url string
    19  }
    20  
    21  func (m *mockDriver) Open(url string) (Driver, error) {
    22  	return &mockDriver{
    23  		url: url,
    24  	}, nil
    25  }
    26  
    27  func (m *mockDriver) Close() error {
    28  	return nil
    29  }
    30  
    31  func (m *mockDriver) Lock() error {
    32  	return nil
    33  }
    34  
    35  func (m *mockDriver) Unlock() error {
    36  	return nil
    37  }
    38  
    39  func (m *mockDriver) Run(migration io.Reader) error {
    40  	return nil
    41  }
    42  
    43  func (m *mockDriver) SetVersion(version int, dirty bool) error {
    44  	return nil
    45  }
    46  
    47  func (m *mockDriver) Version() (version int, dirty bool, err error) {
    48  	return 0, false, nil
    49  }
    50  
    51  func (m *mockDriver) Drop() error {
    52  	return nil
    53  }
    54  
    55  func TestRegisterTwice(t *testing.T) {
    56  	Register("mock", &mockDriver{})
    57  
    58  	var err interface{}
    59  	func() {
    60  		defer func() {
    61  			err = recover()
    62  		}()
    63  		Register("mock", &mockDriver{})
    64  	}()
    65  
    66  	if err == nil {
    67  		t.Fatal("expected a panic when calling Register twice")
    68  	}
    69  }
    70  
    71  func TestOpen(t *testing.T) {
    72  	// Make sure the driver is registered.
    73  	// But if the previous test already registered it just ignore the panic.
    74  	// If we don't do this it will be impossible to run this test standalone.
    75  	func() {
    76  		defer func() {
    77  			_ = recover()
    78  		}()
    79  		Register("mock", &mockDriver{})
    80  	}()
    81  
    82  	cases := []struct {
    83  		url string
    84  		err bool
    85  	}{
    86  		{
    87  			"mock://user:pass@tcp(host:1337)/db",
    88  			false,
    89  		},
    90  		{
    91  			"unknown://bla",
    92  			true,
    93  		},
    94  	}
    95  
    96  	for _, c := range cases {
    97  		t.Run(c.url, func(t *testing.T) {
    98  			d, err := Open(c.url)
    99  
   100  			if err == nil {
   101  				if c.err {
   102  					t.Fatal("expected an error for an unknown driver")
   103  				} else {
   104  					if md, ok := d.(*mockDriver); !ok {
   105  						t.Fatalf("expected *mockDriver got %T", d)
   106  					} else if md.url != c.url {
   107  						t.Fatalf("expected %q got %q", c.url, md.url)
   108  					}
   109  				}
   110  			} else if !c.err {
   111  				t.Fatalf("did not expect %q", err)
   112  			}
   113  		})
   114  	}
   115  }