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