Skip to content

Commit

Permalink
CV2-6038: fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
melsawy committed Feb 8, 2025
1 parent 68a203e commit da65c51
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 20 deletions.
12 changes: 5 additions & 7 deletions app/lib/check_elastic_search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,11 @@ def update_elasticsearch_doc_bg(options)
data = get_elasticsearch_data(options[:data], options[:skip_get_data])
fields = {}
options[:keys].each do |k|
unless data[k].nil?
if data[k].class.to_s == 'Hash'
value = get_fresh_value(data[k].with_indifferent_access)
fields[k] = value unless value.nil?
else
fields[k] = data[k]
end
if data[k].class.to_s == 'Hash'
value = get_fresh_value(data[k].with_indifferent_access)
fields[k] = value
else
fields[k] = data[k]
end
end
if fields.count
Expand Down
2 changes: 1 addition & 1 deletion app/models/claim_description.rb
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ def migrate_claim_and_fact_check_logs

def log_relevant_article_results
fc = self.fact_check
self.project_media.delay.log_relevant_results(fc.class.name, fc.id, User.current&.id, self.class.actor_session_id)
self.project_media.delay.log_relevant_results(fc.class.name, fc.id, User.current&.id, self.class.actor_session_id) unless fc.nil?
end

def cant_apply_article_to_item_if_article_is_in_the_trash
Expand Down
6 changes: 6 additions & 0 deletions app/models/concerns/project_media_getters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -213,4 +213,10 @@ def team_avatar
def fact_check
self.claim_description&.fact_check
end

def explainers_title
# Get the title for all explainer assinged to the item
explainer_titles = self.explainer_items.map(&:explainer).map(&:title).join(' ')
explainer_titles.blank? ? nil : explainer_titles
end
end
18 changes: 13 additions & 5 deletions app/models/explainer_item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,19 @@ def update_elasticsearch_data
return if self.disable_es_callbacks || RequestStore.store[:disable_es_callbacks]
pm = self.project_media
explainer_titles = pm.explainer_items.map(&:explainer).map(&:title).join(' ')
data = explainer_titles.blank? ? { explainer_title: nil } : { explainer_title: explainer_titles }
updated_at = Time.now
pm.update_columns(updated_at: updated_at)
data[:updated_at] = updated_at.utc
pm.update_elasticsearch_doc(data.keys, data, pm.id, true)
# touch item to update `updated_at` date
if ProjectMedia.exists?(pm.id)
updated_at = Time.now
pm.update_columns(updated_at: updated_at)
data = { updated_at: updated_at.utc }
data['explainer_title'] = {
method: "explainers_title",
klass: pm.class.name,
id: pm.id,
default: nil,
}
pm.update_elasticsearch_doc(data.keys, data, pm.id, true)
end
end

def log_relevant_article_results
Expand Down
14 changes: 7 additions & 7 deletions test/controllers/elastic_search_9_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,16 @@ def setup
assert_equal [pm5.id], results.medias.map(&:id).sort
# remove explainer
ExplainerItem.where(explainer_id: ex2_a.id, project_media_id: pm2.id).destroy_all
ExplainerItem.where(explainer_id: ex3.id, project_media_id: pm2.id).destroy_all
cd4.project_id = nil
ExplainerItem.where(explainer_id: ex3.id, project_media_id: pm3.id).destroy_all
cd4.project_media_id = nil
cd4.save!
sleep 1
results = CheckSearch.new({ has_article: ['ANY_VALUE'] }.to_json)
assert_equal [pm.id, pm2.id, pm4], results.medias.map(&:id).sort
assert_equal [pm.id, pm2.id, pm4.id], results.medias.map(&:id).sort
results = CheckSearch.new({ has_article: ['NO_VALUE'] }.to_json)
assert_equal [pm3.id, pm5.id], results.medias.map(&:id).sort
# remove fact-check and explainer
cd.project_id = nil
cd.project_media_id = nil
cd.save!
ExplainerItem.where(explainer_id: ex2_b.id, project_media_id: pm2.id).destroy_all
ExplainerItem.where(explainer_id: ex4.id, project_media_id: pm4.id).destroy_all
Expand All @@ -53,12 +53,12 @@ def setup
results = CheckSearch.new({ has_article: ['NO_VALUE'] }.to_json)
assert_equal [pm.id, pm2.id, pm3.id, pm4.id, pm5.id], results.medias.map(&:id).sort
# re-assing fact or explainer
cd.project_id = pm2.id
cd.project_media_id = pm2.id
cd.save!
pm5.explainers << ex
pm5.explainers << ex4
sleep 1
results = CheckSearch.new({ has_article: ['ANY_VALUE'] }.to_json)
assert_equal [pm2.id, pm5], results.medias.map(&:id).sort
assert_equal [pm2.id, pm5.id], results.medias.map(&:id).sort
results = CheckSearch.new({ has_article: ['NO_VALUE'] }.to_json)
assert_equal [pm.id, pm3.id, pm4.id], results.medias.map(&:id).sort
end
Expand Down

0 comments on commit da65c51

Please sign in to comment.