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:
Javi Martín
2019-09-07 16:31:00 +02:00
parent 8bb5462253
commit 49751f46ec
2 changed files with 37 additions and 17 deletions

View File

@@ -511,28 +511,29 @@ describe "Consul Schema" do
expect(received_tags).to match_array ["Parks", "Health"]
end
it "uppercase and lowercase tags work ok together for proposals" do
create(:tag, name: "Health")
create(:tag, name: "health")
create(:proposal, tag_list: "health")
create(:proposal, tag_list: "Health")
context "uppercase and lowercase tags" do
let(:uppercase_tag) { create(:tag, name: "Health") }
let(:lowercase_tag) { create(:tag, name: "health") }
response = execute("{ tags { edges { node { name } } } }")
received_tags = extract_fields(response, "tags", "name")
it "works OK when both tags are present for proposals" do
create(:proposal).tags = [uppercase_tag]
create(:proposal).tags = [lowercase_tag]
expect(received_tags).to match_array ["Health", "health"]
end
response = execute("{ tags { edges { node { name } } } }")
received_tags = extract_fields(response, "tags", "name")
it "uppercase and lowercase tags work ok together for debates" do
create(:tag, name: "Health")
create(:tag, name: "health")
create(:debate, tag_list: "Health")
create(:debate, tag_list: "health")
expect(received_tags).to match_array ["Health", "health"]
end
response = execute("{ tags { edges { node { name } } } }")
received_tags = extract_fields(response, "tags", "name")
it "works OK when both tags are present for proposals" do
create(:debate).tags = [uppercase_tag]
create(:debate).tags = [lowercase_tag]
expect(received_tags).to match_array ["Health", "health"]
response = execute("{ tags { edges { node { name } } } }")
received_tags = extract_fields(response, "tags", "name")
expect(received_tags).to match_array ["Health", "health"]
end
end
it "does not display tags for hidden proposals" do