SpeciesFileGroup / taxonworks

Workbench for biodiversity informatics.
http://taxonworks.org
MIT License
87 stars 26 forks source link

`Combination.protonyms_matching_original_relationships` bug with other protonym relationships #2524

Closed LordFlashmeow closed 3 years ago

LordFlashmeow commented 3 years ago

Describe the bug

A clear and concise description of what the bug is.

Combination.protonyms_matching_original_relationships fails to find original relationships when the species (object_taxon_name) has other TaxonNameRelationships besides the OriginalCombination ones.

As you can see below, the query does not take into account other relationships the protonym may have.

https://github.com/SpeciesFileGroup/taxonworks/blob/26246656ef8fbcc611a085c72a4fcbd6a47c5081/app/models/combination.rb#L222

The SQL query looks like this—notice the HAVING COUNT line:

SELECT "taxon_names".*
FROM "taxon_names"
         INNER JOIN (SELECT "j".*
                     FROM "taxon_names" "j"
                              INNER JOIN "taxon_name_relationships"
                                         ON "taxon_name_relationships"."object_taxon_name_id" = "j"."id"
                              INNER JOIN "taxon_name_relationships" "b_genus"
                                         ON "b_genus"."object_taxon_name_id" = "j"."id" 
                                                AND "b_genus"."type" = 'TaxonNameRelationship::OriginalCombination::OriginalGenus' 
                                                AND "b_genus"."subject_taxon_name_id" = 65317
                              INNER JOIN "taxon_name_relationships" "b_species"
                                         ON "b_species"."object_taxon_name_id" = "j"."id" 
                                                AND "b_species"."type" = 'TaxonNameRelationship::OriginalCombination::OriginalSpecies' 
                                                AND "b_species"."subject_taxon_name_id" = 65320
                     GROUP BY "j"."id"
                     HAVING COUNT("taxon_name_relationships"."object_taxon_name_id") = 2) join_alias
                    ON join_alias."id" = "taxon_names"."id"
WHERE "taxon_names"."type" = 'Protonym';
proceps commented 3 years ago

@LordFlashmeow I pushed a change, do you have means to test it? I also put a test in spec_combination.rb but not sure if it catches what you experienced.

    specify '.matching_protonyms 6' do
      species.update(original_genus: genus, original_subgenus: genus, original_species: species)
      species2.update(original_genus: genus, original_species: species, original_subspecies: species2)
      tr = TaxonNameRelationship.create(subject_taxon_name: species2, object_taxon_name: species, type: 'TaxonNameRelationship::Iczn::Invalidating::Synonym')
      expect(Combination.matching_protonyms(nil, genus: genus.id, species: species.id).to_a).to contain_exactly()
    end
proceps commented 3 years ago

@LordFlashmeow Sorry was wrong test. Replicated it. It is fixed now.