ref: master
test/unit/geo_ref_test.rb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# -*- coding: utf-8 -*- require 'test_helper' class GeoRefTest < ActiveSupport::TestCase ll = { salvador: [-12.9, -38.5], rio_de_janeiro: [-22.9, -43.1], new_york: [ 40.7, -74.0], tokyo: [ 35.6, 139.6] } should 'calculate the distance between lat,lng points' do assert_equal 1215, Noosfero::GeoRef.dist(*(ll[:salvador]+ll[:rio_de_janeiro])).round assert_equal 6998, Noosfero::GeoRef.dist(*(ll[:salvador]+ll[:new_york])).round assert_equal 17503, Noosfero::GeoRef.dist(*(ll[:salvador]+ll[:tokyo])).round end should 'calculate the distance between a lat,lng points and a profile' do env = fast_create Environment, name: 'SomeSite' @acme = Enterprise.create! environment: env, identifier: 'acme', name: 'ACME', city: 'Salvador', state: 'Bahia', country: 'BR', lat: -12.9, lng: -38.5 def sql_dist_to(ll) ApplicationRecord.connection.execute( "SELECT #{Noosfero::GeoRef.sql_dist ll[0], ll[1]} as dist" + " FROM profiles WHERE id = #{@acme.id};" ).first['dist'].to_f.round end assert_equal 1215, sql_dist_to(ll[:rio_de_janeiro]) assert_equal 6998, sql_dist_to(ll[:new_york]) assert_equal 17503, sql_dist_to(ll[:tokyo]) end def round_ll(ll) ll.map{|n| n.is_a?(Float) ? n.to_i : n } end should 'get lat/lng from address' do Rails.cache.clear Noosfero::GeoRef.stubs(:location_to_georef).with('Salvador, Bahia, BR').returns([-12, -38, :SUCCESS]) ll = Noosfero::GeoRef.location_to_georef 'Salvador, Bahia, BR' assert_equal [-12, -38, :SUCCESS], round_ll(ll) end should 'get and cache lat/lng from address' do Rails.cache.clear Noosfero::GeoRef.stubs(:location_to_georef).with('Curitiba, Paraná, BR').returns([-25, -49, :SUCCESS]) ll = Noosfero::GeoRef.location_to_georef 'Curitiba, Paraná, BR' assert_equal [-25, -49, :SUCCESS], round_ll(ll) Noosfero::GeoRef.stubs(:location_to_georef).with('Curitiba, Paraná, BR').returns([-25, -49, :SUCCESS, :CACHE]) ll = Noosfero::GeoRef.location_to_georef 'Curitiba, Paraná, BR' assert_equal [-25, -49, :SUCCESS, :CACHE], round_ll(ll) end should 'notify a non existent address' do Rails.cache.clear orig_env = ENV['RAILS_ENV'] ENV['RAILS_ENV'] = 'X' # cancel throw for test mode on process_rest_req. Noosfero::GeoRef.stubs(:location_to_georef).with('Nowhere, Nocountry, XYZ').returns([0, 0, :ZERO_RESULTS]) ll = Noosfero::GeoRef.location_to_georef 'Nowhere, Nocountry, XYZ' ENV['RAILS_ENV'] = orig_env # restore value to do not mess with other tests. assert_equal [0, 0, :ZERO_RESULTS], round_ll(ll) end end |