Skip to content

Commit

Permalink
create a wrapper method so this is easier to read
Browse files Browse the repository at this point in the history
  • Loading branch information
vasconsaurus committed Sep 13, 2024
1 parent 49f71c0 commit dffb66d
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 14 deletions.
2 changes: 1 addition & 1 deletion app/models/concerns/project_media_creators.rb
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ def create_claim_description_and_fact_check

def create_tags_in_background
if self.set_tags.is_a?(Array)
GenericWorker.perform_in(1.second, 'ProjectMedia', 'create_tags', project_media_id: self.id, tags_json: self.set_tags.to_json, user_id: self.user_id)
ProjectMedia.run_later_in(1.second, 'create_tags', project_media_id: self.id, tags_json: self.set_tags.to_json, user_id: self.user_id)
end
end
end
5 changes: 5 additions & 0 deletions config/initializers/class_extensions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class Class
def run_later_in(time, klass_method, *method_args)
GenericWorker.perform_in(time, self.to_s, klass_method, *method_args)
end
end
50 changes: 37 additions & 13 deletions test/models/project_media_8_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,43 +6,67 @@ def setup
require 'sidekiq/testing'
end

test "create tags when project media id and tags are present" do
t = create_team
p = create_project team: t
pm = create_project_media project: p
test ":create_tags should create tags when project media id and tags are present" do
team = create_team
project = create_project team: team
pm = create_project_media project: project

assert_nothing_raised do
ProjectMedia.create_tags(project_media_id: pm.id, tags_json: ['one', 'two'].to_json)
end
assert_equal 2, pm.annotations('tag').count
end

test "does not raise an error when no project media is sent" do
test ":create_tags should not raise an error when no project media is sent" do
assert_nothing_raised do
CheckSentry.expects(:notify).once
ProjectMedia.create_tags(project_media_id: nil, tags_json: ['one', 'two'].to_json)
end
end

test "when creating an item with tag/tags, tags should be created in the background" do
test "when creating an item with tag/tags, after_create :create_tags_in_background callback should create tags in the background" do
Sidekiq::Testing.inline!

t = create_team
p = create_project team: t
pm = create_project_media project: p, tags: ['one']
team = create_team
project = create_project team: team
pm = create_project_media project: project, tags: ['one']

assert_equal 1, pm.annotations('tag').count
end

test "when creating an item with multiple tags, only one job should be scheduled" do
test "when creating an item with multiple tags, after_create :create_tags_in_background callback should only schedule one job" do
Sidekiq::Testing.fake!

t = create_team
p = create_project team: t
team = create_team
project = create_project team: team

assert_nothing_raised do
create_project_media project: p, tags: ['one', 'two', 'three']
create_project_media project: project, tags: ['one', 'two', 'three']
end
assert_equal 1, GenericWorker.jobs.size
end

test "when using :run_later_in to schedule multiple tags creation, it should only schedule one job" do
Sidekiq::Testing.fake!

team = create_team
project = create_project team: team
pm = create_project_media project: project

ProjectMedia.run_later_in(0.second, 'create_tags', project_media_id: pm.id, tags_json: ['one', 'two', 'three'].to_json, user_id: pm.user_id)

assert_equal 1, GenericWorker.jobs.size
end

test "when using :run_later_in to schedule multiple tags creation, tags should be created" do
Sidekiq::Testing.inline!

team = create_team
project = create_project team: team
pm = create_project_media project: project

ProjectMedia.run_later_in(0.second, 'create_tags', project_media_id: pm.id, tags_json: ['one', 'two', 'three'].to_json, user_id: pm.user_id)

assert_equal 3, pm.annotations('tag').count
end
end

0 comments on commit dffb66d

Please sign in to comment.