graphql_test.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 TestGraphQL(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. "foo": "bar1",
  25. }))
  26. gm.StoreNode("main", data.NewGraphNodeFromMap(map[string]interface{}{
  27. "key": "c",
  28. "kind": "b",
  29. "foo": "bar2",
  30. }))
  31. q := &GraphQLFunc{gm}
  32. if _, err := q.DocString(); err != nil {
  33. t.Error(err)
  34. return
  35. }
  36. if _, err := q.Run("", nil, nil, 0, []interface{}{""}); err == nil ||
  37. err.Error() != "Function requires at least 2 parameters: partition and query with optionally a map of variables and an operation name" {
  38. t.Error(err)
  39. return
  40. }
  41. if _, err := q.Run("", nil, nil, 0, []interface{}{"", "", ""}); err == nil ||
  42. err.Error() != "Third parameter must be a map" {
  43. t.Error(err)
  44. return
  45. }
  46. res, err := q.Run("", nil, nil, 0, []interface{}{"main",
  47. `query foo($x: string) { b(key:$x) { foo }}`, map[interface{}]interface{}{
  48. "x": "c",
  49. }, "foo"})
  50. if err != nil {
  51. t.Error(err)
  52. return
  53. }
  54. if fmt.Sprint(res) != "map[data:map[b:[map[foo:bar2]]]]" {
  55. t.Error("Unexpected result:", res)
  56. return
  57. }
  58. _, err = q.Run("", nil, nil, 0, []interface{}{"main", "aaaaa"})
  59. if err == nil || err.Error() != "Fatal GraphQL operation error in db.query: Missing operation (No executable expression found) (Line:1 Pos:1)" {
  60. t.Error(err)
  61. return
  62. }
  63. }