Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

refactoring Ruleset score calculation + tests for this

  • Loading branch information...
commit 1b3bf01258c3472e876b05ddfc7ad67141f12809 1 parent 6a1f5b1
@chriskottom authored
Showing with 43 additions and 16 deletions.
  1. +5 −10 lib/rslow/ruleset.rb
  2. +16 −6 test/ruleset_test.rb
  3. +22 −0 test/test_helper.rb
View
15 lib/rslow/ruleset.rb
@@ -37,16 +37,11 @@ def evaluate(resource)
private
def compute_weighted_average(scores, weights)
- max_composite_score = RSlow::Rule::MAX_SCORE * weights.reduce(:+)
-
- composite_score = 0
- scores.each_with_index do |score, index|
- composite_score += score * weights[index]
- end
-
- normalized_score = (composite_score.to_f / max_composite_score) *
- RSlow::Rule::MAX_SCORE
- normalized_score.round(0)
+ total_weight = weights.reduce(:+)
+ weighted_total_score = weights.zip(scores).
+ map { |parts| parts[0] * parts[1] }.
+ reduce(:+)
+ (weighted_total_score.to_f / total_weight).round(0)
end
end
end
View
22 test/ruleset_test.rb
@@ -1,9 +1,10 @@
require_relative "test_helper"
-require "test/unit"
-require "rslow"
+require "mocha"
class RulesetTest < Test::Unit::TestCase
+ include TestHelper
+
def empty_ruleset
RSlow::Ruleset.new(:testing)
end
@@ -23,19 +24,28 @@ def one_rule_ruleset
end
end
- def test_simple_ruleset_creation
+ def test_empty_ruleset_creation
ruleset = empty_ruleset
assert_not_nil(ruleset)
assert_equal(0, ruleset.count)
end
- def test_complex_ruleset_creation
+ def test_simple_ruleset_creation
ruleset = one_rule_ruleset
assert_not_nil(ruleset)
assert_equal(1, ruleset.count)
end
- def test_ruleset_evaluation
- ruleset =
+ def test_ruleset_scoring
+ RSlow::Ruleset.any_instance.stubs(:rule).returns(*mock_rules)
+ fake_weights = RULE_CONFIG.map { |fake_rule| fake_rule[:weight] }
+ fake_scores = RULE_CONFIG.map { |fake_rule| fake_rule[:evaluate][:score] }
+ expected_score = weighted_average_of_fake_scores(fake_scores, fake_weights)
+
+ ruleset = empty_ruleset
+ mock_rules.each { |rule| ruleset << rule }
+
+ evaluation = ruleset.evaluate(nil)
+ assert_equal(expected_score, JSON.parse(evaluation)["score"])
end
end
View
22 test/test_helper.rb
@@ -16,6 +16,11 @@ module TestHelper
HTTP_SUCCESS = { code: "200", message: "OK" }
GIF_SUCCESS = HTTP_SUCCESS.merge({ :"Content-Type" => "image/gif" })
+ RULE_CONFIG = [
+ { type: :"RuleOne", weight: 10, evaluate: { score: 50 } },
+ { type: :"RuleTwo", weight: 5, evaluate: { score: 75 } },
+ { type: :"RuleTres", weight: 8, evaluate: { score: 90 } }
+ ]
def mock_http_response
http_mock = mock("Net::HTTPResponse")
@@ -43,5 +48,22 @@ def mock_image_resource
content_type: GIF_SUCCESS[:"Content-Type"])
image_mock
end
+
+ def mock_rules
+ rule_mocks = []
+ RULE_CONFIG.each do |config|
+ rule_mock = mock("RSlow::Rule")
+ rule_mock.stubs(config)
+ rule_mock.stubs(:[]).with(:weight).returns(config[:weight])
+ rule_mocks << rule_mock
+ end
+ rule_mocks
+ end
+
+ def weighted_average_of_fake_scores(scores, weights)
+ total_weight = weights.reduce(:+)
+ total_weighted_scores = weights.zip(scores).map { |f| f[0] * f[1] }.reduce(:+)
+ (total_weighted_scores.to_f / total_weight).round(0)
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.