eql_test.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. * EliasDB
  3. *
  4. * Copyright 2016 Matthias Ladkau. All rights reserved.
  5. *
  6. * This Source Code Form is subject to the terms of the Mozilla Public
  7. * License, v. 2.0. If a copy of the MPL was not distributed with this
  8. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  9. */
  10. package rumble
  11. import (
  12. "fmt"
  13. "testing"
  14. "devt.de/krotik/eliasdb/api"
  15. "devt.de/krotik/eliasdb/graph"
  16. "devt.de/krotik/eliasdb/graph/data"
  17. "devt.de/krotik/eliasdb/graph/graphstorage"
  18. )
  19. func TestQuery(t *testing.T) {
  20. mr := &mockRuntime{}
  21. mgs := graphstorage.NewMemoryGraphStorage("mystorage")
  22. gm := graph.NewGraphManager(mgs)
  23. api.GM = gm
  24. gm.StoreNode("main", data.NewGraphNodeFromMap(map[string]interface{}{
  25. "key": "a",
  26. "kind": "b",
  27. }))
  28. gm.StoreNode("main", data.NewGraphNodeFromMap(map[string]interface{}{
  29. "key": "c",
  30. "kind": "d",
  31. }))
  32. q := &QueryFunc{}
  33. if q.Name() != "db.query" {
  34. t.Error("Unexpected result:", q.Name())
  35. return
  36. }
  37. if err := q.Validate(2, mr); err != nil {
  38. t.Error(err)
  39. return
  40. }
  41. if err := q.Validate(1, mr); err == nil || err.Error() != "Invalid construct Function query requires 2 parameters: partition and a query string" {
  42. t.Error(err)
  43. return
  44. }
  45. res, err := q.Execute([]interface{}{"main", "get b"}, nil, mr)
  46. if err != nil {
  47. t.Error(err)
  48. return
  49. }
  50. if res := res.(map[interface{}]interface{})["rows"]; fmt.Sprint(res) != "[[a]]" {
  51. t.Error("Unexpected result:", res)
  52. return
  53. }
  54. if res := res.(map[interface{}]interface{})["cols"]; fmt.Sprint(res) != "[B Key]" {
  55. t.Error("Unexpected result:", res)
  56. return
  57. }
  58. _, err = q.Execute([]interface{}{"main", "got b"}, nil, mr)
  59. if err == nil || err.Error() != "Invalid state EQL error in db.query: Invalid construct (Unknown query type: got) (Line:1 Pos:1)" {
  60. t.Error(err)
  61. return
  62. }
  63. }