diff --git a/.travis.yml b/.travis.yml index a595948..9f0fba2 100644 --- a/.travis.yml +++ b/.travis.yml @@ -23,5 +23,5 @@ before_script: - psql -U postgres -c 'CREATE DATABASE go_restful;'; script: - - glide up -u -s + - make test - $HOME/gopath/bin/goveralls -service=travis-ci diff --git a/apis/util_test.go b/apis/util_test.go new file mode 100644 index 0000000..bbafa8b --- /dev/null +++ b/apis/util_test.go @@ -0,0 +1,43 @@ +package apis + +import ( + "fmt" + "net/http" + "testing" + + "github.com/go-ozzo/ozzo-routing" + "github.com/stretchr/testify/assert" +) + +func Test_getPaginatedListFromRequest(t *testing.T) { + tests := []struct { + Tag string + Page, PerPage int + ExpPage, ExpPerPage int + }{ + {"t1", 1, 10, 1, 10}, + {"t2", -1, -1, 1, DEFAULT_PAGE_SIZE}, + {"t2", 0, 0, 1, DEFAULT_PAGE_SIZE}, + {"t3", 2, MAX_PAGE_SIZE + 1, 2, MAX_PAGE_SIZE}, + } + for _, test := range tests { + url := "http://www.example.com/search?foo=1" + if test.Page >= 0 { + url = fmt.Sprintf("%s&page=%v", url, test.Page) + } + if test.PerPage >= 0 { + url = fmt.Sprintf("%s&per_page=%v", url, test.PerPage) + } + req, _ := http.NewRequest("GET", url, nil) + c := routing.NewContext(nil, req) + pl := getPaginatedListFromRequest(c, 100000) + assert.Equal(t, test.ExpPage, pl.Page) + assert.Equal(t, test.ExpPerPage, pl.PerPage) + } +} + +func Test_parseInt(t *testing.T) { + assert.Equal(t, 123, parseInt("123", 1)) + assert.Equal(t, 1, parseInt("a123", 1)) + assert.Equal(t, 1, parseInt("", 1)) +} diff --git a/app/version.go b/app/version.go index 833086a..03cec42 100644 --- a/app/version.go +++ b/app/version.go @@ -1,3 +1,6 @@ package app -var Version = "v1.0" +// Version specifies the current version of the application. +// The value of this variable is replaced with the latest git tag +// by "make" while building or running the application. +var Version = "1.0" diff --git a/daos/mock_test.go b/daos/mock_test.go index da64fc0..3f97125 100644 --- a/daos/mock_test.go +++ b/daos/mock_test.go @@ -51,4 +51,3 @@ func (rs *requestScope) SetRollback(v bool) { func (rs *requestScope) Now() time.Time { return time.Now() } - diff --git a/errors/api_error_test.go b/errors/api_error_test.go index c5644fe..56875b2 100644 --- a/errors/api_error_test.go +++ b/errors/api_error_test.go @@ -2,6 +2,7 @@ package errors import ( "testing" + "github.com/stretchr/testify/assert" ) diff --git a/errors/errors.go b/errors/errors.go index 09a9853..b498942 100644 --- a/errors/errors.go +++ b/errors/errors.go @@ -12,14 +12,17 @@ type validationError struct { Error string `json:"error"` } +// InternalServerError creates a new API error representing an internal server error (HTTP 500) func InternalServerError(err error) *APIError { return NewAPIError(http.StatusInternalServerError, "INTERNAL_SERVER_ERROR", Params{"error": err.Error()}) } +// NotFound creates a new API error representing a resource-not-found error (HTTP 404) func NotFound(resource string) *APIError { return NewAPIError(http.StatusNotFound, "NOT_FOUND", Params{"resource": resource}) } +// InvalidData converts a data validation error into an API error (HTTP 400) func InvalidData(errs validation.Errors) *APIError { result := []validationError{} fields := []string{} diff --git a/errors/errors_test.go b/errors/errors_test.go index 9607fd6..a741c60 100644 --- a/errors/errors_test.go +++ b/errors/errors_test.go @@ -1,9 +1,9 @@ package errors import ( + errs "errors" "net/http" "testing" - errs "errors" "github.com/go-ozzo/ozzo-validation" "github.com/stretchr/testify/assert" diff --git a/models/artist.go b/models/artist.go index 8bb9474..8769658 100644 --- a/models/artist.go +++ b/models/artist.go @@ -2,11 +2,13 @@ package models import "github.com/go-ozzo/ozzo-validation" +// Artist represents an artist record. type Artist struct { - Id int `json:"id" db:"id"` - Name string `json:"name" db:"name"` + Id int `json:"id" db:"id"` + Name string `json:"name" db:"name"` } +// Validate validates the Artist fields. func (m Artist) Validate(attrs ...string) error { return validation.StructRules{}. Add("Name", validation.Required, validation.Length(0, 120)). diff --git a/services/artist.go b/services/artist.go index c20bb46..4607a96 100644 --- a/services/artist.go +++ b/services/artist.go @@ -68,7 +68,7 @@ func (s *ArtistService) Delete(rs app.RequestScope, id int) (*models.Artist, err return artist, err } -// Count returns the number of artists. +// Count returns the number of artists. func (s *ArtistService) Count(rs app.RequestScope) (int, error) { return s.dao.Count(rs) } diff --git a/services/artist_test.go b/services/artist_test.go index 21b5bc9..d6aaa99 100644 --- a/services/artist_test.go +++ b/services/artist_test.go @@ -38,7 +38,7 @@ func TestArtistService_Create(t *testing.T) { // dao error _, err = s.Create(nil, &models.Artist{ - Id: 100, + Id: 100, Name: "ddd", }) assert.NotNil(t, err) @@ -103,7 +103,7 @@ func newMockArtistDAO() artistDAO { } } -type mockArtistDAO struct{ +type mockArtistDAO struct { records []models.Artist } @@ -117,7 +117,7 @@ func (m *mockArtistDAO) Get(rs app.RequestScope, id int) (*models.Artist, error) } func (m *mockArtistDAO) Query(rs app.RequestScope, offset, limit int) ([]models.Artist, error) { - return m.records[offset:offset+limit], nil + return m.records[offset : offset+limit], nil } func (m *mockArtistDAO) Count(rs app.RequestScope) (int, error) { diff --git a/testdata/init.go b/testdata/init.go index 589e0f4..fd07e02 100644 --- a/testdata/init.go +++ b/testdata/init.go @@ -7,12 +7,12 @@ import ( "strings" "github.com/go-ozzo/ozzo-dbx" - _ "github.com/lib/pq" + _ "github.com/lib/pq" // initialize posgresql for test "github.com/qiangxue/golang-restful-starter-kit/app" ) var ( - db *dbx.DB + DB *dbx.DB ) func init() { @@ -21,7 +21,7 @@ func init() { if err != nil { panic(err) } - db, err = dbx.MustOpen("postgres", config.GetString("dsn")) + DB, err = dbx.MustOpen("postgres", config.GetString("dsn")) if err != nil { panic(err) } @@ -30,10 +30,10 @@ func init() { // ResetDB re-create the database schema and re-populate the initial data using the SQL statements in db.sql. // This method is mainly used in tests. func ResetDB() *dbx.DB { - if err := runSQLFile(db, getSQLFile()); err != nil { + if err := runSQLFile(DB, getSQLFile()); err != nil { panic(fmt.Errorf("Error while initializing test database: %s", err)) } - return db + return DB } func getSQLFile() string {