123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597 |
- /*
- * 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 (
- "testing"
- )
- func TestQueryPagination(t *testing.T) {
- queryURL := "http://localhost" + TESTPORT + EndpointQuery
- st, _, res := sendTestRequest(queryURL+"//main?q=get+Song+with+ordering(ascending+key)&offset=p&limit=2", "GET", nil)
- if st != "400 Bad Request" || res != "Invalid parameter value: offset should be a positive integer number" {
- t.Error("Unexpected response:", st, res)
- return
- }
- st, _, res = sendTestRequest(queryURL+"//main?q=get+Song+with+ordering(ascending+key)&offset=2&limit=p", "GET", nil)
- if st != "400 Bad Request" || res != "Invalid parameter value: limit should be a positive integer number" {
- t.Error("Unexpected response:", st, res)
- return
- }
- st, h, res := sendTestRequest(queryURL+"//main?q=get+Song+with+ordering(ascending+key)&offset=2&limit=3", "GET", nil)
- if st != "200 OK" || res != `
- {
- "header": {
- "data": [
- "1:n:key",
- "1:n:name",
- "1:n:ranking"
- ],
- "format": [
- "auto",
- "auto",
- "auto"
- ],
- "labels": [
- "Song Key",
- "Song Name",
- "Ranking"
- ],
- "primary_kind": "Song"
- },
- "rows": [
- [
- "Aria3",
- "Aria3",
- 4
- ],
- [
- "Aria4",
- "Aria4",
- 18
- ],
- [
- "DeadSong2",
- "DeadSong2",
- 6
- ]
- ],
- "selections": [
- false,
- false,
- false
- ],
- "sources": [
- [
- "n:Song:Aria3",
- "n:Song:Aria3",
- "n:Song:Aria3"
- ],
- [
- "n:Song:Aria4",
- "n:Song:Aria4",
- "n:Song:Aria4"
- ],
- [
- "n:Song:DeadSong2",
- "n:Song:DeadSong2",
- "n:Song:DeadSong2"
- ]
- ],
- "total_selections": 0
- }`[1:] {
- t.Error("Unexpected response:", st, res)
- return
- }
- // Check header values
- if tc := h.Get(HTTPHeaderTotalCount); tc != "9" {
- t.Error("Unexpected total count:", tc)
- return
- }
- rid := h.Get(HTTPHeaderCacheID)
- if _, ok := ResultCache.Get(rid); !ok {
- t.Error("Given result id should be in the cache")
- return
- }
- st, _, res = sendTestRequest(queryURL+"//main?rid="+rid+"&offset=5&limit=0", "GET", nil)
- if st != "200 OK" || res != `
- {
- "header": {
- "data": [
- "1:n:key",
- "1:n:name",
- "1:n:ranking"
- ],
- "format": [
- "auto",
- "auto",
- "auto"
- ],
- "labels": [
- "Song Key",
- "Song Name",
- "Ranking"
- ],
- "primary_kind": "Song"
- },
- "rows": [],
- "selections": [],
- "sources": [],
- "total_selections": 0
- }`[1:] {
- t.Error("Unexpected response:", st, res)
- return
- }
- st, _, res = sendTestRequest(queryURL+"//main?rid="+rid+"&offset=5&limit=1", "GET", nil)
- if st != "200 OK" || res != `
- {
- "header": {
- "data": [
- "1:n:key",
- "1:n:name",
- "1:n:ranking"
- ],
- "format": [
- "auto",
- "auto",
- "auto"
- ],
- "labels": [
- "Song Key",
- "Song Name",
- "Ranking"
- ],
- "primary_kind": "Song"
- },
- "rows": [
- [
- "FightSong4",
- "FightSong4",
- 3
- ]
- ],
- "selections": [
- false
- ],
- "sources": [
- [
- "n:Song:FightSong4",
- "n:Song:FightSong4",
- "n:Song:FightSong4"
- ]
- ],
- "total_selections": 0
- }`[1:] {
- t.Error("Unexpected response:", st, res)
- return
- }
- st, _, res = sendTestRequest(queryURL+"//main?rid="+rid, "GET", nil)
- if st != "200 OK" || res != `
- {
- "header": {
- "data": [
- "1:n:key",
- "1:n:name",
- "1:n:ranking"
- ],
- "format": [
- "auto",
- "auto",
- "auto"
- ],
- "labels": [
- "Song Key",
- "Song Name",
- "Ranking"
- ],
- "primary_kind": "Song"
- },
- "rows": [
- [
- "Aria1",
- "Aria1",
- 8
- ],
- [
- "Aria2",
- "Aria2",
- 2
- ],
- [
- "Aria3",
- "Aria3",
- 4
- ],
- [
- "Aria4",
- "Aria4",
- 18
- ],
- [
- "DeadSong2",
- "DeadSong2",
- 6
- ],
- [
- "FightSong4",
- "FightSong4",
- 3
- ],
- [
- "LoveSong3",
- "LoveSong3",
- 1
- ],
- [
- "MyOnlySong3",
- "MyOnlySong3",
- 19
- ],
- [
- "StrangeSong1",
- "StrangeSong1",
- 5
- ]
- ],
- "selections": [
- false,
- false,
- false,
- false,
- false,
- false,
- false,
- false,
- false
- ],
- "sources": [
- [
- "n:Song:Aria1",
- "n:Song:Aria1",
- "n:Song:Aria1"
- ],
- [
- "n:Song:Aria2",
- "n:Song:Aria2",
- "n:Song:Aria2"
- ],
- [
- "n:Song:Aria3",
- "n:Song:Aria3",
- "n:Song:Aria3"
- ],
- [
- "n:Song:Aria4",
- "n:Song:Aria4",
- "n:Song:Aria4"
- ],
- [
- "n:Song:DeadSong2",
- "n:Song:DeadSong2",
- "n:Song:DeadSong2"
- ],
- [
- "n:Song:FightSong4",
- "n:Song:FightSong4",
- "n:Song:FightSong4"
- ],
- [
- "n:Song:LoveSong3",
- "n:Song:LoveSong3",
- "n:Song:LoveSong3"
- ],
- [
- "n:Song:MyOnlySong3",
- "n:Song:MyOnlySong3",
- "n:Song:MyOnlySong3"
- ],
- [
- "n:Song:StrangeSong1",
- "n:Song:StrangeSong1",
- "n:Song:StrangeSong1"
- ]
- ],
- "total_selections": 0
- }`[1:] {
- t.Error("Unexpected response:", st, res)
- return
- }
- _, _, res = sendTestRequest(queryURL+"//main?rid="+rid+"&offset=500&limit=1", "GET", nil)
- if res != "Offset exceeds available rows" {
- t.Error("Unexpected response:", res)
- return
- }
- _, _, res = sendTestRequest(queryURL+"//main?rid=abc&offset=5&limit=1", "GET", nil)
- if res != "Unknown result ID (rid parameter)" {
- t.Error("Unexpected response:", res)
- return
- }
- }
- func TestQuery(t *testing.T) {
- queryURL := "http://localhost" + TESTPORT + EndpointQuery
- // POST requests should not be allowed
- _, _, res := sendTestRequest(queryURL, "POST",
- []byte(`{"msg":"Hello!"}`))
- if res != "Method Not Allowed" {
- t.Error("Unexpected response:", res)
- return
- }
- // Test error message
- _, _, res = sendTestRequest(queryURL+"main", "GET", nil)
- if res != "Missing query (q parameter)" {
- t.Error("Unexpected response:", res)
- return
- }
- _, _, res = sendTestRequest(queryURL+"main/bla/bla?q=get+Song", "GET", nil)
- if res != "Invalid resource specification: bla/bla" {
- t.Error("Unexpected response:", res)
- return
- }
- _, _, res = sendTestRequest(queryURL+"/?q=get+Song", "GET", nil)
- if res != "Need a partition" {
- t.Error("Unexpected response:", res)
- return
- }
- _, _, res = sendTestRequest(queryURL+"main/?q=get+BLA", "GET", nil)
- if res != "EQL error in Main query: Unknown node kind (BLA) (Line:1 Pos:5)" {
- t.Error("Unexpected response:", res)
- return
- }
- // Test first real query
- st, _, res := sendTestRequest(queryURL+"//main?q=get+Song+with+ordering(ascending+key)", "GET", nil)
- st, _, res2 := sendTestRequest(queryURL+"//main?q=get+Song+with+ordering(ascending+key)&offset=0&limit=9", "GET", nil)
- if st != "200 OK" || res2 != res || res != `
- {
- "header": {
- "data": [
- "1:n:key",
- "1:n:name",
- "1:n:ranking"
- ],
- "format": [
- "auto",
- "auto",
- "auto"
- ],
- "labels": [
- "Song Key",
- "Song Name",
- "Ranking"
- ],
- "primary_kind": "Song"
- },
- "rows": [
- [
- "Aria1",
- "Aria1",
- 8
- ],
- [
- "Aria2",
- "Aria2",
- 2
- ],
- [
- "Aria3",
- "Aria3",
- 4
- ],
- [
- "Aria4",
- "Aria4",
- 18
- ],
- [
- "DeadSong2",
- "DeadSong2",
- 6
- ],
- [
- "FightSong4",
- "FightSong4",
- 3
- ],
- [
- "LoveSong3",
- "LoveSong3",
- 1
- ],
- [
- "MyOnlySong3",
- "MyOnlySong3",
- 19
- ],
- [
- "StrangeSong1",
- "StrangeSong1",
- 5
- ]
- ],
- "selections": [
- false,
- false,
- false,
- false,
- false,
- false,
- false,
- false,
- false
- ],
- "sources": [
- [
- "n:Song:Aria1",
- "n:Song:Aria1",
- "n:Song:Aria1"
- ],
- [
- "n:Song:Aria2",
- "n:Song:Aria2",
- "n:Song:Aria2"
- ],
- [
- "n:Song:Aria3",
- "n:Song:Aria3",
- "n:Song:Aria3"
- ],
- [
- "n:Song:Aria4",
- "n:Song:Aria4",
- "n:Song:Aria4"
- ],
- [
- "n:Song:DeadSong2",
- "n:Song:DeadSong2",
- "n:Song:DeadSong2"
- ],
- [
- "n:Song:FightSong4",
- "n:Song:FightSong4",
- "n:Song:FightSong4"
- ],
- [
- "n:Song:LoveSong3",
- "n:Song:LoveSong3",
- "n:Song:LoveSong3"
- ],
- [
- "n:Song:MyOnlySong3",
- "n:Song:MyOnlySong3",
- "n:Song:MyOnlySong3"
- ],
- [
- "n:Song:StrangeSong1",
- "n:Song:StrangeSong1",
- "n:Song:StrangeSong1"
- ]
- ],
- "total_selections": 0
- }`[1:] {
- t.Error("Unexpected response:", st, res)
- return
- }
- }
- func TestGroupingInfo(t *testing.T) {
- queryURL := "http://localhost" + TESTPORT + EndpointQuery
- st, _, res := sendTestRequest(queryURL+"//main?q=get+Song+with+ordering(ascending+key)&offset=2&limit=3&groups=1", "GET", nil)
- if st != "200 OK" || res != `
- {
- "groups": [
- [
- "Best"
- ],
- [],
- []
- ],
- "header": {
- "data": [
- "1:n:key",
- "1:n:name",
- "1:n:ranking"
- ],
- "format": [
- "auto",
- "auto",
- "auto"
- ],
- "labels": [
- "Song Key",
- "Song Name",
- "Ranking"
- ],
- "primary_kind": "Song"
- },
- "rows": [
- [
- "Aria3",
- "Aria3",
- 4
- ],
- [
- "Aria4",
- "Aria4",
- 18
- ],
- [
- "DeadSong2",
- "DeadSong2",
- 6
- ]
- ],
- "selections": [
- false,
- false,
- false
- ],
- "sources": [
- [
- "n:Song:Aria3",
- "n:Song:Aria3",
- "n:Song:Aria3"
- ],
- [
- "n:Song:Aria4",
- "n:Song:Aria4",
- "n:Song:Aria4"
- ],
- [
- "n:Song:DeadSong2",
- "n:Song:DeadSong2",
- "n:Song:DeadSong2"
- ]
- ],
- "total_selections": 0
- }`[1:] {
- t.Error("Unexpected response:", st, res)
- return
- }
- st, _, res = sendTestRequest(queryURL+"//main?q=get+Song+primary+'group'+with+ordering(ascending+key)&offset=2&limit=3&groups=1", "GET", nil)
- if st != "400 Bad Request" || res != "Could not determine key of primary node - query needs a primary expression" {
- t.Error("Unexpected response:", st, res)
- return
- }
- }
|