summaryrefslogtreecommitdiff
path: root/spec/models/gym_spec.rb
blob: 961f1f8136accd93202617851b709d1dc4fc1b75 (plain)
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
require "rails_helper"

describe Gym do
  subject { build(:gym) }

  describe "#validations" do
    it "validates the presence of name" do
      subject.name = nil
      expect(subject).to be_invalid
      expect(subject.errors[:name]).to be_present
    end
  end

  describe "#location" do
    it "updates the location" do
      VCR.use_cassette("geo-location") do
        subject.location_attributes = {
          address: "123 street sw",
          city: "edmonton",
          region: "alberta",
          country: "canada",
        }
        subject.save!
      end

      expect(subject.location.address).to eql("123 street sw")
      expect(subject.location.city).to eql("edmonton")
      expect(subject.location.region).to eql("alberta")
      expect(subject.location.country).to eql("canada")
    end
  end

  describe ".closest_to" do
    let(:calgary) { create(:calgary) }
    let(:edmonton) { create(:edmonton) }
    let!(:calgary_gym) { create(:gym, location: calgary) }
    let!(:edmonton_gym) { create(:gym, location: edmonton) }

    it "returns gyms near the location" do
      results = Gym.closest_to(calgary)
      expect(results).to match_array([calgary_gym])
    end

    it "returns all the gyms" do
      results = Gym.closest_to(nil)
      expect(results).to match_array([calgary_gym, edmonton_gym])
    end

    it "returns all the gym when coordinates are empty" do
      results = Gym.closest_to(build(:no_where))
      expect(results).to match_array([calgary_gym, edmonton_gym])
    end
  end

  describe ".search_with" do
    let!(:calgary_gym) { create(:calgary_gym, name: "SAIT") }
    let!(:edmonton_gym) { create(:edmonton_gym, name: "NAIT") }
    let!(:portland_gym) { create(:portland_gym, name: "24 Hour Fitness") }

    it "returns all gyms" do
      results = Gym.search_with({})
      expect(results).to match_array([calgary_gym, edmonton_gym, portland_gym])
    end

    it "returns gyms with a matching name" do
      results = Gym.search_with(q: "sait")
      expect(results).to match_array([calgary_gym])
    end

    it "returns all gyms from a city" do
      results = Gym.search_with(q: "calgary")
      expect(results).to match_array([calgary_gym])
    end

    it "returns all gyms from a region" do
      results = Gym.search_with(q: "AB")
      expect(results).to match_array([calgary_gym, edmonton_gym])
    end

    it "returns all gyms from a country" do
      results = Gym.search_with(q: "US")
      expect(results).to match_array([portland_gym])
    end
  end

  describe ".search_yelp" do
    it "returns results" do
      VCR.use_cassette("yelp-calgary") do
        results = Gym.search_yelp(city: "Calgary")
        expect(results).to be_present
        expect(results.count).to be > 0
        expect(results.first).to be_instance_of(Gym)
      end
    end

    it "returns the next page of results" do
      VCR.use_cassette("yelp-calgary-page-2") do
        results = Gym.search_yelp(city: "Calgary", page: 2)
        expect(results).to be_present
        expect(results.count).to be > 0
        expect(results.first).to be_instance_of(Gym)
      end
    end

    it "finds a college gym" do
      VCR.use_cassette("yelp-sait") do
        expect(Gym.search_yelp(
          q: "SAIT",
          city: "Calgary",
          categories: %w{gyms stadiumsarenas},
        ).map(&:name)).to match_array(["Sait Campus Centre"])
      end
    end
  end if ENV['YELP_TOKEN_SECRET'].present?

  describe "#full_address" do
    let(:location) { build(:location) }

    it "returns the full address" do
      subject.location = location
      expected = [
        location.address,
        location.city,
        location.region,
        location.country
      ].join(", ")
      expect(subject.full_address).to eql(expected)
    end
  end

  describe "#duplicate?" do
    it "returns true when a dup is found" do
      subject.location = create(:portland)
      subject.save!
      create(:gym, location: create(:portland))

      expect(subject.duplicate?).to be_truthy
    end

    it 'returns true when another gym has the same yelp id' do
      subject.yelp_id = "hello-world"
      create(:gym, yelp_id: subject.yelp_id)

      expect(subject.duplicate?).to be_truthy
    end

    it "returns false when no dups are found" do
      subject.yelp_id = "hello-world"
      subject.location = create(:portland)
      subject.save!
      expect(subject.duplicate?).to be_falsey
    end
  end
end