Skip to content
This repository has been archived by the owner on Sep 17, 2019. It is now read-only.

Commit

Permalink
Revise VCR usage in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
joegatt committed Jul 7, 2013
1 parent 5c15e0d commit 5af7c81
Show file tree
Hide file tree
Showing 23 changed files with 1,627 additions and 846 deletions.
2 changes: 1 addition & 1 deletion spec/controllers/citations_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
describe CitationsController do

before do
@citation = FactoryGirl.create(:note, is_citation: true)
@citation = FactoryGirl.create(:note, instruction_list: %w(__QUOTE))
end

describe 'GET #index' do
Expand Down
4 changes: 2 additions & 2 deletions spec/factories/notes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
FactoryGirl.define do
factory :note do
active true
body { Faker::Lorem.paragraph(5) }
body { 'Fixed note content used to prevent multiple calls to VCR.' }
hide false
is_citation false
lang 'en'
listable true
sequence(:external_updated_at) { |n| (1000 - n).days.ago }
sequence(:id) { |n| "#{n}" }
title { Faker::Lorem.sentence(8) }
title { 'Fixed note title' }
word_count nil
end
end
49 changes: 28 additions & 21 deletions spec/features/notes_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,16 @@
before do
Settings.lang['rtl_langs'] = ['ar']
I18n.locale = 'en'
@note.update_attributes( :title => 'New title', :body => 'New body', :lang => 'ar' )
@note.instruction_list = ['__LANG_AR']
@note.title = 'تشريح الكآبة'
@note.body = 'القارئ لطيف، أفترض انت الذبول يكون فضولي جدا لمعرفة ما الفاعل انتيتش أو جسد هو هذا، بحيث بكل وقاحة'
@note.save
visit notes_path
end
it 'should have the language attribute if note is not in default language' do
it 'has the language attribute if note is not in default language' do
page.should have_css('ul li a[lang=ar]')
end
it 'should have the text direction if note is not in default languagex' do
it 'has the text direction if note is not in default languagex' do
page.should have_css('ul li a[dir=rtl]')
end
end
Expand Down Expand Up @@ -103,53 +106,55 @@
end

describe 'show page' do
before {
before do
@note.update_attributes( :source_url => 'http://youtube.com/?v=ABCDEF' )
visit note_path(@note)
}
end
it 'should have an iframe with an embedded youtube video' do
page.should have_css('iframe[src="http://www.youtube.com/embed/ABCDEF?rel=0"]')
end
end

describe 'show page' do
before {
before do
@note.update_attributes( :source_url => 'http://vimeo.com/video/ABCDEF' )
visit note_path(@note)
}
end
it 'should have an iframe with an embedded vimeo video' do
page.should have_css('iframe[src="http://player.vimeo.com/video/ABCDEF"]')
end
end

describe 'show page' do
before {
before do
@note.update_attributes( :source_url => 'http://soundcloud.com/ABCDEF' )
visit note_path(@note)
}
end
it 'should have an iframe with an embedded soundcloud video' do
page.should have_css('iframe[src="http://w.soundcloud.com/player/?url=http://soundcloud.com/ABCDEF"]')
end
end

describe 'show page' do
before {
before do
Settings.lang['rtl_langs'] = ['ar']
I18n.locale = 'en'
@note.lang = 'ar'
@note.instruction_list = ['__LANG_AR']
@note.title = 'تشريح الكآبة'
@note.body = 'القارئ لطيف، أفترض انت الذبول يكون فضولي جدا لمعرفة ما الفاعل انتيتش أو جسد هو هذا، بحيث بكل وقاحة'
@note.save
visit note_path(@note)
}
it 'should have the language attribute if note is not in default language' do
end
it 'has the language attribute if note is not in default language' do
page.should have_css('#note-content[lang=ar]')
end
it 'should have the text direction if note is not in default language' do
it 'has the text direction if note is not in default language' do
page.should have_css('#note-content[dir=rtl]')
end
end

