eql_test.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 dbfunc
  11. import (
  12. "fmt"
  13. "testing"
  14. "devt.de/krotik/eliasdb/graph"
  15. "devt.de/krotik/eliasdb/graph/data"
  16. "devt.de/krotik/eliasdb/graph/graphstorage"
  17. )
  18. func TestQuery(t *testing.T) {
  19. mgs := graphstorage.NewMemoryGraphStorage("mystorage")
  20. gm := graph.NewGraphManager(mgs)
  21. gm.StoreNode("main", data.NewGraphNodeFromMap(map[string]interface{}{
  22. "key": "a",
  23. "kind": "b",
  24. }))
  25. gm.StoreNode("main", data.NewGraphNodeFromMap(map[string]interface{}{
  26. "key": "c",
  27. "kind": "d",
  28. }))
  29. q := &QueryFunc{gm}
  30. if _, err := q.DocString(); err != nil {
  31. t.Error(err)
  32. return
  33. }
  34. if _, err := q.Run("", nil, nil, 0, []interface{}{""}); err == nil ||
  35. err.Error() != "Function requires 2 parameters: partition and a query string" {
  36. t.Error(err)
  37. return
  38. }
  39. res, err := q.Run("", nil, nil, 0, []interface{}{"main", "get b"})
  40. if err != nil {
  41. t.Error(err)
  42. return
  43. }
  44. if res := res.(map[interface{}]interface{})["rows"]; fmt.Sprint(res) != "[[a]]" {
  45. t.Error("Unexpected result:", res)
  46. return
  47. }
  48. if res := res.(map[interface{}]interface{})["cols"]; fmt.Sprint(res) != "[B Key]" {
  49. t.Error("Unexpected result:", res)
  50. return
  51. }
  52. _, err = q.Run("", nil, nil, 0, []interface{}{"main", "got b"})
  53. if err == nil || err.Error() != "EQL error in db.query: Invalid construct (Unknown query type: got) (Line:1 Pos:1)" {
  54. t.Error(err)
  55. return
  56. }
  57. }