Fix tests for uppercase tags
The tests in the `spec/lib/graphql_spec.rb` failed sometimes because creating a record with a tag list of ["health"] when both "health" and "Health" tags exist might assign either one of them. These tests usually pass because we create two records and just by chance usually one of the records gets one tag and the other one gets the other tag. However, the test was written as if we expected the first record to get the first tag and the second record to get the second tag, while very often the tests were passing because the first record got the second tag and the second record got the first tag. And when both records get the same tag, the tests fail. So I've changed these tests to tags are assigned directly and, since we want to test the `tag_list` method, I've also added some tests to the Tag model, which reflect the current behaviour: a random tag is assigned when several tags with the same case-insensitive name exist. Another option to assign the right tag to the record we're creating would be to add `ActsAsTaggableOn.strict_case_match = true` to an initializer. However, that would also create new tags on the database when we accidentally assign a tag like "hEaLth" (like in the test we add in this commit). Ideally we would have a strict case match for existing tags and a non-strict case match for new tags, but I haven't found a way to do it.
This commit is contained in:
@@ -511,11 +511,13 @@ describe "Consul Schema" do
|
|||||||
expect(received_tags).to match_array ["Parks", "Health"]
|
expect(received_tags).to match_array ["Parks", "Health"]
|
||||||
end
|
end
|
||||||
|
|
||||||
it "uppercase and lowercase tags work ok together for proposals" do
|
context "uppercase and lowercase tags" do
|
||||||
create(:tag, name: "Health")
|
let(:uppercase_tag) { create(:tag, name: "Health") }
|
||||||
create(:tag, name: "health")
|
let(:lowercase_tag) { create(:tag, name: "health") }
|
||||||
create(:proposal, tag_list: "health")
|
|
||||||
create(:proposal, tag_list: "Health")
|
it "works OK when both tags are present for proposals" do
|
||||||
|
create(:proposal).tags = [uppercase_tag]
|
||||||
|
create(:proposal).tags = [lowercase_tag]
|
||||||
|
|
||||||
response = execute("{ tags { edges { node { name } } } }")
|
response = execute("{ tags { edges { node { name } } } }")
|
||||||
received_tags = extract_fields(response, "tags", "name")
|
received_tags = extract_fields(response, "tags", "name")
|
||||||
@@ -523,17 +525,16 @@ describe "Consul Schema" do
|
|||||||
expect(received_tags).to match_array ["Health", "health"]
|
expect(received_tags).to match_array ["Health", "health"]
|
||||||
end
|
end
|
||||||
|
|
||||||
it "uppercase and lowercase tags work ok together for debates" do
|
it "works OK when both tags are present for proposals" do
|
||||||
create(:tag, name: "Health")
|
create(:debate).tags = [uppercase_tag]
|
||||||
create(:tag, name: "health")
|
create(:debate).tags = [lowercase_tag]
|
||||||
create(:debate, tag_list: "Health")
|
|
||||||
create(:debate, tag_list: "health")
|
|
||||||
|
|
||||||
response = execute("{ tags { edges { node { name } } } }")
|
response = execute("{ tags { edges { node { name } } } }")
|
||||||
received_tags = extract_fields(response, "tags", "name")
|
received_tags = extract_fields(response, "tags", "name")
|
||||||
|
|
||||||
expect(received_tags).to match_array ["Health", "health"]
|
expect(received_tags).to match_array ["Health", "health"]
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
it "does not display tags for hidden proposals" do
|
it "does not display tags for hidden proposals" do
|
||||||
proposal = create(:proposal, tag_list: "Health")
|
proposal = create(:proposal, tag_list: "Health")
|
||||||
|
|||||||
@@ -34,4 +34,23 @@ describe Tag do
|
|||||||
expect(tag).to be_valid
|
expect(tag).to be_valid
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context "Same tag uppercase and lowercase" do
|
||||||
|
before do
|
||||||
|
create(:tag, name: "Health")
|
||||||
|
create(:tag, name: "health")
|
||||||
|
end
|
||||||
|
|
||||||
|
it "assigns only one of the existing tags (we can't control which one)" do
|
||||||
|
debate = create(:debate, tag_list: "Health")
|
||||||
|
|
||||||
|
expect([["Health"], ["health"]]).to include debate.reload.tag_list
|
||||||
|
end
|
||||||
|
|
||||||
|
it "assigns existing tags instead of creating new similar ones" do
|
||||||
|
debate = create(:debate, tag_list: "hEaLth")
|
||||||
|
|
||||||
|
expect([["Health"], ["health"]]).to include debate.reload.tag_list
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user