Skip to content

Commit

Permalink
No longer stubbing paging tokens.
Browse files Browse the repository at this point in the history
Fixes #804
  • Loading branch information
trevorrowe committed Jun 9, 2015
1 parent d6d793c commit d367f7d
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 322 deletions.
1 change: 1 addition & 0 deletions aws-sdk-core/lib/aws-sdk-core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ module Signers

# @api private
module Stubbing
autoload :EmptyStub, 'aws-sdk-core/stubbing/empty_stub'
module Protocols
autoload :EC2, 'aws-sdk-core/stubbing/protocols/ec2'
autoload :Json, 'aws-sdk-core/stubbing/protocols/json'
Expand Down
121 changes: 10 additions & 111 deletions aws-sdk-core/lib/aws-sdk-core/client_stubs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,11 @@ def next_stub(operation_name)
end

# @api private
def stub_data(operation_name, data = nil)
Stub.new(operation(operation_name).output).format(data || {})
def stub_data(operation_name)
operation = self.operation(operation_name)
stub = Stubbing::EmptyStub.new(operation.output).stub
remove_paging_tokens(operation[:pager], stub)
stub
end

private
Expand Down Expand Up @@ -208,117 +211,13 @@ def protocol_helper
end.new
end

class Stub

include Seahorse::Model::Shapes

# @param [Seahorse::Models::Shapes::ShapeRef] rules
def initialize(rules)
@rules = rules
end

# @param [Hash] data An optional hash of data to format into the stubbed
# object.
def format(data = {})
if @rules.nil?
empty_stub(data)
else
validate_data(data)
stub(@rules, data)
end
end

private

def stub(ref, value)
case ref.shape
when StructureShape then stub_structure(ref, value)
when ListShape then stub_list(ref, value || [])
when MapShape then stub_map(ref, value || {})
else stub_scalar(ref, value)
end
end

def stub_structure(ref, hash)
if hash
structure_obj(ref, hash)
else
nil
end
end

def structure_obj(ref, hash)
stubs = ref[:struct_class].new
ref.shape.members.each do |member_name, member_ref|
if hash.key?(member_name) && hash[member_name].nil?
stubs[member_name] = nil
else
value = structure_value(ref, member_name, member_ref, hash)
stubs[member_name] = stub(member_ref, value)
end
end
stubs
end

def structure_value(ref, member_name, member_ref, hash)
if hash.key?(member_name)
hash[member_name]
elsif
StructureShape === member_ref.shape &&
ref.shape.required.include?(member_name)
then
{}
else
nil
def remove_paging_tokens(pager, stub)
if pager
pager.instance_variable_get("@tokens").keys.each do |path|
key = path.split(/\b/)[0]
stub[key] = nil
end
end

def stub_list(ref, array)
stubs = []
array.each do |value|
stubs << stub(ref.shape.member, value)
end
stubs
end

def stub_map(ref, hash)
stubs = {}
hash.each do |key, value|
stubs[key] = stub(ref.shape.value, value)
end
stubs
end

def stub_scalar(ref, value)
if value.nil?
case ref.shape
when StringShape then ref.shape.name
when IntegerShape then 0
when FloatShape then 0.0
when BooleanShape then false
when TimestampShape then Time.now
else nil
end
else
value
end
end

def empty_stub(data)
if data.empty?
EmptyStructure.new
else
msg = 'unable to generate a stubbed response from the given data; '
msg << 'this operation does not return data'
raise ArgumentError, msg
end
end

def validate_data(data)
args = [@rules, { validate_required:false }]
ParamValidator.new(*args).validate!(data)
end

end
end
end
53 changes: 53 additions & 0 deletions aws-sdk-core/lib/aws-sdk-core/stubbing/empty_stub.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
module Aws
module Stubbing
class EmptyStub

include Seahorse::Model::Shapes

# @param [Seahorse::Models::Shapes::ShapeRef] rules
def initialize(rules)
@rules = rules
end

# @return [Structure]
def stub
stub_ref(@rules)
end

private

def stub_ref(ref, visited = [])
if visited.include?(ref.shape)
return nil
else
visited = visited + [ref.shape]
end
case ref.shape
when StructureShape then stub_structure(ref, visited)
when ListShape then []
when MapShape then {}
else stub_scalar(ref)
end
end

def stub_structure(ref, visited)
ref.shape.members.inject(ref[:struct_class].new) do |struct, (mname, mref)|
struct[mname] = stub_ref(mref, visited)
struct
end
end

def stub_scalar(ref)
case ref.shape
when StringShape then ref.shape.name || 'string'
when IntegerShape then 0
when FloatShape then 0.0
when BooleanShape then false
when TimestampShape then Time.now
else nil
end
end

end
end
end
49 changes: 49 additions & 0 deletions aws-sdk-core/spec/aws/stubbing/empty_stub_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
require 'spec_helper'
require 'stringio'

module Aws
module Stubbing
describe EmptyStub do

it 'supports complex recursive structures' do
now = Time.now
allow(Time).to receive(:now).and_return(now)
rules = ApiHelper.sample_shapes
rules = Api::ShapeMap.new(rules).shape_ref('shape' => 'StructureShape')
stub = EmptyStub.new(rules).stub
expect(stub.to_h).to eq({
nested_list: [],
nested_map: {},
number_list: [],
string_map: {},
byte: "ByteShape",
boolean: false,
character: "CharacterShape",
double: 0.0,
float: 0.0,
integer: 0,
long: 0,
string: "StringShape",
timestamp: now,
})
end

it 'does not stub paging markers' do
client = S3::Client.new(stub_responses:true)
resp = client.list_objects(bucket:'bucket')
expect(resp.to_h).to eq({
common_prefixes: [],
contents: [],
delimiter: "Delimiter",
encoding_type: "EncodingType",
is_truncated: false,
marker: "Marker",
max_keys: 0,
name: "BucketName",
prefix: "Prefix",
})
end

end
end
end
Loading

0 comments on commit d367f7d

Please sign in to comment.