mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-27 01:43:22 +00:00
Import graphs including anonymous objects
This commit is contained in:
@@ -14,7 +14,11 @@ module DataFoodConsortium
|
||||
|
||||
def self.type_map
|
||||
@type_map ||= TYPES.each_with_object({}) do |clazz, result|
|
||||
type_uri = clazz.new(nil).semanticType
|
||||
# Methods with variable arguments have a negative arity of -n-1
|
||||
# where n is the number of required arguments.
|
||||
number_of_required_args = -1 * (clazz.instance_method(:initialize).arity + 1)
|
||||
args = Array.new(number_of_required_args)
|
||||
type_uri = clazz.new(*args).semanticType
|
||||
result[type_uri] = clazz
|
||||
end
|
||||
end
|
||||
@@ -48,11 +52,12 @@ module DataFoodConsortium
|
||||
end
|
||||
|
||||
def build_subject(type_statement)
|
||||
id = type_statement.subject.value
|
||||
# Not all subjects have an id, some are anonymous.
|
||||
id = type_statement.subject.try(:value)
|
||||
type = type_statement.object.value
|
||||
clazz = self.class.type_map[type]
|
||||
|
||||
clazz.new(id)
|
||||
clazz.new(*[id].compact)
|
||||
end
|
||||
|
||||
def apply_statements(statements)
|
||||
|
||||
@@ -31,6 +31,18 @@ describe DataFoodConsortium::Connector::Importer, vcr: true do
|
||||
name: "Ocra",
|
||||
)
|
||||
end
|
||||
let(:quantity) do
|
||||
DataFoodConsortium::Connector::QuantitativeValue.new(
|
||||
unit: piece,
|
||||
value: 5,
|
||||
)
|
||||
end
|
||||
let(:piece) do
|
||||
unless connector.MEASURES.respond_to?(:UNIT)
|
||||
connector.loadMeasures(read_file("measures"))
|
||||
end
|
||||
connector.MEASURES.UNIT.QUANTITYUNIT.PIECE
|
||||
end
|
||||
|
||||
it "imports a single object with simple properties" do
|
||||
result = import(product)
|
||||
@@ -59,6 +71,18 @@ describe DataFoodConsortium::Connector::Importer, vcr: true do
|
||||
expect(tomato.totalTheoreticalStock).to eq 3
|
||||
end
|
||||
|
||||
it "imports a graph including anonymous objects" do
|
||||
product.quantity = quantity
|
||||
|
||||
tomato, items = import(product)
|
||||
|
||||
expect(tomato.name).to eq "Tomato"
|
||||
expect(items.value).to eq 5
|
||||
|
||||
# Pending matching concepts:
|
||||
#expect(items.unit).to eq piece
|
||||
end
|
||||
|
||||
it "imports properties with lists" do
|
||||
result = import(enterprise, product, second_product)
|
||||
|
||||
@@ -73,4 +97,10 @@ describe DataFoodConsortium::Connector::Importer, vcr: true do
|
||||
json = connector.export(*args)
|
||||
connector.import(json)
|
||||
end
|
||||
|
||||
def read_file(name)
|
||||
JSON.parse(
|
||||
Rails.root.join("engines/dfc_provider/vendor/#{name}.json").read
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user