VRFS-3316 : Review query method and spec to verify.
This commit is contained in:
parent
74b8c81a8a
commit
bd4b3380c2
|
|
@ -12,20 +12,40 @@ module JamRuby
|
|||
validates :target_id, uniqueness: {scope: :user_id, message: "There is already a review for this User and Target."}
|
||||
|
||||
class << self
|
||||
def index(options={})
|
||||
if(options.key?(:include_deleted))
|
||||
arel = Review.select("*")
|
||||
else
|
||||
arel = Review.where("deleted_at IS NULL")
|
||||
end
|
||||
|
||||
if(options.key?(:target_id))
|
||||
arel = arel.where("target_id=?", options[:target_id])
|
||||
end
|
||||
|
||||
if(options.key?(:user_id))
|
||||
arel = arel.where("user_id=?", options[:user_id])
|
||||
end
|
||||
|
||||
arel
|
||||
end
|
||||
|
||||
# Create review_summary records by grouping reviews
|
||||
def reduce()
|
||||
ReviewSummary.transaction do
|
||||
ReviewSummary.destroy_all
|
||||
Review.select("target_id, target_type AS target_type, AVG(rating) as avg_rating, count(*) as review_count, SUM(CASE WHEN rating>=3.0 THEN 1 ELSE 0 END) AS pos_count").group("target_type, target_id")
|
||||
.each do |r|
|
||||
#puts "Reducing reviews: #{r.inspect} #{r.pos_count} / #{r.review_count}"
|
||||
ReviewSummary.create!(
|
||||
target_id: r.target_id,
|
||||
target_type: r.target_type,
|
||||
avg_rating: r.avg_rating,
|
||||
wilson_score: ci_lower_bound(r.pos_count, r.review_count),
|
||||
review_count: r.review_count
|
||||
)
|
||||
Review.select("target_id, target_type AS target_type, AVG(rating) as avg_rating, count(*) as review_count, SUM(CASE WHEN rating>=3.0 THEN 1 ELSE 0 END) AS pos_count")
|
||||
.where("deleted_at IS NULL")
|
||||
.group("target_type, target_id")
|
||||
.each do |r|
|
||||
wilson_score=ci_lower_bound(r.pos_count, r.review_count)
|
||||
ReviewSummary.create!(
|
||||
target_id: r.target_id,
|
||||
target_type: r.target_type,
|
||||
avg_rating: r.avg_rating,
|
||||
wilson_score: wilson_score,
|
||||
review_count: r.review_count
|
||||
)
|
||||
end # each
|
||||
end # transaction
|
||||
end # reduce
|
||||
|
|
|
|||
|
|
@ -53,12 +53,13 @@ describe Review do
|
|||
|
||||
review2 = Review.create(target:target, rating:5, user:FactoryGirl.create(:user))
|
||||
review2.valid?.should be_true
|
||||
Review.count.should eq(2)
|
||||
Review.should have(2).items
|
||||
Review.index.should have(2).items
|
||||
|
||||
# Reduce and check:
|
||||
ReviewSummary.count.should eq(0)
|
||||
ReviewSummary.should have(0).items
|
||||
Review.reduce()
|
||||
ReviewSummary.count.should eq(1)
|
||||
ReviewSummary.should have(1).items
|
||||
ReviewSummary.first.avg_rating.should eq(4.0)
|
||||
|
||||
puts "ORIG: #{ReviewSummary.all.inspect}"
|
||||
|
|
@ -67,6 +68,8 @@ describe Review do
|
|||
|
||||
# Create some more and verify:
|
||||
5.times {Review.create(target:target, rating:5, user:FactoryGirl.create(:user))}
|
||||
Review.index.should have(7).items
|
||||
|
||||
Review.reduce()
|
||||
|
||||
ReviewSummary.first.wilson_score.should > ws_orig
|
||||
|
|
@ -125,12 +128,12 @@ describe Review do
|
|||
review.valid?.should be_true
|
||||
review2 = Review.create(target:target, rating:5, user:FactoryGirl.create(:user))
|
||||
review2.valid?.should be_true
|
||||
Review.count.should eq(2)
|
||||
Review.should have(2).items
|
||||
|
||||
# Reduce and check:
|
||||
ReviewSummary.count.should eq(0)
|
||||
ReviewSummary.should have(0).items
|
||||
Review.reduce()
|
||||
ReviewSummary.count.should eq(1)
|
||||
ReviewSummary.should have(1).items
|
||||
ReviewSummary.first.avg_rating.should eq(4.0)
|
||||
|
||||
puts "ORIG: #{ReviewSummary.all.inspect}"
|
||||
|
|
@ -141,7 +144,7 @@ describe Review do
|
|||
# Create some more and verify:
|
||||
5.times {Review.create(target:target, rating:5, user:FactoryGirl.create(:user))}
|
||||
Review.reduce()
|
||||
ReviewSummary.count.should eq(1)
|
||||
ReviewSummary.should have(1).items
|
||||
ReviewSummary.first.wilson_score.should > ws_orig
|
||||
ReviewSummary.first.avg_rating.should > avg_orig
|
||||
|
||||
|
|
@ -149,21 +152,23 @@ describe Review do
|
|||
# Create some more with a different target and verify:
|
||||
target2=FactoryGirl.create(:jam_track)
|
||||
5.times {Review.create(target:target2, rating:5, user:FactoryGirl.create(:user))}
|
||||
Review.index.should have(12).items
|
||||
Review.index(target_id: target2).should have(5).items
|
||||
Review.reduce()
|
||||
summaries = ReviewSummary.index()
|
||||
summaries.count.should eq(2)
|
||||
summaries.should have(2).items
|
||||
summaries[0].wilson_score.should > summaries[1].wilson_score
|
||||
|
||||
summaries = ReviewSummary.index(target_id: target2)
|
||||
puts "MULTIPLE TARGET ALL: #{summaries.inspect}"
|
||||
summaries.count.should eq(1)
|
||||
summaries.should have(1).items
|
||||
summaries[0].target_id.should eq(target2.id)
|
||||
|
||||
summaries = ReviewSummary.index(target_type: "JamRuby::JamTrack")
|
||||
summaries.count.should eq(2)
|
||||
summaries.should have(2).items
|
||||
|
||||
summaries = ReviewSummary.index(minimum_reviews: 6)
|
||||
summaries.count.should eq(1)
|
||||
summaries.should have(1).items
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
Loading…
Reference in New Issue