require 'spec_helper' describe Medivo::Appointment do let(:lab_code) { '20060' } describe "finding appointments" do describe "with valid data" do before do @all_times = ["11/03/2011|08:30 AM", "11/04/2011|08:30 PM", "11/04/2011|08:30 AM"] end let(:body) { {'times'=> @all_times} } def returned_times(date, am_pm, freeze_date = date) Timecop.freeze(Date.parse(freeze_date)) stub_appointment_request(lab_code, date, am_pm, body) value = Medivo::Appointment.find(lab_code, date, am_pm) Timecop.return value end it "filters out past date times" do returned_times('2011-11-04', '').should_not include "11/03/2011|08:30 AM" end it "filters out past or pm times" do returned_times('2011-11-04', 'AM').should == ["11/04/2011|08:30 AM"] end it "filters out past or am times" do returned_times('2011-11-04', 'PM').should == ["11/04/2011|08:30 PM"] end it "filters out invalid time strings" do @all_times = ["11/02011|08:30 AM", "11/04/2011|11:30 AM"] returned_times('2011-11-04', '').should == ["11/04/2011|11:30 AM"] end it "orders the time strings by date" do returned_times('2011-11-04', '').should == ["11/04/2011|08:30 AM", "11/04/2011|08:30 PM"] end it "orders the time strings by nearness to search date" do @all_times = ["11/06/2011|12:30 AM", "11/04/2011|08:30 PM", "11/05/2011|08:30 AM"] returned_times('2011-11-06', '', "2011-11-04").should == ["11/06/2011|12:30 AM", "11/05/2011|08:30 AM", "11/04/2011|08:30 PM"] end end describe "with invalid request returns error message" do let(:body) { {'message'=> "invalid data"} } it "for status 400" do date = '11/01/2011' stub_appointment_request(lab_code, date, '', body, 400) expect { Medivo::Appointment.find(lab_code, date) }.to raise_error end end end describe "making appointments" do let(:time) { Time.zone.parse("2011-11-17 09:30 AM") } let(:user) { {first_name: 'dude', last_name: 'man', date_of_birth: '12/28/1980', email_address: 'dudeman@dude.com', phone_number: '4155551212'} } let(:confirmation_code) { '123' } it "can make the appointment and returns nil" do body = {confirmation: confirmation_code} stub_appointment_confirmation(lab_code, time, user, body, 200) Medivo::Appointment.make(lab_code, time, user).should == confirmation_code end it " the appointment and returns nil" do body = {confirmation: nil} stub_appointment_confirmation(lab_code, time, user, body, 200) Medivo::Appointment.make(lab_code, time, user).should == nil end end end