spec/calendar_spec.rb in calendarium-romanum-remote-0.1.0 vs spec/calendar_spec.rb in calendarium-romanum-remote-0.2.0

- old
+ new

@@ -1,18 +1,21 @@ require_relative 'spec_helper' +# integration tests exercizing the whole stack and +# making (a lot of) actual HTTP requests! describe CalendariumRomanum::Remote::Calendar do CR = CalendariumRomanum - CRR = CalendariumRomanum::Remote + CRRemote = CalendariumRomanum::Remote let(:year) { 2016 } - let(:calendar) { described_class.new year, REMOTE_CALENDAR_URI } + let(:uri) { REMOTE_CALENDAR_URI } + let(:calendar) { described_class.new year, uri } describe '#year' do it 'returns the year passed to the constructor' do y = 2200 - cal = described_class.new y, REMOTE_CALENDAR_URI + cal = described_class.new y, uri expect(cal.year).to eq y end end describe '#lectionary' do @@ -23,19 +26,19 @@ it { expect(calendar.ferial_lectionary).to be 1 } end describe '#==' do describe 'same year and URI' do - let(:c) { described_class.new year, REMOTE_CALENDAR_URI } + let(:c) { described_class.new year, uri } it 'is same' do expect(c == calendar).to be true end end describe 'year differs' do - let(:c) { described_class.new year + 1, REMOTE_CALENDAR_URI } + let(:c) { described_class.new year + 1, uri } it 'is different' do expect(c == calendar).to be false end end @@ -48,19 +51,17 @@ end end end describe 'API-compatibility with Calendar' do - cls = CR::Calendar - describe 'instance methods' do let(:year) { 2000 } let(:origin) { CR::Calendar.new(year) } - let(:mirror) { CRR::Calendar.new(year, REMOTE_CALENDAR_URI) } + let(:mirror) { CRRemote::Calendar.new(year, uri) } - cls.public_instance_methods.each do |method| + CR::Calendar.public_instance_methods.each do |method| describe method do it 'is defined by origin' do expect(origin).to respond_to method end @@ -79,26 +80,78 @@ end end end describe 'returns the same data as Calendar' do - let(:year) { 2016 } - - # classical calendar + # classical calendar with the same settings as `calendar` let(:sanctorale) { CR::Data::GENERAL_ROMAN_ENGLISH.load } - let(:calendar) { CR::Calendar.new(year, sanctorale) } + let(:local_calendar) { CR::Calendar.new(year, sanctorale) } - # remote calendar with the same settings - let(:uri) { REMOTE_CALENDAR_URI } - let(:remote) { described_class.new(year, uri) } + let(:remote_calendar) { calendar } days = (CR::Temporale::Dates.first_advent_sunday(2016) ... CR::Temporale::Dates.first_advent_sunday(2017)) days.each do |date| it date do - c = calendar.day date - r = remote.day date + c = local_calendar.day date + r = remote_calendar.day date expect(r).to eq c + end + end + end + + describe 'errors' do + let(:date) { Date.new year, 1, 1 } + + describe 'server does not exist' do + let(:uri) { 'http://does-not-exist.local/' } + + it 'throws SocketError' do + expect do + calendar.day date + end.to raise_exception ::SocketError + end + end + + describe 'server exists, wrong calendar URI' do + let(:wrong_calendar_uri) { uri + 'another-path-segment/' } + let(:calendar) { described_class.new year, wrong_calendar_uri } + + it 'throws CalendariumRomanum::Remote::UnexpectedResponseError' do + expect do + calendar.day date + end.to raise_exception CRRemote::UnexpectedResponseError, /Unexpected HTTP status 404/ + end + end + + describe 'bad request - invalid date' do + # this shouldn't ever happen, but it's the easiest way + # to check how the calendar behaves in bad request scenarios + let(:date) do + d = Date.new + d.stub(:year) { 2000 } + d.stub(:month) { 13 } + d.stub(:day) { 40 } + + d + end + + it 'throws CalendariumRomanum::Remote::UnexpectedResponseError' do + expect do + calendar.day date + end.to raise_exception CRRemote::UnexpectedResponseError, /Unexpected HTTP status 400/ + end + end + + describe 'unsupported response content' do + it 'fails' do + # HTTP request will return empty JSON + response = HTTPI::Response.new 200, {}, '{}' + allow(HTTPI).to receive(:get).and_return(response) + + expect do + calendar.day date + end.to raise_exception CRRemote::InvalidDataError, /Invalid data: {:date=>\["is missing"/ end end end end