Skip to content

Instantly share code, notes, and snippets.

@alainravet
Created March 23, 2014 16:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alainravet/9725781 to your computer and use it in GitHub Desktop.
Save alainravet/9725781 to your computer and use it in GitHub Desktop.
# TODO :
# 1 : return the attributes that are missing
# 2 : in views, also test presence of documents
#
require_relative 'completeness_measure'
require 'minitest/spec'
require 'minitest/autorun'
describe CompletenessMeasure do
#----------------------------------------------------------------------
class Sample1
include CompletenessMeasure
attr_reader :a, :b, :c, :d
def initialize(a=nil, b=nil, c=nil, d=nil)
@a, @b, @c, @d = a, b, c, d
end
end
#----------------------------------------------------------------------
it '<test utils : percent(..) >' do
percent(0, out_of: 0).must_equal 100
percent(3, out_of: 3).must_equal 100
percent(2, out_of: 3).must_equal 66
end
#----------------------------------------------------------------------
it '0 out of 0 is a success / 100%' do
#----------------------------------------------------------------------
criteria = []
Sample1.new(0 , 0).eval_completeness(criteria).must_equal 100
Sample1.new(0 , 0).eval_completeness(criteria).must_equal percent(0, out_of: 0)
end
#----------------------------------------------------------------------
describe 'simple symbols : [:a, :b] : complete if :a and :b attributes are set' do
#----------------------------------------------------------------------
let(:criteria) { [:a, :b] }
# +---+---> : watch those 2 attributes
it '100% : all attributes are set' do
Sample1.new( 1, 1, nil, nil).eval_completeness(criteria).must_equal 100
Sample1.new( 1, 1, nil, nil).eval_completeness(criteria).must_equal percent(2, out_of: 2)
# +----+---> : not watched -> ignore their nilness
# +----+-------------> : watched -> count as complete
end
it 'nil values are detected for the watched attributes' do
Sample1.new(nil, 1, nil, nil).eval_completeness(criteria).must_equal percent(1, out_of: 2)
Sample1.new(nil, nil, nil, nil).eval_completeness(criteria).must_equal percent(0, out_of: 2)
# +----+---> : not watched -> ignore their nilness
# +----+-------------> : watched -> count as incomplete
end
end
#----------------------------------------------------------------------
describe '..{:left => :right}.. : test the right attribute only if the left attribute is not nil' do
#----------------------------------------------------------------------
let(:criteria) {
[
{a: :b}
]
}
it "doesn't test the right attribute if the base (left) attribute is nil" do
Sample1.new( nil, nil).eval_completeness(criteria).must_equal 100
# +--------> base attribute is nil => ignore the rest
end
it 'tests the right attribute when the base (left) attribute is set - SUCCESS' do
Sample1.new( 2, 3).eval_completeness(criteria).must_equal percent(2, out_of: 2)
# + +---> conditionally tested attribute
# +--------> base attribute
end
it 'tests the right attribute when the base (left) attribute is set - FAILURE' do
Sample1.new( 2, nil).eval_completeness(criteria).must_equal percent(1, out_of: 2)
end
end
#----------------------------------------------------------------------
describe '..{:left => {value_1 => [:r_1, :r_2]}}.. : test the right attribute only if the left attribute has a given value' do
#----------------------------------------------------------------------
let(:criteria) do
[
{a:
{ #:a => [:b],
1 => [:b, :c],
2 => [:b]
}
}
]
end
# value == 3 : not in clause -> ignore the dependents
it("the base attribute is tested even when its value not in the condition clause ") do
# 1°: +--------> base value not in cond. clause ..
Sample1.new( nil, nil, nil).eval_completeness(criteria).must_equal percent(0, out_of: 1)
# 2°: + +---> .. ignore the dependent attributes
end
it("100% : don't test the dependent attributes when the base attribute value is not in the condition clause ") do
# 1°: +--------> base value not in cond. clause ..
Sample1.new( 666, nil, nil).eval_completeness(criteria).must_equal percent(1, out_of: 1)
# 2°: + +---> .. ignore the dependent attributes
end
# value == 1 : must test next 2 params
it('100% : all the conditionally required attributes are set') do
# +--------> base value == 1 -> must test next 2 params
Sample1.new( 1, 2, 3).eval_completeness(criteria).must_equal 100
Sample1.new( 1, 2, 3).eval_completeness(criteria).must_equal percent(1+1+1, out_of: 3)
end
it '33% : all the conditionally required fields are nil -> all count for 0' do
# +--------> base value == 1 -> must test next 2 params
Sample1.new( 1, nil, nil).eval_completeness(criteria).must_equal percent(1+0+0, out_of: 3)
end
it '66% : 1 of the conditionally required fields is empty -> it counts for 0' do
Sample1.new( 1, 2, nil).eval_completeness(criteria).must_equal percent( 1+1, out_of: 3)
end
# value == 2
it 'idem, with another value from the condition clause' do
# 1°: +--------> base value == 2 -> must test next param
Sample1.new( 2, nil, nil).eval_completeness(criteria).must_equal percent( 1+0, out_of: 2)
# 2°: + ------> .. 1 failure
Sample1.new( 2, 'any', nil).eval_completeness(criteria).must_equal percent( 1+1, out_of: 2)
# 2°: + ------> .. 1 success
end
end
describe '{x => [:y, :z]} test the right attributeS presence only if the left attribute is present' do
it 'each present field counts for 1 point' do
criteria = [:a, {b: [:c, :d]}]
Sample1.new( 1, 2, 3, 4 ).eval_completeness(criteria).must_equal percent(4, out_of: 4)
Sample1.new( 1, 2, 3, nil).eval_completeness(criteria).must_equal percent(3, out_of: 4)
Sample1.new( 1, 2, nil, 4).eval_completeness(criteria).must_equal percent(3, out_of: 4)
Sample1.new( 1, nil, nil, nil).eval_completeness(criteria).must_equal percent(1, out_of: 4)
end
end
def percent(points, out_of:0)
max_points = out_of
return 100 if max_points == 0 # 0 out of 0 == 100%
(100 * points / max_points).to_i
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment