From 231cf91cc29f73077e2814c19d0d89adb7f54520 Mon Sep 17 00:00:00 2001 From: Roman Rizzi Date: Wed, 8 Nov 2023 13:05:36 -0300 Subject: [PATCH] FIX: Don't divide by zero if there is no emotion data for TL group (#285) --- lib/modules/sentiment/entry_point.rb | 2 +- spec/lib/modules/sentiment/entry_point_spec.rb | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) 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