Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unit test for excel #55

Merged
merged 5 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please ensure to remove the extra blank lines following this entry to maintain a clean and professional changelog format.

- ## [](https://github.com/nao1215/sqly/compare/v0.7.0...) (2024-04-30)
+ ## [v0.7.0](https://github.com/nao1215/sqly/compare/v0.7.0...) (2024-04-30)

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
## [](https://github.com/nao1215/sqly/compare/v0.7.0...) (2024-04-30)
## [v0.7.0](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.
78 changes: 78 additions & 0 deletions infrastructure/persistence/excel_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [golangci] reported by reviewdog 🐶
Error return value of os.Remove is not checked (errcheck)

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)
}
})
}
}

Check failure on line 78 in infrastructure/persistence/excel_test.go

View workflow job for this annotation

GitHub Actions / Unit test (mac) (macos-latest)

expected declaration, found '}'

Check failure on line 78 in infrastructure/persistence/excel_test.go

View workflow job for this annotation

GitHub Actions / Unit test (windows) (windows-latest)

expected declaration, found '}'

Check failure on line 78 in infrastructure/persistence/excel_test.go

View workflow job for this annotation

GitHub Actions / Unit test (linux) (ubuntu-latest)

expected declaration, found '}'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [golangci] reported by reviewdog 🐶
syntax error: non-declaration statement outside function body (typecheck)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [golangci] reported by reviewdog 🐶
expected declaration, found '}' (typecheck)

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"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [golangci] reported by reviewdog 🐶
string not support data definition language has 4 occurrences, make it a constant (goconst)

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"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [golangci] reported by reviewdog 🐶
string not support transaction control language has 5 occurrences, make it a constant (goconst)

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)
}
})
}
Loading