Skip to content

Commit

Permalink
add linter & tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wroge committed Dec 6, 2024
1 parent f3cf4de commit 246a644
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
- name: golangci-lint
uses: golangci/golangci-lint-action@v6
with:
version: v1.60
version: v1.62

- name: Test
run: go test -coverprofile=coverage.txt ./...
Expand Down
53 changes: 53 additions & 0 deletions sqlt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package sqlt_test

import (
"context"
"errors"
"testing"

"github.com/DATA-DOG/go-sqlmock"
Expand Down Expand Up @@ -29,6 +30,11 @@ func TestOne(t *testing.T) {
}

stmt := sqlt.QueryStmt[Param, Book](
sqlt.End(func(err error, runner *sqlt.Runner) {
if runner.SQL.String() != "SELECT id, title FROM books WHERE title = ?" {
t.Fail()
}
}),
sqlt.Parse(`
SELECT
{{ ScanInt64 Dest.ID "id" }}
Expand All @@ -47,6 +53,43 @@ func TestOne(t *testing.T) {
}
}

func TestOneError(t *testing.T) {
db, mock, err := sqlmock.New(sqlmock.QueryMatcherOption(sqlmock.QueryMatcherEqual))
if err != nil {
t.Fatal(err)
}

mock.ExpectQuery("SELECT id, title FROM books WHERE title = ?").WithArgs("TEST").WillReturnError(errors.New("ERROR"))

type Param struct {
Title string
}

type Book struct {
ID int64
Title string
}

stmt := sqlt.QueryStmt[Param, Book](
sqlt.End(func(err error, runner *sqlt.Runner) {
if err == nil || err.Error() != "ERROR" {
t.Fail()
}
}),
sqlt.Parse(`
SELECT
{{ ScanInt64 Dest.ID "id" }}
{{- ScanString Dest.Title ", title" }}
FROM books WHERE title = {{ .Title }}
`),
)

_, err = stmt.One(context.Background(), db, Param{Title: "TEST"})
if err == nil || err.Error() != "ERROR" {
t.Fail()
}
}

func TestFirst(t *testing.T) {
db, mock, err := sqlmock.New(sqlmock.QueryMatcherOption(sqlmock.QueryMatcherEqual))
if err != nil {
Expand All @@ -69,6 +112,11 @@ func TestFirst(t *testing.T) {
}

stmt := sqlt.QueryStmt[Param, Book](
sqlt.End(func(err error, runner *sqlt.Runner) {
if runner.SQL.String() != "SELECT id, title FROM books WHERE title = ?" {
t.Fail()
}
}),
sqlt.Parse(`
SELECT
{{ ScanInt64 Dest.ID "id" }}
Expand Down Expand Up @@ -109,6 +157,11 @@ func TestAll(t *testing.T) {
}

stmt := sqlt.QueryStmt[Param, Book](
sqlt.End(func(err error, runner *sqlt.Runner) {
if runner.SQL.String() != "SELECT id, title FROM books WHERE title = ?" {
t.Fail()
}
}),
sqlt.Parse(`
SELECT
{{ ScanInt64 Dest.ID "id" }}
Expand Down

0 comments on commit 246a644

Please sign in to comment.