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_str, am_pm, freeze_date = date_str) freeze_date = Date.strptime(freeze_date, "%m/%d/%Y") Timecop.freeze(freeze_date) stub_appointment_request(lab_code, date_str, am_pm, body) date = Date.strptime(date_str, "%m/%d/%Y") times = Medivo::Appointment.find(lab_code, date, am_pm) Timecop.return times end it "filters out past date times" do @all_times = ["11/16/2011|01:00 PM"] returned_times('11/19/2011', '') end it "filters out past date times" do returned_times('11/04/2011', '').should_not include "11/03/2011|08:30 AM" end it "filters out past or pm times" do returned_times('11/04/2011', 'AM').should == ["11/04/2011|08:30 AM"] end it "filters out past or am times" do returned_times('11/04/2011', '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('11/04/2011', '').should == ["11/04/2011|11:30 AM"] end it "orders the time strings by date" do returned_times('11/04/2011', '').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", "11/06/2011|01:30 PM", "11/04/2011|10:00 AM", "11/05/2011|3:30 PM"] returned_times('11/06/2011', '', "11/04/2011").should == ["11/06/2011|12:30 AM", "11/06/2011|01:30 PM", "11/05/2011|08:30 AM", "11/05/2011|3:30 PM", "11/04/2011|10:00 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