Skip to content

Commit

Permalink
Merge 7328a34 into 31089ab
Browse files Browse the repository at this point in the history
  • Loading branch information
nao1215 authored Apr 30, 2024
2 parents 31089ab + 7328a34 commit 90c3e46
Show file tree
Hide file tree
Showing 8 changed files with 316 additions and 92 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
## [](https://github.com/nao1215/sqly/compare/v0.6.5...) (2024-04-29)
## [](https://github.com/nao1215/sqly/compare/v0.7.0...) (2024-04-30)

2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ clean: ## Clean project
-rm -rf $(APP) cover.out cover.html

test: ## Start test
env GOOS=$(GOOS) $(GO_TEST) -cover $(GO_PKGROOT) -coverprofile=cover.out
env GOOS=$(GOOS) $(GO_TEST) -cover $(GO_PKGROOT) -coverpkg=./... -coverprofile=cover.out
$(GO_TOOL) cover -html=cover.out -o cover.html

coverage-tree: test ## Generate coverage tree
Expand Down
173 changes: 83 additions & 90 deletions doc/img/cover-tree.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
77 changes: 77 additions & 0 deletions infrastructure/persistence/excel_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package persistence

import (
"os"
"path/filepath"
"testing"

"github.com/google/go-cmp/cmp"
"github.com/nao1215/sqly/domain/model"
)

func Test_excelRepository_List(t *testing.T) {
t.Run("list excel data", func(t *testing.T) {
r := NewExcelRepository()

excel, err := r.List("testdata/sample.xlsx", "test_sheet")
if err != nil {
t.Fatal(err)
}

want := &model.Excel{
Name: "test_sheet",
Header: model.Header{"id", "name"},
Records: []model.Record{
{"1", "Gina"},
{"2", "Yulia"},
{"3", "Vika"},
},
}

if diff := cmp.Diff(excel, want); diff != "" {
t.Fatalf("differs: (-got +want)\n%s", diff)
}
})
}

func Test_excelRepository_Dump(t *testing.T) {
t.Run("dump excel data", func(t *testing.T) {
r := NewExcelRepository()

excel := &model.Table{
Name: "test_sheet",
Header: model.Header{"id", "name"},
Records: []model.Record{
{"1", "Gina"},
{"2", "Yulia"},
{"3", "Vika"},
{"4", "Hartlova"},
},
}

tempFilePath := filepath.Join(os.TempDir(), "dump.xlsx")
defer os.Remove(tempFilePath) // Clean up the temporary file after the test
if err := r.Dump(tempFilePath, excel); err != nil {
t.Fatal(err)
}

got, err := r.List(tempFilePath, "test_sheet")
if err != nil {
t.Fatal(err)
}
want := &model.Excel{
Name: "test_sheet",
Header: model.Header{"id", "name"},
Records: []model.Record{
{"1", "Gina"},
{"2", "Yulia"},
{"3", "Vika"},
{"4", "Hartlova"},
},
}

if diff := cmp.Diff(got, want); diff != "" {
t.Fatalf("differs: (-got +want)\n%s", diff)
}
})
}
Binary file not shown.
Binary file added infrastructure/persistence/testdata/sample.xlsx
Binary file not shown.
1 change: 1 addition & 0 deletions testdata/golden/ddl_error.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
not support data definition language: CREATE, DROP, ALTER, REINDEX
153 changes: 153 additions & 0 deletions usecase/sqlite3_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
package usecase

import (
"context"
"strings"
"testing"
)

