Skip to content

Commit

Permalink
Add tolerance argument for Interactors::MatchLiveTimes; update spec.
Browse files Browse the repository at this point in the history
  • Loading branch information
moveson committed Feb 16, 2018
1 parent 3e95b58 commit 749c3a9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
7 changes: 4 additions & 3 deletions app/services/interactors/match_live_times.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ def self.perform!(args)
def initialize(args)
ArgsValidator.validate(params: args,
required: [:event, :live_times],
exclusive: [:event, :live_times],
exclusive: [:event, :live_times, :tolerance],
class: self.class)
@event = args[:event]
@live_times = args[:live_times]
@tolerance = args[:tolerance] || 1.minute
@errors = []
@resources = {matched_live_times: [], unmatched_live_times: []}
validate_setup
Expand All @@ -27,7 +28,7 @@ def perform!

private

attr_reader :event, :live_times, :errors, :resources
attr_reader :event, :live_times, :tolerance, :errors, :resources

def match_live_time_to_split_time(live_time)
split_time = matching_split_time(live_time)
Expand All @@ -42,7 +43,7 @@ def match_live_time_to_split_time(live_time)
def matching_split_time(live_time)
live_time.absolute_time &&
!live_time.matched? &&
event.split_times.where(match_attributes(live_time)).find { |st| st.day_and_time == live_time.absolute_time }
event.split_times.where(match_attributes(live_time)).find { |st| (st.day_and_time - live_time.absolute_time).abs <= tolerance }
end

def match_attributes(live_time)
Expand Down
26 changes: 21 additions & 5 deletions spec/services/interactors/match_live_times_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
require 'rails_helper'

RSpec.describe Interactors::MatchLiveTimes do
subject { Interactors::MatchLiveTimes.new(event: event, live_times: live_times) }
subject { Interactors::MatchLiveTimes.new(event: event, live_times: live_times, tolerance: tolerance) }
let(:tolerance) { nil }
let!(:split_time_1) { create(:split_time, effort: effort, lap: 1, split: split_1, bitkey: in_bitkey, time_from_start: 0) }
let!(:split_time_2) { create(:split_time, effort: effort, lap: 1, split: split_2, bitkey: in_bitkey, time_from_start: 60.minutes) }
let!(:split_time_3) { create(:split_time, effort: effort, lap: 1, split: split_2, bitkey: out_bitkey, time_from_start: 70.minutes) }
Expand Down Expand Up @@ -77,20 +78,35 @@
let(:matching_live_times) { live_times.first(3) }
let(:non_matching_live_times) { live_times.last(1) }

it 'creates new split_times for unmatched live_times only' do
it 'sets split_time for matching live_times only' do
verify_live_times
end
end

context 'when split and bitkey are the same and time is within tolerance' do
let(:matching_split_times) { split_times }
let(:live_times) { [live_time_1, live_time_2, live_time_3] }
let(:matching_live_times) { live_times }
let(:non_matching_live_times) { [] }
let(:tolerance) { 1.minute }

before { live_time_2.update(absolute_time: split_time_2.day_and_time - 30.seconds) }

it 'sets split_time for all live_times' do
verify_live_times
end
end

context 'when split and bitkey are the same but time is different' do
context 'when split and bitkey are the same but time is outside of tolerance' do
let(:matching_split_times) { [split_time_1, split_time_3] }
let(:live_times) { [live_time_1, live_time_2, live_time_3] }
let(:matching_live_times) { [live_time_1, live_time_3] }
let(:non_matching_live_times) { [live_time_2] }
let(:tolerance) { 10.seconds }

before { live_time_2.update(absolute_time: split_time_2.day_and_time + 1.minute) }
before { live_time_2.update(absolute_time: split_time_2.day_and_time - 30.seconds) }

it 'creates new split_times for unmatched live_times only' do
it 'sets split_time for matching live_times only' do
verify_live_times
end
end
Expand Down

0 comments on commit 749c3a9

Please sign in to comment.