123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353 |
- /*
- * EliasDB
- *
- * Copyright 2016 Matthias Ladkau. All rights reserved.
- *
- * This Source Code Form is subject to the terms of the Mozilla Public
- * License, v. 2.0. If a copy of the MPL was not distributed with this
- * file, You can obtain one at http://mozilla.org/MPL/2.0/.
- */
- package v1
- import (
- "bytes"
- "encoding/json"
- "testing"
- )
- func TestEql(t *testing.T) {
- queryURL := "http://localhost" + TESTPORT + EndpointEql
- // Test error messages
- _, _, res := sendTestRequest(queryURL, "POST", nil)
- if res != "Could not decode request body: EOF" {
- t.Error("Unexpected response:", res)
- return
- }
- _, _, res = sendTestRequest(queryURL+"main/n", "POST", []byte(`
- {
- "foo" : "bar"
- }
- `[1:]))
- if res != "Need either a query or an ast parameter" {
- t.Error("Unexpected response:", res)
- return
- }
- _, _, res = sendTestRequest(queryURL+"main/n", "POST", []byte(`
- {
- "query" : "get =bla where foo = 'bar'"
- }
- `[1:]))
- if res != "Parse error in request: Lexical error (Invalid node kind '=bla' - can only contain [a-zA-Z0-9_]) (Line:1 Pos:5)" {
- t.Error("Unexpected response:", res)
- return
- }
- _, _, res = sendTestRequest(queryURL+"main/n", "POST", []byte(`
- {
- "ast" : "foobar"
- }
- `[1:]))
- if res != "Plain AST object expected as 'ast' value" {
- t.Error("Unexpected response:", res)
- return
- }
- _, _, res = sendTestRequest(queryURL+"main/n", "POST", []byte(`
- {
- "ast" : {
- "foo" : "bar"
- }
- }
- `[1:]))
- if res != "Found plain ast node without a name: map[foo:bar]" {
- t.Error("Unexpected response:", res)
- return
- }
- _, _, res = sendTestRequest(queryURL+"main/n", "POST", []byte(`
- {
- "ast" : {
- "name" : "foo",
- "value" : "bar"
- }
- }
- `[1:]))
- if res != "Could not find template for foo (tempkey: foo)" {
- t.Error("Unexpected response:", res)
- return
- }
- // Test parsing and pretty printing
- _, _, res = sendTestRequest(queryURL+"main/n", "POST", []byte(`
- {
- "query": "get bla where foo = bar \nwith\n ordering(ascending name)"
- }
- `[1:]))
- if res != `
- {
- "ast": {
- "children": [
- {
- "name": "value",
- "value": "bla"
- },
- {
- "children": [
- {
- "children": [
- {
- "name": "value",
- "value": "foo"
- },
- {
- "name": "value",
- "value": "bar"
- }
- ],
- "name": "=",
- "value": "="
- }
- ],
- "name": "where",
- "value": "where"
- },
- {
- "children": [
- {
- "children": [
- {
- "children": [
- {
- "name": "value",
- "value": "name"
- }
- ],
- "name": "asc",
- "value": "ascending"
- }
- ],
- "name": "ordering",
- "value": "ordering"
- }
- ],
- "name": "with",
- "value": "with"
- }
- ],
- "name": "get",
- "value": "get"
- }
- }`[1:] {
- t.Error("Unexpected response:", res)
- return
- }
- var astInput map[string]interface{}
- var astText bytes.Buffer
- json.NewDecoder(bytes.NewBufferString(res)).Decode(&astInput)
- json.NewEncoder(&astText).Encode(astInput)
- _, _, res = sendTestRequest(queryURL, "POST", astText.Bytes())
- if res != `
- {
- "query": "get bla where foo = bar \nwith\n ordering(ascending name)"
- }`[1:] {
- t.Error("Unexpected result:", res)
- return
- }
- }
- func TestEqlSpecial(t *testing.T) {
- queryURL := "http://localhost" + TESTPORT + EndpointEql
- // And and or AST nodes might have more than 2 children
- res := `
- {
- "ast": {
- "children": [
- {
- "name": "value",
- "value": "bla"
- },
- {
- "children": [
- {
- "name": "and",
- "value": "and",
- "children": [
- {
- "children": [
- {
- "name": "value",
- "value": "foo1"
- },
- {
- "name": "value",
- "value": "bar1"
- }
- ],
- "name": "=",
- "value": "="
- }, {
- "children": [
- {
- "name": "value",
- "value": "foo2"
- },
- {
- "name": "value",
- "value": "bar2"
- }
- ],
- "name": "=",
- "value": "="
- }, {
- "children": [
- {
- "name": "value",
- "value": "foo3"
- },
- {
- "name": "value",
- "value": "bar3"
- }
- ],
- "name": "=",
- "value": "="
- }
- ]
- }
- ],
- "name": "where",
- "value": "where"
- }
- ],
- "name": "get",
- "value": "get"
- }
- }`[1:]
- var astInput map[string]interface{}
- var astText bytes.Buffer
- err := json.NewDecoder(bytes.NewBufferString(res)).Decode(&astInput)
- if err != nil {
- t.Error(err)
- return
- }
- json.NewEncoder(&astText).Encode(astInput)
- _, _, res = sendTestRequest(queryURL, "POST", astText.Bytes())
- if res != `
- {
- "query": "get bla where foo1 = bar1 and foo2 = bar2 and foo3 = bar3"
- }`[1:] {
- t.Error("Unexpected result:", res)
- return
- }
- res = `
- {
- "ast": {
- "children": [
- {
- "name": "value",
- "value": "bla"
- },
- {
- "children": [
- {
- "name": "or",
- "value": "or",
- "children": [
- {
- "children": [
- {
- "name": "value",
- "value": "foo1"
- },
- {
- "name": "value",
- "value": "bar1"
- }
- ],
- "name": "=",
- "value": "="
- }, {
- "children": [
- {
- "name": "value",
- "value": "foo2"
- },
- {
- "name": "value",
- "value": "bar2"
- }
- ],
- "name": "=",
- "value": "="
- }, {
- "children": [
- {
- "name": "value",
- "value": "foo3"
- },
- {
- "name": "value",
- "value": "bar3"
- }
- ],
- "name": "=",
- "value": "="
- }
- ]
- }
- ],
- "name": "where",
- "value": "where"
- }
- ],
- "name": "get",
- "value": "get"
- }
- }`[1:]
- err = json.NewDecoder(bytes.NewBufferString(res)).Decode(&astInput)
- if err != nil {
- t.Error(err)
- return
- }
- astText = bytes.Buffer{}
- json.NewEncoder(&astText).Encode(astInput)
- _, _, res = sendTestRequest(queryURL, "POST", astText.Bytes())
- if res != `
- {
- "query": "get bla where foo1 = bar1 or foo2 = bar2 or foo3 = bar3"
- }`[1:] {
- t.Error("Unexpected result:", res)
- return
- }
- }
|