github.com/octohelm/storage@v0.0.0-20240516030302-1ac2cc1ea347/internal/sql/adapter/postgres/adapter_test.go (about)

     1  package postgres
     2  
     3  import (
     4  	"net/url"
     5  	"testing"
     6  
     7  	"github.com/davecgh/go-spew/spew"
     8  	"github.com/octohelm/storage/internal/sql/adapter"
     9  	"github.com/octohelm/storage/internal/testutil"
    10  	"github.com/octohelm/storage/pkg/migrator"
    11  	"github.com/octohelm/storage/testdata/model"
    12  )
    13  
    14  func NewAdapter(t testing.TB) adapter.Adapter {
    15  	t.Helper()
    16  
    17  	ctx := testutil.NewContext(t)
    18  
    19  	u, _ := url.Parse("postgres://postgres@localhost/migrate_test_v1?sslmode=disable")
    20  
    21  	a, err := Open(ctx, u)
    22  	if err != nil {
    23  		panic(err)
    24  	}
    25  	t.Cleanup(func() {
    26  		a.Close()
    27  	})
    28  
    29  	return a
    30  }
    31  
    32  func Test(t *testing.T) {
    33  	a := NewAdapter(t)
    34  
    35  	t.Run("#Catalog", func(t *testing.T) {
    36  		ctx := testutil.NewContext(t)
    37  		tables, err := a.Catalog(ctx)
    38  		testutil.Expect(t, err, testutil.Be[error](nil))
    39  		spew.Dump(tables.TableNames())
    40  	})
    41  }
    42  
    43  func TestMigrate(t *testing.T) {
    44  	a := NewAdapter(t)
    45  
    46  	t.Run("Create Catalog", func(t *testing.T) {
    47  		ctx := testutil.NewContext(t)
    48  
    49  		cat := testutil.CatalogFrom(&model.User{})
    50  		err := migrator.Migrate(ctx, a, cat)
    51  		testutil.Expect(t, err, testutil.Be[error](nil))
    52  
    53  		t.Run("Migrate To TableV2", func(t *testing.T) {
    54  			catV2 := testutil.CatalogFrom(&model.UserV2{})
    55  
    56  			err := migrator.Migrate(ctx, a, catV2)
    57  			testutil.Expect(t, err, testutil.Be[error](nil))
    58  
    59  			t.Run("Rollback", func(t *testing.T) {
    60  				err := migrator.Migrate(ctx, a, cat)
    61  				testutil.Expect(t, err, testutil.Be[error](nil))
    62  			})
    63  		})
    64  	})
    65  }