Skip to content

Commit c094fe3

Browse files
committed
Cleaned up the error specs, increased rcov to 100%
1 parent 1b61014 commit c094fe3

File tree

4 files changed

+65
-80
lines changed

4 files changed

+65
-80
lines changed

Rakefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ Spec::Rake::SpecTask.new(:rcov) do |spec|
2828
spec.libs << 'lib' << 'spec'
2929
spec.pattern = 'spec/**/*_spec.rb'
3030
spec.rcov = true
31+
spec.rcov_opts = ['--exclude', 'spec']
3132
end
3233

3334
task :spec => :check_dependencies

spec/alchemy_api/base_spec.rb

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
require File.dirname(__FILE__) + '/../spec_helper'
2+
3+
describe AlchemyApi::Base do
4+
describe "#check_json_for_errors_and_raise!" do
5+
before(:each) do
6+
@json = {
7+
'status' => 'ERROR',
8+
'url' => 'http://google.com',
9+
'statusInfo' => nil # replace in each test.
10+
}
11+
end
12+
13+
it "should raise an error if the API key is invalid" do
14+
@json['statusInfo'] = 'invalid-api-key'
15+
lambda {
16+
AlchemyApi::Base.check_json_for_errors_and_raise!(@json)
17+
}.should raise_error(AlchemyApi::InvalidApiKeyError)
18+
end
19+
20+
it "should raise an error if the page is not retrievable" do
21+
@json['statusInfo'] = 'cannot-retrieve'
22+
lambda {
23+
AlchemyApi::Base.check_json_for_errors_and_raise!(@json)
24+
}.should raise_error(AlchemyApi::CannotRetrieveUrlError)
25+
end
26+
27+
it "should raise an error if the page is not valid HTML" do
28+
@json['statusInfo'] = 'page-is-not-html'
29+
lambda {
30+
AlchemyApi::Base.check_json_for_errors_and_raise!(@json)
31+
}.should raise_error(AlchemyApi::PageIsNotValidHtmlError)
32+
end
33+
34+
it "should raise an error if the sent HTML was not valid" do
35+
@json['statusInfo'] = 'invalid-html'
36+
lambda {
37+
AlchemyApi::Base.check_json_for_errors_and_raise!(@json)
38+
}.should raise_error(AlchemyApi::InvalidHtmlError)
39+
end
40+
41+
it "should raise an error if the content exceeds the max limit" do
42+
@json['statusInfo'] = 'content-exceeds-size-limit'
43+
lambda {
44+
AlchemyApi::Base.check_json_for_errors_and_raise!(@json)
45+
}.should raise_error(AlchemyApi::ContentExceedsMaxLimitError)
46+
end
47+
48+
it "should raise an error if the content cannot be retrieve due to redirection limit" do
49+
@json['statusInfo'] = 'cannot-retrieve:http-redirect-limit'
50+
lambda {
51+
AlchemyApi::Base.check_json_for_errors_and_raise!(@json)
52+
}.should raise_error(AlchemyApi::RedirectionLimitError)
53+
end
54+
55+
it "should raise an UnknownError if we get something we don't recognize" do
56+
@json['statusInfo'] = 'fdsafdsfdsafdskjldklfdad'
57+
lambda {
58+
AlchemyApi::Base.check_json_for_errors_and_raise!(@json)
59+
}.should raise_error(AlchemyApi::UnknownError)
60+
end
61+
end
62+
end

spec/alchemy_api/text_extraction_spec.rb

Lines changed: 0 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,6 @@
11
require File.dirname(__FILE__) + "/../spec_helper"
22

33
describe AlchemyApi::TextExtraction do
4-
shared_examples_for 'error handler' do
5-
before(:each) do
6-
@response = mock('response')
7-
@response.stub!(:code).and_return(200)
8-
@json = {
9-
'status' => 'ERROR',
10-
'url' => 'http://google.com',
11-
'statusInfo' => nil # replace in each test.
12-
}
13-
end
14-
15-
it "should raise an error if the API key is invalid" do
16-
@json['statusInfo'] = 'invalid-api-key'
17-
@response.stub!(:body).and_return(@json.to_json)
18-
lambda {
19-
AlchemyApi::TextExtraction.send(@method, @response)
20-
}.should raise_error(AlchemyApi::InvalidApiKeyError)
21-
end
22-
23-
it "should raise an error if the page is not retrievable" do
24-
@json['statusInfo'] = 'cannot-retrieve'
25-
@response.stub!(:body).and_return(@json.to_json)
26-
lambda {
27-
AlchemyApi::TextExtraction.send(@method, @response)
28-
}.should raise_error(AlchemyApi::CannotRetrieveUrlError)
29-
end
30-
31-
it "should raise an error if the page is not valid HTML" do
32-
@json['statusInfo'] = 'page-is-not-html'
33-
@response.stub!(:body).and_return(@json.to_json)
34-
lambda {
35-
AlchemyApi::TextExtraction.send(@method, @response)
36-
}.should raise_error(AlchemyApi::PageIsNotValidHtmlError)
37-
end
38-
39-
it "should raise an error if the sent HTML was not valid" do
40-
@json['statusInfo'] = 'invalid-html'
41-
@response.stub!(:body).and_return(@json.to_json)
42-
lambda {
43-
AlchemyApi::TextExtraction.send(@method, @response)
44-
}.should raise_error(AlchemyApi::InvalidHtmlError)
45-
end
46-
47-
it "should raise an error if the content exceeds the max limit" do
48-
@json['statusInfo'] = 'content-exceeds-size-limit'
49-
@response.stub!(:body).and_return(@json.to_json)
50-
lambda {
51-
AlchemyApi::TextExtraction.send(@method, @response)
52-
}.should raise_error(AlchemyApi::ContentExceedsMaxLimitError)
53-
end
54-
55-
it "should raise an error if the content cannot be retrieve due to redirection limit" do
56-
@json['statusInfo'] = 'cannot-retrieve:http-redirect-limit'
57-
@response.stub!(:body).and_return(@json.to_json)
58-
lambda {
59-
AlchemyApi::TextExtraction.send(@method, @response)
60-
}.should raise_error(AlchemyApi::RedirectionLimitError)
61-
end
62-
end
63-
644
typhoeus_spec_cache('spec/cache/text_extraction/get_text_from_url') do |hydra|
655
describe "#get_text_from_url" do
666
it "should extract text" do
@@ -151,24 +91,4 @@
15191
end
15292
end
15393
end
154-
155-
156-
describe "#get_title_from_url_handler" do
157-
describe "error handling" do
158-
before(:each) do
159-
@method = :get_title_from_url_handler
160-
end
161-
162-
it_should_behave_like 'error handler'
163-
end
164-
end
165-
166-
describe "#get_text_from_url_handler" do
167-
describe "error handling" do
168-
before(:each) do
169-
@method = :get_text_from_url_handler
170-
end
171-
it_should_behave_like 'error handler'
172-
end
173-
end
17494
end

spec/spec_helper.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
require 'spec'
55
require 'spec/autorun'
66

7+
AlchemyApi.api_key = '3468d2194732163c69b0ac76079ca9f9c4193226'
8+
79
require 'typhoeus_spec_cache'
810

911
Spec::Runner.configure do |config|

0 commit comments

Comments
 (0)