[PATCH conductor 4/9] RSpec tests for provider selection classes

ifarkas at redhat.com ifarkas at redhat.com
Thu Aug 16 14:44:04 UTC 2012


From: Imre Farkas <ifarkas at redhat.com>

---
 src/spec/lib/provider_selection/base.rb           | 69 +++++++++++++++++++++++
 src/spec/lib/provider_selection/match.rb          | 50 ++++++++++++++++
 src/spec/lib/provider_selection/priority_group.rb | 66 ++++++++++++++++++++++
 src/spec/lib/provider_selection_spec.rb           | 64 ---------------------
 4 files changed, 185 insertions(+), 64 deletions(-)
 create mode 100644 src/spec/lib/provider_selection/base.rb
 create mode 100644 src/spec/lib/provider_selection/match.rb
 create mode 100644 src/spec/lib/provider_selection/priority_group.rb
 delete mode 100644 src/spec/lib/provider_selection_spec.rb

diff --git a/src/spec/lib/provider_selection/base.rb b/src/spec/lib/provider_selection/base.rb
new file mode 100644
index 0000000..b5f70e3
--- /dev/null
+++ b/src/spec/lib/provider_selection/base.rb
@@ -0,0 +1,69 @@
+#
+#   Copyright 2012 Red Hat, Inc.
+#
+#   Licensed under the Apache License, Version 2.0 (the "License");
+#   you may not use this file except in compliance with the License.
+#   You may obtain a copy of the License at
+#
+#       http://www.apache.org/licenses/LICENSE-2.0
+#
+#   Unless required by applicable law or agreed to in writing, software
+#   distributed under the License is distributed on an "AS IS" BASIS,
+#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+#   See the License for the specific language governing permissions and
+#   limitations under the License.
+#
+
+require 'spec_helper'
+
+describe ProviderSelection::Base do
+
+  before(:each) do
+    @account1 = FactoryGirl.create(:mock_provider_account, :label => "test_account1")
+    @account2 = FactoryGirl.create(:mock_provider_account, :label => "test_account2")
+    @account3 = FactoryGirl.create(:mock_provider_account, :label => "test_account3")
+
+    possible1 = FactoryGirl.build(:instance_match, :provider_account => @account1)
+    possible2 = FactoryGirl.build(:instance_match, :provider_account => @account2)
+    possible3 = FactoryGirl.build(:instance_match, :provider_account => @account2)
+    possible4 = FactoryGirl.build(:instance_match, :provider_account => @account3)
+    possible5 = FactoryGirl.build(:instance_match, :provider_account => @account2)
+
+    instance1 = Factory.build(:instance)
+    instance1.stub!(:matches).and_return([[possible1, possible2], []])
+    instance2 = Factory.build(:instance)
+    instance2.stub!(:matches).and_return([[possible3, possible4], []])
+    instance3 = Factory.build(:instance)
+    instance3.stub!(:matches).and_return([[possible5], []])
+
+    instances = [instance1, instance2, instance3]
+    @provider_selection = ProviderSelection::Base.new(instances)
+  end
+
+  it "should give back valid match" do
+    match = @provider_selection.next_match
+    match.provider_account.should eql(@account2)
+  end
+
+  it "should find common provider account" do
+    common_provider_accounts = @provider_selection.send(:find_common_provider_accounts)
+    common_provider_accounts.should eql([@account2])
+  end
+
+  it "common provider account should not include any redundant element" do
+    common_provider_accounts = @provider_selection.send(:find_common_provider_accounts)
+    common_provider_accounts.length.should eql(common_provider_accounts.uniq_by(&:to_json).length)
+  end
+
+  it "should calculate initial rank" do
+    rank = @provider_selection.calculate
+    rank.priority_groups.length.should eql(1)
+
+    priority_group = rank.priority_groups.first
+    priority_group.matches.length.should eql(1)
+
+    match = priority_group.matches.first
+    match.provider_account.should eql(@account2)
+  end
+
+end
\ No newline at end of file
diff --git a/src/spec/lib/provider_selection/match.rb b/src/spec/lib/provider_selection/match.rb
new file mode 100644
index 0000000..9732613
--- /dev/null
+++ b/src/spec/lib/provider_selection/match.rb
@@ -0,0 +1,50 @@
+#
+#   Copyright 2012 Red Hat, Inc.
+#
+#   Licensed under the Apache License, Version 2.0 (the "License");
+#   you may not use this file except in compliance with the License.
+#   You may obtain a copy of the License at
+#
+#       http://www.apache.org/licenses/LICENSE-2.0
+#
+#   Unless required by applicable law or agreed to in writing, software
+#   distributed under the License is distributed on an "AS IS" BASIS,
+#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+#   See the License for the specific language governing permissions and
+#   limitations under the License.
+#
+
+require 'spec_helper'
+
+describe ProviderSelection::Match do
+
+  it "nil score should worth more than any defined score" do
+    match_with_assigned_score = ProviderSelection::Match.new(:provider_account => nil, :score => 0)
+    match_with_nil_score = ProviderSelection::Match.new(:provider_account => nil)
+    match_with_assigned_score.calculated_score.should be < match_with_nil_score.calculated_score
+  end
+
+  it "should be able to penalize if score is under the upper limit" do
+    match = ProviderSelection::Match.new(:provider_account => nil, :score => 0)
+    lambda { match.penalize_by(20) }.should change(match, :calculated_score).from(0)
+  end
+
+  it "should not be able to penalize if score is equal to the upper limit" do
+    match = ProviderSelection::Match.new(:provider_account => nil,
+                                         :score => ProviderSelection::Match::UPPER_LIMIT)
+    lambda { match.penalize_by(20) }.should_not change(match, :calculated_score).from(0)
+  end
+
+
+  it "should be able to reward if score is above the lower limit" do
+    match = ProviderSelection::Match.new(:provider_account => nil, :score => 0)
+    lambda { match.reward_by(20) }.should change(match, :calculated_score).from(0)
+  end
+
+  it "should not be able to reward if score is equal to the lower limit" do
+    match = ProviderSelection::Match.new(:provider_account => nil,
+                                         :score => ProviderSelection::Match::LOWER_LIMIT)
+    lambda { match.reward_by(20) }.should_not change(match, :calculated_score).from(0)
+  end
+
+end
\ No newline at end of file
diff --git a/src/spec/lib/provider_selection/priority_group.rb b/src/spec/lib/provider_selection/priority_group.rb
new file mode 100644
index 0000000..7d7c25e
--- /dev/null
+++ b/src/spec/lib/provider_selection/priority_group.rb
@@ -0,0 +1,66 @@
+#
+#   Copyright 2012 Red Hat, Inc.
+#
+#   Licensed under the Apache License, Version 2.0 (the "License");
+#   you may not use this file except in compliance with the License.
+#   You may obtain a copy of the License at
+#
+#       http://www.apache.org/licenses/LICENSE-2.0
+#
+#   Unless required by applicable law or agreed to in writing, software
+#   distributed under the License is distributed on an "AS IS" BASIS,
+#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+#   See the License for the specific language governing permissions and
+#   limitations under the License.
+#
+
+require 'spec_helper'
+
+describe ProviderSelection::PriorityGroup do
+
+  it "should process user defined priority groups" do
+    priority_group_ar = FactoryGirl.create(:provider_priority_group)
+    provider_account = FactoryGirl.create :mock_provider_account
+    priority_group_ar.provider_accounts << provider_account
+
+    match = ProviderSelection::Match.new(:provider_account => provider_account)
+    priority_group = ProviderSelection::PriorityGroup.create_from_active_record(priority_group_ar, [match])
+    priority_group.should_not be nil
+  end
+
+  it "should properly filter provider accounts while processing user defined priority groups" do
+    priority_group_ar = FactoryGirl.create(:provider_priority_group)
+    provider_account = FactoryGirl.create :mock_provider_account
+    provider = provider_account.provider
+    priority_group_ar.provider_accounts << provider_account
+    3.times do
+      other_provider_account = FactoryGirl.build(:mock_provider_account_seq, :provider => provider)
+      other_provider_account.stub!(:validate_credentials).and_return(true)
+      other_provider_account.save
+
+      priority_group_ar.provider_accounts << other_provider_account
+    end
+
+    allowed_match = ProviderSelection::Match.new(:provider_account => provider_account)
+    priority_group = ProviderSelection::PriorityGroup.create_from_active_record(priority_group_ar, [allowed_match])
+    priority_group.matches.length.should eql(1)
+    priority_group.matches.first.provider_account.should eql(provider_account)
+
+  end
+
+  it "should be able to delete existing matches" do
+    priority_group = ProviderSelection::PriorityGroup.new(0)
+    priority_group.matches <<
+      ProviderSelection::Match.new(:provider_account => 'Provider Account 1',
+                                   :score => 100)
+    priority_group.matches <<
+        ProviderSelection::Match.new(:provider_account => 'Provider Account 2',
+                                     :score => 100)
+    priority_group.matches <<
+        ProviderSelection::Match.new(:provider_account => 'Provider Account 3',
+                                     :score => 0)
+
+    lambda { priority_group.delete_matches(:score, [100]) }.should change(priority_group.matches, :length).from(3).to(1)
+  end
+
+end
\ No newline at end of file
diff --git a/src/spec/lib/provider_selection_spec.rb b/src/spec/lib/provider_selection_spec.rb
deleted file mode 100644
index cf1c252..0000000
--- a/src/spec/lib/provider_selection_spec.rb
+++ /dev/null
@@ -1,64 +0,0 @@
-#
-#   Copyright 2012 Red Hat, Inc.
-#
-#   Licensed under the Apache License, Version 2.0 (the "License");
-#   you may not use this file except in compliance with the License.
-#   You may obtain a copy of the License at
-#
-#       http://www.apache.org/licenses/LICENSE-2.0
-#
-#   Unless required by applicable law or agreed to in writing, software
-#   distributed under the License is distributed on an "AS IS" BASIS,
-#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-#   See the License for the specific language governing permissions and
-#   limitations under the License.
-#
-
-require 'spec_helper'
-
-describe ProviderSelection do
-
-  before(:each) do
-    @account1 = FactoryGirl.create(:mock_provider_account, :label => "test_account1")
-    @account2 = FactoryGirl.create(:mock_provider_account, :label => "test_account2")
-    @account3 = FactoryGirl.create(:mock_provider_account, :label => "test_account3")
-
-    possible1 = FactoryGirl.build(:instance_match, :provider_account => @account1)
-    possible2 = FactoryGirl.build(:instance_match, :provider_account => @account2)
-    possible3 = FactoryGirl.build(:instance_match, :provider_account => @account2)
-    possible4 = FactoryGirl.build(:instance_match, :provider_account => @account3)
-    possible5 = FactoryGirl.build(:instance_match, :provider_account => @account2)
-
-    instance1 = Factory.build(:instance)
-    instance1.stub!(:matches).and_return([[possible1, possible2], []])
-    instance2 = Factory.build(:instance)
-    instance2.stub!(:matches).and_return([[possible3, possible4], []])
-    instance3 = Factory.build(:instance)
-    instance3.stub!(:matches).and_return([[possible5], []])
-
-    instances = [instance1, instance2, instance3]
-    @provider_selection = ProviderSelection::Base.new(instances)
-  end
-
-  it "should give back valid match" do
-    match = @provider_selection.next_match
-    match.provider_account.should eql(@account2)
-  end
-
-  it "should find common provider account" do
-    common_provider_accounts = @provider_selection.send(:find_common_provider_accounts)
-    common_provider_accounts.should eql([@account2])
-  end
-
-  it "should calculate initial rank" do
-    rank = @provider_selection.calculate
-    rank.priority_groups.length.should eql(1)
-
-    priority_group = rank.priority_groups.first
-    priority_group.matches.length.should eql(1)
-
-    match = priority_group.matches.first
-    match.provider_account.should eql(@account2)
-  end
-
-end
\ No newline at end of file
-- 
1.7.11.2




More information about the aeolus-devel mailing list