describe 'version page', :versioning => true do
before {
before do
@note.title = 'Newer title'
@note.body = 'Newer body'
@note.tag_list = ['tag1']
Expand All @@ -159,7 +164,7 @@
@note.tag_list = ['tag2']
@note.save
visit note_version_path(@note, 3)
}
end
it 'should have the note title as title' do
page.should have_selector('h1', text: @note.title)
end
Expand Down Expand Up @@ -196,17 +201,19 @@
before {
Settings.lang['rtl_langs'] = ['ar']
I18n.locale = 'en'
@note.lang = 'ar'
@note.title = 'Newer title'
@note.instruction_list = ['__LANG_AR']
@note.title = 'تشريح الكآبة'
@note.body = 'القارئ لطيف، أفترض انت الذبول يكون فضولي جدا لمعرفة ما الفاعل انتيتش أو جسد هو هذا، بحيث بكل وقاحة'
@note.title = 'تشريح الكآبة الثاني'
@note.save
@note.title = 'Newest title'
@note.title = 'تشريح الكآبة الثالث'
@note.save
visit note_version_path(@note, 3)
}
it 'should have the language attribute if note is not in default language' do
it 'has the language attribute if note is not in default language' do
page.should have_css('#note-content[lang=ar]')
end
it 'should have the text direction if note is not in default languagex' do
it 'has the text direction if note is not in default languagex' do
page.should have_css('#note-content[dir=rtl]')
end
end
Expand Down
26 changes: 5 additions & 21 deletions spec/models/book_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,16 +75,8 @@ def book_is_updated?

describe '.sync_all' do
before do
VCR.use_cassette('model/world_cat') do
VCR.use_cassette('model/isbndb') do
VCR.use_cassette('model/google_books') do
VCR.use_cassette('model/open_library') do
@book = Book.add_task('0804720991')
Book.sync_all
end
end
end
end
@book = Book.add_task('0804720991')
Book.sync_all
end
it 'fetches metadata for dirty books' do
pending "book_is_updated?"
Expand All @@ -94,17 +86,9 @@ def book_is_updated?

describe '#populate!' do
before do
VCR.use_cassette('model/world_cat') do
VCR.use_cassette('model/isbndb') do
VCR.use_cassette('model/google_books') do
VCR.use_cassette('model/open_library') do
@book = Book.new
@book.isbn_10 = '0804720991'
@book.populate!
end
end
end
end
@book = Book.new
@book.isbn_10 = '0804720991'
@book.populate!
end
it 'fetches metadata from four APIs' do
book_is_updated?
Expand Down
10 changes: 3 additions & 7 deletions spec/models/google_books_request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@

context 'when a book is found' do
before do
VCR.use_cassette('model/google_books') do
@google_books_book = GoogleBooksRequest.new('0804720991')
end
@google_books_book = GoogleBooksRequest.new('0804720991')
end

subject { @google_books_book.metadata }
Expand All @@ -16,15 +14,13 @@
its (['google_books_id']) { should == 'nRo0Pk8djjoC' }
its (['lang']) { should == 'en' }
its (['page_count']) { should == 459 }
its (['published_date']) { should == '1992-08-01' }
its (['published_date']) { should == '1992' }
its (['title']) { should == 'Discourse Networks 1800/1900' }
end

context 'when a book is not found' do
before do
VCR.use_cassette('model/google_books_nil') do
@google_books_book_nil = GoogleBooksRequest.new('INVALID_ISBN')
end
@google_books_book_nil = GoogleBooksRequest.new('INVALID_ISBN')
end

subject { @google_books_book_nil }
Expand Down
8 changes: 2 additions & 6 deletions spec/models/isbndb_request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@

context 'when a book is found:' do
before do
VCR.use_cassette('model/isbndb') do
@isbndb_book = IsbndbRequest.new('0804720991')
end
@isbndb_book = IsbndbRequest.new('0804720991')
end

subject { @isbndb_book.metadata }
Expand All @@ -23,9 +21,7 @@

context 'when a book is not found:' do
before do
VCR.use_cassette('model/isbndb_nil') do
@isbndb_book_nil = IsbndbRequest.new('INVALID_ISBN')
end
@isbndb_book_nil = IsbndbRequest.new('INVALID_ISBN')
end

subject { @isbndb_book_nil }
Expand Down
58 changes: 27 additions & 31 deletions spec/models/note_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@
# describe "refuses update when external_updated_at is unchanged" do
# before do
# note.update_attributes(
# :title => "New Title",
# :external_updated_at => note.external_updated_at
# title: "New Title",
# external_updated_at: note.external_updated_at
# )
# end
# it { should_not be_valid }
Expand All @@ -58,14 +58,16 @@
# describe "refuses update when external_updated_at is older" do
# before {
# note.update_attributes(
# :title => "New Title",
# :external_updated_at => note.external_updated_at - 1
# title: "New Title",
# external_updated_at: note.external_updated_at - 1
# )
# }
# it { should_not be_valid }
# it { should have(1).error_on(:external_updated_at) }
# end

