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 "gender field must be Male or Female"
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 "save failure returning status code 422 sets an error message instead of throwing exception" do
stub_request(:post, "http://test:test@test.medivo.com/customers.xml").
to_return(:body => 'Invalid PSC location',
:status => 422)
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, # let's pretend this is invalid psc
: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 == ["Invalid PSC location"]
# 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 "getting requisitions with" do
before(:each) do
@requisition_id = 170420
order = Medivo::Order.new(:requisition_number=> @requisition_id)
stub_order_requisition(@requisition_id, medivo_xml_fixture_path("lc_order_with_requisition.xml"))
end
let(:order) { Medivo::Order.find_with_requisition(@requisition_id) }
it "#find_with_requisition finds the order including the requisition info" do
order.should_not be_nil
order.sameday_requisition.should_not be_nil
end
it "#pdf_requisition returns requisition as pdf" do
pdf = order.pdf_requisition
text = pdf_stream_to_text(pdf)
text.should match /This order expires 2012-02-12/
text.should match /Account #:111111111 Req\/Control #:170420/
text.should match /Draw Location LABCORP, BEVERLY HILLS, 465 N ROXBURY DR STE 715 BEVERLY HILLS, CA/
end
it "class method #pdf_requisition returns pdf requisition" do
pdf = Medivo::Order.pdf_requisition(@requisition_id)
text = pdf_stream_to_text(pdf)
text.should match /This order expires 2012-02-12/
text.should match /Account #:111111111 Req\/Control #:170420/
text.should match /Draw Location LABCORP, BEVERLY HILLS, 465 N ROXBURY DR STE 715 BEVERLY HILLS, CA/
end
end
describe "getting results with" do
before(:each) do
@requisition_id = 170853
order = Medivo::Order.new(:requisition_number=>@requisition_id)
stub_order_result(@requisition_id, medivo_xml_fixture_path("lc_order_with_positive_results.xml"))
end
let(:order) { Medivo::Order.find_with_results(@requisition_id) }
it "#find_with_results finds the order including results info" do
order.should_not be_nil
end
it "#results returns results info like test names" do
order.results.should_not be_nil
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 "#results_summary returns has reconcilled results" do
order.results_summary.should_not be_nil
end
it "#pdf_result returns has results as pdf" do
pdf = order.pdf_result
text = pdf_stream_to_text(pdf)
text.should match /COLLECTED:2011-09-06 RECEIVED:2011-09-06 REPORTED:2011-09-06/
end
it "class method #pdf_result returns has results as pdf" do
pdf = Medivo::Order.pdf_result(@requisition_id)
text = pdf_stream_to_text(pdf)
text.should match /COLLECTED:2011-09-06 RECEIVED:2011-09-06 REPORTED:2011-09-06/
end
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.strip_space.should == File.read("#{ENGINE_PATH}/spec/fixtures/xml/valid_uhc_order.xml").strip_space
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