Skip to content

Commit

Permalink
wrapper names with " (#4)
Browse files Browse the repository at this point in the history
* wrapper names with "

* add more tests

* create new table config on tests

* create new table config on tests

* insert on reply

* insert on reply

* fix tests

* debug

* debug

* debug

* bug fix multiple quotes
  • Loading branch information
felipeweb committed Aug 10, 2017
1 parent 7da58d1 commit 19c7291
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 7 deletions.
7 changes: 2 additions & 5 deletions tables.go
Expand Up @@ -38,7 +38,6 @@ func GetTables(w http.ResponseWriter, r *http.Request) {
}

sqlTables = fmt.Sprint(sqlTables, order)

sc := postgres.Query(sqlTables, values...)
if sc.Err() != nil {
http.Error(w, sc.Err().Error(), http.StatusBadRequest)
Expand Down Expand Up @@ -93,7 +92,6 @@ func GetTablesByDatabaseAndSchema(w http.ResponseWriter, r *http.Request) {
valuesAux = append(valuesAux, database)
valuesAux = append(valuesAux, schema)
valuesAux = append(valuesAux, values...)

sc := postgres.Query(sqlSchemaTables, valuesAux...)
if sc.Err() != nil {
http.Error(w, sc.Err().Error(), http.StatusBadRequest)
Expand Down Expand Up @@ -127,8 +125,7 @@ func SelectFromTables(w http.ResponseWriter, r *http.Request) {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}

query := fmt.Sprintf("%s %s.%s.%s", selectStr, database, schema, table)
query := fmt.Sprintf(`%s "%s"."%s"."%s"`, selectStr, database, schema, table)

countQuery, err := postgres.CountByRequest(r)
if err != nil {
Expand All @@ -137,7 +134,7 @@ func SelectFromTables(w http.ResponseWriter, r *http.Request) {
return
}
if countQuery != "" {
query = fmt.Sprintf("%s %s.%s.%s", countQuery, database, schema, table)
query = fmt.Sprintf(`%s "%s"."%s"."%s"`, countQuery, database, schema, table)
}

joinValues, err := postgres.JoinByRequest(r)
Expand Down
7 changes: 5 additions & 2 deletions tables_test.go
Expand Up @@ -80,6 +80,7 @@ func TestSelectFromTables(t *testing.T) {
}{
{"execute select in a table with array", "/prest/public/testarray", "GET", http.StatusOK, "[{\"id\":100,\"data\":[\"Gohan\",\"Goten\"]}]"},
{"execute select in a table without custom where clause", "/prest/public/test", "GET", http.StatusOK, ""},
{"execute select in a table case sentive", "/prest/public/Reply", "GET", http.StatusOK, "[{\"id\":1,\"name\":\"prest tester\"}]"},
{"execute select in a table with count all fields *", "/prest/public/test?_count=*", "GET", http.StatusOK, ""},
{"execute select in a table with count function", "/prest/public/test?_count=name", "GET", http.StatusOK, ""},
{"execute select in a table with custom where clause", "/prest/public/test?name=$eq.nuveo", "GET", http.StatusOK, ""},
Expand All @@ -90,7 +91,7 @@ func TestSelectFromTables(t *testing.T) {
{"execute select in a table with select *", "/prest/public/test5?_select=*", "GET", http.StatusOK, ""},

{"execute select in a table with group by clause", "/prest/public/test_group_by_table?_select=age,sum:salary&_groupby=age", "GET", http.StatusOK, "[{\"age\":20,\"sum\":1350}, \n {\"age\":19,\"sum\":7997}]"},
{"Execute select in a table with group by and having clause", "/prest/public/test_group_by_table?_select=age,sum:salary&_groupby=age->>having:sum:salary:$gt:3000", "GET", http.StatusOK, "[{\"age\":19,\"sum\":7997}]"},
{"execute select in a table with group by and having clause", "/prest/public/test_group_by_table?_select=age,sum:salary&_groupby=age->>having:sum:salary:$gt:3000", "GET", http.StatusOK, "[{\"age\":19,\"sum\":7997}]"},

{"execute select in a view without custom where clause", "/prest/public/view_test", "GET", http.StatusOK, ""},
{"execute select in a view with count all fields *", "/prest/public/view_test?_count=*", "GET", http.StatusOK, ""},
Expand All @@ -101,7 +102,6 @@ func TestSelectFromTables(t *testing.T) {
{"execute select in a view with custom where clause and pagination", "/prest/public/view_test?player=$eq.gopher&_page=1&_page_size=20", "GET", http.StatusOK, ""},
{"execute select in a view with select fields", "/prest/public/view_test?_select=player", "GET", http.StatusOK, ""},

// errors
{"execute select in a table with invalid join clause", "/prest/public/test?_join=inner:test2:test2.name", "GET", http.StatusBadRequest, ""},
{"execute select in a table with invalid where clause", "/prest/public/test?0name=$eq.nuveo", "GET", http.StatusBadRequest, ""},
{"execute select in a table with order clause and column invalid", "/prest/public/test?_order=0name", "GET", http.StatusBadRequest, ""},
Expand All @@ -122,6 +122,9 @@ func TestSelectFromTables(t *testing.T) {

for _, tc := range testCases {
t.Log(tc.description)
//config.PrestConf = &config.Prest{}
//config.Load()

if tc.body != "" {
doRequest(t, server.URL+tc.url, nil, tc.method, tc.status, "SelectFromTables", tc.body)
continue
Expand Down
5 changes: 5 additions & 0 deletions testdata/prest.toml
Expand Up @@ -18,6 +18,11 @@ restrict = true # can access only the tables listed below
permissions = ["read", "write", "delete"]
fields = ["id", "name"]

[[access.tables]]
name = "Reply"
permissions = ["read", "write", "delete"]
fields = ["id", "name"]

[[access.tables]]
name = "testarray"
permissions = ["read", "write", "delete"]
Expand Down
2 changes: 2 additions & 0 deletions testdata/schema.sh
Expand Up @@ -8,6 +8,7 @@ psql -h $DB_HOST -p $DB_PORT -U $DB_USER -c "create database $DB_NAME;"

# Create tables
psql -d $DB_NAME -h $DB_HOST -p $DB_PORT -U $DB_USER -c "create table test(id serial, name text);"
psql -d $DB_NAME -h $DB_HOST -p $DB_PORT -U $DB_USER -c "create table \"Reply\"(id serial, name text);"
psql -d $DB_NAME -h $DB_HOST -p $DB_PORT -U $DB_USER -c "create table test2(name text, number integer);"
psql -d $DB_NAME -h $DB_HOST -p $DB_PORT -U $DB_USER -c "create table test3(id serial, name text UNIQUE);"
psql -d $DB_NAME -h $DB_HOST -p $DB_PORT -U $DB_USER -c "create table test4(id serial primary key, name text UNIQUE);"
Expand All @@ -27,6 +28,7 @@ psql -d $DB_NAME -h $DB_HOST -p $DB_PORT -U $DB_USER -c "create table test_group

# Inserts
psql -d $DB_NAME -h $DB_HOST -p $DB_PORT -U $DB_USER -c "insert into test (name) values ('prest tester');"
psql -d $DB_NAME -h $DB_HOST -p $DB_PORT -U $DB_USER -c "insert into \"Reply\" (name) values ('prest tester');"
psql -d $DB_NAME -h $DB_HOST -p $DB_PORT -U $DB_USER -c "insert into test (name) values ('tester02');"
psql -d $DB_NAME -h $DB_HOST -p $DB_PORT -U $DB_USER -c "insert into test2 (name, number) values ('tester02', 2);"
psql -d $DB_NAME -h $DB_HOST -p $DB_PORT -U $DB_USER -c "insert into test3 (name) values ('prest');"
Expand Down

0 comments on commit 19c7291

Please sign in to comment.