Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

5010 – Fix tag with trailing space error #1982

Merged
merged 4 commits into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/models/annotations/tag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def self.run_bulk_create_callbacks(ids_json, pmids_json)
def get_tag_text_reference
if self.tag.is_a?(String)
team_id = self.team&.id
tag_text = TagText.where(text: self.tag, team_id: team_id).last
tag_text = TagText.where(text: self.tag.strip, team_id: team_id).last
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also I suggest to trim database value so we can catch existing one with trailing space
i.e
tag_text = TagText.where(team_id: team_id).where('trim(text) = ?', self.tag.strip).last

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I see that we normalize tag before saving in DB and did self.text.strip.gsub(/^#/, '') so we can skip this comment

if tag_text.nil? && team_id.present?
tag_text = TagText.new
tag_text.text = self.tag
Expand Down
69 changes: 69 additions & 0 deletions test/controllers/graphql_controller_12_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -609,4 +609,73 @@ def teardown
assert_equal "#{CheckConfig.get('checkdesk_base_url')}/images/checklogo.png", JSON.parse(@response.body)['data']['user']['profile_image']
assert_equal "#{CheckConfig.get('checkdesk_base_url')}/images/checklogo.png", JSON.parse(@response.body)['data']['user']['source']['image']
end

test "should treat ' tag' and 'tag' as the same tag, and not try to create a new tag" do
t = create_team
a = ApiKey.create!
b = create_bot_user api_key_id: a.id
create_team_user team: t, user: b
p = create_project team: t
authenticate_with_token(a)

query1 = ' mutation create {
createProjectMedia(input: {
project_id: ' + p.id.to_s + ',
media_type: "Blank",
channel: {main: 1},
set_tags: ["science"],
set_status: "verified",
set_claim_description: "Claim #1.",
set_fact_check: {
title: "Title #1",
language: "en",
}
}) {
project_media {
full_url,
tags {
edges {
node {
tag_text
}
}
}
}
}
} '

post :create, params: { query: query1, team: t.slug }
assert_response :success
assert_equal 'science', JSON.parse(@response.body)['data']['createProjectMedia']['project_media']['tags']['edges'][0]['node']['tag_text']

query2 = ' mutation create {
createProjectMedia(input: {
project_id: ' + p.id.to_s + ',
media_type: "Blank",
channel: {main: 1},
set_tags: [" science"],
set_status: "verified",
set_claim_description: "Claim #2.",
set_fact_check: {
title: "Title #2",
language: "en",
}
}) {
project_media {
full_url,
tags {
edges {
node {
tag_text
}
}
}
}
}
} '

post :create, params: { query: query2, team: t.slug }
assert_response :success
assert_equal 'science', JSON.parse(@response.body)['data']['createProjectMedia']['project_media']['tags']['edges'][0]['node']['tag_text']
end
end
13 changes: 13 additions & 0 deletions test/models/tag_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -271,4 +271,17 @@ def setup
tt2.delete
TagText.update_tags(tt1.id, t.id, tt2.id)
end

test "should treat ' tag' and 'tag' as the same tag, and not try to create a new tag" do
t = create_team
p = create_project team: t
pm1 = create_project_media project: p
pm2 = create_project_media project: p

create_tag tag: 'foo', annotated: pm1

assert_nothing_raised do
create_tag tag: ' foo', annotated: pm2
end
end
end
Loading