Repository: nuveo/prest (go)

Issue:
count schema
- pREST version (or commit ref): a50fcf1
- pREST endpoint: /schemas?_count=*
- PostgreSQL version: 9.6
- Operating system: Linux
- Go version: 1.7
- Log gist: nil
## Description

![image](https://cloud.githubusercontent.com/assets/3127847/22717757/02ea0fc2-ed84-11e6-8905-cbef2348e378.png)

![image](https://cloud.githubusercontent.com/assets/3127847/22717763/0e465d3a-ed84-11e6-8b8f-7c7846c1d103.png)



When i count the number of schemas, the correct would not be the sum of the aggregate of values?
...


Pull request description:
fix  schemas count
closes #101 

Patch:
diff --git a/adapters/postgres/postgres.go b/adapters/postgres/postgres.go
index ca982d1..2205047 100644
--- a/adapters/postgres/postgres.go
+++ b/adapters/postgres/postgres.go
@@ -125,11 +125,12 @@ func WhereByRequest(r *http.Request, initialPlaceholderID int) (whereSyntax stri
 }
 
 // DatabaseClause return a SELECT `query`
-func DatabaseClause(req *http.Request) (query string) {
+func DatabaseClause(req *http.Request) (query string, hasCount bool) {
 	queries := req.URL.Query()
-	hasCount := queries.Get("_count")
+	countQuery := queries.Get("_count")
 
-	if hasCount != "" {
+	if countQuery != "" {
+		hasCount = true
 		query = fmt.Sprintf(statements.DatabasesSelect, statements.FieldCountDatabaseName)
 	} else {
 		query = fmt.Sprintf(statements.DatabasesSelect, statements.FieldDatabaseName)
@@ -138,11 +139,12 @@ func DatabaseClause(req *http.Request) (query string) {
 }
 
 // SchemaClause return a SELECT `query`
-func SchemaClause(req *http.Request) (query string) {
+func SchemaClause(req *http.Request) (query string, hasCount bool) {
 	queries := req.URL.Query()
-	hasCount := queries.Get("_count")
+	countQuery := queries.Get("_count")
 
-	if hasCount != "" {
+	if countQuery != "" {
+		hasCount = true
 		query = fmt.Sprintf(statements.SchemasSelect, statements.FieldCountSchemaName)
 	} else {
 		query = fmt.Sprintf(statements.SchemasSelect, statements.FieldSchemaName)
diff --git a/controllers/databases.go b/controllers/databases.go
index 146990b..62b8e1c 100644
--- a/controllers/databases.go
+++ b/controllers/databases.go
@@ -18,7 +18,7 @@ func GetDatabases(w http.ResponseWriter, r *http.Request) {
 		return
 	}
 
-	query := postgres.DatabaseClause(r)
+	query, hasCount := postgres.DatabaseClause(r)
 	sqlDatabases := fmt.Sprint(query, statements.DatabasesWhere)
 
 	if requestWhere != "" {
@@ -28,7 +28,7 @@ func GetDatabases(w http.ResponseWriter, r *http.Request) {
 	order, _ := postgres.OrderByRequest(r)
 	if order != "" {
 		sqlDatabases = fmt.Sprint(sqlDatabases, order)
-	} else {
+	} else if !hasCount {
 		sqlDatabases = fmt.Sprint(sqlDatabases, fmt.Sprintf(statements.DatabasesOrderBy, statements.FieldDatabaseName))
 	}
 
@@ -39,7 +39,6 @@ func GetDatabases(w http.ResponseWriter, r *http.Request) {
 	}
 
 	sqlDatabases = fmt.Sprint(sqlDatabases, " ", page)
-
 	object, err := postgres.Query(sqlDatabases, values...)
 	if err != nil {
 		log.Println(err)
diff --git a/controllers/schemas.go b/controllers/schemas.go
index 722117d..66003b0 100644
--- a/controllers/schemas.go
+++ b/controllers/schemas.go
@@ -4,7 +4,6 @@ import (
 	"fmt"
 	"log"
 	"net/http"
-	"strings"
 
 	"github.com/nuveo/prest/adapters/postgres"
 	"github.com/nuveo/prest/statements"
@@ -19,20 +18,16 @@ func GetSchemas(w http.ResponseWriter, r *http.Request) {
 		return
 	}
 
-	sqlSchemas := postgres.SchemaClause(r)
+	sqlSchemas, hasCount := postgres.SchemaClause(r)
 
 	if requestWhere != "" {
 		sqlSchemas = fmt.Sprint(sqlSchemas, " WHERE ", requestWhere)
 	}
 
-	if strings.Contains(sqlSchemas, "COUNT") {
-		sqlSchemas = fmt.Sprint(sqlSchemas, fmt.Sprintf(statements.SchemasGroupBy, statements.FieldSchemaName))
-	}
-
 	order, _ := postgres.OrderByRequest(r)
 	if order != "" {
 		sqlSchemas = fmt.Sprint(sqlSchemas, order)
-	} else {
+	} else if !hasCount {
 		sqlSchemas = fmt.Sprint(sqlSchemas, fmt.Sprintf(statements.SchemasOrderBy, statements.FieldSchemaName))
 	}
 
@@ -43,7 +38,6 @@ func GetSchemas(w http.ResponseWriter, r *http.Request) {
 	}
 
 	sqlSchemas = fmt.Sprint(sqlSchemas, " ", page)
-
 	object, err := postgres.Query(sqlSchemas, values...)
 	if err != nil {
 		log.Println(err)


Test patch:
diff --git a/adapters/postgres/postgres_test.go b/adapters/postgres/postgres_test.go
index 871560f..7e1ed1b 100644
--- a/adapters/postgres/postgres_test.go
+++ b/adapters/postgres/postgres_test.go
@@ -536,7 +536,7 @@ func TestDatabaseClause(t *testing.T) {
 			t.Errorf("expected no errors on NewRequest, got: %v", err)
 		}
 
-		query := DatabaseClause(r)
+		query, _ := DatabaseClause(r)
 		if query != tc.queryExpected {
 			t.Errorf("query unexpected, got: %s", query)
 		}
@@ -561,7 +561,7 @@ func TestSchemaClause(t *testing.T) {
 			t.Errorf("expected no errors on NewRequest, got: %v", err)
 		}
 
-		query := SchemaClause(r)
+		query, _ := SchemaClause(r)
 		if query != tc.queryExpected {
 			t.Errorf("query unexpected, got: %s", query)
 		}
diff --git a/controllers/databases_test.go b/controllers/databases_test.go
index 82231bb..3092a7b 100644
--- a/controllers/databases_test.go
+++ b/controllers/databases_test.go
@@ -20,7 +20,7 @@ func TestGetDatabases(t *testing.T) {
 		{"Get databases with custom where clause", "/databases?datname=$eq.prest", "GET", 200},
 		{"Get databases with custom order clause", "/databases?_order=datname", "GET", 200},
 		{"Get databases with custom where clause and pagination", "/databases?datname=$eq.prest&_page=1&_page_size=20", "GET", 200},
-		{"Get databases with COUNT clause", "/databases?_count=", "GET", 200},
+		{"Get databases with COUNT clause", "/databases?_count=*", "GET", 200},
 		{"Get databases with custom where invalid clause", "/databases?0datname=prest", "GET", 400},
 		{"Get databases with custom where and pagination invalid", "/databases?datname=$eq.prest&_page=A", "GET", 400},
 		{"Get databases with noexistent column", "/databases?datatata=$eq.test", "GET", 500},
diff --git a/controllers/schemas_test.go b/controllers/schemas_test.go
index 1218514..886841d 100644
--- a/controllers/schemas_test.go
+++ b/controllers/schemas_test.go
@@ -15,15 +15,16 @@ func TestGetSchemas(t *testing.T) {
 		url         string
 		method      string
 		status      int
+		body        string
 	}{
-		{"Get schemas without custom where clause", "/schemas", "GET", 200},
-		{"Get schemas with custom where clause", "/schemas?schema_name=$eq.public", "GET", 200},
-		{"Get schemas with custom order clause", "/schemas?schema_name=$eq.public&_order=schema_name", "GET", 200},
-		{"Get schemas with custom where clause and pagination", "/schemas?schema_name=$eq.public&_page=1&_page_size=20", "GET", 200},
-		{"Get schemas with COUNT clause", "/schemas?_count=*", "GET", 200},
-		{"Get schemas with custom where invalid clause", "/schemas?0schema_name=$eq.public", "GET", 400},
-		{"Get schemas with custom where and pagination invalid", "/schemas?schema_name=$eq.public&_page=A", "GET", 400},
-		{"Get schemas with noexistent column", "/schemas?schematame=$eq.test", "GET", 500},
+		{"Get schemas without custom where clause", "/schemas", "GET", 200, "[{\"schema_name\":\"information_schema\"},{\"schema_name\":\"pg_catalog\"},{\"schema_name\":\"pg_temp_1\"},{\"schema_name\":\"pg_toast\"},{\"schema_name\":\"pg_toast_temp_1\"},{\"schema_name\":\"public\"}]"},
+		{"Get schemas with custom where clause", "/schemas?schema_name=$eq.public", "GET", 200, "[{\"schema_name\":\"public\"}]"},
+		{"Get schemas with custom order clause", "/schemas?schema_name=$eq.public&_order=schema_name", "GET", 200, "[{\"schema_name\":\"public\"}]"},
+		{"Get schemas with custom where clause and pagination", "/schemas?schema_name=$eq.public&_page=1&_page_size=20", "GET", 200, "[{\"schema_name\":\"public\"}]"},
+		{"Get schemas with COUNT clause", "/schemas?_count=*", "GET", 200, "[{\"count\":6}]"},
+		{"Get schemas with custom where invalid clause", "/schemas?0schema_name=$eq.public", "GET", 400, "invalid identifier: 0schema_name\n"},
+		{"Get schemas with custom where and pagination invalid", "/schemas?schema_name=$eq.public&_page=A", "GET", 400, "Paging error\n"},
+		{"Get schemas with noexistent column", "/schemas?schematame=$eq.test", "GET", 500, "pq: column \"schematame\" does not exist\n"},
 	}
 
 	router := mux.NewRouter()
@@ -34,6 +35,7 @@ func TestGetSchemas(t *testing.T) {
 	r := api.Request{}
 	for _, tc := range testCases {
 		t.Log(tc.description)
-		doRequest(t, server.URL+tc.url, r, tc.method, tc.status, "GetSchemas")
+		doRequest(t, server.URL+tc.url, r, tc.method, tc.status, "GetSchemas", tc.body)
+
 	}
 }
diff --git a/controllers/utils_test.go b/controllers/utils_test.go
index c203904..a25bb41 100644
--- a/controllers/utils_test.go
+++ b/controllers/utils_test.go
@@ -148,7 +148,7 @@ func doValidPatchRequest(t *testing.T, url string, r api.Request, where string)
 	}
 }
 
-func doRequest(t *testing.T, url string, r api.Request, method string, expectedStatus int, where string) {
+func doRequest(t *testing.T, url string, r api.Request, method string, expectedStatus int, where string, expectedBody ...string) {
 	fmt.Println("Test:", where)
 	var byt []byte
 	var err error
@@ -175,10 +175,16 @@ func doRequest(t *testing.T, url string, r api.Request, method string, expectedS
 		t.Errorf("expected %d, got: %d", expectedStatus, resp.StatusCode)
 	}
 
-	_, err = ioutil.ReadAll(resp.Body)
+	body, err := ioutil.ReadAll(resp.Body)
 	if err != nil {
 		t.Error("error on ioutil ReadAll", err)
 	}
+
+	if len(expectedBody) > 0 {
+		if string(body) != expectedBody[0] {
+			t.Errorf("expected %q, got: %q", expectedBody, string(body))
+		}
+	}
 }
 
 func createMockScripts(base string) {