func TestSQLite3Interactor_ExecSQL(t *testing.T) {
t.Run("execute CREATE error", func(t *testing.T) {
interactor := NewSQLite3Interactor(nil, nil)

si := NewSQLite3Interactor(interactor, NewSQL())
_, _, got := si.ExecSQL(context.Background(), "CREATE TABLE test (id INTEGER PRIMARY KEY, name TEXT)")

want := "not support data definition language"
if !strings.Contains(got.Error(), want) {
t.Errorf("want: %v, got: %v", want, got)
}
})

t.Run("execute DROP error", func(t *testing.T) {
interactor := NewSQLite3Interactor(nil, nil)

si := NewSQLite3Interactor(interactor, NewSQL())
_, _, got := si.ExecSQL(context.Background(), "DROP TABLE test")

want := "not support data definition language"
if !strings.Contains(got.Error(), want) {
t.Errorf("want: %v, got: %v", want, got)
}
})

t.Run("execute ALTER error", func(t *testing.T) {
interactor := NewSQLite3Interactor(nil, nil)

si := NewSQLite3Interactor(interactor, NewSQL())
_, _, got := si.ExecSQL(context.Background(), "ALTER TABLE test ADD COLUMN age INTEGER")

want := "not support data definition language"
if !strings.Contains(got.Error(), want) {
t.Errorf("want: %v, got: %v", want, got)
}
})

t.Run("execute REINDEX error", func(t *testing.T) {
interactor := NewSQLite3Interactor(nil, nil)

si := NewSQLite3Interactor(interactor, NewSQL())
_, _, got := si.ExecSQL(context.Background(), "REINDEX test")

want := "not support data definition language"
if !strings.Contains(got.Error(), want) {
t.Errorf("want: %v, got: %v", want, got)
}
})

t.Run("execute BEGIN error", func(t *testing.T) {
interactor := NewSQLite3Interactor(nil, nil)

si := NewSQLite3Interactor(interactor, NewSQL())
_, _, got := si.ExecSQL(context.Background(), "BEGIN")

want := "not support transaction control language"
if !strings.Contains(got.Error(), want) {
t.Errorf("want: %v, got: %v", want, got)
}
})

t.Run("execute COMMIT error", func(t *testing.T) {
interactor := NewSQLite3Interactor(nil, nil)

si := NewSQLite3Interactor(interactor, NewSQL())
_, _, got := si.ExecSQL(context.Background(), "COMMIT")

want := "not support transaction control language"
if !strings.Contains(got.Error(), want) {
t.Errorf("want: %v, got: %v", want, got)
}
})

t.Run("execute ROLLBACK error", func(t *testing.T) {
interactor := NewSQLite3Interactor(nil, nil)

si := NewSQLite3Interactor(interactor, NewSQL())
_, _, got := si.ExecSQL(context.Background(), "ROLLBACK")

want := "not support transaction control language"
if !strings.Contains(got.Error(), want) {
t.Errorf("want: %v, got: %v", want, got)
}
})

t.Run("execute SAVEPOINT error", func(t *testing.T) {
interactor := NewSQLite3Interactor(nil, nil)

si := NewSQLite3Interactor(interactor, NewSQL())
_, _, got := si.ExecSQL(context.Background(), "SAVEPOINT test")

want := "not support transaction control language"
if !strings.Contains(got.Error(), want) {
t.Errorf("want: %v, got: %v", want, got)
}
})

t.Run("execute RELEASE error", func(t *testing.T) {
interactor := NewSQLite3Interactor(nil, nil)

si := NewSQLite3Interactor(interactor, NewSQL())
_, _, got := si.ExecSQL(context.Background(), "RELEASE test")

want := "not support transaction control language"
if !strings.Contains(got.Error(), want) {
t.Errorf("want: %v, got: %v", want, got)
}
})

t.Run("execute GRANT error", func(t *testing.T) {
interactor := NewSQLite3Interactor(nil, nil)

si := NewSQLite3Interactor(interactor, NewSQL())
_, _, got := si.ExecSQL(context.Background(), "GRANT SELECT ON test TO user")

want := "not support data control language"
if !strings.Contains(got.Error(), want) {
t.Errorf("want: %v, got: %v", want, got)
}
})

t.Run("execute REVOKE error", func(t *testing.T) {
interactor := NewSQLite3Interactor(nil, nil)

si := NewSQLite3Interactor(interactor, NewSQL())
_, _, got := si.ExecSQL(context.Background(), "REVOKE SELECT ON test FROM user")

want := "not support data control language"
if !strings.Contains(got.Error(), want) {
t.Errorf("want: %v, got: %v", want, got)
}
})

t.Run("execute undifined statement error", func(t *testing.T) {
interactor := NewSQLite3Interactor(nil, nil)

si := NewSQLite3Interactor(interactor, NewSQL())
_, _, got := si.ExecSQL(context.Background(), "UNDEFINED STATEMENT")

want := "this input is not sql query or sqly helper command:"
if !strings.Contains(got.Error(), want) {
t.Errorf("want: %v, got: %v", want, got)
}
})
}

0 comments on commit 90c3e46

Please sign in to comment.