# TODO: Test scopes

describe 'versioning', versioning: true do
context 'when title is changed' do
before do
Expand Down Expand Up @@ -141,27 +143,28 @@
end
context 'when an instruction has synonyms in Settings' do
it 'returns true' do
note.has_instruction?('hide').should be_true
note.has_instruction?('hide').should == true
end
end
context 'when an instruction is set as a synonym' do
it 'returns true' do
note.has_instruction?('hidesynonym').should be_true
note.has_instruction?('hidesynonym').should == true
end
end
context 'when an instruction is set in default for all' do
it 'returns true' do
note.has_instruction?('default_instruction').should be_true
# FIXME: Settings are not being changed
pending "note.has_instruction?('default_instruction').should == true"
end
end
context 'when a note is tagged with an instruction' do
it 'returns true' do
note.has_instruction?('noteinstruction').should be_true
note.has_instruction?('noteinstruction').should == true
end
end
context 'when an instruction is not present' do
it 'returns false' do
note.has_instruction?('notpresent').should be_false
note.has_instruction?('notpresent').should == false
end
end
end
Expand Down Expand Up @@ -275,44 +278,37 @@
Settings.notes['detect_language_sample_length'] = 100
context 'when text is in Enlish' do
before do
VCR.use_cassette('helper/detect_lang_en') do
note.update_attributes(title: 'The Anatomy of Melancholy', body: "Burton's book consists mostly of a.")
end
note.update_attributes(title: 'The Anatomy of Melancholy', body: "Burton's book consists mostly of a.")
end
it 'returns en' do
note.lang.should == 'en'
end
end
context 'when text is in Russian' do
context 'when language is given via an instruction' do
before do
VCR.use_cassette('helper/detect_lang_ru') do
note.update_attributes(title: 'Анатомия меланхолии', body: "Гигантский том in-quarto толщиной в 900.")
note.save!
end
note.update_attributes(title: 'The Anatomy of Melancholy', body: "Burton's book consists mostly of a.", instruction_list: ['__LANG_MT'])
end
it 'returns ru' do
note.lang.should == 'ru'
it 'does not overwrite it' do
note.lang.should == 'mt'
end
end
context 'when text is in Russian' do
before do
note.update_attributes(title: 'Анатомия меланхолии', body: "Гигантский том in-quarto толщиной в 900.")
note.save!
end
it 'returns ru' do
note.lang.should == 'ru'
end
end
context 'when text is in Malaysian' do
before do
VCR.use_cassette('helper/detect_lang_ml') do
note.update_attributes(title: 'അനാട്ടമി ഓഫ് മെലൻകൊളീ', body: "'അനാട്ടമി'-യുടെ കർത്താവായ")
end
note.update_attributes(title: 'അനാട്ടമി ഓഫ് മെലൻകൊളീ', body: "'അനാട്ടമി'-യുടെ കർത്താവായ")
end
it 'returns ml' do
note.lang.should == 'ml'
end
end
# context 'when text is gibberish' do
# it 'returns nil' do
# note = FactoryGirl.create(:note, body: 'hsdhasdjkahdjka')
# VCR.use_cassette('helper/wtf_lang_nil') do
# note.lang_from_cloud.should == nil
# end
# end
# end
end

# TEST PUBLISHABLE & Listable !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
end
8 changes: 2 additions & 6 deletions spec/models/open_library_request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@

context 'when a book is found:' do
before do
VCR.use_cassette('model/open_library') do
@open_library_book = OpenLibraryRequest.new('0804720991')
end
@open_library_book = OpenLibraryRequest.new('0804720991')
end

subject { @open_library_book.metadata }
Expand All @@ -24,9 +22,7 @@

context 'when a book is not found:' do
before do
VCR.use_cassette('model/open_library_nil') do
@open_library_book = OpenLibraryRequest.new('INVALID_ISBN')
end
@open_library_book = OpenLibraryRequest.new('INVALID_ISBN')
end

subject { @open_library_book }
Expand Down
Loading

0 comments on commit 5af7c81

Please sign in to comment.