Skip to content

Commit ec6decb

Browse files
committed
Follow the repo's factory and spec-layout conventions
Self-review against existing patterns rather than anything new. Three deviations, all in specs added earlier on this branch. Four places built records with `Model.create!` where the repo uses factories. `create(:promotion_rate, checkin_id: ...)` is already the established call in `checkins_controller_spec.rb`, so there was a working pattern to follow and these did not. Switched to factories throughout, and added the two that were missing: spec/factories/harvey_bradshaw_indices.rb spec/factories/oracle_requests.rb `spec/factories/topic_followings.rb` existed but was an empty block, so `create(:topic_following)` raised on the model's `encrypted_user_id` presence validation -- the factory was declared but unusable. It now supplies one, which is why three call sites here collapse to a bare `create(:topic_following)`. All four new or amended factories are valid with no arguments; I checked that explicitly rather than relying on them happening to work with the arguments these specs pass. `spec/mailers/` is one file per mailer class, and `app/mailers/promotion_rate/` holds two classes, so the combined `promotion_rate_mailers_spec.rb` is split into `spec/mailers/promotion_rate/statistic_mailer_spec.rb` and `low_rate_mailer_spec.rb`, mirroring the app tree the way the other spec directories do. 561 examples, 0 failures. standardrb clean. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
1 parent 4eb61c8 commit ec6decb

10 files changed

Lines changed: 102 additions & 72 deletions

‎backend/spec/controllers/api/v1/harvey_bradshaw_indices_controller_spec.rb‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252

5353
describe "show" do
5454
let!(:index) do
55-
HarveyBradshawIndex.create!(attributes.merge(checkin_id: checkin.id))
55+
create(:harvey_bradshaw_index, attributes.merge(checkin_id: checkin.id))
5656
end
5757

5858
it "returns the requested index" do

‎backend/spec/controllers/api/v1/oracle_requests_controller_spec.rb‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
end
3333

3434
describe "update" do
35-
let!(:oracle_request) { OracleRequest.create!(age: 30, token: "the-token") }
35+
let!(:oracle_request) { create(:oracle_request, age: 30, token: "the-token") }
3636

3737
it "accepts an edit from the holder of the token" do
3838
request.headers["X-Oracle-Token"] = "the-token"
@@ -74,7 +74,7 @@
7474
end
7575

7676
describe "show" do
77-
let!(:oracle_request) { OracleRequest.create!(age: 30, token: "the-token") }
77+
let!(:oracle_request) { create(:oracle_request, age: 30, token: "the-token") }
7878

7979
it "returns the request" do
8080
get :show, params: {id: oracle_request.id.to_s}

‎backend/spec/controllers/api/v1/promotion_rates_controller_spec.rb‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
end
3030

3131
describe "update" do
32-
let!(:promotion_rate) { PromotionRate.create!(checkin_id: checkin.id, score: 5) }
32+
let!(:promotion_rate) { create(:promotion_rate, checkin_id: checkin.id, score: 5) }
3333

