Skip to content

Commit

Permalink
more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Qiang Xue committed Aug 16, 2016
1 parent 5aadec6 commit fc1b2fb
Show file tree
Hide file tree
Showing 11 changed files with 66 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
43 changes: 43 additions & 0 deletions apis/util_test.go
Original file line number Diff line number Diff line change
@@ -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))
}
5 changes: 4 additions & 1 deletion app/version.go
Original file line number Diff line number Diff line change
@@ -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"
1 change: 0 additions & 1 deletion daos/mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,3 @@ func (rs *requestScope) SetRollback(v bool) {
func (rs *requestScope) Now() time.Time {
return time.Now()
}

1 change: 1 addition & 0 deletions errors/api_error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package errors

import (
"testing"

"github.com/stretchr/testify/assert"
)

Expand Down
3 changes: 3 additions & 0 deletions errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down
2 changes: 1 addition & 1 deletion errors/errors_test.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
6 changes: 4 additions & 2 deletions models/artist.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)).
Expand Down
2 changes: 1 addition & 1 deletion services/artist.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
6 changes: 3 additions & 3 deletions services/artist_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -103,7 +103,7 @@ func newMockArtistDAO() artistDAO {
}
}

type mockArtistDAO struct{
type mockArtistDAO struct {
records []models.Artist
}

Expand All @@ -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) {
Expand Down
10 changes: 5 additions & 5 deletions testdata/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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)
}
Expand All @@ -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 {
Expand Down

0 comments on commit fc1b2fb

Please sign in to comment.