-
Notifications
You must be signed in to change notification settings - Fork 185
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Qiang Xue
committed
Aug 16, 2016
1 parent
58947b6
commit 1c9a3e6
Showing
17 changed files
with
990 additions
and
680 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
|
@@ -26,3 +26,5 @@ _testmain.go | |
glide.lock | ||
coverage.out | ||
coverage-all.out | ||
build | ||
server |
This file contains 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,27 @@ | ||
language: go | ||
|
||
go: | ||
- 1.6 | ||
- 1.7 | ||
- tip | ||
|
||
cache: | ||
directories: | ||
- vendor | ||
|
||
addons: | ||
postgresql: "9.5" | ||
|
||
install: | ||
- go get golang.org/x/tools/cmd/cover | ||
- go get github.com/mattn/goveralls | ||
- go get github.com/axw/gocov/gocov | ||
- go get -u github.com/Masterminds/glide | ||
- glide up -s -u | ||
|
||
before_script: | ||
- psql -U postgres -c 'CREATE DATABASE go_restful;'; | ||
|
||
script: | ||
- glide up -u -s | ||
- $HOME/gopath/bin/goveralls -service=travis-ci |
This file contains 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
This file contains 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
This file contains 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,43 @@ | ||
package app | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
"github.com/spf13/viper" | ||
) | ||
|
||
type Config interface { | ||
Get(key string) interface{} | ||
GetBool(key string) bool | ||
GetFloat64(key string) float64 | ||
GetInt(key string) int | ||
GetString(key string) string | ||
GetStringMap(key string) map[string]interface{} | ||
GetStringMapString(key string) map[string]string | ||
GetStringSlice(key string) []string | ||
GetTime(key string) time.Time | ||
GetDuration(key string) time.Duration | ||
IsSet(key string) bool | ||
} | ||
|
||
func LoadConfig(paths ...string) (Config, error) { | ||
v := viper.New() | ||
v.SetDefault("error_file", "config/errors.yaml") | ||
v.SetDefault("server_port", "8080") | ||
|
||
v.SetEnvPrefix("restful") | ||
v.BindEnv("dsn") | ||
v.BindEnv("server_port") | ||
|
||
v.SetConfigType("yaml") | ||
v.SetConfigName("app") | ||
for _, path := range paths { | ||
v.AddConfigPath(path) | ||
} | ||
if err := v.ReadInConfig(); err != nil { | ||
return nil, fmt.Errorf("Failed to read the configuration file: %s", err) | ||
} | ||
|
||
return v, nil | ||
} |
This file contains 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
This file contains 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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
package app | ||
|
||
var Version = "1.0" | ||
var Version = "v1.0" |
This file contains 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 @@ | ||
dsn: "postgres://postgres:postgres@127.0.0.1:5432/go_restful?sslmode=disable" |
This file contains 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
This file contains 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,104 @@ | ||
package daos | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/qiangxue/golang-restful-starter-kit/app" | ||
"github.com/qiangxue/golang-restful-starter-kit/models" | ||
"github.com/qiangxue/golang-restful-starter-kit/testdata" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestArtistDAO(t *testing.T) { | ||
db := testdata.ResetDB() | ||
dao := NewArtistDAO() | ||
|
||
{ | ||
// Get | ||
testDBCall(db, func(rs app.RequestScope) { | ||
artist, err := dao.Get(rs, 2) | ||
assert.Nil(t, err) | ||
if assert.NotNil(t, artist) { | ||
assert.Equal(t, 2, artist.Id) | ||
} | ||
}) | ||
} | ||
|
||
{ | ||
// Create | ||
testDBCall(db, func(rs app.RequestScope) { | ||
artist := &models.Artist{ | ||
Id: 1000, | ||
Name: "tester", | ||
} | ||
err := dao.Create(rs, artist) | ||
assert.Nil(t, err) | ||
assert.NotEqual(t, 1000, artist.Id) | ||
assert.NotZero(t, artist.Id) | ||
}) | ||
} | ||
|
||
{ | ||
// Update | ||
testDBCall(db, func(rs app.RequestScope) { | ||
artist := &models.Artist{ | ||
Id: 2, | ||
Name: "tester", | ||
} | ||
err := dao.Update(rs, artist.Id, artist) | ||
assert.Nil(t, rs.Tx().Commit()) | ||
assert.Nil(t, err) | ||
}) | ||
} | ||
|
||
{ | ||
// Update with error | ||
testDBCall(db, func(rs app.RequestScope) { | ||
artist := &models.Artist{ | ||
Id: 2, | ||
Name: "tester", | ||
} | ||
err := dao.Update(rs, 99999, artist) | ||
assert.Nil(t, rs.Tx().Commit()) | ||
assert.NotNil(t, err) | ||
}) | ||
} | ||
|
||
{ | ||
// Delete | ||
testDBCall(db, func(rs app.RequestScope) { | ||
err := dao.Delete(rs, 2) | ||
assert.Nil(t, rs.Tx().Commit()) | ||
assert.Nil(t, err) | ||
}) | ||
} | ||
|
||
{ | ||
// Delete with error | ||
testDBCall(db, func(rs app.RequestScope) { | ||
err := dao.Delete(rs, 99999) | ||
assert.Nil(t, rs.Tx().Commit()) | ||
assert.NotNil(t, err) | ||
}) | ||
} | ||
|
||
{ | ||
// Query | ||
testDBCall(db, func(rs app.RequestScope) { | ||
artists, err := dao.Query(rs, 1, 3) | ||
assert.Nil(t, rs.Tx().Commit()) | ||
assert.Nil(t, err) | ||
assert.Equal(t, 3, len(artists)) | ||
}) | ||
} | ||
|
||
{ | ||
// Count | ||
testDBCall(db, func(rs app.RequestScope) { | ||
count, err := dao.Count(rs) | ||
assert.Nil(t, rs.Tx().Commit()) | ||
assert.Nil(t, err) | ||
assert.NotZero(t, count) | ||
}) | ||
} | ||
} |
This file contains 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,54 @@ | ||
package daos | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/go-ozzo/ozzo-dbx" | ||
"github.com/qiangxue/golang-restful-starter-kit/app" | ||
) | ||
|
||
func testDBCall(db *dbx.DB, f func(rs app.RequestScope)) { | ||
rs := mockRequestScope(db) | ||
|
||
defer func() { | ||
rs.Tx().Rollback() | ||
}() | ||
|
||
f(rs) | ||
} | ||
|
||
type requestScope struct { | ||
app.Logger | ||
tx *dbx.Tx | ||
} | ||
|
||
func mockRequestScope(db *dbx.DB) app.RequestScope { | ||
tx, _ := db.Begin() | ||
return &requestScope{ | ||
tx: tx, | ||
} | ||
} | ||
|
||
func (rs *requestScope) UserID() string { | ||
return "tester" | ||
} | ||
|
||
func (rs *requestScope) Tx() *dbx.Tx { | ||
return rs.tx | ||
} | ||
|
||
func (rs *requestScope) SetTx(tx *dbx.Tx) { | ||
rs.tx = tx | ||
} | ||
|
||
func (rs *requestScope) Rollback() bool { | ||
return false | ||
} | ||
|
||
func (rs *requestScope) SetRollback(v bool) { | ||
} | ||
|
||
func (rs *requestScope) Now() time.Time { | ||
return time.Now() | ||
} | ||
|
Oops, something went wrong.