3434
it "updates the score and feedback" do
3535
put :update, params: {
@@ -55,7 +55,7 @@
5555
end
5656

5757
describe "show" do
58-
let!(:promotion_rate) { PromotionRate.create!(checkin_id: checkin.id, score: 5) }
58+
let!(:promotion_rate) { create(:promotion_rate, checkin_id: checkin.id, score: 5) }
5959

6060
it "returns the rating" do
6161
get :show, params: {id: promotion_rate.id.to_s}

‎backend/spec/controllers/api/v1/topic_followings_controller_spec.rb‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
let(:symptom) { create(:symptom) }
77

88
let!(:topic_following) do
9-
TopicFollowing.create!(encrypted_user_id: user.encrypted_id, tag_ids: [tag.id])
9+
create(:topic_following, encrypted_user_id: user.encrypted_id, tag_ids: [tag.id])
1010
end
1111

1212
before { sign_in user }
@@ -21,7 +21,7 @@
2121

2222
context "when it belongs to somebody else" do
2323
let!(:topic_following) do
24-
TopicFollowing.create!(encrypted_user_id: create(:user).encrypted_id)
24+
create(:topic_following)
2525
end
2626

2727
it "is refused" do
@@ -72,7 +72,7 @@
7272

7373
context "when it belongs to somebody else" do
7474
let!(:topic_following) do
75-
TopicFollowing.create!(encrypted_user_id: create(:user).encrypted_id)
75+
create(:topic_following)
7676
end
7777

7878
it "is refused and changes nothing" do
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
FactoryBot.define do
2+
factory :harvey_bradshaw_index do
3+
checkin_id { FactoryBot.create(:checkin).id }
4+
5+
stools { 3 }
6+
well_being { 1 }
7+
abdominal_mass { 0 }
8+
abdominal_pain { 2 }
9+
end
10+
end
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
FactoryBot.define do
2+
factory :oracle_request do
3+
age { 30 }
4+
sequence(:token) { |n| "oracle-token-#{n}" }
5+
end
6+
end
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
FactoryBot.define do
22
factory :topic_following do
3+
encrypted_user_id { FactoryBot.create(:user).encrypted_id }
34
end
45
end
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
require "rails_helper"
2+
3+
# Reached only from `rake oneoff:send_promotion_rate_low_rate`, which stringifies every
4+
# value before handing it to the view. The examples use the same shape that task
5+
# produces rather than model instances.
6+
RSpec.describe PromotionRate::LowRateMailer, type: :mailer do
7+
# ApplicationMailer captures the From address into default_params at class load, so
8+
# stubbing the config it reads would be too late. Override the stored default instead.
9+
before do
10+
allow(described_class).to receive(:default_params)
11+
.and_return(described_class.default_params.merge(from: "from@some.email"))
12+
end
13+
14+
let(:start_date) { "17-09-2017" }
15+
let(:end_date) { "22-09-2017" }
16+
let(:objects) do
17+
[{"_id" => "1", "score" => "2", "feedback" => "rough week", "date" => "2017-09-18"}]
18+
end
19+
let(:mail) { described_class.show("ops@flaredown.com", objects, start_date, end_date) }
20+
21+
it "renders the headers" do
22+
expect(mail.subject).to eq I18n.t("promotion_rate.low_rate.subject")
23+
expect(mail.to).to eq ["ops@flaredown.com"]
24+
expect(mail.from).to eq ["from@some.email"]
25+
end
26+
27+
it "shows the date range" do
28+
expect(mail.body.encoded).to include "#{start_date} - #{end_date}"
29+
end
30+
31+
it "shows the score, the feedback and the date" do
32+
body = mail.body.encoded
33+
34+
expect(body).to include "rough week"
35+
expect(body).to match(/Score:\s*2/)
36+
expect(body).to include "2017-09-18"
37+
end
38+
end
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
require "rails_helper"
2+
3+
# Reached only from `rake oneoff:send_promotion_rate_statistic`, which hands the output
4+
# of `PromotionRate.group_by_score_and_date` straight to the view -- string-keyed
5+
# hashes, not model instances. The examples use the same shape that task produces.
6+
RSpec.describe PromotionRate::StatisticMailer, type: :mailer do
7+
# ApplicationMailer captures the From address into default_params at class load, so
8+
# stubbing the config it reads would be too late. Override the stored default instead.
9+
before do
10+
allow(described_class).to receive(:default_params)
11+
.and_return(described_class.default_params.merge(from: "from@some.email"))
12+
end
13+
14+
let(:start_date) { "17-09-2017" }
15+
let(:end_date) { "22-09-2017" }
16+
let(:objects) { [{"_id" => 3, "count" => 7}, {"_id" => 1, "count" => 2}] }
17+
let(:mail) { described_class.show("ops@flaredown.com", objects, start_date, end_date) }
18+
19+
it "renders the headers" do
20+
expect(mail.subject).to eq I18n.t("promotion_rate.statistic.subject")
21+
expect(mail.to).to eq ["ops@flaredown.com"]
22+
expect(mail.from).to eq ["from@some.email"]
23+
end
24+
25+
it "shows the date range and each score with its tally" do
26+
body = mail.body.encoded
27+
28+
expect(body).to include "#{start_date} - #{end_date}"
29+
expect(body).to include "Amount:"
30+
expect(body).to match(/Score:\s*1/)
31+
expect(body).to match(/Score:\s*3/)
32+
end
33+
34+
it "orders the scores ascending regardless of the order given" do
35+
scores = mail.body.encoded.scan(/Score:\s*(\d+)/).flatten
36+
37+
expect(scores).to eq %w[1 3]
38+
end
39+
end

‎backend/spec/mailers/promotion_rate_mailers_spec.rb‎

Lines changed: 0 additions & 64 deletions
This file was deleted.

0 commit comments

Comments
 (0)