-
-
Notifications
You must be signed in to change notification settings - Fork 1
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
Changes from 4 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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) | ||
|
||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚫 [golangci] reported by reviewdog 🐶 |
||
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 GitHub Actions / Unit test (mac) (macos-latest)
Check failure on line 78 in infrastructure/persistence/excel_test.go GitHub Actions / Unit test (windows) (windows-latest)
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚫 [golangci] reported by reviewdog 🐶 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚫 [golangci] reported by reviewdog 🐶 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
not support data definition language: CREATE, DROP, ALTER, REINDEX |
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" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚫 [golangci] reported by reviewdog 🐶 |
||
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" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚫 [golangci] reported by reviewdog 🐶 |
||
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) | ||
} | ||
}) | ||
} |
There was a problem hiding this comment.
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.
Committable suggestion