Skip to content
This repository was archived by the owner on Jul 22, 2025. It is now read-only.

Commit 2b52ea5

Browse files
committed
job test
1 parent 8355a23 commit 2b52ea5

File tree

2 files changed

+36
-30
lines changed

2 files changed

+36
-30
lines changed

app/jobs/regular/generate_inferred_concepts.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,11 @@ def execute(args = {})
3737
def process_batch(item_ids, item_type, match_only)
3838
klass = item_type.singularize.classify.constantize
3939
items = klass.where(id: item_ids)
40+
manager = DiscourseAi::InferredConcepts::Manager.new
4041

4142
items.each do |item|
4243
begin
43-
process_item(item, item_type, match_only)
44+
process_item(item, item_type, match_only, manager)
4445
rescue => e
4546
Rails.logger.error(
4647
"Error generating concepts from #{item_type.singularize} #{item.id}: #{e.message}\n#{e.backtrace.join("\n")}",
@@ -49,9 +50,8 @@ def process_batch(item_ids, item_type, match_only)
4950
end
5051
end
5152

52-
def process_item(item, item_type, match_only)
53+
def process_item(item, item_type, match_only, manager)
5354
# Use the Manager method that handles both identifying and creating concepts
54-
manager = DiscourseAi::InferredConcepts::Manager.new
5555
if match_only
5656
if item_type == "topics"
5757
manager.match_topic_to_concepts(item)

spec/jobs/regular/generate_inferred_concepts_spec.rb

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,18 @@
99

1010
describe "#execute" do
1111
it "does nothing with blank item_ids" do
12-
expect(DiscourseAi::InferredConcepts::Manager).not_to receive(:match_topic_to_concepts)
12+
expect_any_instance_of(DiscourseAi::InferredConcepts::Manager).not_to receive(
13+
:match_topic_to_concepts,
14+
)
1315

1416
subject.execute(item_type: "topics", item_ids: [])
1517
subject.execute(item_type: "topics", item_ids: nil)
1618
end
1719

1820
it "does nothing with blank item_type" do
19-
expect(DiscourseAi::InferredConcepts::Manager).not_to receive(:match_topic_to_concepts)
21+
expect_any_instance_of(DiscourseAi::InferredConcepts::Manager).not_to receive(
22+
:match_topic_to_concepts,
23+
)
2024

2125
subject.execute(item_type: "", item_ids: [topic.id])
2226
subject.execute(item_type: nil, item_ids: [topic.id])
@@ -30,15 +34,15 @@
3034

3135
context "with topics" do
3236
it "processes topics in match_only mode" do
33-
expect(DiscourseAi::InferredConcepts::Manager).to receive(:match_topic_to_concepts).with(
34-
topic,
35-
)
37+
expect_any_instance_of(DiscourseAi::InferredConcepts::Manager).to receive(
38+
:match_topic_to_concepts,
39+
).with(topic)
3640

3741
subject.execute(item_type: "topics", item_ids: [topic.id], match_only: true)
3842
end
3943

4044
it "processes topics in generation mode" do
41-
expect(DiscourseAi::InferredConcepts::Manager).to receive(
45+
expect_any_instance_of(DiscourseAi::InferredConcepts::Manager).to receive(
4246
:generate_concepts_from_topic,
4347
).with(topic)
4448

@@ -47,7 +51,9 @@
4751

4852
it "handles topics that don't exist" do
4953
# Non-existent IDs should be silently skipped (no error expected)
50-
expect(DiscourseAi::InferredConcepts::Manager).not_to receive(:match_topic_to_concepts)
54+
expect_any_instance_of(DiscourseAi::InferredConcepts::Manager).not_to receive(
55+
:match_topic_to_concepts,
56+
)
5157

5258
subject.execute(
5359
item_type: "topics",
@@ -59,12 +65,11 @@
5965
it "processes multiple topics" do
6066
topic2 = Fabricate(:topic)
6167

62-
expect(DiscourseAi::InferredConcepts::Manager).to receive(:match_topic_to_concepts).with(
63-
topic,
64-
)
65-
expect(DiscourseAi::InferredConcepts::Manager).to receive(:match_topic_to_concepts).with(
66-
topic2,
67-
)
68+
manager_instance = instance_double(DiscourseAi::InferredConcepts::Manager)
69+
allow(DiscourseAi::InferredConcepts::Manager).to receive(:new).and_return(manager_instance)
70+
71+
expect(manager_instance).to receive(:match_topic_to_concepts).with(topic)
72+
expect(manager_instance).to receive(:match_topic_to_concepts).with(topic2)
6873

6974
subject.execute(item_type: "topics", item_ids: [topic.id, topic2.id], match_only: true)
7075
end
@@ -83,15 +88,15 @@
8388

8489
context "with posts" do
8590
it "processes posts in match_only mode" do
86-
expect(DiscourseAi::InferredConcepts::Manager).to receive(:match_post_to_concepts).with(
87-
post,
88-
)
91+
expect_any_instance_of(DiscourseAi::InferredConcepts::Manager).to receive(
92+
:match_post_to_concepts,
93+
).with(post)
8994

9095
subject.execute(item_type: "posts", item_ids: [post.id], match_only: true)
9196
end
9297

9398
it "processes posts in generation mode" do
94-
expect(DiscourseAi::InferredConcepts::Manager).to receive(
99+
expect_any_instance_of(DiscourseAi::InferredConcepts::Manager).to receive(
95100
:generate_concepts_from_post,
96101
).with(post)
97102

@@ -100,7 +105,9 @@
100105

101106
it "handles posts that don't exist" do
102107
# Non-existent IDs should be silently skipped (no error expected)
103-
expect(DiscourseAi::InferredConcepts::Manager).not_to receive(:match_post_to_concepts)
108+
expect_any_instance_of(DiscourseAi::InferredConcepts::Manager).not_to receive(
109+
:match_post_to_concepts,
110+
)
104111

105112
subject.execute(
106113
item_type: "posts",
@@ -112,21 +119,20 @@
112119
it "processes multiple posts" do
113120
post2 = Fabricate(:post)
114121

115-
expect(DiscourseAi::InferredConcepts::Manager).to receive(:match_post_to_concepts).with(
116-
post,
117-
)
118-
expect(DiscourseAi::InferredConcepts::Manager).to receive(:match_post_to_concepts).with(
119-
post2,
120-
)
122+
manager_instance = instance_double(DiscourseAi::InferredConcepts::Manager)
123+
allow(DiscourseAi::InferredConcepts::Manager).to receive(:new).and_return(manager_instance)
124+
125+
expect(manager_instance).to receive(:match_post_to_concepts).with(post)
126+
expect(manager_instance).to receive(:match_post_to_concepts).with(post2)
121127

122128
subject.execute(item_type: "posts", item_ids: [post.id, post2.id], match_only: true)
123129
end
124130
end
125131

126132
it "handles exceptions during processing" do
127-
allow(DiscourseAi::InferredConcepts::Manager).to receive(:match_topic_to_concepts).and_raise(
128-
StandardError.new("Test error"),
129-
)
133+
allow_any_instance_of(DiscourseAi::InferredConcepts::Manager).to receive(
134+
:match_topic_to_concepts,
135+
).and_raise(StandardError.new("Test error"))
130136

131137
expect(Rails.logger).to receive(:error).with(
132138
/Error generating concepts from topic #{topic.id}/,

0 commit comments

Comments
 (0)