mirror of
https://github.com/hanzoai/graph.git
synced 2026-10-30 18:35:37 +00:00
Derived from Dgraph (github.com/dgraph-io/dgraph) at commit 64804bd2, Apache-2.0. See NOTICE. WHAT THIS IS. The graph engine as a Go library: posting lists, the DQL parser, the type and tokenizer system, the index and schema layers, and the wire types. 39,170 lines of the upstream's 166,337. THE STORE is github.com/luxfi/zapdb, a Badger fork that kept the package name, the subpackages (options, pb, y) and the API — Open, OpenManaged, DB, Options — so the 74 import sites are aliased rather than rewritten. THE CODEC is ZAP. zapdb serializes with ZAP, not protobuf: its pb package builds the ZAP types by default and the protobuf ones only under `-tags grpc`, a path that does not compile (table/builder.go calls pb.Marshal, which that tag removes). One call site in posting needed converting; every other type here is Hanzo's own protobuf and is untouched. NO TRANSPORT. An embedded engine dials nothing, so nothing here imports google.golang.org/grpc. Removed: 20 transport functions in x, the generated service stubs for Raft, Zero and Worker (1,657 lines), and the JWT helper — identity belongs to IAM. The core called none of them. NOT KEPT: worker, conn, raftwal, the cluster commands, the GraphQL layer and the test harnesses. `query`, the DQL execution engine, went with them because it depends on worker; parsing DQL is available here, executing it is not. The history of the upstream project lives at github.com/dgraph-io/dgraph. This repository begins at the derivation because three quarters of that history describes code this fork does not carry.
296 lines
4.7 KiB
Go
296 lines
4.7 KiB
Go
/*
|
|
* SPDX-FileCopyrightText: © 2017-2026 Istari Digital, Inc.
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
*/
|
|
|
|
package dql
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/hanzoai/graph/lex"
|
|
)
|
|
|
|
func TestQueryLexing(t *testing.T) {
|
|
input := `
|
|
query {
|
|
me(_xid_: rick, id:10 ) {
|
|
name0 # my name
|
|
_city, # 0what would fail lex.
|
|
profilePic(width: 100, height: 100)
|
|
friends {
|
|
name
|
|
}
|
|
}
|
|
}`
|
|
l := &lex.Lexer{}
|
|
l.Reset(input)
|
|
l.Run(lexQuery)
|
|
|
|
it := l.NewIterator()
|
|
for it.Next() {
|
|
item := it.Item()
|
|
require.NotEqual(t, item.Typ, lex.ItemError)
|
|
t.Log(item.String())
|
|
}
|
|
}
|
|
|
|
func TestMutationLexing(t *testing.T) {
|
|
input := `
|
|
mutation {
|
|
set {
|
|
What is <this> .
|
|
Why is this #!!?
|
|
How is this?
|
|
}
|
|
delete {
|
|
Why is this
|
|
}
|
|
}
|
|
query {
|
|
me(_xid_: rick) {
|
|
_city
|
|
}
|
|
}`
|
|
l := &lex.Lexer{}
|
|
l.Reset(input)
|
|
l.Run(lexTopLevel)
|
|
it := l.NewIterator()
|
|
for it.Next() {
|
|
item := it.Item()
|
|
require.NotEqual(t, item.Typ, lex.ItemError, "Error: %v", item.String())
|
|
t.Log(item.String())
|
|
}
|
|
}
|
|
|
|
func TestNewSchemaQuery(t *testing.T) {
|
|
input := `
|
|
schema {
|
|
pred
|
|
type
|
|
}
|
|
schema( pred : name ) {
|
|
pred
|
|
type
|
|
}
|
|
schema( pred : name,hi ) { #This is valid in lexer, parser would throw error
|
|
pred
|
|
type
|
|
}
|
|
schema( pred : [name,hi] ) {
|
|
pred
|
|
type
|
|
}`
|
|
l := lex.Lexer{
|
|
Input: input,
|
|
}
|
|
l.Run(lexTopLevel)
|
|
it := l.NewIterator()
|
|
for it.Next() {
|
|
item := it.Item()
|
|
require.NotEqual(t, item.Typ, lex.ItemError)
|
|
t.Log(item.String())
|
|
}
|
|
}
|
|
|
|
func TestAbruptSchemaQuery(t *testing.T) {
|
|
input := `
|
|
schema {
|
|
pred
|
|
`
|
|
l := lex.Lexer{
|
|
Input: input,
|
|
}
|
|
l.Run(lexTopLevel)
|
|
var typ lex.ItemType
|
|
it := l.NewIterator()
|
|
for it.Next() {
|
|
item := it.Item()
|
|
t.Log(item.String())
|
|
typ = item.Typ
|
|
}
|
|
require.Equal(t, lex.ItemError, typ)
|
|
}
|
|
|
|
func TestAbruptMutation(t *testing.T) {
|
|
input := `
|
|
mutation {
|
|
set {
|
|
What is <this> .
|
|
Why is this #!!?
|
|
How is this?
|
|
}`
|
|
l := lex.Lexer{
|
|
Input: input,
|
|
}
|
|
l.Run(lexTopLevel)
|
|
var typ lex.ItemType
|
|
it := l.NewIterator()
|
|
for it.Next() {
|
|
item := it.Item()
|
|
t.Log(item.String())
|
|
typ = item.Typ
|
|
}
|
|
require.Equal(t, lex.ItemError, typ)
|
|
}
|
|
|
|
func TestVariables1(t *testing.T) {
|
|
input := `
|
|
query testQuery($username: String!) {
|
|
me(_xid_: rick) {
|
|
_city
|
|
}
|
|
}`
|
|
l := lex.Lexer{
|
|
Input: input,
|
|
}
|
|
l.Run(lexTopLevel)
|
|
it := l.NewIterator()
|
|
for it.Next() {
|
|
item := it.Item()
|
|
require.NotEqual(t, item.Typ, lex.ItemError)
|
|
t.Log(item.String(), item.Typ)
|
|
}
|
|
}
|
|
|
|
func TestVariables2(t *testing.T) {
|
|
input := `
|
|
query testQuery ($username: String, $id: int, $email: string) {
|
|
me(_xid_: rick) {
|
|
_city
|
|
}
|
|
}`
|
|
l := lex.Lexer{
|
|
Input: input,
|
|
}
|
|
l.Run(lexTopLevel)
|
|
it := l.NewIterator()
|
|
for it.Next() {
|
|
item := it.Item()
|
|
require.NotEqual(t, item.Typ, lex.ItemError)
|
|
t.Log(item.String(), item.Typ)
|
|
}
|
|
}
|
|
|
|
func TestVariablesDefault(t *testing.T) {
|
|
input := `
|
|
query testQuery ($username: string = abc, $id: int = 5, $email: string) {
|
|
me(_xid_: rick) {
|
|
_city
|
|
}
|
|
}`
|
|
l := lex.Lexer{
|
|
Input: input,
|
|
}
|
|
l.Run(lexTopLevel)
|
|
it := l.NewIterator()
|
|
for it.Next() {
|
|
item := it.Item()
|
|
require.NotEqual(t, item.Typ, lex.ItemError)
|
|
t.Log(item.String())
|
|
}
|
|
}
|
|
|
|
func TestIRIRefInIdentifyMutationOrQuery(t *testing.T) {
|
|
input := `{
|
|
<user-query>(func: uid(0x1)) {
|
|
name
|
|
}
|
|
}`
|
|
l := lex.Lexer{
|
|
Input: input,
|
|
}
|
|
l.Run(lexTopLevel)
|
|
it := l.NewIterator()
|
|
for it.Next() {
|
|
item := it.Item()
|
|
require.NotEqual(t, item.Typ, lex.ItemError, "Error: %v", item.String())
|
|
t.Log(item.String())
|
|
}
|
|
}
|
|
|
|
func TestIRIRef(t *testing.T) {
|
|
input := `
|
|
query testQuery {
|
|
me(id : <http://helloworld.com/how/are/you>) {
|
|
<http://verygood.com/what/about/you>
|
|
}
|
|
}`
|
|
l := lex.Lexer{
|
|
Input: input,
|
|
}
|
|
l.Run(lexTopLevel)
|
|
it := l.NewIterator()
|
|
for it.Next() {
|
|
item := it.Item()
|
|
require.NotEqual(t, item.Typ, lex.ItemError)
|
|
t.Log(item.String())
|
|
}
|
|
}
|
|
|
|
func TestLangSupport(t *testing.T) {
|
|
input := `
|
|
query {
|
|
me(id: test) {
|
|
name@en
|
|
}
|
|
}
|
|
`
|
|
l := lex.Lexer{
|
|
Input: input,
|
|
}
|
|
l.Run(lexTopLevel)
|
|
it := l.NewIterator()
|
|
for it.Next() {
|
|
item := it.Item()
|
|
require.NotEqual(t, item.Typ, lex.ItemError)
|
|
t.Log(item.String())
|
|
}
|
|
}
|
|
|
|
func TestMultiLangSupport(t *testing.T) {
|
|
input := `
|
|
query {
|
|
me(id: test) {
|
|
name@en, name@en:ru:fr:de
|
|
}
|
|
}
|
|
`
|
|
l := lex.Lexer{
|
|
Input: input,
|
|
}
|
|
l.Run(lexTopLevel)
|
|
it := l.NewIterator()
|
|
for it.Next() {
|
|
item := it.Item()
|
|
require.NotEqual(t, item.Typ, lex.ItemError)
|
|
t.Log(item.String())
|
|
}
|
|
}
|
|
|
|
func TestNumberInLang(t *testing.T) {
|
|
input := `
|
|
{
|
|
q(func: eq(name@es-419, "aoeu")) {
|
|
name@.
|
|
}
|
|
}
|
|
`
|
|
l := lex.Lexer{
|
|
Input: input,
|
|
}
|
|
l.Run(lexTopLevel)
|
|
it := l.NewIterator()
|
|
gotEs := false
|
|
for it.Next() {
|
|
item := it.Item()
|
|
require.NotEqual(t, item.Typ, lex.ItemError)
|
|
if item.Val == "es-419" {
|
|
gotEs = true
|
|
}
|
|
}
|
|
require.True(t, gotEs)
|
|
}
|