added 260907 version of worker toolkit
This commit is contained in:
@@ -0,0 +1,127 @@
|
||||
require "rails_helper"
|
||||
|
||||
RSpec.describe Checkin::Creator do
|
||||
let!(:user) { create(:user) }
|
||||
let!(:condition_tracking) { create(:tracking, :active, :for_condition, user: user) }
|
||||
let!(:condition) { condition_tracking.trackable }
|
||||
let!(:symptom_tracking) { create(:tracking, :active, :for_symptom, user: user) }
|
||||
let!(:symptom) { symptom_tracking.trackable }
|
||||
let!(:treatment_tracking) { create(:tracking, :active, :for_treatment, user: user) }
|
||||
let!(:treatment) { treatment_tracking.trackable }
|
||||
let!(:date) { Time.zone.today }
|
||||
|
||||
subject { Checkin::Creator.new(user.id, date).create! }
|
||||
|
||||
describe "create" do
|
||||
it "returns a new checkin prefilled with active trackables" do
|
||||
expect(subject.id).to be_present
|
||||
expect(subject.user_id).to eq user.id
|
||||
expect(subject.date).to eq date
|
||||
# Condition
|
||||
checkin_condition = subject.conditions[0]
|
||||
expect(checkin_condition.condition_id).to eq condition.id
|
||||
expect(checkin_condition.color_id).to eq condition_tracking.color_id.to_s
|
||||
expect(checkin_condition.position).to eq 0
|
||||
# Symptom
|
||||
checkin_symptom = subject.symptoms[0]
|
||||
expect(checkin_symptom.symptom_id).to eq symptom.id
|
||||
expect(checkin_symptom.color_id).to eq symptom_tracking.color_id.to_s
|
||||
expect(checkin_symptom.position).to eq 0
|
||||
# Treatment
|
||||
checkin_treatment = subject.treatments[0]
|
||||
expect(checkin_treatment.treatment_id).to eq treatment.id
|
||||
expect(checkin_treatment.color_id).to eq treatment_tracking.color_id.to_s
|
||||
expect(checkin_treatment.position).to eq 0
|
||||
expect(checkin_treatment.is_taken).to be false
|
||||
end
|
||||
|
||||
context "when recent dose exists for treatment in user's profile" do
|
||||
before do
|
||||
user.profile.set_most_recent_dose(treatment.id, "20 mg")
|
||||
user.profile.save!
|
||||
end
|
||||
it "sets dose from profile on the new checkin" do
|
||||
checkin_treatment = subject.treatments[0]
|
||||
expect(checkin_treatment.treatment_id).to eq treatment.id
|
||||
saved_dose = user.profile.most_recent_dose_for(treatment.id)
|
||||
expect(checkin_treatment.value).to eq saved_dose
|
||||
end
|
||||
end
|
||||
|
||||
context "when recent position exists for a trackable in user's profile" do
|
||||
before do
|
||||
user.profile.set_most_recent_trackable_position(condition, 5)
|
||||
user.profile.save!
|
||||
end
|
||||
it "sets trackable position from profile on the new checkin" do
|
||||
checkin_condition = subject.conditions[0]
|
||||
expect(checkin_condition.condition_id).to eq condition.id
|
||||
saved_position = user.profile.most_recent_trackable_position_for(condition)
|
||||
expect(checkin_condition.position).to eq saved_position
|
||||
end
|
||||
end
|
||||
|
||||
context "when no recent position exists for a trackable in user's profile" do
|
||||
before { create(:tracking, :active, :for_condition, user: user) }
|
||||
it "sets positions with auto-increment counter starting by 1" do
|
||||
positions = subject.conditions.map(&:position)
|
||||
expect(positions.to_set).to eq [0, 1].to_set
|
||||
end
|
||||
end
|
||||
|
||||
context "when trackables never used before" do
|
||||
it "creates new usage records" do
|
||||
# For Condition
|
||||
expect(subject.conditions).to be_present
|
||||
condition_usage = TrackableUsage.find_by(user: user, trackable: condition)
|
||||
expect(condition_usage).to be_present
|
||||
expect(condition_usage.count).to eq 1
|
||||
# For Symptom
|
||||
expect(subject.symptoms).to be_present
|
||||
symptom_usage = TrackableUsage.find_by(user: user, trackable: symptom)
|
||||
expect(symptom_usage).to be_present
|
||||
expect(symptom_usage.count).to eq 1
|
||||
# For Treatment
|
||||
expect(subject.treatments).to be_present
|
||||
treatment_usage = TrackableUsage.find_by(user: user, trackable: treatment)
|
||||
expect(treatment_usage).to be_present
|
||||
expect(treatment_usage.count).to eq 1
|
||||
end
|
||||
end
|
||||
|
||||
context "when trackables already used before" do
|
||||
let!(:condition_usage) { TrackableUsage.create!(user: user, trackable: condition) }
|
||||
let!(:condition_usage_count) { condition_usage.count }
|
||||
let!(:symptom_usage) { TrackableUsage.create!(user: user, trackable: symptom) }
|
||||
let!(:symptom_usage_count) { symptom_usage.count }
|
||||
let!(:treatment_usage) { TrackableUsage.create!(user: user, trackable: treatment) }
|
||||
let!(:treatment_usage_count) { treatment_usage.count }
|
||||
it "increments count on usage records" do
|
||||
# For Condition
|
||||
expect(subject.conditions).to be_present
|
||||
expect(condition_usage.reload.count).to eq(condition_usage_count + 1)
|
||||
# For Symptom
|
||||
expect(subject.symptoms).to be_present
|
||||
expect(symptom_usage.reload.count).to eq(symptom_usage_count + 1)
|
||||
# For Treatment
|
||||
expect(subject.treatments).to be_present
|
||||
expect(treatment_usage.reload.count).to eq(treatment_usage_count + 1)
|
||||
end
|
||||
end
|
||||
|
||||
context "when postal code is set on previous checkin" do
|
||||
let(:weather) { create :weather }
|
||||
let(:postal_code) { "55403" }
|
||||
let(:position) { Position.create(postal_code: postal_code) }
|
||||
|
||||
let!(:previous_checkin) { create :checkin, user_id: user.id, position_id: position.id }
|
||||
|
||||
before { expect(WeatherRetriever).to receive(:get).and_return(weather) }
|
||||
|
||||
it "should ask for weather" do
|
||||
expect(subject.position.postal_code).to eq(postal_code)
|
||||
expect(subject.weather_id).to eq(weather.id)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,207 @@
|
||||
require "rails_helper"
|
||||
|
||||
RSpec.describe Checkin::Updater do
|
||||
let(:user) { create(:user) }
|
||||
let(:checkin) { create(:checkin, user_id: user.id, date: Time.zone.today) }
|
||||
|
||||
subject { Checkin::Updater.new(user, params).update! }
|
||||
|
||||
context "with permitted params" do
|
||||
let(:params) { ActionController::Parameters.new(id: checkin.id.to_s, checkin: {note: "Blah"}) }
|
||||
it "returns updated checkin" do
|
||||
expect(subject.id).to eq checkin.id
|
||||
expect(subject.note).to eq params[:checkin][:note]
|
||||
end
|
||||
end
|
||||
|
||||
context "with non-permitted params" do
|
||||
let(:params) { ActionController::Parameters.new(id: checkin.id.to_s, checkin: {date: Date.yesterday}) }
|
||||
it "returns checkin with no changes" do
|
||||
expect(subject.id).to eq checkin.id
|
||||
expect(subject.date).not_to eq params[:checkin][:date]
|
||||
end
|
||||
end
|
||||
|
||||
context "when recent dose exists for treatment in user's profile" do
|
||||
let(:treatment) { create(:treatment) }
|
||||
|
||||
let(:params) do
|
||||
ActionController::Parameters.new(
|
||||
id: checkin.id.to_s,
|
||||
checkin: {treatments_attributes: [{treatment_id: treatment.id}]}
|
||||
)
|
||||
end
|
||||
|
||||
before do
|
||||
user.profile.set_most_recent_dose(treatment.id, "20 mg")
|
||||
user.profile.save!
|
||||
end
|
||||
it "auto-sets the most recently used dose on added treatments" do
|
||||
returned_treatment = subject.treatments[0]
|
||||
expect(returned_treatment.treatment_id).to eq treatment.id
|
||||
saved_dose = user.profile.most_recent_dose_for(treatment.id)
|
||||
expect(returned_treatment.value).to eq saved_dose
|
||||
end
|
||||
end
|
||||
|
||||
context "trackable usages" do
|
||||
context "when removing a trackable" do
|
||||
let!(:checkin_condition) { create(:checkin_condition, checkin: checkin) }
|
||||
let!(:condition) { Condition.find(checkin_condition.condition_id) }
|
||||
let!(:usage) { create(:trackable_usage, user: user, trackable: condition) }
|
||||
let(:params) do
|
||||
ActionController::Parameters.new(
|
||||
id: checkin.id.to_s,
|
||||
checkin: {
|
||||
conditions_attributes: [{
|
||||
id: checkin_condition.id.to_s,
|
||||
condition_id: checkin_condition.condition_id,
|
||||
_destroy: "1"
|
||||
}]
|
||||
}
|
||||
)
|
||||
end
|
||||
context "used once before" do
|
||||
it "removes trackable and usage record" do
|
||||
expect(subject.conditions.map(&:id)).not_to include checkin_condition.id
|
||||
expect(TrackableUsage.find_by(user: user, trackable: condition)).to be_nil
|
||||
end
|
||||
end
|
||||
context "used more than once before" do
|
||||
let!(:previous_count) { usage.count }
|
||||
before { usage.increment! :count }
|
||||
it "removes trackable and decrements count on usage record" do
|
||||
expect(subject.conditions.map(&:id)).not_to include checkin_condition.id
|
||||
expect(usage.count).to eq(previous_count + 1)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context "when adding a trackable" do
|
||||
let!(:condition) { create(:condition) }
|
||||
let(:params) do
|
||||
ActionController::Parameters.new(
|
||||
id: checkin.id.to_s,
|
||||
checkin: {
|
||||
conditions_attributes: [{
|
||||
condition_id: condition.id
|
||||
}]
|
||||
}
|
||||
)
|
||||
end
|
||||
context "never used before" do
|
||||
it "adds trackable and creates a new usage record" do
|
||||
expect(subject.conditions.map(&:condition_id)).to include condition.id
|
||||
expect(TrackableUsage.find_by(user: user, trackable: condition)).to be_present
|
||||
end
|
||||
end
|
||||
context "already used before" do
|
||||
let!(:usage) { TrackableUsage.find_or_create_by(user: user, trackable: condition) }
|
||||
let!(:previous_count) { usage.count }
|
||||
it "adds trackable and increments count on usage record" do
|
||||
expect(subject.conditions.map(&:condition_id)).to include condition.id
|
||||
expect(usage.reload.count).to eq(previous_count + 1)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context "trackables positions" do
|
||||
let(:n) { 5 }
|
||||
let(:trackables) do
|
||||
result = []
|
||||
n.times do |i|
|
||||
result << create(:checkin_condition, checkin: checkin, position: i)
|
||||
end
|
||||
result
|
||||
end
|
||||
let(:trackables_attrs) do
|
||||
trackables.map do |t|
|
||||
{
|
||||
id: t.id.to_s,
|
||||
condition_id: t.condition_id,
|
||||
position: t.position
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
context "when adding a trackable and client didn't pass position" do
|
||||
let(:condition) { create(:condition) }
|
||||
let(:params) do
|
||||
ActionController::Parameters.new(
|
||||
id: checkin.id.to_s,
|
||||
checkin: {
|
||||
conditions_attributes: trackables_attrs + [{condition_id: condition.id}]
|
||||
}
|
||||
)
|
||||
end
|
||||
it "auto-sets position to last" do
|
||||
added_trackable = subject.conditions.find_by(condition_id: condition.id)
|
||||
expect(added_trackable.position).to eq n
|
||||
end
|
||||
end
|
||||
|
||||
context "when removing a trackable" do
|
||||
let(:params) do
|
||||
ActionController::Parameters.new(
|
||||
id: checkin.id.to_s,
|
||||
checkin: {
|
||||
conditions_attributes: trackables_attrs
|
||||
}
|
||||
)
|
||||
end
|
||||
let(:pos) { 1 }
|
||||
before do
|
||||
trackable_to_remove = params[:checkin][:conditions_attributes].find { |a| a[:position].eql? pos }
|
||||
trackable_to_remove[:_destroy] = "1"
|
||||
end
|
||||
it "updates positions of removed trackable's successors" do
|
||||
successors_attrs = params[:checkin][:conditions_attributes].select { |a| a[:position] > pos }
|
||||
successors_attrs.each do |successor_attrs|
|
||||
successor = subject.conditions.find(successor_attrs[:id])
|
||||
expect(successor.position).to eq successor_attrs[:position] - 1
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context "when reordering trackables" do
|
||||
let(:params) do
|
||||
ActionController::Parameters.new(
|
||||
id: checkin.id.to_s,
|
||||
checkin: {
|
||||
conditions_attributes: trackables_attrs
|
||||
}
|
||||
)
|
||||
end
|
||||
before do
|
||||
# swap first and last trackable's positions
|
||||
attrs = params[:checkin][:conditions_attributes]
|
||||
attrs[0][:position] = attrs[trackables_attrs.length - 1][:position]
|
||||
end
|
||||
context "on today's checkin" do
|
||||
it "updates trackables positions in checkin and saves them in profile" do
|
||||
params[:checkin][:conditions_attributes].each do |condition_attr|
|
||||
checkin_condition = subject.conditions.find(condition_attr[:id])
|
||||
expect(checkin_condition.position).to eq condition_attr[:position]
|
||||
condition = Condition.find(condition_attr[:condition_id])
|
||||
saved_position = user.profile.most_recent_trackable_position_for(condition)
|
||||
expect(saved_position).to eq condition_attr[:position]
|
||||
end
|
||||
end
|
||||
end
|
||||
context "on a past checkin" do
|
||||
let(:checkin) { create(:checkin, user_id: user.id, date: Time.zone.today - 1.day) }
|
||||
|
||||
it "updates trackables positions in checkin but doesn't save them in profile" do
|
||||
params[:checkin][:conditions_attributes].each do |condition_attr|
|
||||
checkin_condition = subject.conditions.find(condition_attr[:id])
|
||||
expect(checkin_condition.position).to eq condition_attr[:position]
|
||||
condition = Condition.find(condition_attr[:condition_id])
|
||||
saved_position = user.profile.most_recent_trackable_position_for(condition)
|
||||
expect(saved_position).to eq nil
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user