Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bogdanRada committed Mar 1, 2020
1 parent 05496d6 commit 66c339e
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 35 deletions.
4 changes: 2 additions & 2 deletions lib/washout_builder/document/complex_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def find_complex_class_name(classes_defined = [])
# @api public
def check_duplicate_complex_class(classes_defined, complex_class)
complex_obj_found = classes_defined.find { |hash| hash if hash[:class] == complex_class && hash[:obj].find_param_structure.keys != self.find_param_structure.keys }
raise "Duplicate use of `#{basic_type}` type name. Consider using classified types" if !complex_obj_found.nil? && struct? && !classified?
raise "Duplicate use of `#{basic_type}` type name. Consider using classified types." if !complex_obj_found.nil? && struct? && !classified?
end

# finds the complex class ancestors if the current object is classified, otherwise returns nil
Expand Down Expand Up @@ -197,7 +197,7 @@ def complex_type_hash(class_name, object, ancestors)
# @param [Array<Hash>] classes_defined An Array with all the complex types that have been detected so far
# @return [Array<Class>] An Array of classes from which the class that is sent as parameter inherits from
# @api public
def get_class_ancestors(config, class_name, classes_defined)
def get_class_ancestors(config, class_name, classes_defined)
ancestors = get_ancestors(class_name)
return if ancestors.blank?
base_param_class = WashoutBuilder::Type.base_param_class
Expand Down
60 changes: 30 additions & 30 deletions spec/lib/washout_builder/document/complex_type_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,29 @@
let(:namespaced_object) { get_wash_out_param(Api::TestType) }

it 'returns the complex class name' do
expect(subject.find_complex_class_name).to eq('ProjectType')
expect(subject.find_complex_class_name).to eq('Project')
end

it 'returns the complex class with namespace' do
expect(namespaced_object.find_complex_class_name).to eq('Api::TestType')
expect(namespaced_object.find_complex_class_name).to eq('Project')
end

it 'returns error if classname already detected (only used for hashes)' do
it 'does not return error if classname already detected (only used for hashes) and same structure' do
subject.stubs(:classified?).returns(false)
subject.stubs(:basic_type).returns('ProjectType')
defined = [{ class: 'ProjectType' }]
expect { subject.find_complex_class_name(defined) }.to raise_error(RuntimeError, 'Duplicate use of `ProjectType` type name. Consider using classified types.')
classes_defined = [{ class: 'ProjectType', obj: get_wash_out_param(ProjectType), ancestors: [] }]
expect { subject.find_complex_class_name(classes_defined) }.to_not raise_error(RuntimeError, 'Duplicate use of `ProjectType` type name. Consider using classified types.')
end

it 'returns the param structure' do
expect(subject.find_param_structure).to eq("project"=>"struct")
it 'returns error if classname already detected (only used for hashes)' do
subject.stubs(:classified?).returns(false)
subject.stubs(:basic_type).returns('ProjectType')
classes_defined = [{ class: 'ProjectType', obj: get_wash_out_param(Api::TestType), ancestors: [] }]
expect { subject.find_complex_class_name(classes_defined) }.to raise_error(RuntimeError, 'Duplicate use of `ProjectType` type name. Consider using classified types.')
end

it 'fixes the first descendant ' do
descendant = get_wash_out_param(ProjectType.wash_out_param_map)
subject.fix_descendant_wash_out_type(soap_config, ProjectType)
expect(subject.name).to eq(descendant.name)
expect(subject.map[0].find_param_structure).to eq(descendant.map[0].find_param_structure)
it 'returns the param structure' do
expect(subject.find_param_structure).to eq("description"=>"string", "name"=>"string", "users"=>"struct")
end

it 'same as ancestor' do
Expand Down Expand Up @@ -75,18 +75,18 @@

describe '#complex_type_descendants' do
it 'returns empty array if not struct?' do
defined = []
classes_defined = []
subject.stubs(:struct?).returns(false)
expect(subject.complex_type_descendants(soap_config, defined)).to eq(defined)
expect(subject.complex_type_descendants(soap_config, classes_defined)).to eq(classes_defined)
end

it 'returns the descendants if struct?' do
defined = []
classes_defined = []
subject.map.each do |obj|
obj.expects(:get_nested_complex_types).with(soap_config, defined).returns([obj.name])
obj.expects(:get_nested_complex_types).with(soap_config, classes_defined).returns([obj.name])
end
subject.stubs(:struct?).returns(true)
expect(subject.complex_type_descendants(soap_config, defined)).to eq(subject.map.map(&:name))
expect(subject.complex_type_descendants(soap_config, classes_defined)).to eq(subject.map.map(&:name))
end
end

