Skip to content

Commit

Permalink
replaced flatten calls with more efficient flat_map and added specs f…
Browse files Browse the repository at this point in the history
…or invalid mapping items
  • Loading branch information
thearifismail committed Aug 28, 2019
1 parent fd5f595 commit 50c0ca0
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
6 changes: 3 additions & 3 deletions app/models/transformation_mapping_item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def validate_destination_cluster
def validate_source_datastore
tm = transformation_mapping
tmis = tm.transformation_mapping_items.where(:source_type => "EmsCluster")
src_cluster_storages = tmis.collect(&:source).collect(&:storages).flatten
src_cluster_storages = tmis.collect(&:source).flat_map(&:storages)
source_storage = source

unless src_cluster_storages.include?(source_storage)
Expand All @@ -45,10 +45,10 @@ def validate_destination_datastore

if destination.kind_of?(Storage) # red hat
tmis = tm.transformation_mapping_items.where(:destination_type=> "EmsCluster")
dst_cluster_storages = tmis.collect(&:destination).collect(&:storages).flatten
dst_cluster_storages = tmis.collect(&:destination).flat_map(&:storages)
elsif destination.kind_of?(CloudVolume) # Openstack
tmis = tm.transformation_mapping_items.where(:destination_type => "CloudTenant")
dst_cluster_storages = tmis.collect(&:destination).collect(&:cloud_volumes).flatten
dst_cluster_storages = tmis.collect(&:destination).flat_map(&:cloud_volumes)
end

unless dst_cluster_storages.include?(destination_storage)
Expand Down
9 changes: 8 additions & 1 deletion spec/models/transformation_mapping_item_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,14 @@
let(:ops_mapping) { FactoryBot.create(:transformation_mapping, :transformation_mapping_items => [tmi_ops_cluster]) }

let(:valid_source) { FactoryBot.create(:transformation_mapping_item, :source => src_storage, :destination => cloud_volume_openstack, :transformation_mapping_id => ops_mapping.id) }
let(:invalid_source) { FactoryBot.build(:transformation_mapping_item, :source => cloud_volume_openstack, :destination => src_storage, :transformation_mapping_id => ops_mapping.id) }

it "valid source" do
expect(valid_source.valid?).to be(true)
end
it "invalid source" do
expect(invalid_source.valid?).to be(false)
end
end

context "destination red hat" do
Expand All @@ -84,11 +88,14 @@

context "source validation" do
let(:valid_storage) { FactoryBot.create(:transformation_mapping_item, :source => src_storage, :destination => dst_storage, :transformation_mapping_id => rh_mapping.id) }
let(:invalid_storage) { FactoryBot.create(:transformation_mapping_item, :source => src_storage, :destination => dst_storage, :transformation_mapping_id => rh_mapping.id) }
let(:invalid_storage) { FactoryBot.build(:transformation_mapping_item, :source => dst_storage, :destination => src_storage, :transformation_mapping_id => rh_mapping.id) }

it "validate rhev destination" do
expect(valid_storage.valid?).to be(true)
end
it "invalidate badrhev destination" do
expect(invalid_storage.valid?).to be(false)
end
end
end
end
Expand Down

0 comments on commit 50c0ca0

Please sign in to comment.