require "ar_helper" require "study_engine/study_id/bank" module StudyEngine describe StudyID::Bank do let!(:study_id_a) { described_class.find_or_create_from_full_study_id("FIX-AAA-1000") } let!(:study_id_b) { described_class.find_or_create_from_full_study_id("FIX-AAA-1001") } let!(:study_id_c) { described_class.find_or_create_from_full_study_id("FIX-BBB-1000") } let!(:study_id_d) { described_class.find_or_create_from_full_study_id("OUT-AAA-1000") } describe ".find_or_create_from_full_study_id study_id" do it "returns a record if one already exists" do expect { subject = described_class.find_or_create_from_full_study_id("FIX-BBB-1000") subject.should == study_id_c }.to_not change { described_class.count } end it "creates a new record if one doesn't exist" do expect { described_class.find_or_create_from_full_study_id("TAU-BAM-1000") }.to change { described_class.count }.by(1) end end it ".studies" do described_class.studies.should == %w(FIX OUT) end it ".sites" do described_class.sites.should == %w(AAA BBB) end it ".ids_grouped_by_study_and_site" do described_class.ids_grouped_by_study_and_site.should == { "FIXAAA" => ["1000","1001"], "FIXBBB" => ["1000"], "OUTAAA" => ["1000"], } end it ".studies_grouped_by_site" do described_class.studies_grouped_by_site.should == { "AAA" => Set.new(["FIX", "OUT"]), "BBB" => Set.new(["FIX"]), } end describe "valid?" do subject { study_id_a } it "ensures study is three uppercase letters" do subject.study_id_study = "AB" subject.should_not be_valid subject.errors.full_messages.should == ["Study id study is invalid"] end it "ensures site is three uppercase letters" do subject.study_id_site = "abc" subject.should_not be_valid subject.errors.full_messages.should == ["Study id site is invalid"] end it "ensures id is four numbers" do subject.study_id_id = "123" subject.should_not be_valid subject.errors.full_messages.should == ["Study id is invalid"] end end end end