diff --git a/lib/modules/sentiment/entry_point.rb b/lib/modules/sentiment/entry_point.rb index f097e340..bacc7af6 100644 --- a/lib/modules/sentiment/entry_point.rb +++ b/lib/modules/sentiment/entry_point.rb @@ -103,7 +103,7 @@ module DiscourseAi y: tl_emotion_avgs.sum do |tl_emotion_avg| tl_emotion_avg.public_send("avg_#{e}").to_i - end / tl_emotion_avgs.size, + end / [tl_emotion_avgs.size, 1].max, } end, } diff --git a/spec/lib/modules/sentiment/entry_point_spec.rb b/spec/lib/modules/sentiment/entry_point_spec.rb index 2b5354ca..b970357c 100644 --- a/spec/lib/modules/sentiment/entry_point_spec.rb +++ b/spec/lib/modules/sentiment/entry_point_spec.rb @@ -121,6 +121,19 @@ RSpec.describe DiscourseAi::Sentiment::EntryPoint do expect(tl_01_point[:y]).to eq(emotion_1[tl_01_point[:x].downcase.to_sym]) expect(tl_234_point[:y]).to eq(emotion_2[tl_234_point[:x].downcase.to_sym]) end + + it "doesn't try to divide by zero if there are no data in a TL group" do + post_1.user.update!(trust_level: TrustLevel[3]) + post_2.user.update!(trust_level: TrustLevel[3]) + + emotion_classification(post_1, emotion_1) + emotion_classification(post_2, emotion_2) + + report = Report.find("post_emotion") + tl_01_point = report.data[0][:data].first + + expect(tl_01_point[:y]).to be_zero + end end end end