require 'spec_helper' describe Medivo::Order do it "requires fields to be present" do order = Medivo::Order.new({:email=>"dude"}) order.valid? order.errors.messages.keys.should_not include :email order.errors.messages.keys.should include :address, :zip, :dob, :home_phone, :psc, :test_types, :gender, :account_number end it "requires proper gender" do order = Medivo::Order.new({:gender=>"dude"}) order.valid? order.errors.messages[:gender].should include "is not included in the list" end it "requires proper dob" do order = Medivo::Order.new({:dob=>"20110103"}) order.valid? order.errors.messages[:dob].should == nil order.dob = "201101" order.valid? order.errors.messages[:dob].should include "invalid dob. needs to be in '%Y%m%d' format" end it "test_types should be an string of test id's delimited by comma" do order = Medivo::Order.new({:test_types=>[1, 2]}) order.valid? order.errors.messages[:test_types].should be_present end it "sets default fields" do order = Medivo::Order.new order.draw_location.should == 'PSC' order.take_tests_same_day.should == true end it "has requisition_id field alias to requisition_number" do @order = Medivo::Order.new(:requisition_number=>30) @order.requisition_id.should == @order.requisition_number end it "save with incorrect test ids returning 404 sets an error message instead of throwing exception" do stub_request(:post, "http://test:test@test.medivo.com/customers.xml"). to_return(:body => 'Test type not found: 2020', :status => 404) order = Medivo::Order.new( :first_name=>'dan', :last_name=>'dude', :email => "dude@dude.com", :address=> "my house", :city=> "dudeville", :state=> "CA", :zip=> "90210", :dob=> "19801201", :home_phone=> "4155551212", :psc=> 11, :test_types=> "2020", :account_number => "423", :gender => "Male", :dob => "19801201") order.save.should == false order.valid?.should == false order.errors.messages.keys.should include :base order.errors.messages[:base].should == ["Test type not found: 2020"] # fixing the problem .. it now saves and is valid stub_request(:post, "http://test:test@test.medivo.com/customers.xml"). to_return(:status => 200) order.save.should == true order.valid?.should == true end it "creates valid order" do order = Medivo::Order.new( :first_name=>'dan', :last_name=>'dude', :email => "dude@dude.com", :address=> "my house", :city=> "dudeville", :state=> "CA", :zip=> "90210", :dob=> "19801201", :home_phone=> "4155551212", :psc=> 11, # lab_id :test_types=> "2020", # test_id's :account_number => "423", :gender => "Male", :dob => "19801201") order.valid?.should == true stub_request(:post, "http://test:test@test.medivo.com/customers.xml"). to_return(:body => order.to_xml, :status => 200) order.save.should == true end describe "finding order" do before do @order = Medivo::Order.new(:requisition_number=>30) stub_request(:get, "http://test:test@test.medivo.com/customers/#{@order.requisition_number}.xml"). to_return(:status => 200, :body => @order.to_xml, :headers => {}) end it "finds medivo order" do Medivo::Order.find(@order.requisition_number).should == @order end it "#get_order finds medivo order" do Medivo::Order.get_order(@order.requisition_number).should == @order end end describe "#results" do before(:all) do @reqiuisition_number = 170853 order = Medivo::Order.new(:requisition_number=>@reqiuisition_number) stub_request(:get, "test:test@test.medivo.com/customers/#{order.requisition_id}.xml?include=reconciled_results"). to_return(:body => File.read(("#{ENGINE_PATH}/spec/fixtures/lc_positive_results.xml"))) end describe "#find_with_results" do before(:all) { @order = Medivo::Order.find_with_results(@reqiuisition_number) } it "gets order with results" do @order.should_not be_nil end it "has test names" do @order.results.collect(&:result_lab_name).should == ["HEPATITIS C ANTIBODY", "SIGNAL TO CUT-OFF", "HCV RNA, QUANTITATIVE REAL TIME PCR", "HCV RNA, QUANTITATIVE REAL TIME PCR", nil] end it "has reconcilled results" do @order.results_summary.should_not be_nil end end end end describe Medivo::PrimaryInsurance do it "makes valid model" do insurance_info = Medivo::PrimaryInsurance.new( :relationship=>1, :company_name=>"Dude Insurance", :policy_number=>"1234", :group_number =>"555" ) insurance_info.valid?.should == true end it "shows error messages for invalid model" do insurance_info = Medivo::PrimaryInsurance.new( :company_name=>"Dude Insurance", :policy_number=>"1234", :group_number =>"555" ) insurance_info.valid?.should == false insurance_info.errors.messages.keys.should == [:relationship] insurance_info.errors.messages[:relationship].should include "can't be blank" end end describe Medivo::InsuranceOrder do it "requires primary insurance" do order = Medivo::InsuranceOrder.new order.valid? order.errors.messages[:primary_insurance].should include "primary_insurance field must be present" end it "shows missing fields from primary insurance" do order = Medivo::InsuranceOrder.new(:primary_insurance => Medivo::PrimaryInsurance.new) order.valid? order.errors.messages[:"primary_insurance.company_name"].should include "can't be blank" order.errors.messages[:"primary_insurance.relationship"].should include "can't be blank" end end describe "creating orders" do it "UHC Commercial insurance order" do insurance_info = Medivo::PrimaryInsurance.new( :relationship=>1, :company_name=>"Dude Insurance", :policy_number=>"1234", :group_number =>"555" ) order = Medivo::InsuranceOrder.new( :primary_insurance => insurance_info, :diagnosis_code => [{:icd=>9, :code=>'250.00'}], :first_name=>'dan', :last_name=>'dude', :email => "dude@dude.com", :address=> "my house", :city=> "dudeville", :state=> "CA", :zip=> "90210", :dob=> "19801201", :home_phone=> "4155551212", :psc=> 1752, # lab_id :test_types=>"001453,120295", # test_id's :account_number => "05021390", :gender => "Male", :dob => "19801201" ) mock.proxy(order).to_xml({}) do |value| value.gsub(/\s/, '').should == File.read("#{ENGINE_PATH}/spec/fixtures/valid_uhc_order.xml").gsub(/\s/, '') value end stub_request(:post, "http://test:test@test.medivo.com/customers.xml").to_return(:status=> 200) order.save order.errors.messages.should be_empty end end