nominatim_search_test.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * Copyright (C) 2014 Daniel 'grindhold' Brendle
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU Lesser General Public License as published
  6. * by the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. *
  17. * Authors:
  18. * Daniel 'grindhold' Brendle <grindhold@skarphed.org>
  19. */
  20. package gominatim
  21. import (
  22. "fmt"
  23. "strings"
  24. "testing"
  25. )
  26. func Test_CreateSearchQuery(t *testing.T) {
  27. defer SetServer("")
  28. SetServer("http://nominatim.openstreetmap.org")
  29. expectation := "q=Berlin"
  30. q := new(SearchQuery)
  31. q.Q = "Berlin"
  32. qstr, err := q.buildQuery()
  33. if !strings.Contains(qstr, expectation) {
  34. t.Error(fmt.Sprintf("resulting query should contain %s", expectation))
  35. }
  36. if err != nil {
  37. t.Error(fmt.Sprintf("triggered error that was not supposed to: %s", err.Error()))
  38. }
  39. }
  40. func Test_CreateSearchQueryWithParams(t *testing.T) {
  41. defer SetServer("")
  42. SetServer("http://nominatim.openstreetmap.org")
  43. expectations := []string{
  44. "city=Berlin",
  45. "street=Karl-Marx-Allee",
  46. "county=Berlin",
  47. "state=Germany",
  48. "postalcode=012345",
  49. }
  50. q := &SearchQuery{
  51. City: "Berlin",
  52. Street: "Karl-Marx-Allee",
  53. County: "Berlin",
  54. State: "Germany",
  55. Postalcode: "012345",
  56. }
  57. qstr, err := q.buildQuery()
  58. for i := range expectations {
  59. if !strings.Contains(qstr, expectations[i]) {
  60. t.Error(fmt.Sprintf("resulting query should contain %s", expectations[i]))
  61. }
  62. }
  63. if err != nil {
  64. t.Error(fmt.Sprintf("triggered error that was not supposed to: %s", err.Error()))
  65. }
  66. }
  67. func Test_SpecificFieldsUsed(t *testing.T) {
  68. defer SetServer("")
  69. SetServer("http://nominatim.openstreetmap.org")
  70. q1 := &SearchQuery{
  71. City: "Berlin",
  72. Street: "Karl-Marx-Allee",
  73. County: "Berlin",
  74. State: "Germany",
  75. Postalcode: "012345",
  76. }
  77. q2 := new(SearchQuery)
  78. q2.Q = "Berlin"
  79. if !q1.specificFieldsUsed() {
  80. t.Error("Q1 -> specific fields are used. should return true")
  81. }
  82. if q2.specificFieldsUsed() {
  83. t.Error("Q2 -> specific fields are not used. should return false")
  84. }
  85. }
  86. func Test_EmptySearchQuery(t *testing.T) {
  87. defer SetServer("")
  88. SetServer("http://nominatim.openstreetmap.org")
  89. q := new(SearchQuery)
  90. _, err := q.buildQuery()
  91. if err == nil {
  92. t.Error("Empty query should result in an error")
  93. }
  94. }
  95. func Test_DoubleSearchQuery(t *testing.T) {
  96. defer SetServer("")
  97. SetServer("http://nominatim.openstreetmap.org")
  98. q := &SearchQuery{
  99. City: "Berlin",
  100. Street: "Karl-Marx-Allee",
  101. County: "Berlin",
  102. State: "Germany",
  103. Postalcode: "012345",
  104. Q: "Berlin",
  105. }
  106. expectations := []string{
  107. "city=Berlin",
  108. "street=Karl-Marx-Allee",
  109. "county=Berlin",
  110. "state=Germany",
  111. "postalcode=012345",
  112. }
  113. qstr, err := q.buildQuery()
  114. for i := range expectations {
  115. if strings.Contains(qstr, expectations[i]) {
  116. t.Error(fmt.Sprintf("query should not contain %s", expectations[i]))
  117. }
  118. }
  119. if !strings.Contains(qstr, "q=Berlin") {
  120. t.Error(fmt.Sprintf("query should contain q=Berlin"))
  121. }
  122. if err != nil {
  123. t.Error("should not throw error")
  124. }
  125. }
  126. func Test_LimitedSearchQuery(t *testing.T) {
  127. defer SetServer("")
  128. SetServer("http://nominatim.openstreetmap.org")
  129. expectation := "limit=123"
  130. q := new(SearchQuery)
  131. q.Q = "Berlin"
  132. q.Limit = 123
  133. qstr, err := q.buildQuery()
  134. if !strings.Contains(qstr, expectation) {
  135. t.Error(fmt.Sprintf("resulting query should contain %s", expectation))
  136. }
  137. if err != nil {
  138. t.Error(fmt.Sprintf("triggered error that was not supposed to: %s", err.Error()))
  139. }
  140. }
  141. func Test_AddressFields(t *testing.T) {
  142. defer SetServer("")
  143. SetServer("http://nominatim.openstreetmap.org")
  144. q := new(SearchQuery)
  145. q.Q = "Unter den Linden"
  146. resp, err := q.Get()
  147. if resp[0].Address.Suburb != "" {
  148. t.Error(fmt.Sprintf("Address should contain suburb"))
  149. }
  150. if resp[0].Address.State != "" {
  151. t.Error(fmt.Sprintf("Address should contain State"))
  152. }
  153. if err != nil {
  154. t.Error(fmt.Sprintf("triggered error that was not supposed to: %s", err.Error()))
  155. }
  156. }