diff options
| author | mo khan <mo@mokhan.ca> | 2015-05-19 07:55:04 -0700 |
|---|---|---|
| committer | mo khan <mo@mokhan.ca> | 2015-05-21 21:26:10 -0600 |
| commit | e89bc7cb1467063ce65e3e78d77c8f4635132d0f (patch) | |
| tree | 4e7664935ebe396ac0b048e0421e15a034f15b61 /spec | |
| parent | b769c085489679d362ccacd4e5d514f663379089 (diff) | |
create a workout record for each recors in the sqlite db.
Diffstat (limited to 'spec')
| -rw-r--r-- | spec/controllers/training_sessions_controller_spec.rb | 44 | ||||
| -rw-r--r-- | spec/factories/training_sessions.rb | 5 | ||||
| -rw-r--r-- | spec/fixtures/backup.stronglifts | bin | 0 -> 4701 bytes | |||
| -rw-r--r-- | spec/jobs/process_backup_job_spec.rb | 16 | ||||
| -rw-r--r-- | spec/models/training_session_spec.rb | 68 | ||||
| -rw-r--r-- | spec/routing/dashboard_routing_spec.rb | 4 | ||||
| -rw-r--r-- | spec/support/stronglifts_program.rb | 12 |
7 files changed, 147 insertions, 2 deletions
diff --git a/spec/controllers/training_sessions_controller_spec.rb b/spec/controllers/training_sessions_controller_spec.rb new file mode 100644 index 0000000..98f782c --- /dev/null +++ b/spec/controllers/training_sessions_controller_spec.rb @@ -0,0 +1,44 @@ +require "rails_helper" + +describe TrainingSessionsController do + let(:user) { create(:user) } + + before :each do + http_login(user) + end + + describe "#index" do + include_context "stronglifts_program" + let!(:training_session_a) { create(:training_session, user: user, workout: workout_a) } + let!(:training_session_b) { create(:training_session, user: user, workout: workout_b) } + + it "loads all my training sessions" do + get :index + expect(assigns(:training_sessions)).to match_array([training_session_a, training_session_b]) + end + end + + describe "#upload" do + let(:backup_file) { Rails.root.join("spec", "fixtures", "stronglifts.backup").to_s } + + before :each do + allow(ProcessBackupJob).to receive(:perform_later) + end + + it "uploads a new backup" do + post :upload, backup: backup_file + expect(ProcessBackupJob).to have_received(:perform_later).with(user, backup_file) + end + + it "redirects to the dashboard" do + post :upload, backup: backup_file + expect(response).to redirect_to(dashboard_path) + end + + it 'displays a friendly message' do + post :upload, backup: backup_file + translation = I18n.translate("training_sessions.upload.success") + expect(flash[:notice]).to eql(translation) + end + end +end diff --git a/spec/factories/training_sessions.rb b/spec/factories/training_sessions.rb new file mode 100644 index 0000000..22bdf4a --- /dev/null +++ b/spec/factories/training_sessions.rb @@ -0,0 +1,5 @@ +FactoryGirl.define do + factory :training_session do + association :user + end +end diff --git a/spec/fixtures/backup.stronglifts b/spec/fixtures/backup.stronglifts Binary files differnew file mode 100644 index 0000000..57cfbcd --- /dev/null +++ b/spec/fixtures/backup.stronglifts diff --git a/spec/jobs/process_backup_job_spec.rb b/spec/jobs/process_backup_job_spec.rb new file mode 100644 index 0000000..16dde3e --- /dev/null +++ b/spec/jobs/process_backup_job_spec.rb @@ -0,0 +1,16 @@ +require 'rails_helper' + +describe ProcessBackupJob, type: :job do + include_context "stronglifts_program" + let(:user) { create(:user) } + + describe "#perform" do + let(:backup_file) { Rails.root.join("spec", "fixtures", "backup.stronglifts").to_s } + + it "adds each workout to the list of training sessions for the user" do + subject.perform(user, backup_file) + + expect(user.training_sessions.count).to eql(31) + end + end +end diff --git a/spec/models/training_session_spec.rb b/spec/models/training_session_spec.rb new file mode 100644 index 0000000..c499375 --- /dev/null +++ b/spec/models/training_session_spec.rb @@ -0,0 +1,68 @@ +require "rails_helper" + +describe TrainingSession, type: :model do + describe ".create_workout_from" do + include_context "stronglifts_program" + let(:user) { create(:user) } + + let(:row) do + [ + 31, + "2015-05-13 06:10:21", + "A", + "{\"weight\":{\"lastWeightKg\":90,\"lastWeightLb\":200},\"success\":false,\"set1\":-1,\"set2\":-1,\"set3\":-1,\"set4\":-1,\"set5\":-1,\"messageDismissed\":false,\"workoutType\":0,\"warmup\":{\"exerciseType\":1,\"targetWeight\":200,\"warmupSets\":[{\"completed\":false},{\"completed\":false},{\"completed\":false},{\"completed\":false},{\"completed\":false}]}}", + "{\"weight\":{\"lastWeightKg\":65,\"lastWeightLb\":145},\"success\":true,\"set1\":5,\"set2\":5,\"set3\":5,\"set4\":5,\"set5\":5,\"messageDismissed\":false,\"workoutType\":0,\"warmup\":{\"exerciseType\":2,\"targetWeight\":145,\"warmupSets\":[{\"completed\":true},{\"completed\":true},{\"completed\":true},{\"completed\":true}]}}", + "{\"weight\":{\"lastWeightKg\":60,\"lastWeightLb\":130},\"success\":false,\"set1\":5,\"set2\":4,\"set3\":4,\"set4\":4,\"set5\":4,\"messageDismissed\":false,\"workoutType\":0,\"warmup\":{\"exerciseType\":3,\"targetWeight\":130,\"warmupSets\":[{\"completed\":true}]}}", + "", + "209LB", + "{\"set1\":8,\"set2\":4,\"set3\":4,\"messageDismissed\":false,\"exercise\":0,\"weight\":{\"lastWeightKg\":0,\"lastWeightLb\":0}}", + 0 + ] + end + + let(:workout_row) do + WorkoutRow.new( + id: row[0], + date: DateTime.parse(row[1]), + workout: row[2], + exercise_1: JSON.parse(row[3]), + exercise_2: JSON.parse(row[4]), + exercise_3: JSON.parse(row[5]), + note: row[6], + body_weight: row[7], + arm_work: JSON.parse(row[8]) + ) + end + + it 'creates a new workout' do + training_session = user.training_sessions.create_workout_from(workout_row) + + expect(training_session).to be_persisted + expect(training_session.occurred_at).to eql(workout_row.date) + expect(training_session.workout).to eql(workout_a) + expect(training_session.exercise_sessions.count).to eql(3) + expect(training_session.exercise_sessions.map { |x| x.exercise.name }).to match_array(["Squat", "Bench Press", "Barbell Row"]) + + squat_session = training_session.exercise_sessions.find_by(exercise_workout: squat_workout) + expect(squat_session.sets[0]).to eql('0') + expect(squat_session.sets[1]).to eql('0') + expect(squat_session.sets[2]).to eql('0') + expect(squat_session.sets[3]).to eql('0') + expect(squat_session.sets[4]).to eql('0') + + bench_session = training_session.exercise_sessions.find_by(exercise_workout: bench_workout) + expect(bench_session.sets[0]).to eql("5") + expect(bench_session.sets[1]).to eql("5") + expect(bench_session.sets[2]).to eql("5") + expect(bench_session.sets[3]).to eql("5") + expect(bench_session.sets[4]).to eql("5") + + row_session = training_session.exercise_sessions.find_by(exercise_workout: row_workout) + expect(row_session.sets[0]).to eql("5") + expect(row_session.sets[1]).to eql("4") + expect(row_session.sets[2]).to eql("4") + expect(row_session.sets[3]).to eql("4") + expect(row_session.sets[4]).to eql("4") + end + end +end diff --git a/spec/routing/dashboard_routing_spec.rb b/spec/routing/dashboard_routing_spec.rb index 1682f08..da37fa1 100644 --- a/spec/routing/dashboard_routing_spec.rb +++ b/spec/routing/dashboard_routing_spec.rb @@ -1,7 +1,7 @@ require "rails_helper" -RSpec.describe "/dashboard", type: :routing do +describe "/dashboard", type: :routing do it "routes to the items listing" do - expect(get: "/dashboard").to route_to("sessions#new") + expect(get: "/dashboard").to route_to("training_sessions#index") end end diff --git a/spec/support/stronglifts_program.rb b/spec/support/stronglifts_program.rb new file mode 100644 index 0000000..1854d7d --- /dev/null +++ b/spec/support/stronglifts_program.rb @@ -0,0 +1,12 @@ +shared_context "stronglifts_program" do + let!(:program) { Program.create!(name: "StrongLifts 5×5") } + let!(:squat) { Exercise.new(name: "Squat") } + let!(:workout_a) { program.workouts.create name: "A" } + let!(:squat_workout) { workout_a.add_exercise(squat, sets: 5, repetitions: 5) } + let!(:bench_workout) { workout_a.add_exercise(Exercise.new(name: "Bench Press"), sets: 5, repetitions: 5) } + let!(:row_workout) { workout_a.add_exercise(Exercise.new(name: "Barbell Row"), sets: 5, repetitions: 5) } + let!(:workout_b) { program.workouts.create name: "B" } + let!(:squat_workout_b) { workout_b.add_exercise(squat, sets: 5, repetitions: 5) } + let!(:overhead_press_workout) { workout_b.add_exercise(Exercise.new(name: "Overhead Press"), sets: 5, repetitions: 5) } + let!(:deadlift_workout) { workout_b.add_exercise(Exercise.new(name: "Deadlift"), sets: 1, repetitions: 5) } +end |
