summaryrefslogtreecommitdiff
path: root/spec/models/csv/import_spec.rb
blob: 5030a7d88bb8fccd6c8aabaa5e47cc3ed2b29b69 (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
require "rails_helper"

describe Csv::Import do
  include_context "stronglifts_program"
  subject { Csv::Import.new(user, program) }
  let(:user) { create(:user) }

  describe "#can_parse?" do
    let(:directory) { Dir.mktmpdir }

    context "when the directory contains a csv export" do
      before :each do
        file_path = "#{directory}/spreadsheet-stronglifts-csv20151114-1138"
        FileUtils.touch(file_path)
      end

      it "returns true" do
        expect(subject.can_parse?(directory)).to be_truthy
      end
    end

    context "when the directory does not have csv export" do
      it "returns false" do
        expect(subject.can_parse?(directory)).to be_falsey
      end
    end
  end

  describe "#import_from" do
    let(:directory) { Dir.mktmpdir }
    let(:backup_file) do
      Rails.root.join("spec", "fixtures", "spreadsheet-stronglifts.csv")
    end

    before :each do
      `cp #{backup_file} #{directory}/spreadsheet-stronglifts.csv#{rand}`
    end

    after :each do
      FileUtils.remove_entry(directory)
    end

    it "imports each training session" do
      subject.import_from(directory)
      workout = user.workouts.order(:occurred_at).first

      expected_date = user.time_zone.local_to_utc(Time.utc(2015, 03, 02))
      expect(workout.occurred_at).to eql(expected_date)
      expect(workout.routine).to eql(routine_a)
      expect(workout.body_weight).to eql(205.0.lbs)
      expect(workout.sets.count).to eql(15)
      expect(
        workout.sets.map { |x| x.exercise.name }.uniq
      ).to match_array(["Squat", "Bench Press", "Barbell Row"])
    end

    it "imports the completed squat exercise" do
      subject.import_from(directory)

      workout = user.workouts.order(:occurred_at).first
      squat_session = workout.progress_for(squat)

      expect(squat_session.to_sets).to eql([5, 5, 5, 5, 5])
    end

    it "imports the completed bench exercise" do
      subject.import_from(directory)

      workout = user.workouts.order(:occurred_at).first
      bench_session = workout.progress_for(bench_press)
      expect(bench_session.sets.count).to eql(5)
      expect(bench_session.to_sets).to eql([5, 5, 5, 5, 5])
    end

    it "imports the completed barbell row exercise" do
      subject.import_from(directory)

      workout = user.workouts.order(:occurred_at).first
      progress = workout.progress_for(barbell_row)
      expect(progress.to_sets).to eql([5, 5, 5, 5, 5])
      sets = progress.sets.to_a
      expect(sets.at(0).target_weight).to eql(65.0)
      expect(sets.at(0).actual_repetitions).to eql(5)
      expect(sets.at(1).target_weight).to eql(65.0)
      expect(sets.at(1).actual_repetitions).to eql(5)
      expect(sets.at(2).target_weight).to eql(65.0)
      expect(sets.at(2).actual_repetitions).to eql(5)
      expect(sets.at(3).target_weight).to eql(65.0)
      expect(sets.at(3).actual_repetitions).to eql(5)
      expect(sets.at(4).target_weight).to eql(65.0)
      expect(sets.at(4).actual_repetitions).to eql(5)
    end

    it "excludes items that have already been imported" do
      subject.import_from(directory)
      subject.import_from(directory)
      expect(user.workouts.count).to eql(356)
    end
  end

  describe "#import" do
    it "imports dips" do
      row = '11/06/15,,,215,Squat,,265,5,5,5,,,Bench Press,,170,5,5,5,5,5,Barbell Row,,150,5,5,5,5,5,Weighted Dips,,12.5,5,5,5,Planks,,0,60,60,60,,,,,'.split(',')
      subject.import(row)

      workout = user.workouts.first
      progress = workout.progress_for(dips)
      expect(progress).to_not be_nil
      expect(progress.to_sets).to eql([5, 5, 5])
    end

    it "imports chinups" do
      row = '03/14/15,,,205,Squat,,85,5,5,5,5,5,Overhead Press,,70,5,5,5,5,5,Deadlift,,115,5,,,,,Chinups,,0,5,3,2,,,,,,,,,'.split(',')
      subject.import(row)

      workout = user.workouts.first
      progress = workout.progress_for(chinups)
      expect(progress).to_not be_nil
      expect(progress.to_sets).to eql([5, 3, 2])
      sets = progress.sets.to_a
      expect(sets.at(0).target_weight).to eql(0.0)
      expect(sets.at(0).target_repetitions).to eql(5)
      expect(sets.at(0).actual_repetitions).to eql(5)
      expect(sets.at(1).target_weight).to eql(0.0)
      expect(sets.at(1).target_repetitions).to eql(5)
      expect(sets.at(1).actual_repetitions).to eql(3)
      expect(sets.at(2).target_weight).to eql(0.0)
      expect(sets.at(2).target_repetitions).to eql(5)
      expect(sets.at(2).actual_repetitions).to eql(2)
    end

    it "imports the correct number of sets" do
      row = "06/05/16,,,231,Squat,,285,5,5,5,,,Overhead Press,,127,5,5,5,3,2,Deadlift,,305,5,,,,,Chinups,,0,5,4,4,Close Grip Bench Press,,110,5,5,5,,,,,".split(',')

      subject.import(row)
      workout = user.workouts.first
      progress = workout.progress_for(squat)
      expect(progress.sets.count).to eql(3)
      expect(progress.to_sets).to eql([5, 5, 5])

      progress = workout.progress_for(deadlift)
      expect(progress.sets.count).to eql(1)
      expect(progress.to_sets).to eql([5])
    end
  end
end