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

Already on GitHub? Sign in to your account

AO3-5269 Fix regex for getting sortable work authors #3199

Merged
merged 3 commits into from Dec 8, 2017

Conversation

Projects
None yet
2 participants
Contributor

redsummernight commented Dec 5, 2017

Issue

https://otwarchive.atlassian.net/browse/AO3-5269

Purpose

The hyphen should be escaped, otherwise we're removing the first character if it's anything in the range from "+" to "=".

Testing

See issue.

AO3-5269 Fix regex for getting sortable work authors
The hyphen should be escaped.
spec/models/work_search_form_spec.rb
+ describe "sorting results" do
+ describe "by authors" do
+ before do
+ %w(-zzz _yasuho 21st_wombat 007aardvark 6tasmanian_devil).each do |pseud_name|
@elzj

elzj Dec 5, 2017

Owner

My only thought here is that since it's the work method that was broken, maybe we should test the behavior of it primarily in a work spec unit test. Then when we know it's saving the values correctly, we might not need to create as many works here and we can save some runtime that way.

spec/models/work_spec.rb
@@ -102,6 +102,26 @@
end
end
+ describe "authors to sort on" do
@elzj

elzj Dec 7, 2017

Owner

So this is totally fine, but while I'm nitpicking, I'd say you want the spec names to be a little more readable, and, whenever possible, to not touch the database if you can avoid it. I would make it something like this:

describe "#set_author_sorting" do
    let(:work) { build(:work) }

    context "when the pseuds start with special characters" do
      it "should remove those characters" do
        work.authors = [Pseud.new(name: "-jolyne")]
        work.set_author_sorting
        expect(work.authors_to_sort_on).to eq "jolyne"

        work.authors = [Pseud.new(name: "_hermes")]
        work.set_author_sorting
        expect(work.authors_to_sort_on).to eq "hermes"
      end
    end

    context "when the pseuds start with numbers" do
      it "should not remove them" do
        work.authors = [Pseud.new(name: "007james")]
        work.set_author_sorting
        expect(work.authors_to_sort_on).to eq "007james"        
      end
    end

    context "when the work is anonymous" do
      it "should set the author sorting to Anonymous" do
        work.in_anon_collection = true
        work.authors = [Pseud.new(name: "stealthy")]
        work.set_author_sorting
        expect(work.authors_to_sort_on).to eq "Anonymous"
      end
    end

    context "when the work has multiple pseuds" do
      it "should combine them with commas" do
        work.authors = [Pseud.new(name: "diavolo"), Pseud.new(name: "doppio")]
        work.set_author_sorting
        expect(work.authors_to_sort_on).to eq "diavolo,  doppio"
      end
    end
  end

There, you can add as many examples as you like, and it's not going to add significantly to the test runtime because nothing gets saved. And then the integration test in the feature will confirm that the whole thing works together - that the field gets saved and that we can sort on it.

@redsummernight

redsummernight Dec 8, 2017

Contributor

Thanks! It didn't occur to me to just call work.set_author_sorting, I'll update the specs.

Should I remove or keep the new tests in spec/models/work_search_form_spec.rb?

AO3-5269 Test pseuds starting with numbers and anonymous works
Improve spec names.
Avoid saving to the database.

@elzj elzj merged commit 84bd685 into otwcode:master Dec 8, 2017

3 of 4 checks passed

hound Hound is busy reviewing changes...
Scrutinizer 1 new issues
Details
codeclimate All good!
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details

@redsummernight redsummernight deleted the redsummernight:AO3-5269-sorted-authors branch Dec 8, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment