master
1require "rails_helper"
2
3describe Gym do
4 subject { build(:gym) }
5
6 describe "#validations" do
7 it "validates the presence of name" do
8 subject.name = nil
9 expect(subject).to be_invalid
10 expect(subject.errors[:name]).to be_present
11 end
12 end
13
14 describe "#location" do
15 it "updates the location" do
16 VCR.use_cassette("geo-location") do
17 subject.location_attributes = {
18 address: "123 street sw",
19 city: "edmonton",
20 region: "alberta",
21 country: "canada",
22 }
23 subject.save!
24 end
25
26 expect(subject.location.address).to eql("123 street sw")
27 expect(subject.location.city).to eql("edmonton")
28 expect(subject.location.region).to eql("alberta")
29 expect(subject.location.country).to eql("canada")
30 end
31 end
32
33 describe ".closest_to" do
34 let(:calgary) { create(:calgary) }
35 let(:edmonton) { create(:edmonton) }
36 let!(:calgary_gym) { create(:gym, location: calgary) }
37 let!(:edmonton_gym) { create(:gym, location: edmonton) }
38
39 it "returns gyms near the location" do
40 results = Gym.closest_to(calgary)
41 expect(results).to match_array([calgary_gym])
42 end
43
44 it "returns all the gyms" do
45 results = Gym.closest_to(nil)
46 expect(results).to match_array([calgary_gym, edmonton_gym])
47 end
48
49 it "returns all the gym when coordinates are empty" do
50 results = Gym.closest_to(build(:no_where))
51 expect(results).to match_array([calgary_gym, edmonton_gym])
52 end
53 end
54
55 describe ".search_with" do
56 let!(:calgary_gym) { create(:calgary_gym, name: "SAIT") }
57 let!(:edmonton_gym) { create(:edmonton_gym, name: "NAIT") }
58 let!(:portland_gym) { create(:portland_gym, name: "24 Hour Fitness") }
59
60 it "returns all gyms" do
61 results = Gym.search_with({})
62 expect(results).to match_array([calgary_gym, edmonton_gym, portland_gym])
63 end
64
65 it "returns gyms with a matching name" do
66 results = Gym.search_with(q: "sait")
67 expect(results).to match_array([calgary_gym])
68 end
69
70 it "returns all gyms from a city" do
71 results = Gym.search_with(q: "calgary")
72 expect(results).to match_array([calgary_gym])
73 end
74
75 it "returns all gyms from a region" do
76 results = Gym.search_with(q: "AB")
77 expect(results).to match_array([calgary_gym, edmonton_gym])
78 end
79
80 it "returns all gyms from a country" do
81 results = Gym.search_with(q: "US")
82 expect(results).to match_array([portland_gym])
83 end
84 end
85
86 describe ".search_yelp" do
87 it "returns results" do
88 VCR.use_cassette("yelp-calgary") do
89 results = Gym.search_yelp(city: "Calgary")
90 expect(results).to be_present
91 expect(results.count).to be > 0
92 expect(results.first).to be_instance_of(Gym)
93 end
94 end
95
96 it "returns the next page of results" do
97 VCR.use_cassette("yelp-calgary-page-2") do
98 results = Gym.search_yelp(city: "Calgary", page: 2)
99 expect(results).to be_present
100 expect(results.count).to be > 0
101 expect(results.first).to be_instance_of(Gym)
102 end
103 end
104
105 it "finds a college gym" do
106 VCR.use_cassette("yelp-sait") do
107 expect(Gym.search_yelp(
108 q: "SAIT",
109 city: "Calgary",
110 categories: %w{gyms stadiumsarenas},
111 ).map(&:name)).to match_array(["Sait Campus Centre"])
112 end
113 end
114 end if ENV['YELP_TOKEN_SECRET'].present?
115
116 describe "#full_address" do
117 let(:location) { build(:location) }
118
119 it "returns the full address" do
120 subject.location = location
121 expected = [
122 location.address,
123 location.city,
124 location.region,
125 location.country
126 ].join(", ")
127 expect(subject.full_address).to eql(expected)
128 end
129 end
130
131 describe "#duplicate?" do
132 it "returns true when a dup is found" do
133 subject.location = create(:portland)
134 subject.save!
135 create(:gym, location: create(:portland))
136
137 expect(subject.duplicate?).to be_truthy
138 end
139
140 it 'returns true when another gym has the same yelp id' do
141 subject.yelp_id = "hello-world"
142 create(:gym, yelp_id: subject.yelp_id)
143
144 expect(subject.duplicate?).to be_truthy
145 end
146
147 it "returns false when no dups are found" do
148 subject.yelp_id = "hello-world"
149 subject.location = create(:portland)
150 subject.save!
151 expect(subject.duplicate?).to be_falsey
152 end
153 end
154end