Skip to content

Commit

Permalink
Merge pull request #11 from webdestroya/infinite-loops-for-days
Browse files Browse the repository at this point in the history
Fixing infinite loop bug
  • Loading branch information
bobbytables committed Nov 5, 2014
2 parents 6154580 + 8009a6c commit c19a592
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 20 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
source 'https://rubygems.org'

gem 'simplecov'
# Specify your gem's dependencies in droplet_kit.gemspec
gemspec
8 changes: 7 additions & 1 deletion lib/droplet_kit/paginated_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,13 @@ def each(start = 0)
end

def last?
@current_page == (self.total.to_f / per_page.to_f).ceil
@current_page == total_pages || self.total.zero?
end

def total_pages
return nil if self.total.nil?

(self.total.to_f / per_page.to_f).ceil
end

def ==(other)
Expand Down
11 changes: 11 additions & 0 deletions spec/lib/droplet_kit/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,17 @@
end
end

describe "#method_missing" do

context "called with an existing method" do
it { expect{ client.actions}.to_not raise_error }
end

context "called with a missing method" do
it { expect{client.this_is_wrong}.to raise_error(NoMethodError) }
end
end

describe '#connection' do
it 'populates the authorization header correctly' do
expect(client.connection.headers['Authorization']).to eq("Bearer #{client.access_token}")
Expand Down
21 changes: 21 additions & 0 deletions spec/lib/droplet_kit/models/base_model_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
require 'spec_helper'

RSpec.describe DropletKit::BaseModel do
subject(:resource) do
Class.new(DropletKit::BaseModel) do |base|
attribute :droplet_limit
def self.name
'SomeModel'
end
end
end

describe '#inspect' do
it 'returns the information about the current user' do
instance = resource.new(droplet_limit: 5)
expect(instance.inspect).to include("<SomeModel")
expect(instance.inspect).to include("@droplet_limit=>5")
expect(instance.inspect).to include("<SomeModel {:@droplet_limit=>5}>")
end
end
end
37 changes: 22 additions & 15 deletions spec/lib/droplet_kit/paginated_resource_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,8 @@
let(:request_count) { RequestCounter.new(0) }

let(:connection) { Faraday.new {|b| b.adapter :test, stubs } }
let(:stubs) do
Faraday::Adapter::Test::Stubs.new do |stub|
stub.get('/droplets') do |env|
request_count.count += 1
uri = Addressable::URI.parse(env[:url].to_s)
page = (uri.query_values['page'] || 1).to_i
per_page = (uri.query_values['per_page'] || 20).to_i
range = (0...per_page).map do |num|
num + ((page - 1) * per_page)
end

[200, {}, { objects: range, meta: { total: 40 } }.to_json ]
end
end
end
let(:response_size) { 40 }
let(:stubs) { stub_pager_request(response_size) }
let(:action) { ResourceKit::Action.new(:find, :get, '/droplets') }

before do
Expand All @@ -38,6 +25,26 @@
end
end

describe "#total_pages" do
let(:instance) { DropletKit::PaginatedResource.new(action, resource) }
it "returns nil if no request made" do
expect(instance.total_pages).to be_nil
end

it "returns correct page count after request made" do
instance.take(20)
expect(instance.total_pages).to eq(2)
end

context "when results are empty" do
let(:stubs) { stub_pager_request(0) }
it "returns 0" do
instance.take(1)
expect(instance.total_pages).to eq(0)
end
end
end

describe '#each' do
subject(:paginated) { DropletKit::PaginatedResource.new(action, resource) }

Expand Down
8 changes: 8 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
require 'simplecov'

SimpleCov.start

require 'droplet_kit'
require 'webmock/rspec'

SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
SimpleCov::Formatter::HTMLFormatter
]

Dir['./spec/support/**/*.rb'].each do |file|
require file
end
Expand Down
4 changes: 0 additions & 4 deletions spec/support/null_hash_load.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
class NullHashLoad
def self.load(hash)
hash
end

def self.dump(hash)
hash
end
Expand Down
19 changes: 19 additions & 0 deletions spec/support/request_stub_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,23 @@ def stub_do_api(path, verb = :any)
def api_fixture(fixture_name)
Pathname.new('./spec/fixtures/').join("#{fixture_name}.json").read
end

def stub_pager_request(total_results = 40)
Faraday::Adapter::Test::Stubs.new do |stub|
stub.get('/droplets') do |env|
request_count.count += 1
uri = Addressable::URI.parse(env[:url].to_s)
page = (uri.query_values['page'] || 1).to_i
per_page = (uri.query_values['per_page'] || 20).to_i

max_elems = [total_results, per_page].min

range = (0...max_elems).map do |num|
num + ((page - 1) * per_page)
end

[200, {}, { objects: range, meta: { total: total_results } }.to_json ]
end
end
end
end

0 comments on commit c19a592

Please sign in to comment.