Expand All @@ -97,21 +97,20 @@
let(:expected) { [complex_type_hash] }

it 'returns the complex class ancestors' do
defined = []
subject.expects(:find_complex_class_name).with(defined).returns(complex_class)
subject.expects(:fix_descendant_wash_out_type).with(soap_config, complex_class).returns(true)
subject.expects(:complex_type_ancestors).with(soap_config, complex_class, defined).returns(ancestors)
classes_defined = []
subject.expects(:find_complex_class_name).with(classes_defined).returns(complex_class)
subject.expects(:find_class_from_string).with(complex_class).returns(nil)
subject.expects(:complex_type_ancestors).with(soap_config, complex_class, classes_defined).returns(ancestors)
subject.expects(:complex_type_hash).with(complex_class, subject, ancestors).returns(complex_type_hash)
subject.expects(:complex_type_descendants).with(soap_config, [complex_type_hash]).returns(expected)
expect(subject.get_nested_complex_types(soap_config, defined)).to eq(expected)
expect(subject.get_nested_complex_types(soap_config, classes_defined)).to eq(expected)
end

it 'returns the the descendants' do
defined = nil
classes_defined = nil
subject.expects(:find_complex_class_name).with([]).returns(nil)
subject.expects(:fix_descendant_wash_out_type).with(soap_config, nil).returns(true)
subject.expects(:complex_type_descendants).with(soap_config, []).returns(expected)
expect(subject.get_nested_complex_types(soap_config, defined)).to eq(expected)
expect(subject.get_nested_complex_types(soap_config, classes_defined)).to eq(expected)
end
end

Expand All @@ -135,15 +134,15 @@

describe '#get_class_ancestors' do
let(:class_name) { 'ProjectType' }
let(:defined) { [] }
let(:classes_defined) { [] }
let(:ancestors) { ['SomeInexistentClass'] }
let(:ancestor_structure) { { ancestors[0].to_s.downcase => 'bla' } }
let(:top_ancestors) {}
let(:complex_type_hash) { { class: class_name, obj: subject, ancestors: ancestors } }

it 'returns nil if no ancestors' do
subject.expects(:get_ancestors).with(class_name).returns(nil)
expect(subject.get_class_ancestors(soap_config, class_name, defined)).to eq(nil)
expect(subject.get_class_ancestors(soap_config, class_name, classes_defined)).to eq(nil)
end

it 'returns the ancestors and the top ones' do
Expand All @@ -153,15 +152,16 @@
WashOut::Param.stubs(:parse_def).returns([namespaced_object])
subject.expects(:same_structure_as_ancestor?).with(namespaced_object).returns(false)
subject.expects(:complex_type_hash).returns(complex_type_hash)
expect(subject.get_class_ancestors(soap_config, class_name, defined)).to eq([complex_type_hash])
expect(subject.get_class_ancestors(soap_config, class_name, classes_defined)).to eq([complex_type_hash])
end

it 'returns nil if same structure as ancestor' do
namespaced_object
subject.expects(:get_ancestors).with(class_name).returns(ancestors)
subject.expects(:ancestor_structure).with(ancestors).returns(ancestor_structure)
WashOut::Param.stubs(:parse_def).returns([namespaced_object])
WashOut::Param.stubs(:parse_def).with(soap_config, ancestor_structure).returns([namespaced_object])
subject.expects(:same_structure_as_ancestor?).with(namespaced_object).returns(true)
expect(subject.get_class_ancestors(soap_config, class_name, defined)).to eq(nil)
expect(subject.get_class_ancestors(soap_config, class_name, classes_defined)).to eq(nil)
end
end
end
2 changes: 1 addition & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def get_wash_out_param(class_or_struct, soap_config = OpenStruct.new(
camelize_wsdl: false,
namespace: '/api/wsdl'
))
WashOut::Param.parse_builder_def(soap_config, class_or_struct)[0]
WashOut::Param.parse_def(soap_config, class_or_struct)[0]
end

class Hash
Expand Down
3 changes: 1 addition & 2 deletions spec/support/complex_types/test_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ module Api
class TestType < WashOut::Type
map project: {
name: :string,
description: :string,
title: :string,
users: [{ mail: :string }],
# 'dada' => [Project]
}
end
end

0 comments on commit 66c339e

Please sign in to comment.