forked from openfoodfoundation/openfoodnetwork
-
Notifications
You must be signed in to change notification settings - Fork 0
/
importer_spec.rb
175 lines (153 loc) · 5.51 KB
/
importer_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# frozen_string_literal: true
require_relative "../../../spec_helper"
require "data_food_consortium/connector/connector"
describe DataFoodConsortium::Connector::Importer do
let(:connector) { DataFoodConsortium::Connector::Connector.instance }
let(:enterprise) do
DataFoodConsortium::Connector::Enterprise.new(
"https://example.net/foo-food-inc",
suppliedProducts: [product, second_product],
)
end
let(:catalog_item) do
DataFoodConsortium::Connector::CatalogItem.new(
"https://example.net/tomatoItem",
product:,
)
end
let(:product) do
DataFoodConsortium::Connector::SuppliedProduct.new(
"https://example.net/tomato",
name: "Tomato",
description: "Awesome tomato",
totalTheoreticalStock: 3,
productType: non_local_vegetable,
)
end
let(:product_data) do
<<~JSON
{
"@context":"http://static.datafoodconsortium.org/ontologies/context.json",
"@id":"https://example.net/tomato",
"@type":"dfc-b:SuppliedProduct",
"dfc-b:name":"Tomato",
"dfc-b:description":"Awesome tomato",
"dfc-b:alcoholPercentage":0.0,
"dfc-b:lifetime":"",
"dfc-b:usageOrStorageCondition":"",
"dfc-b:totalTheoreticalStock":3,
"dfc-b:hasType": "dfc-pt:non-local-vegetable"
}
JSON
end
let(:product_data_with_context_v1_8) do
<<~JSON
{
"@context": {
"dfc-b": "https://github.com/datafoodconsortium/ontology/releases/latest/download/DFC_BusinessOntology.owl#",
"dfc-m": "https://github.com/datafoodconsortium/taxonomies/releases/latest/download/measures.rdf#",
"dfc-pt": "https://github.com/datafoodconsortium/taxonomies/releases/latest/download/productTypes.rdf#",
"dfc-b:hasType":{"@type":"@id"}
},
"@id":"https://example.net/tomato",
"@type":"dfc-b:SuppliedProduct",
"dfc-b:name":"Tomato",
"dfc-b:description":"Awesome tomato",
"dfc-b:alcoholPercentage":0.0,
"dfc-b:lifetime":"",
"dfc-b:usageOrStorageCondition":"",
"dfc-b:totalTheoreticalStock":3,
"dfc-b:hasType": "dfc-pt:non-local-vegetable"
}
JSON
end
let(:second_product) do
DataFoodConsortium::Connector::SuppliedProduct.new(
"https://example.net/ocra",
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.PIECE
end
let(:non_local_vegetable) do
connector.PRODUCT_TYPES.VEGETABLE.NON_LOCAL_VEGETABLE
end
before { connector.loadProductTypes(read_file("productTypes")) }
it "imports a single object with simple properties" do
result = import(product)
expect(result).to be_a product.class
expect(result.semanticType).to eq product.semanticType
expect(result.semanticId).to eq "https://example.net/tomato"
expect(result.name).to eq "Tomato"
expect(result.description).to eq "Awesome tomato"
expect(result.productType).to eq non_local_vegetable
expect(result.totalTheoreticalStock).to eq 3
end
it "imports an object with referenced context" do
result = connector.import(product_data)
expect(result).to be_a DataFoodConsortium::Connector::SuppliedProduct
expect(result.semanticType).to eq "dfc-b:SuppliedProduct"
expect(result.semanticId).to eq "https://example.net/tomato"
expect(result.name).to eq "Tomato"
expect(result.description).to eq "Awesome tomato"
expect(result.productType).to eq non_local_vegetable
expect(result.totalTheoreticalStock).to eq 3
end
it "imports an object with DFC v1.8 context" do
result = connector.import(product_data_with_context_v1_8)
expect(result).to be_a DataFoodConsortium::Connector::SuppliedProduct
expect(result.semanticType).to eq "dfc-b:SuppliedProduct"
expect(result.semanticId).to eq "https://example.net/tomato"
expect(result.name).to eq "Tomato"
expect(result.description).to eq "Awesome tomato"
expect(result.productType).to eq non_local_vegetable
expect(result.totalTheoreticalStock).to eq 3
end
it "imports a graph with multiple objects" do
result = import(catalog_item, product)
expect(result).to be_a Array
expect(result.size).to eq 2
item, tomato = result
expect(item).to be_a catalog_item.class
expect(item.semanticType).to eq catalog_item.semanticType
expect(item.semanticId).to eq "https://example.net/tomatoItem"
expect(tomato.name).to eq "Tomato"
expect(tomato.description).to eq "Awesome tomato"
expect(tomato.productType).to eq non_local_vegetable
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(tomato.quantity).to eq items
expect(tomato.productType).to eq non_local_vegetable
expect(items.value).to eq 5
expect(items.unit).to eq piece
end
it "imports properties with lists" do
result = import(enterprise, product, second_product)
expect(result.size).to eq 3
enterprise, tomato, ocra = result
expect(enterprise.suppliedProducts).to eq [tomato, ocra]
end
def import(*args)
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