super-graph/core/internal/psql/mutate_test.go

124 lines
3.3 KiB
Go
Raw Permalink Normal View History

package psql_test
2019-09-05 06:09:56 +02:00
import (
"encoding/json"
"testing"
)
2019-10-05 08:17:08 +02:00
func singleUpsert(t *testing.T) {
gql := `mutation {
product(upsert: $upsert) {
2019-10-05 08:17:08 +02:00
id
name
}
}`
vars := map[string]json.RawMessage{
2019-12-25 07:24:30 +01:00
"upsert": json.RawMessage(` { "name": "my_name", "description": "my_desc" }`),
}
2020-02-10 07:45:37 +01:00
compileGQLToPSQL(t, gql, vars, "user")
}
func singleUpsertWhere(t *testing.T) {
gql := `mutation {
product(upsert: $upsert, where: { price : { gt: 3 } }) {
id
name
}
}`
2019-10-05 08:17:08 +02:00
vars := map[string]json.RawMessage{
2019-12-25 07:24:30 +01:00
"upsert": json.RawMessage(` { "name": "my_name", "description": "my_desc" }`),
2019-10-05 08:17:08 +02:00
}
2020-02-10 07:45:37 +01:00
compileGQLToPSQL(t, gql, vars, "user")
2019-10-05 08:17:08 +02:00
}
func bulkUpsert(t *testing.T) {
gql := `mutation {
product(upsert: $upsert) {
2019-10-05 08:17:08 +02:00
id
name
}
}`
vars := map[string]json.RawMessage{
2019-12-25 07:24:30 +01:00
"upsert": json.RawMessage(` [{ "name": "my_name", "description": "my_desc" }]`),
2019-10-05 08:17:08 +02:00
}
2020-02-10 07:45:37 +01:00
compileGQLToPSQL(t, gql, vars, "user")
2019-10-05 08:17:08 +02:00
}
2019-09-06 07:17:45 +02:00
func delete(t *testing.T) {
gql := `mutation {
product(delete: true, where: { id: { eq: 1 } }) {
id
name
}
}`
vars := map[string]json.RawMessage{
2019-12-25 07:24:30 +01:00
"update": json.RawMessage(` { "name": "my_name", "description": "my_desc" }`),
2019-09-06 07:17:45 +02:00
}
2020-02-10 07:45:37 +01:00
compileGQLToPSQL(t, gql, vars, "user")
2019-09-05 06:09:56 +02:00
}
2019-12-25 07:24:30 +01:00
// func blockedInsert(t *testing.T) {
// gql := `mutation {
// user(insert: $data) {
// id
// }
// }`
// sql := `WITH "users" AS (WITH "input" AS (SELECT '$1' :: json AS j) INSERT INTO "users" ("full_name", "email") SELECT "full_name", "email" FROM input i, json_populate_record(NULL::users, i.j) t WHERE false RETURNING *) SELECT json_object_agg('user', json_0) FROM (SELECT row_to_json((SELECT "json_row_0" FROM (SELECT "users_0"."id" AS "id") AS "json_row_0")) AS "json_0" FROM (SELECT "users"."id" FROM "users" LIMIT ('1') :: integer) AS "users_0" LIMIT ('1') :: integer) AS "sel_0"`
2019-12-25 07:24:30 +01:00
// vars := map[string]json.RawMessage{
// "data": json.RawMessage(`{"email": "reannagreenholt@orn.com", "full_name": "Flo Barton"}`),
// }
// resSQL, err := compileGQLToPSQL(gql, vars, "bad_dude")
// if err != nil {
// t.Fatal(err)
// }
// fmt.Println(string(resSQL))
// if string(resSQL) != sql {
// t.Fatal(errNotExpected)
// }
// }
// func blockedUpdate(t *testing.T) {
// gql := `mutation {
// user(where: { id: { lt: 5 } }, update: $data) {
// id
// email
// }
// }`
// sql := `WITH "users" AS (WITH "input" AS (SELECT '$1' :: json AS j) UPDATE "users" SET ("full_name", "email") = (SELECT "full_name", "email" FROM input i, json_populate_record(NULL::users, i.j) t) WHERE false RETURNING *) SELECT json_object_agg('user', json_0) FROM (SELECT row_to_json((SELECT "json_row_0" FROM (SELECT "users_0"."id" AS "id", "users_0"."email" AS "email") AS "json_row_0")) AS "json_0" FROM (SELECT "users"."id", "users"."email" FROM "users" LIMIT ('1') :: integer) AS "users_0" LIMIT ('1') :: integer) AS "sel_0"`
2019-12-25 07:24:30 +01:00
// vars := map[string]json.RawMessage{
// "data": json.RawMessage(`{"email": "reannagreenholt@orn.com", "full_name": "Flo Barton"}`),
// }
// resSQL, err := compileGQLToPSQL(gql, vars, "bad_dude")
// if err != nil {
// t.Fatal(err)
// }
// if string(resSQL) != sql {
// t.Fatal(errNotExpected)
// }
// }
2019-10-14 08:51:36 +02:00
func TestCompileMutate(t *testing.T) {
2019-10-05 08:17:08 +02:00
t.Run("singleUpsert", singleUpsert)
t.Run("singleUpsertWhere", singleUpsertWhere)
2019-10-05 08:17:08 +02:00
t.Run("bulkUpsert", bulkUpsert)
2019-09-06 07:17:45 +02:00
t.Run("delete", delete)
2019-12-25 07:24:30 +01:00
// t.Run("blockedInsert", blockedInsert)
// t.Run("blockedUpdate", blockedUpdate)
2019-09-05 06:09:56 +02:00
}