2019-03-24 14:57:29 +01:00
|
|
|
package qcode
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2019-05-13 01:27:26 +02:00
|
|
|
/*
|
2019-03-24 14:57:29 +01:00
|
|
|
func compareOp(op1, op2 Operation) error {
|
|
|
|
if op1.Type != op2.Type {
|
|
|
|
return errors.New("operator type mismatch")
|
|
|
|
}
|
|
|
|
|
|
|
|
if op1.Name != op2.Name {
|
|
|
|
return errors.New("operator name mismatch")
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(op1.Args) != len(op2.Args) {
|
|
|
|
return errors.New("operator args length mismatch")
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range op1.Args {
|
|
|
|
if !reflect.DeepEqual(op1.Args[i], op2.Args[i]) {
|
|
|
|
return fmt.Errorf("operator args: %v != %v", op1.Args[i], op2.Args[i])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(op1.Fields) != len(op2.Fields) {
|
|
|
|
return errors.New("operator field length mismatch")
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range op1.Fields {
|
|
|
|
if !reflect.DeepEqual(op1.Fields[i].Args, op2.Fields[i].Args) {
|
|
|
|
return fmt.Errorf("operator field args: %v != %v", op1.Fields[i].Args, op2.Fields[i].Args)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range op1.Fields {
|
|
|
|
if !reflect.DeepEqual(op1.Fields[i].Children, op2.Fields[i].Children) {
|
|
|
|
return fmt.Errorf("operator field fields: %v != %v", op1.Fields[i].Children, op2.Fields[i].Children)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2019-05-13 01:27:26 +02:00
|
|
|
*/
|
2019-03-24 14:57:29 +01:00
|
|
|
|
2019-04-20 16:45:12 +02:00
|
|
|
func TestCompile(t *testing.T) {
|
|
|
|
qcompile, _ := NewCompiler(Config{})
|
2019-06-15 04:17:21 +02:00
|
|
|
|
|
|
|
_, err := qcompile.CompileQuery([]byte(`
|
|
|
|
product(id: 15) {
|
2019-04-20 16:45:12 +02:00
|
|
|
id
|
|
|
|
name
|
2019-06-15 04:17:21 +02:00
|
|
|
}`))
|
|
|
|
|
2019-04-20 16:45:12 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-03-24 14:57:29 +01:00
|
|
|
}
|
2019-04-20 23:27:03 +02:00
|
|
|
|
2019-06-16 21:41:28 +02:00
|
|
|
func TestInvalidCompile1(t *testing.T) {
|
2019-04-21 01:42:08 +02:00
|
|
|
qcompile, _ := NewCompiler(Config{})
|
2019-06-15 04:17:21 +02:00
|
|
|
_, err := qcompile.CompileQuery([]byte(`#`))
|
|
|
|
|
2019-04-21 01:42:08 +02:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal(errors.New("expecting an error"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-16 21:41:28 +02:00
|
|
|
func TestInvalidCompile2(t *testing.T) {
|
|
|
|
qcompile, _ := NewCompiler(Config{})
|
|
|
|
_, err := qcompile.CompileQuery([]byte(`{u(where:{not:0})}`))
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal(errors.New("expecting an error"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-20 23:27:03 +02:00
|
|
|
func TestEmptyCompile(t *testing.T) {
|
|
|
|
qcompile, _ := NewCompiler(Config{})
|
2019-06-15 04:17:21 +02:00
|
|
|
_, err := qcompile.CompileQuery([]byte(``))
|
|
|
|
|
2019-04-20 23:27:03 +02:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal(errors.New("expecting an error"))
|
|
|
|
}
|
|
|
|
}
|
2019-05-15 04:32:12 +02:00
|
|
|
|
2019-06-15 04:17:21 +02:00
|
|
|
var gql = []byte(`
|
2019-06-01 08:03:09 +02: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-15 04:17:21 +02:00
|
|
|
}`)
|
2019-06-01 08:03:09 +02:00
|
|
|
|
2019-05-15 04:32:12 +02:00
|
|
|
func BenchmarkQCompile(b *testing.B) {
|
|
|
|
qcompile, _ := NewCompiler(Config{})
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
b.ReportAllocs()
|
|
|
|
|
|
|
|
for n := 0; n < b.N; n++ {
|
2019-06-01 08:03:09 +02:00
|
|
|
_, err := qcompile.CompileQuery(gql)
|
2019-05-15 04:32:12 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-01 08:03:09 +02:00
|
|
|
func BenchmarkQCompileP(b *testing.B) {
|
|
|
|
qcompile, _ := NewCompiler(Config{})
|
2019-05-15 04:32:12 +02:00
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
b.ReportAllocs()
|
|
|
|
|
2019-06-01 08:03:09 +02:00
|
|
|
b.RunParallel(func(pb *testing.PB) {
|
|
|
|
for pb.Next() {
|
|
|
|
_, err := qcompile.CompileQuery(gql)
|
2019-05-15 04:32:12 +02:00
|
|
|
|
2019-06-01 08:03:09 +02:00
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
2019-05-15 04:32:12 +02:00
|
|
|
}
|
2019-06-01 08:03:09 +02:00
|
|
|
})
|
2019-05-15 04:32:12 +02:00
|
|
|
}
|