Skip to content

Commit ea1f125

Browse files
committed
Add test suite
1 parent a30dba9 commit ea1f125

7 files changed

Lines changed: 691 additions & 10 deletions

File tree

Makefile

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
main_package_path = ./cmd/sqliteadmin
2+
binary_name = sqliteadmin
3+
4+
# ==================================================================================== #
5+
# QUALITY CONTROL
6+
# ==================================================================================== #
7+
8+
## audit: run quality control checks
9+
.PHONY: audit
10+
audit: test
11+
go mod tidy -diff
12+
go mod verify
13+
test -z "$(shell gofmt -l .)"
14+
go vet ./...
15+
go run honnef.co/go/tools/cmd/staticcheck@latest -checks=all,-ST1000,-U1000 ./...
16+
go run golang.org/x/vuln/cmd/govulncheck@latest ./...
17+
18+
## test: run all tests
19+
.PHONY: test
20+
test:
21+
go test -v -race ./...
22+
23+
## test/cover: run all tests and display coverage
24+
.PHONY: test/cover
25+
test/cover:
26+
go test -v -race -buildvcs -coverprofile=/tmp/coverage.out ./...
27+
go tool cover -html=/tmp/coverage.out
28+
29+
# ==================================================================================== #
30+
# DEVELOPMENT
31+
# ==================================================================================== #
32+
33+
## tidy: tidy modfiles and format .go files
34+
.PHONY: tidy
35+
tidy:
36+
go mod tidy -v
37+
go fmt ./...
38+
39+
## build: build the application
40+
.PHONY: build
41+
build:
42+
# Include additional build steps, like TypeScript, SCSS or Tailwind compilation here...
43+
go build -o=/tmp/bin/${binary_name} ${main_package_path}
44+
45+
## run: run the application
46+
.PHONY: run
47+
run: build
48+
/tmp/bin/${binary_name}
49+
50+
## run/live: run the application with reloading on file changes
51+
# .PHONY: run/live
52+
# run/live:

errors.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,23 @@ var (
1313
ErrInvalidInput = errors.New("invalid input")
1414
)
1515

16-
type ApiError struct {
16+
type APIError struct {
1717
StatusCode int `json:"statusCode"`
1818
Message string `json:"message"`
1919
}
2020

21-
func (e ApiError) Error() string {
21+
func (e APIError) Error() string {
2222
return fmt.Sprintf("api error: %d, %s", e.StatusCode, e.Message)
2323
}
2424

25-
func apiErrUnauthorized() ApiError {
26-
return ApiError{StatusCode: http.StatusUnauthorized, Message: "Invalid credentials"}
25+
func apiErrUnauthorized() APIError {
26+
return APIError{StatusCode: http.StatusUnauthorized, Message: "Invalid credentials"}
2727
}
2828

29-
func apiErrBadRequest(details string) ApiError {
30-
return ApiError{StatusCode: http.StatusBadRequest, Message: "Bad request: " + details}
29+
func apiErrBadRequest(details string) APIError {
30+
return APIError{StatusCode: http.StatusBadRequest, Message: "Bad request: " + details}
3131
}
3232

33-
func apiErrSomethingWentWrong() ApiError {
34-
return ApiError{StatusCode: http.StatusInternalServerError, Message: "Something went wrong"}
33+
func apiErrSomethingWentWrong() APIError {
34+
return APIError{StatusCode: http.StatusInternalServerError, Message: "Something went wrong"}
3535
}

examples/stdlib/stdlib.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"database/sql"
66
"fmt"
77
"log"
8+
"log/slog"
89
"net/http"
910
"os/signal"
1011
"syscall"
@@ -21,7 +22,15 @@ func main() {
2122
if err != nil {
2223
log.Fatalf("Error opening database: %v", err)
2324
}
24-
sHandler := sqliteadmin.NewHandler(db, "user", "password")
25+
logger := slog.Default()
26+
27+
config := sqliteadmin.Config{
28+
DB: db,
29+
Username: "user",
30+
Password: "password",
31+
Logger: logger,
32+
}
33+
sHandler := sqliteadmin.NewHandler(config)
2534

2635
mux := http.NewServeMux()
2736

go.mod

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,10 @@ require (
99
github.com/mitchellh/mapstructure v1.5.0
1010
github.com/rs/cors v1.11.1
1111
)
12+
13+
require (
14+
github.com/davecgh/go-spew v1.1.1 // indirect
15+
github.com/pmezard/go-difflib v1.0.0 // indirect
16+
github.com/stretchr/testify v1.10.0
17+
gopkg.in/yaml.v3 v3.0.1 // indirect
18+
)

go.sum

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
2+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
13
github.com/go-chi/chi/v5 v5.2.0 h1:Aj1EtB0qR2Rdo2dG4O94RIU35w2lvQSj6BRA4+qwFL0=
24
github.com/go-chi/chi/v5 v5.2.0/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
35
github.com/go-chi/cors v1.2.1 h1:xEC8UT3Rlp2QuWNEr4Fs/c2EAGVKBwy/1vHx3bppil4=
@@ -6,5 +8,13 @@ github.com/mattn/go-sqlite3 v1.14.24 h1:tpSp2G2KyMnnQu99ngJ47EIkWVmliIizyZBfPrBW
68
github.com/mattn/go-sqlite3 v1.14.24/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
79
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
810
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
11+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
12+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
913
github.com/rs/cors v1.11.1 h1:eU3gRzXLRK57F5rKMGMZURNdIG4EoAmX8k94r9wXWHA=
1014
github.com/rs/cors v1.11.1/go.mod h1:XyqrcTp5zjWr1wsJ8PIRZssZ8b/WMcMf71DJnit4EMU=
15+
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
16+
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
17+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
18+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
19+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
20+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

queryhandlers.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,7 @@ func editRow(db *sql.DB, tableName string, row map[string]interface{}) error {
488488
return nil
489489
}
490490

491-
func writeError(w http.ResponseWriter, err ApiError) {
491+
func writeError(w http.ResponseWriter, err APIError) {
492492
w.WriteHeader(err.StatusCode)
493493
json.NewEncoder(w).Encode(err)
494494
}

0 commit comments

Comments
 (0)