geo_location_test.rb 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. require 'integration_test_helper'
  2. require 'webmock/minitest'
  3. class GeoLocationTest < ActiveSupport::TestCase
  4. setup do
  5. @mock = true
  6. #WebMock.allow_net_connect!
  7. end
  8. # check
  9. test 'check simple results' do
  10. if @mock
  11. stub_request(:get, 'http://maps.googleapis.com/maps/api/geocode/json?address=Marienstrasse%2013,%2010117%20Berlin&sensor=true')
  12. .to_return(status: 200, body: '{"results":[{"geometry":{"location":{"lat": 52.5219143, "lng": 13.3832647}}}]}', headers: {})
  13. end
  14. result = Service::GeoLocation.geocode('Marienstrasse 13, 10117 Berlin')
  15. assert(result)
  16. assert_equal(52.5219143, result[0])
  17. assert_equal(13.3832647, result[1])
  18. if @mock
  19. stub_request(:get, 'http://maps.googleapis.com/maps/api/geocode/json?address=Marienstrasse%2013%2010117%20Berlin&sensor=true')
  20. .to_return(status: 200, body: '{"results":[{"geometry":{"location":{"lat": 52.5219143, "lng": 13.3832647}}}]}', headers: {})
  21. end
  22. result = Service::GeoLocation.geocode('Marienstrasse 13 10117 Berlin')
  23. assert(result)
  24. assert_equal(52.5219143, result[0])
  25. assert_equal(13.3832647, result[1])
  26. if @mock
  27. stub_request(:get, 'http://maps.googleapis.com/maps/api/geocode/json?address=Martinsbruggstrasse%2035,%209016%20St.%20Gallen&sensor=true')
  28. .to_return(status: 200, body: '{"results":[{"geometry":{"location":{"lat": 47.4366557, "lng": 9.4098904}}}]}', headers: {})
  29. end
  30. result = Service::GeoLocation.geocode('Martinsbruggstrasse 35, 9016 St. Gallen')
  31. assert(result)
  32. assert_equal(47.4366557, result[0])
  33. assert_equal(9.4098904, result[1])
  34. if @mock
  35. stub_request(:get, 'http://maps.googleapis.com/maps/api/geocode/json?address=Martinsbruggstrasse%2035%209016%20St.%20Gallen&sensor=true')
  36. .to_return(status: 200, body: '{"results":[{"geometry":{"location":{"lat": 47.4366557, "lng": 9.4098904}}}]}', headers: {})
  37. end
  38. result = Service::GeoLocation.geocode('Martinsbruggstrasse 35 9016 St. Gallen')
  39. assert(result)
  40. assert_equal(47.4366557, result[0])
  41. assert_equal(9.4098904, result[1])
  42. end
  43. test 'check user results' do
  44. if @mock
  45. stub_request(:get, 'http://maps.googleapis.com/maps/api/geocode/json?address=Marienstrasse%2013%2010117%20Berlin&sensor=true')
  46. .to_return(status: 200, body: '{"results":[{"geometry":{"location":{"lat": 52.5219143, "lng": 13.3832647}}}]}', headers: {})
  47. end
  48. user1 = User.create(
  49. login: 'some_geo_login1',
  50. firstname: 'First',
  51. lastname: 'Last',
  52. email: 'some_geo_login1@example.com',
  53. password: 'test',
  54. address: 'Marienstrasse 13 10117 Berlin',
  55. active: false,
  56. updated_by_id: 1,
  57. created_by_id: 1
  58. )
  59. assert(user1.preferences)
  60. assert(user1.preferences['lat'])
  61. assert(user1.preferences['lng'])
  62. assert_equal(52.5219143, user1.preferences['lat'])
  63. assert_equal(13.3832647, user1.preferences['lng'])
  64. if @mock
  65. stub_request(:get, 'http://maps.googleapis.com/maps/api/geocode/json?address=Marienstrasse%2013,%2010117,%20Berlin&sensor=true')
  66. .to_return(status: 200, body: '{"results":[{"geometry":{"location":{"lat": 52.5219143, "lng": 13.3832647}}}]}', headers: {})
  67. end
  68. user2 = User.create(
  69. login: 'some_geo_login2',
  70. firstname: 'First',
  71. lastname: 'Last',
  72. email: 'some_geo_login2@example.com',
  73. password: 'test',
  74. street: 'Marienstrasse 13',
  75. city: 'Berlin',
  76. zip: '10117',
  77. active: false,
  78. updated_by_id: 1,
  79. created_by_id: 1
  80. )
  81. assert(user2.preferences)
  82. assert(user2.preferences['lat'])
  83. assert(user2.preferences['lng'])
  84. assert_equal(52.5219143, user2.preferences['lat'])
  85. assert_equal(13.3832647, user2.preferences['lng'])
  86. if @mock
  87. stub_request(:get, 'http://maps.googleapis.com/maps/api/geocode/json?address=Martinsbruggstrasse%2035,%209016%20St.%20Gallen&sensor=true')
  88. .to_return(status: 200, body: '{"results":[{"geometry":{"location":{"lat": 47.4366557, "lng": 9.4098904}}}]}', headers: {})
  89. end
  90. user3 = User.create(
  91. login: 'some_geo_login3',
  92. firstname: 'First',
  93. lastname: 'Last',
  94. email: 'some_geo_login3@example.com',
  95. password: 'test',
  96. address: 'Martinsbruggstrasse 35, 9016 St. Gallen',
  97. active: false,
  98. updated_by_id: 1,
  99. created_by_id: 1
  100. )
  101. assert(user3.preferences)
  102. assert(user3.preferences['lat'])
  103. assert(user3.preferences['lng'])
  104. assert_equal(47.4366557, user3.preferences['lat'])
  105. assert_equal(9.4098904, user3.preferences['lng'])
  106. if @mock
  107. stub_request(:get, 'http://maps.googleapis.com/maps/api/geocode/json?address=Martinsbruggstrasse%2035,%209016,%20St.%20Gallen&sensor=true')
  108. .to_return(status: 200, body: '{"results":[{"geometry":{"location":{"lat": 47.4366557, "lng": 9.4098904}}}]}', headers: {})
  109. end
  110. user4 = User.create(
  111. login: 'some_geo_login4',
  112. firstname: 'First',
  113. lastname: 'Last',
  114. email: 'some_geo_login4@example.com',
  115. password: 'test',
  116. street: 'Martinsbruggstrasse 35',
  117. city: 'St. Gallen',
  118. zip: '9016',
  119. active: false,
  120. updated_by_id: 1,
  121. created_by_id: 1
  122. )
  123. assert(user4.preferences)
  124. assert(user4.preferences['lat'])
  125. assert(user4.preferences['lng'])
  126. assert_equal(47.4366557, user4.preferences['lat'])
  127. assert_equal(9.4098904, user4.preferences['lng'])
  128. end
  129. end