github.com/unigraph-dev/dgraph@v1.1.1-0.20200923154953-8b52b426f765/gql/state_test.go (about) 1 /* 2 * Copyright 2015-2018 Dgraph Labs, Inc. and Contributors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package gql 18 19 import ( 20 "testing" 21 22 "github.com/stretchr/testify/require" 23 24 "github.com/dgraph-io/dgraph/lex" 25 ) 26 27 func TestQueryLexing(t *testing.T) { 28 input := ` 29 query { 30 me(_xid_: rick, id:10 ) { 31 name0 # my name 32 _city, # 0what would fail lex. 33 profilePic(width: 100, height: 100) 34 friends { 35 name 36 } 37 } 38 }` 39 l := &lex.Lexer{} 40 l.Reset(input) 41 l.Run(lexQuery) 42 43 it := l.NewIterator() 44 for it.Next() { 45 item := it.Item() 46 require.NotEqual(t, item.Typ, lex.ItemError) 47 t.Log(item.String()) 48 } 49 } 50 51 func TestMutationLexing(t *testing.T) { 52 input := ` 53 mutation { 54 set { 55 What is <this> . 56 Why is this #!!? 57 How is this? 58 } 59 delete { 60 Why is this 61 } 62 } 63 query { 64 me(_xid_: rick) { 65 _city 66 } 67 }` 68 l := &lex.Lexer{} 69 l.Reset(input) 70 l.Run(lexTopLevel) 71 it := l.NewIterator() 72 for it.Next() { 73 item := it.Item() 74 require.NotEqual(t, item.Typ, lex.ItemError, "Error: %v", item.String()) 75 t.Log(item.String()) 76 } 77 } 78 79 func TestNewSchemaQuery(t *testing.T) { 80 input := ` 81 schema { 82 pred 83 type 84 } 85 schema( pred : name ) { 86 pred 87 type 88 } 89 schema( pred : name,hi ) { #This is valid in lexer, parser would throw error 90 pred 91 type 92 } 93 schema( pred : [name,hi] ) { 94 pred 95 type 96 }` 97 l := lex.Lexer{ 98 Input: input, 99 } 100 l.Run(lexTopLevel) 101 it := l.NewIterator() 102 for it.Next() { 103 item := it.Item() 104 require.NotEqual(t, item.Typ, lex.ItemError) 105 t.Log(item.String()) 106 } 107 } 108 109 func TestAbruptSchemaQuery(t *testing.T) { 110 input := ` 111 schema { 112 pred 113 ` 114 l := lex.Lexer{ 115 Input: input, 116 } 117 l.Run(lexTopLevel) 118 var typ lex.ItemType 119 it := l.NewIterator() 120 for it.Next() { 121 item := it.Item() 122 t.Log(item.String()) 123 typ = item.Typ 124 } 125 require.Equal(t, lex.ItemError, typ) 126 } 127 128 func TestAbruptMutation(t *testing.T) { 129 input := ` 130 mutation { 131 set { 132 What is <this> . 133 Why is this #!!? 134 How is this? 135 }` 136 l := lex.Lexer{ 137 Input: input, 138 } 139 l.Run(lexTopLevel) 140 var typ lex.ItemType 141 it := l.NewIterator() 142 for it.Next() { 143 item := it.Item() 144 t.Log(item.String()) 145 typ = item.Typ 146 } 147 require.Equal(t, lex.ItemError, typ) 148 } 149 150 func TestVariables1(t *testing.T) { 151 input := ` 152 query testQuery($username: String!) { 153 me(_xid_: rick) { 154 _city 155 } 156 }` 157 l := lex.Lexer{ 158 Input: input, 159 } 160 l.Run(lexTopLevel) 161 it := l.NewIterator() 162 for it.Next() { 163 item := it.Item() 164 require.NotEqual(t, item.Typ, lex.ItemError) 165 t.Log(item.String(), item.Typ) 166 } 167 } 168 169 func TestVariables2(t *testing.T) { 170 input := ` 171 query testQuery ($username: String, $id: int, $email: string) { 172 me(_xid_: rick) { 173 _city 174 } 175 }` 176 l := lex.Lexer{ 177 Input: input, 178 } 179 l.Run(lexTopLevel) 180 it := l.NewIterator() 181 for it.Next() { 182 item := it.Item() 183 require.NotEqual(t, item.Typ, lex.ItemError) 184 t.Log(item.String(), item.Typ) 185 } 186 } 187 188 func TestVariablesDefault(t *testing.T) { 189 input := ` 190 query testQuery ($username: string = abc, $id: int = 5, $email: string) { 191 me(_xid_: rick) { 192 _city 193 } 194 }` 195 l := lex.Lexer{ 196 Input: input, 197 } 198 l.Run(lexTopLevel) 199 it := l.NewIterator() 200 for it.Next() { 201 item := it.Item() 202 require.NotEqual(t, item.Typ, lex.ItemError) 203 t.Log(item.String()) 204 } 205 } 206 207 func TestIRIRef(t *testing.T) { 208 input := ` 209 query testQuery { 210 me(id : <http://helloworld.com/how/are/you>) { 211 <http://verygood.com/what/about/you> 212 } 213 }` 214 l := lex.Lexer{ 215 Input: input, 216 } 217 l.Run(lexTopLevel) 218 it := l.NewIterator() 219 for it.Next() { 220 item := it.Item() 221 require.NotEqual(t, item.Typ, lex.ItemError) 222 t.Log(item.String()) 223 } 224 } 225 226 func TestLangSupport(t *testing.T) { 227 input := ` 228 query { 229 me(id: test) { 230 name@en 231 } 232 } 233 ` 234 l := lex.Lexer{ 235 Input: input, 236 } 237 l.Run(lexTopLevel) 238 it := l.NewIterator() 239 for it.Next() { 240 item := it.Item() 241 require.NotEqual(t, item.Typ, lex.ItemError) 242 t.Log(item.String()) 243 } 244 } 245 246 func TestMultiLangSupport(t *testing.T) { 247 input := ` 248 query { 249 me(id: test) { 250 name@en, name@en:ru:fr:de 251 } 252 } 253 ` 254 l := lex.Lexer{ 255 Input: input, 256 } 257 l.Run(lexTopLevel) 258 it := l.NewIterator() 259 for it.Next() { 260 item := it.Item() 261 require.NotEqual(t, item.Typ, lex.ItemError) 262 t.Log(item.String()) 263 } 264 } 265 266 func TestNumberInLang(t *testing.T) { 267 input := ` 268 { 269 q(func: eq(name@es-419, "aoeu")) { 270 name@. 271 } 272 } 273 ` 274 l := lex.Lexer{ 275 Input: input, 276 } 277 l.Run(lexTopLevel) 278 it := l.NewIterator() 279 gotEs := false 280 for it.Next() { 281 item := it.Item() 282 require.NotEqual(t, item.Typ, lex.ItemError) 283 if item.Val == "es-419" { 284 gotEs = true 285 } 286 } 287 require.True(t, gotEs) 288 }