Skip to content

Commit

Permalink
Improvements testing package (#93)
Browse files Browse the repository at this point in the history
* Fixed PREST_CONF env

* Update vendor package

* Change testing package on adapters/postgres

* Change testing package on controllers

* Change testing package on config
  • Loading branch information
marioidival authored and Thiago Avelino committed Feb 1, 2017
1 parent 592c92e commit a788d54
Show file tree
Hide file tree
Showing 76 changed files with 1,267 additions and 8,075 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Expand Up @@ -14,7 +14,7 @@ services:
- postgresql

env:
- PREST_PG_USER=postgres PREST_PG_DATABASE=prest PREST_PG_PORT=5432 PREST_CONF=../../testdata/prest.toml
- PREST_PG_USER=postgres PREST_PG_DATABASE=prest PREST_PG_PORT=5432 PREST_CONF=$TRAVIS_BUILD_DIR/testdata/prest.toml

before_install:
- go get -u github.com/kardianos/govendor
Expand Down
19 changes: 11 additions & 8 deletions adapters/postgres/connection/conn_test.go
Expand Up @@ -2,15 +2,18 @@ package connection

import (
"testing"

. "github.com/smartystreets/goconvey/convey"
)

func TestMustGet(t *testing.T) {
Convey("Check connection", t, func() {
db := MustGet()
So(db, ShouldNotBeNil)
err := db.Ping()
So(err, ShouldBeNil)
})
t.Log("Open connection")
db := MustGet()
if db == nil {
t.Errorf("expected db connection, but no was!")
}

t.Log("Ping Pong")
err := db.Ping()
if err != nil {
t.Errorf("expected no error, but got: %v", err)
}
}
29 changes: 16 additions & 13 deletions adapters/postgres/postgres.go
Expand Up @@ -159,7 +159,6 @@ func SchemaClause(req *http.Request) (query string) {

// JoinByRequest implements join in queries
func JoinByRequest(r *http.Request) (values []string, err error) {
joinValues := []string{}
queries := r.URL.Query()

if queries.Get("_join") == "" {
Expand All @@ -185,24 +184,28 @@ func JoinByRequest(r *http.Request) (values []string, err error) {
}

joinQuery := fmt.Sprintf(" %s JOIN %s ON %s %s %s ", strings.ToUpper(joinArgs[0]), joinArgs[1], joinArgs[2], op, joinArgs[4])
joinValues = append(joinValues, joinQuery)
values = append(values, joinQuery)

return joinValues, nil
return
}

// SelectFields query
func SelectFields(fields []string) (string, error) {
func SelectFields(fields []string) (sql string, err error) {
if len(fields) == 0 {
return "", errors.New("you must select at least one fields")

err = errors.New("you must select at least one field.")
return
}

for _, field := range fields {
if field != "*" && chkInvalidIdentifier(field) {
return "", fmt.Errorf("invalid identifier %s", field)
err = fmt.Errorf("invalid identifier %s", field)
return
}
}

return fmt.Sprintf("SELECT %s FROM", strings.Join(fields, ",")), nil
sql = fmt.Sprintf("SELECT %s FROM", strings.Join(fields, ","))
return
}

// OrderByRequest implements ORDER BY in queries
Expand All @@ -214,15 +217,15 @@ func OrderByRequest(r *http.Request) (values string, err error) {
values = " ORDER BY "
orderingArr := strings.Split(reqOrder, ",")

for i, s := range orderingArr {
if chkInvalidIdentifier(s) {
for i, field := range orderingArr {
if chkInvalidIdentifier(field) {
err = errors.New("Invalid identifier")
values = ""
return
}
field := s

if strings.HasPrefix(s, "-") {
field = fmt.Sprintf("%s DESC", s[1:])
if strings.HasPrefix(field, "-") {
field = fmt.Sprintf("%s DESC", field[1:])
}

values = fmt.Sprintf("%s %s", values, field)
Expand All @@ -233,7 +236,7 @@ func OrderByRequest(r *http.Request) (values string, err error) {
}
}
}
return values, nil
return
}

// CountByRequest implements COUNT(fields) OPERTATION
Expand Down

0 comments on commit a788d54

Please sign in to comment.