require "ar_helper" require "study_engine/existing_assessments_form" require "study_engine/result" require "study_engine/config" module StudyEngine describe ExistingAssessmentsForm do before do stub_const "StudyID::Bank", double(studies: %w(FIX OUT PAN TAO OXY)) StudyEngine.events = ["Baseline", "6 month", "12 month"] end describe "#study_id_site=" do it "upcases input" do subject.study_id_site = "aaa" subject.study_id_site.should == "AAA" end end describe "study_id" do it "enforces format" do subject.study_id_study = "omg" subject.study_id_site = "wtf" subject.study_id_id = "bbq" subject.valid? subject.errors.full_messages.should include("METRC study ID must be in the following format: XXX-XXX-####") end it "enforces presence" do subject.valid? subject.errors.full_messages.should include("METRC study ID can not be blank. Enter ID in the following format: XXX-XXX-####") end end context "with no events" do it "is cool with a baseline event being created" do subject.study_id_study = "OUT" subject.study_id_site = "BBB" subject.study_id_id = "1234" subject.event_datetimes = ["2000-01-01 12:00:00", "", ""] subject.should be_valid subject.save.should be_truthy a, _ = Assessment.all.map(&:attributes).map(&:symbolize_keys) a.should include({ id: 1, created_at: Time.parse("2000-01-01 12:00:00 UTC"), assessment_updated_at: Time.parse("2000-01-01 12:00:00 UTC"), study_id: StudyID.new("OUT-BBB-1234"), event: "Baseline" }) end it "is cool with both baseline and six month events being created" do subject.study_id_study = "OUT" subject.study_id_site = "BBB" subject.study_id_id = "1234" subject.event_datetimes = ["2000-01-01 12:00:00", "2001-01-01 13:00:00", ""] subject.should be_valid subject.save.should be_truthy a1, a2 = Assessment.all.map(&:attributes).map(&:symbolize_keys) a1.should include({ id: 1, created_at: Time.parse("2000-01-01 12:00:00 UTC"), assessment_updated_at: Time.parse("2000-01-01 12:00:00 UTC"), study_id: StudyID.new("OUT-BBB-1234"), event: "Baseline" }) a2.should include({ id: 2, created_at: Time.parse("2001-01-01 13:00:00 UTC"), assessment_updated_at: Time.parse("2001-01-01 13:00:00 UTC"), study_id: StudyID.new("OUT-BBB-1234"), event: "6 month" }) end it "is cool with baseline, six month, and twelve month events being created" do subject.study_id_study = "OUT" subject.study_id_site = "BBB" subject.study_id_id = "1234" subject.event_datetimes = ["2000-01-01 12:00:00", "2001-01-01 13:00:00", "2002-01-01 14:00:00"] subject.should be_valid subject.save.should be_truthy a1, a2, a3 = Assessment.all.map(&:attributes).map(&:symbolize_keys) a1.should include({ id: 1, created_at: Time.parse("2000-01-01 12:00:00 UTC"), assessment_updated_at: Time.parse("2000-01-01 12:00:00 UTC"), study_id: StudyID.new("OUT-BBB-1234"), event: "Baseline" }) a2.should include({ id: 2, created_at: Time.parse("2001-01-01 13:00:00 UTC"), assessment_updated_at: Time.parse("2001-01-01 13:00:00 UTC"), study_id: StudyID.new("OUT-BBB-1234"), event: "6 month" }) a3.should include({ id: 3, created_at: Time.parse("2002-01-01 14:00:00 UTC"), assessment_updated_at: Time.parse("2002-01-01 14:00:00 UTC"), study_id: StudyID.new("OUT-BBB-1234"), event: "12 month", group_index: nil }) end it "complains about at least one event being specified" do subject.valid? subject.errors.full_messages.should include("A baseline, 6 month, or 12 month timestamp must be present") end it "complains if a six month is specified without a baseline" do subject.study_id_study = "OUT" subject.study_id_site = "BBB" subject.study_id_id = "1234" subject.event_datetimes = ["", "2000-01-01 12:00:00", ""] subject.valid? subject.errors.full_messages.should include("Cannot add a 6 month assessment when baseline assessment does not exist") end it "complains if a twelve month is specified without a six month" do subject.study_id_study = "OUT" subject.study_id_site = "BBB" subject.study_id_id = "1234" subject.event_datetimes = ["", "", "2000-01-01 12:00:00"] subject.valid? subject.errors.full_messages.should include("Cannot add a 12 month assessment when 6 month assessment does not exist") end end context "with a baseline event" do before do Assessment.create!({ study_id: "OUT-BBB-1234", event: "Baseline", created_at: "1999-01-01 00:00:00 UTC", assessment_updated_at: "1999-01-01 00:00:00 UTC", }) end it "is cool with a six month event being created" do subject.study_id_study = "OUT" subject.study_id_site = "BBB" subject.study_id_id = "1234" subject.event_datetimes = ["", "2000-01-01 12:00:00", ""] subject.should be_valid subject.save.should be_truthy a1, a2 = Assessment.all.map(&:attributes).map(&:symbolize_keys) a1.should include({ id: 1, created_at: Time.parse("1999-01-01 00:00:00 UTC"), assessment_updated_at: Time.parse("1999-01-01 00:00:00 UTC"), study_id: StudyID.new("OUT-BBB-1234"), event: "Baseline" }) a2.should include({ id: 2, created_at: Time.parse("2000-01-01 12:00:00 UTC"), assessment_updated_at: Time.parse("2000-01-01 12:00:00 UTC"), study_id: StudyID.new("OUT-BBB-1234"), event: "6 month" }) end it "is cool with a six month and twelve month event being created" do subject.study_id_study = "OUT" subject.study_id_site = "BBB" subject.study_id_id = "1234" subject.event_datetimes = ["", "2000-01-01 12:00:00", "2001-01-01 13:00:00"] subject.should be_valid subject.save.should be_truthy a1, a2, a3 = Assessment.all.map(&:attributes).map(&:symbolize_keys) a1.should include({ id: 1, created_at: Time.parse("1999-01-01 00:00:00 UTC"), assessment_updated_at: Time.parse("1999-01-01 00:00:00 UTC"), study_id: StudyID.new("OUT-BBB-1234"), event: "Baseline" }) a2.should include({ id: 2, created_at: Time.parse("2000-01-01 12:00:00 UTC"), assessment_updated_at: Time.parse("2000-01-01 12:00:00 UTC"), study_id: StudyID.new("OUT-BBB-1234"), event: "6 month" }) a3.should include({ id: 3, created_at: Time.parse("2001-01-01 13:00:00 UTC"), assessment_updated_at: Time.parse("2001-01-01 13:00:00 UTC"), study_id: StudyID.new("OUT-BBB-1234"), event: "12 month" }) end end context "with both baseline and six month events" do before do Assessment.create!({ study_id: "OUT-BBB-1234", event: "Baseline", created_at: "1999-01-01 00:00:00 UTC", assessment_updated_at: "1999-01-01 00:00:00 UTC", }) Assessment.create!({ study_id: "OUT-BBB-1234", event: "6 month", created_at: "2000-01-01 12:00:00 UTC", assessment_updated_at: "2000-01-01 12:00:00 UTC", }) end it "is cool with a twelve month event being created" do subject.study_id_study = "OUT" subject.study_id_site = "BBB" subject.study_id_id = "1234" subject.event_datetimes = ["", "", "2001-01-01 13:00:00"] subject.should be_valid subject.save.should be_truthy a1, a2, a3 = Assessment.all.map(&:attributes).map(&:symbolize_keys) a1.should include({ id: 1, created_at: Time.parse("1999-01-01 00:00:00 UTC"), assessment_updated_at: Time.parse("1999-01-01 00:00:00 UTC"), study_id: StudyID.new("OUT-BBB-1234"), event: "Baseline" }) a2.should include({ id: 2, created_at: Time.parse("2000-01-01 12:00:00 UTC"), assessment_updated_at: Time.parse("2000-01-01 12:00:00 UTC"), study_id: StudyID.new("OUT-BBB-1234"), event: "6 month" }) a3.should include({ id: 3, created_at: Time.parse("2001-01-01 13:00:00 UTC"), assessment_updated_at: Time.parse("2001-01-01 13:00:00 UTC"), study_id: StudyID.new("OUT-BBB-1234"), event: "12 month" }) end end end end