2019-03-24 09:57:29 -04:00
|
|
|
package qcode
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2019-09-05 00:09:56 -04:00
|
|
|
func TestCompile1(t *testing.T) {
|
2019-10-14 02:51:36 -04:00
|
|
|
qc, _ := NewCompiler(Config{})
|
|
|
|
qc.AddRole("user", "product", TRConfig{
|
|
|
|
Query: QueryConfig{
|
|
|
|
Columns: []string{"id", "Name"},
|
|
|
|
},
|
|
|
|
})
|
2019-06-14 22:17:21 -04:00
|
|
|
|
2019-10-14 02:51:36 -04:00
|
|
|
_, err := qc.Compile([]byte(`
|
2019-06-14 22:17:21 -04:00
|
|
|
product(id: 15) {
|
2019-04-20 10:45:12 -04:00
|
|
|
id
|
|
|
|
name
|
2019-10-14 02:51:36 -04:00
|
|
|
}`), "user")
|
2019-06-14 22:17:21 -04:00
|
|
|
|
2019-04-20 10:45:12 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-03-24 09:57:29 -04:00
|
|
|
}
|
2019-04-20 17:27:03 -04:00
|
|
|
|
2019-09-05 00:09:56 -04:00
|
|
|
func TestCompile2(t *testing.T) {
|
2019-10-14 02:51:36 -04:00
|
|
|
qc, _ := NewCompiler(Config{})
|
|
|
|
qc.AddRole("user", "product", TRConfig{
|
|
|
|
Query: QueryConfig{
|
|
|
|
Columns: []string{"ID"},
|
|
|
|
},
|
|
|
|
})
|
2019-09-05 00:09:56 -04:00
|
|
|
|
2019-10-14 02:51:36 -04:00
|
|
|
_, err := qc.Compile([]byte(`
|
2019-09-05 00:09:56 -04:00
|
|
|
query { product(id: 15) {
|
|
|
|
id
|
|
|
|
name
|
2019-10-14 02:51:36 -04:00
|
|
|
} }`), "user")
|
2019-09-05 00:09:56 -04:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCompile3(t *testing.T) {
|
2019-10-14 02:51:36 -04:00
|
|
|
qc, _ := NewCompiler(Config{})
|
|
|
|
qc.AddRole("user", "product", TRConfig{
|
|
|
|
Query: QueryConfig{
|
|
|
|
Columns: []string{"ID"},
|
|
|
|
},
|
|
|
|
})
|
2019-09-05 00:09:56 -04:00
|
|
|
|
2019-10-14 02:51:36 -04:00
|
|
|
_, err := qc.Compile([]byte(`
|
2019-09-05 00:09:56 -04:00
|
|
|
mutation {
|
|
|
|
product(id: 15, name: "Test") {
|
|
|
|
id
|
|
|
|
name
|
|
|
|
}
|
2019-10-14 02:51:36 -04:00
|
|
|
}`), "user")
|
2019-09-05 00:09:56 -04:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-16 15:41:28 -04:00
|
|
|
func TestInvalidCompile1(t *testing.T) {
|
2019-04-20 19:42:08 -04:00
|
|
|
qcompile, _ := NewCompiler(Config{})
|
2019-10-14 02:51:36 -04:00
|
|
|
_, err := qcompile.Compile([]byte(`#`), "user")
|
2019-06-14 22:17:21 -04:00
|
|
|
|
2019-04-20 19:42:08 -04:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal(errors.New("expecting an error"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-16 15:41:28 -04:00
|
|
|
func TestInvalidCompile2(t *testing.T) {
|
|
|
|
qcompile, _ := NewCompiler(Config{})
|
2019-10-14 02:51:36 -04:00
|
|
|
_, err := qcompile.Compile([]byte(`{u(where:{not:0})}`), "user")
|
2019-06-16 15:41:28 -04:00
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal(errors.New("expecting an error"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-20 17:27:03 -04:00
|
|
|
func TestEmptyCompile(t *testing.T) {
|
|
|
|
qcompile, _ := NewCompiler(Config{})
|
2019-10-14 02:51:36 -04:00
|
|
|
_, err := qcompile.Compile([]byte(``), "user")
|
2019-06-14 22:17:21 -04:00
|
|
|
|
2019-04-20 17:27:03 -04:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal(errors.New("expecting an error"))
|
|
|
|
}
|
|
|
|
}
|
2019-05-14 22:32:12 -04:00
|
|
|
|
2019-06-14 22:17:21 -04:00
|
|
|
var gql = []byte(`
|
2019-06-01 02:03:09 -04:00
|
|
|
products(
|
|
|
|
# returns only 30 items
|
|
|
|
limit: 30,
|
|
|
|
|
|
|
|
# starts from item 10, commented out for now
|
|
|
|
# offset: 10,
|
|
|
|
|
|
|
|
# orders the response items by highest price
|
|
|
|
order_by: { price: desc },
|
|
|
|
|
|
|
|
# no duplicate prices returned
|
|
|
|
distinct: [ price ]
|
|
|
|
|
|
|
|
# only items with an id >= 30 and < 30 are returned
|
|
|
|
where: { id: { AND: { greater_or_equals: 20, lt: 28 } } }) {
|
|
|
|
id
|
|
|
|
name
|
|
|
|
price
|
2019-06-14 22:17:21 -04:00
|
|
|
}`)
|
2019-06-01 02:03:09 -04:00
|
|
|
|
2019-05-14 22:32:12 -04:00
|
|
|
func BenchmarkQCompile(b *testing.B) {
|
|
|
|
qcompile, _ := NewCompiler(Config{})
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
b.ReportAllocs()
|
|
|
|
|
|
|
|
for n := 0; n < b.N; n++ {
|
2019-10-14 02:51:36 -04:00
|
|
|
_, err := qcompile.Compile(gql, "user")
|
2019-05-14 22:32:12 -04:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-01 02:03:09 -04:00
|
|
|
func BenchmarkQCompileP(b *testing.B) {
|
|
|
|
qcompile, _ := NewCompiler(Config{})
|
2019-05-14 22:32:12 -04:00
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
b.ReportAllocs()
|
|
|
|
|
2019-06-01 02:03:09 -04:00
|
|
|
b.RunParallel(func(pb *testing.PB) {
|
|
|
|
for pb.Next() {
|
2019-10-14 02:51:36 -04:00
|
|
|
_, err := qcompile.Compile(gql, "user")
|
2019-05-14 22:32:12 -04:00
|
|
|
|
2019-06-01 02:03:09 -04:00
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
2019-05-14 22:32:12 -04:00
|
|
|
}
|
2019-06-01 02:03:09 -04:00
|
|
|
})
|
2019-05-14 22:32:12 -04:00
|
|
|
}
|