-
-
Notifications
You must be signed in to change notification settings - Fork 729
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
DFC import: Find broader taxon if we don't have a specific one #13109
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
47 changes: 0 additions & 47 deletions
47
engines/dfc_provider/app/services/dfc_product_type_factory.rb
This file was deleted.
Oops, something went wrong.
31 changes: 31 additions & 0 deletions
31
engines/dfc_provider/app/services/product_type_importer.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# frozen_string_literal: true | ||
|
||
class ProductTypeImporter < DfcBuilder | ||
# Try to find the taxon closest matching the given product type. | ||
# If we don't find any matching taxon, we return a random one. | ||
def self.taxon(product_type) | ||
priority_list = [product_type, *list_broaders(product_type)].compact | ||
|
||
# Optimistic querying. | ||
# We could query all broader taxons in one but then we need to still sort | ||
# them locally and use more memory. That would be a pessimistic query. | ||
# Consider caching the result instead. | ||
taxons = priority_list.lazy.map do |type| | ||
Spree::Taxon.find_by(dfc_id: type.semanticId) | ||
end.compact | ||
|
||
taxons.first || Spree::Taxon.first | ||
end | ||
|
||
def self.list_broaders(type) | ||
return [] if type.nil? | ||
|
||
broaders = type.broaders.map do |id| | ||
DataFoodConsortium::Connector::SKOSParser.concepts[id] | ||
end | ||
|
||
broaders + broaders.flat_map do |broader| | ||
list_broaders(broader) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 0 additions & 49 deletions
49
engines/dfc_provider/spec/services/dfc_product_type_factory_spec.rb
This file was deleted.
Oops, something went wrong.
67 changes: 67 additions & 0 deletions
67
engines/dfc_provider/spec/services/product_type_importer_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative "../spec_helper" | ||
|
||
RSpec.describe ProductTypeImporter do | ||
let(:drink) { | ||
DfcLoader.connector.PRODUCT_TYPES.DRINK | ||
} | ||
let(:soft_drink) { | ||
DfcLoader.connector.PRODUCT_TYPES.DRINK.SOFT_DRINK | ||
} | ||
let(:lemonade) { | ||
DfcLoader.connector.PRODUCT_TYPES.DRINK.SOFT_DRINK.LEMONADE | ||
} | ||
|
||
describe ".taxon" do | ||
it "finds a linked taxon" do | ||
create(:taxon, dfc_id: soft_drink.semanticId) | ||
lemonade_taxon = create(:taxon, dfc_id: lemonade.semanticId) | ||
expect(described_class.taxon(lemonade)).to eq lemonade_taxon | ||
end | ||
|
||
it "falls back to a broader taxon" do | ||
drink_taxon = create(:taxon, dfc_id: drink.semanticId) | ||
expect(described_class.taxon(lemonade)).to eq drink_taxon | ||
end | ||
|
||
it "returns random taxon when none can be found" do | ||
only_taxon = create(:taxon) | ||
expect(described_class.taxon(lemonade)).to eq only_taxon | ||
end | ||
|
||
it "queries the database only until it found a taxon" do | ||
soft_drink_taxon = create(:taxon, dfc_id: soft_drink.semanticId) | ||
|
||
expect { | ||
expect(described_class.taxon(lemonade)).to eq soft_drink_taxon | ||
}.to query_database [ | ||
"Spree::Taxon Load", # query for lemonade, not found | ||
"Spree::Taxon Load", # query for soft drink, found | ||
# no query for drink | ||
] | ||
end | ||
end | ||
|
||
describe ".list_broaders" do | ||
it "returns an empty array if no type is given" do | ||
list = described_class.list_broaders(nil) | ||
expect(list).to eq [] | ||
end | ||
|
||
it "can return an empty list for top concepts" do | ||
list = described_class.list_broaders(drink) | ||
expect(list).to eq [] | ||
end | ||
|
||
it "lists the broader concepts of a type" do | ||
list = described_class.list_broaders(soft_drink) | ||
expect(list).to eq [drink] | ||
end | ||
|
||
it "lists all the broader concepts to the top concepts" do | ||
list = described_class.list_broaders(lemonade) | ||
expect(list).to eq [soft_drink, drink] | ||
end | ||
end | ||
end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice to see different ways of doing things. I probably would have ended up with an
each
orreduce
loop, but this is nice 👍