|
3 | 3 | require_relative "../../../support/sentiment_inference_stubs" |
4 | 4 |
|
5 | 5 | RSpec.describe DiscourseAi::Sentiment::PostClassification do |
6 | | - fab!(:post_1) { Fabricate(:post, post_number: 2) } |
7 | | - |
8 | 6 | before do |
9 | 7 | SiteSetting.ai_sentiment_enabled = true |
10 | 8 | SiteSetting.ai_sentiment_model_configs = |
11 | 9 | "[{\"model_name\":\"SamLowe/roberta-base-go_emotions\",\"endpoint\":\"http://samlowe-emotion.com\",\"api_key\":\"123\"},{\"model_name\":\"j-hartmann/emotion-english-distilroberta-base\",\"endpoint\":\"http://jhartmann-emotion.com\",\"api_key\":\"123\"},{\"model_name\":\"cardiffnlp/twitter-roberta-base-sentiment-latest\",\"endpoint\":\"http://cardiffnlp-sentiment.com\",\"api_key\":\"123\"}]" |
12 | 10 | end |
13 | 11 |
|
14 | 12 | describe "#classify!" do |
| 13 | + fab!(:post_1) { Fabricate(:post, post_number: 2) } |
| 14 | + |
15 | 15 | it "does nothing if the post content is blank" do |
16 | 16 | post_1.update_columns(raw: "") |
17 | 17 |
|
|
28 | 28 |
|
29 | 29 | expect(ClassificationResult.where(target: post_1).count).to eq(expected_analysis) |
30 | 30 | end |
| 31 | + |
| 32 | + it "does nothing if there are no classification model" do |
| 33 | + SiteSetting.ai_sentiment_model_configs = "" |
| 34 | + |
| 35 | + subject.classify!(post_1) |
| 36 | + |
| 37 | + expect(ClassificationResult.where(target: post_1).count).to be_zero |
| 38 | + end |
31 | 39 | end |
32 | 40 |
|
33 | 41 | describe "#classify_bulk!" do |
| 42 | + fab!(:post_1) { Fabricate(:post, post_number: 2) } |
34 | 43 | fab!(:post_2) { Fabricate(:post, post_number: 2) } |
35 | 44 |
|
36 | 45 | it "classifies all given posts" do |
|
43 | 52 | expect(ClassificationResult.where(target: post_1).count).to eq(expected_analysis) |
44 | 53 | expect(ClassificationResult.where(target: post_2).count).to eq(expected_analysis) |
45 | 54 | end |
| 55 | + |
| 56 | + it "does nothing if there are no classification model" do |
| 57 | + SiteSetting.ai_sentiment_model_configs = "" |
| 58 | + |
| 59 | + subject.bulk_classify!(Post.where(id: [post_1.id, post_2.id])) |
| 60 | + |
| 61 | + expect(ClassificationResult.where(target: post_1).count).to be_zero |
| 62 | + expect(ClassificationResult.where(target: post_2).count).to be_zero |
| 63 | + end |
| 64 | + end |
| 65 | + |
| 66 | + describe ".backfill_query" do |
| 67 | + it "excludes posts in personal messages" do |
| 68 | + Fabricate(:private_message_post) |
| 69 | + |
| 70 | + posts = described_class.backfill_query |
| 71 | + |
| 72 | + expect(posts).to be_empty |
| 73 | + end |
| 74 | + |
| 75 | + it "excludes posts in restricted categories" do |
| 76 | + sec_cat = Fabricate(:category, read_restricted: true) |
| 77 | + topic = Fabricate(:topic, category: sec_cat) |
| 78 | + Fabricate(:post, topic: topic) |
| 79 | + |
| 80 | + posts = described_class.backfill_query |
| 81 | + |
| 82 | + expect(posts).to be_empty |
| 83 | + end |
| 84 | + |
| 85 | + it "includes regular posts only" do |
| 86 | + Fabricate(:small_action) |
| 87 | + |
| 88 | + posts = described_class.backfill_query |
| 89 | + |
| 90 | + expect(posts).to be_empty |
| 91 | + end |
| 92 | + |
| 93 | + it "excludes posts from deleted topics" do |
| 94 | + topic = Fabricate(:topic, deleted_at: 1.hour.ago) |
| 95 | + Fabricate(:post, topic: topic) |
| 96 | + |
| 97 | + posts = described_class.backfill_query |
| 98 | + |
| 99 | + expect(posts).to be_empty |
| 100 | + end |
| 101 | + |
| 102 | + it "excludes deleted posts" do |
| 103 | + Fabricate(:post, deleted_at: 1.hour.ago) |
| 104 | + |
| 105 | + posts = described_class.backfill_query |
| 106 | + |
| 107 | + expect(posts).to be_empty |
| 108 | + end |
| 109 | + |
| 110 | + context "with max_age_days" do |
| 111 | + fab!(:age_post) { Fabricate(:post, created_at: 3.days.ago) } |
| 112 | + |
| 113 | + it "includes a post when is younger" do |
| 114 | + posts = described_class.backfill_query(max_age_days: 4) |
| 115 | + |
| 116 | + expect(posts).to contain_exactly(age_post) |
| 117 | + end |
| 118 | + |
| 119 | + it "excludes posts when it's older" do |
| 120 | + posts = described_class.backfill_query(max_age_days: 2) |
| 121 | + |
| 122 | + expect(posts).to be_empty |
| 123 | + end |
| 124 | + end |
| 125 | + |
| 126 | + context "with from_post_id" do |
| 127 | + fab!(:post) |
| 128 | + |
| 129 | + it "includes post if ID is higher" do |
| 130 | + posts = described_class.backfill_query(from_post_id: post.id - 1) |
| 131 | + |
| 132 | + expect(posts).to contain_exactly(post) |
| 133 | + end |
| 134 | + |
| 135 | + it "excludes post if ID is lower" do |
| 136 | + posts = described_class.backfill_query(from_post_id: post.id + 1) |
| 137 | + |
| 138 | + expect(posts).to be_empty |
| 139 | + end |
| 140 | + end |
46 | 141 | end |
47 | 142 | end |
0 commit comments