index_test.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 v1
  11. import (
  12. "strings"
  13. "testing"
  14. "devt.de/krotik/eliasdb/graph"
  15. "devt.de/krotik/eliasdb/storage"
  16. )
  17. func TestIndexQuery(t *testing.T) {
  18. queryURL := "http://localhost" + TESTPORT + EndpointIndexQuery
  19. st, _, res := sendTestRequest(queryURL+"//main/x/", "GET", nil)
  20. if st != "400 Bad Request" || res != "Need a partition, entity type (n or e) and a kind" {
  21. t.Error("Unexpected response:", st, res)
  22. return
  23. }
  24. st, _, res = sendTestRequest(queryURL+"//main/x/bla", "GET", nil)
  25. if st != "400 Bad Request" || res != "Entity type must be n (nodes) or e (edges)" {
  26. t.Error("Unexpected response:", st, res)
  27. return
  28. }
  29. st, _, res = sendTestRequest(queryURL+"//main/n/bla?attr=1", "GET", nil)
  30. if st != "400 Bad Request" || res != "Unknown partition or node kind" {
  31. t.Error("Unexpected response:", st, res)
  32. return
  33. }
  34. st, _, res = sendTestRequest(queryURL+"//main/n/Song", "GET", nil)
  35. if st != "400 Bad Request" || res != "Query string for attr (attribute) is required" {
  36. t.Error("Unexpected response:", st, res)
  37. return
  38. }
  39. st, _, res = sendTestRequest(queryURL+"//main/n/Song?attr=1", "GET", nil)
  40. if st != "400 Bad Request" || res != "Query string for either phrase, word or value is required" {
  41. t.Error("Unexpected response:", st, res)
  42. return
  43. }
  44. _, _, res = sendTestRequest(queryURL+"//main/n/Song?attr=1&word=1", "GET", nil)
  45. if res != "{}" {
  46. t.Error("Unexpected response:", st, res)
  47. return
  48. }
  49. _, _, res = sendTestRequest(queryURL+"//main/n/Song?attr=1&phrase=1", "GET", nil)
  50. if res != "[]" {
  51. t.Error("Unexpected response:", st, res)
  52. return
  53. }
  54. _, _, res = sendTestRequest(queryURL+"//main/n/Song?attr=1&value=1", "GET", nil)
  55. if res != "[]" {
  56. t.Error("Unexpected response:", st, res)
  57. return
  58. }
  59. _, _, res = sendTestRequest(queryURL+"//main/n/Song?attr=name&value=Aria1", "GET", nil)
  60. if res != `
  61. [
  62. "Aria1"
  63. ]`[1:] {
  64. t.Error("Unexpected response:", res)
  65. return
  66. }
  67. _, _, res = sendTestRequest(queryURL+"//main/n/Song?attr=name&phrase=Aria1", "GET", nil)
  68. if res != `
  69. [
  70. "Aria1"
  71. ]`[1:] {
  72. t.Error("Unexpected response:", res)
  73. return
  74. }
  75. _, _, res = sendTestRequest(queryURL+"//main/n/Song?attr=name&word=Aria1", "GET", nil)
  76. if res != `
  77. {
  78. "Aria1": [
  79. 1
  80. ]
  81. }`[1:] {
  82. t.Error("Unexpected response:", res)
  83. return
  84. }
  85. _, _, res = sendTestRequest(queryURL+"//main/e/Wrote?attr=number&word=1", "GET", nil)
  86. if res != `
  87. {
  88. "Aria1": [
  89. 1
  90. ],
  91. "StrangeSong1": [
  92. 1
  93. ]
  94. }`[1:] {
  95. t.Error("Unexpected response:", res)
  96. return
  97. }
  98. msm := gmMSM.StorageManager("main"+"Song"+graph.StorageSuffixNodesIndex,
  99. true).(*storage.MemoryStorageManager)
  100. for i := 2; i < 30; i++ {
  101. msm.AccessMap[uint64(i)] = storage.AccessCacheAndFetchError
  102. }
  103. st, _, res = sendTestRequest(queryURL+"//main/n/Song?attr=name&value=Aria1", "GET", nil)
  104. if st != "500 Internal Server Error" ||
  105. strings.HasPrefix(res, "GraphError: Failed to access graph storage component (Slot not found (mystorage/mainSong.nodeidx - Location") {
  106. t.Error("Unexpected response:", res)
  107. return
  108. }
  109. for i := 2; i < 30; i++ {
  110. delete(msm.AccessMap, uint64(i))
  111. }
  112. msm.AccessMap[1] = storage.AccessCacheAndFetchError
  113. st, _, res = sendTestRequest(queryURL+"//main/n/Song?attr=name&value=Aria1", "GET", nil)
  114. if st != "500 Internal Server Error" ||
  115. res != "GraphError: Failed to access graph storage component (Slot not found (mystorage/mainSong.nodeidx - Location:1))" {
  116. t.Error("Unexpected response:", res)
  117. return
  118. }
  119. delete(msm.AccessMap, 1)
  120. }