File: commands_on_geo_test.rb

package info (click to toggle)
ruby-redis 5.4.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,168 kB
  • sloc: ruby: 11,501; makefile: 117; sh: 24
file content (103 lines) | stat: -rw-r--r-- 3,417 bytes parent folder | download | duplicates (2)
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# frozen_string_literal: true

require "helper"

class TestCommandsGeo < Minitest::Test
  include Helper::Client

  def setup
    super

    added_items_count = r.geoadd("Sicily", 13.361389, 38.115556, "Palermo", 15.087269, 37.502669, "Catania")
    assert_equal 2, added_items_count
  end

  def test_geoadd_with_array_params
    added_items_count = r.geoadd("SicilyArray", [13.361389, 38.115556, "Palermo", 15.087269, 37.502669, "Catania"])
    assert_equal 2, added_items_count
  end

  def test_georadius_with_same_params
    r.geoadd("Chad", 15, 15, "Kanem")
    nearest_cities = r.georadius("Chad", 15, 15, 15, 'km', sort: 'asc')
    assert_equal %w(Kanem), nearest_cities
  end

  def test_georadius_with_sort
    nearest_cities = r.georadius("Sicily", 15, 37, 200, 'km', sort: 'asc')
    assert_equal %w(Catania Palermo), nearest_cities

    farthest_cities = r.georadius("Sicily", 15, 37, 200, 'km', sort: 'desc')
    assert_equal %w(Palermo Catania), farthest_cities
  end

  def test_georadius_with_count
    city = r.georadius("Sicily", 15, 37, 200, 'km', count: 1)
    assert_equal %w(Catania), city
  end

  def test_georadius_with_options_count_sort
    city = r.georadius("Sicily", 15, 37, 200, 'km', sort: :desc, options: :WITHDIST, count: 1)
    assert_equal [["Palermo", "190.4424"]], city
  end

  def test_georadiusbymember_with_sort
    nearest_cities = r.georadiusbymember("Sicily", "Catania", 200, 'km', sort: 'asc')
    assert_equal %w(Catania Palermo), nearest_cities

    farthest_cities = r.georadiusbymember("Sicily", "Catania", 200, 'km', sort: 'desc')
    assert_equal %w(Palermo Catania), farthest_cities
  end

  def test_georadiusbymember_with_count
    city = r.georadiusbymember("Sicily", "Catania", 200, 'km', count: 1)
    assert_equal %w(Catania), city
  end

  def test_georadiusbymember_with_options_count_sort
    city = r.georadiusbymember("Sicily", "Catania", 200, 'km', sort: :desc, options: :WITHDIST, count: 1)
    assert_equal [["Palermo", "166.2742"]], city
  end

  def test_geopos
    location = r.geopos("Sicily", "Catania")
    assert_equal [["15.08726745843887329", "37.50266842333162032"]], location

    locations = r.geopos("Sicily", ["Palermo", "Catania"])
    assert_equal [["13.36138933897018433", "38.11555639549629859"], ["15.08726745843887329", "37.50266842333162032"]], locations
  end

  def test_geopos_nonexistant_location
    location = r.geopos("Sicily", "Rome")
    assert_equal [nil], location

    locations = r.geopos("Sicily", ["Rome", "Catania"])
    assert_equal [nil, ["15.08726745843887329", "37.50266842333162032"]], locations
  end

  def test_geodist
    distination_in_meters = r.geodist("Sicily", "Palermo", "Catania")
    assert_equal "166274.1516", distination_in_meters

    distination_in_feet = r.geodist("Sicily", "Palermo", "Catania", 'ft')
    assert_equal "545518.8700", distination_in_feet
  end

  def test_geodist_with_nonexistant_location
    distination = r.geodist("Sicily", "Palermo", "Rome")
    assert_nil distination
  end

  def test_geohash
    geohash = r.geohash("Sicily", "Palermo")
    assert_equal ["sqc8b49rny0"], geohash

    geohashes = r.geohash("Sicily", ["Palermo", "Catania"])
    assert_equal %w(sqc8b49rny0 sqdtr74hyu0), geohashes
  end

  def test_geohash_with_nonexistant_location
    geohashes = r.geohash("Sicily", ["Palermo", "Rome"])
    assert_equal ["sqc8b49rny0", nil], geohashes
  end
end