-
Notifications
You must be signed in to change notification settings - Fork 0
Add test for Execute #3
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
fbf6e4f
add TestExecute
NathanDennis 0c7dce8
remove wantErr bool for test struct, update error messages
NathanDennis 6058cf8
add lastInsertedID and rowsAffected checks
NathanDennis 081d82b
change tableCreate and table dropping to dbConnection.Exec
NathanDennis 6cfd5f1
update post execute query to use QuerySingleStruct
NathanDennis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| package gsdb | ||
|
|
||
| import ( | ||
| "context" | ||
| "log/slog" | ||
| "os" | ||
| "testing" | ||
| "time" | ||
| ) | ||
|
|
||
| func TestExecute(t *testing.T) { | ||
| textHandler := slog.NewTextHandler(os.Stdout, nil) | ||
| l := slog.New(textHandler) | ||
| NewSQLite3("test.db", l, context.Background()) | ||
|
|
||
| type TestPerson struct { | ||
| Id int `db:"column=id primarykey=yes table=Test"` | ||
| Name string `db:"column=name"` | ||
| Dtadded time.Time `db:"column=dtadded"` | ||
| Status int `db:"column=status"` | ||
| Ignored int `db:"column=ignored omit=yes"` | ||
| } | ||
|
|
||
| tableCreate := ` | ||
| CREATE TABLE IF NOT EXISTS test ( | ||
| id INTEGER PRIMARY KEY AUTOINCREMENT, | ||
| name TEXT NOT NULL, | ||
| dtadded DATETIME DEFAULT CURRENT_TIMESTAMP, | ||
| status INTEGER NOT NULL | ||
| );` | ||
|
|
||
| _, err := DB.dbConnection.Exec("DROP TABLE IF EXISTS test;") | ||
| if err != nil { | ||
| t.Fatalf("failed to execute drop table prior to tests: %v", err) | ||
| } | ||
|
|
||
| _, err = DB.dbConnection.Exec(tableCreate) | ||
| if err != nil { | ||
| t.Fatalf("failed to execute tableCreate SQL prior to tests: %v", err) | ||
| } | ||
|
|
||
| tests := []struct { | ||
| name string | ||
| entry TestPerson | ||
| expectedLastID int64 | ||
| }{ | ||
| { | ||
| name: "Execute an Insert success with all fields", | ||
| entry: TestPerson{Name: "Ronald McDonald", Dtadded: time.Now().UTC(), Status: 1}, | ||
| expectedLastID: 1, | ||
| }, | ||
| { | ||
| name: "Execute an Insert with fields missing - current setup will populate missing fields with Go zero values", | ||
| entry: TestPerson{Dtadded: time.Now().UTC()}, | ||
| expectedLastID: 2, | ||
| }, | ||
| } | ||
|
|
||
| for _, tc := range tests { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| insertSQL, err := DB.Insert(tc.entry) | ||
| if err != nil { | ||
| t.Fatalf("failed to generate Insert SQL: %v", err) | ||
| } | ||
|
|
||
| lastInsertedID, rowsAffected, err := DB.Execute(insertSQL) | ||
| if err != nil { | ||
| t.Fatalf("failed to execute insert during test: %s: %v", tc.name, err) | ||
| } | ||
|
|
||
| result, err := QuerySingleStruct[TestPerson](` | ||
| SELECT name, id FROM test ORDER BY id DESC LIMIT 1; | ||
| `) | ||
| if err != nil { | ||
| t.Fatalf("QuerySingleStruct error during test: %s - %v", tc.name, err) | ||
| } | ||
|
|
||
| switch { | ||
| case result.Name != tc.entry.Name: | ||
| t.Fatalf("queried result name does not match. got: %s want: %s", result.Name, tc.entry.Name) | ||
|
|
||
| case lastInsertedID != tc.expectedLastID: | ||
| t.Fatalf("expected last inserted IDs to match. got: %d want: %d", lastInsertedID, tc.expectedLastID) | ||
|
|
||
| case int64(result.Id) != tc.expectedLastID: | ||
| t.Fatalf("expected result.Id of %d to match expectedLastID: %d but got: %d", int64(result.Id), tc.expectedLastID, lastInsertedID) | ||
|
|
||
| case rowsAffected != 1: | ||
| t.Fatalf("expected only 1 rowsAffected but got %d", rowsAffected) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This is ace! :-) Thank you! .. In this bit can you have a go at using the QueryStruct and QuerySingleStuct to get the data, we get more test coverage then. Maybe use both methods and make sure they both return the same thing?
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.
Add yourself a task for this if you like. I'd rather not hold up the PR.