forked from feast-dev/feast
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_importer.py
More file actions
255 lines (229 loc) · 11.9 KB
/
test_importer.py
File metadata and controls
255 lines (229 loc) · 11.9 KB
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# Copyright 2018 The Feast Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import pandas as pd
import pytest
import ntpath
from feast.sdk.resources.feature import Feature, Granularity, ValueType, \
Datastore
from feast.sdk.importer import _create_feature, Importer
from feast.sdk.utils.gs_utils import is_gs_path
from feast.types.Granularity_pb2 import Granularity as Granularity_pb2
class TestImporter(object):
def test_from_csv(self):
csv_path = "tests/data/driver_features.csv"
entity_name = "driver"
feature_granularity = Granularity.DAY
staging_location = "gs://test-bucket"
id_column = "driver_id"
feature_columns = ["avg_distance_completed",
"avg_customer_distance_completed"]
timestamp_column = "ts"
importer = Importer.from_csv(path=csv_path,
entity=entity_name,
granularity=feature_granularity,
owner=owner,
staging_location=staging_location,
id_column=id_column,
feature_columns=feature_columns,
timestamp_column=timestamp_column)
self._validate_csv_importer(importer, csv_path, entity_name,
feature_granularity, owner,
staging_location, id_column,
feature_columns, timestamp_column)
def test_from_csv_id_column_not_specified(self):
with pytest.raises(ValueError,
match="Column with name driver is not found"):
feature_columns = ["avg_distance_completed",
"avg_customer_distance_completed"]
csv_path = "tests/data/driver_features.csv"
Importer.from_csv(path=csv_path,
entity="driver",
granularity=Granularity.DAY,
staging_location="gs://test-bucket",
feature_columns=feature_columns,
timestamp_column="ts")
def test_from_csv_timestamp_column_not_specified(self):
feature_columns = ["avg_distance_completed",
"avg_customer_distance_completed",
"avg_distance_cancelled"]
csv_path = "tests/data/driver_features.csv"
entity_name = "driver"
granularity = Granularity.DAY
staging_location = "gs://test-bucket"
id_column = "driver_id"
importer = Importer.from_csv(path=csv_path,
entity=entity_name,
granularity=granularity,
owner=owner,
staging_location=staging_location,
id_column=id_column,
feature_columns=feature_columns)
self._validate_csv_importer(importer, csv_path, entity_name,
granularity, owner,
staging_location=staging_location,
id_column=id_column,
feature_columns=feature_columns)
def test_from_csv_feature_columns_not_specified(self):
csv_path = "tests/data/driver_features.csv"
entity_name = "driver"
granularity = Granularity.DAY
staging_location = "gs://test-bucket"
id_column = "driver_id"
timestamp_column = "ts"
importer = Importer.from_csv(path=csv_path,
entity=entity_name,
granularity=granularity,
owner=owner,
staging_location=staging_location,
id_column=id_column,
timestamp_column=timestamp_column)
self._validate_csv_importer(importer, csv_path, entity_name,
granularity, owner,
staging_location=staging_location,
id_column=id_column,
timestamp_column=timestamp_column)
def test_from_csv_staging_location_not_specified(self):
with pytest.raises(ValueError,
match="Specify staging_location for importing local file/dataframe"):
feature_columns = ["avg_distance_completed",
"avg_customer_distance_completed"]
csv_path = "tests/data/driver_features.csv"
Importer.from_csv(path=csv_path,
entity="driver",
granularity=Granularity.DAY,
feature_columns=feature_columns,
timestamp_column="ts")
with pytest.raises(ValueError,
match="Staging location must be in GCS") as e_info:
feature_columns = ["avg_distance_completed",
"avg_customer_distance_completed"]
csv_path = "tests/data/driver_features.csv"
Importer.from_csv(path=csv_path,
entity="driver",
granularity=Granularity.DAY,
staging_location="/home",
feature_columns=feature_columns,
timestamp_column="ts")
def test_from_df(self):
csv_path = "tests/data/driver_features.csv"
df = pd.read_csv(csv_path)
staging_location = "gs://test-bucket"
entity = "driver"
importer = Importer.from_df(df=df,
entity=entity,
granularity=Granularity.DAY,
staging_location=staging_location,
id_column="driver_id",
timestamp_column="ts")
assert importer.require_staging == True
assert ("{}/tmp_{}".format(staging_location, entity)
in importer.remote_path)
for feature in importer.features.values():
assert feature.name in df.columns
assert feature.id == "driver.day." + feature.name
import_spec = importer.spec
assert import_spec.type == "file"
assert import_spec.sourceOptions == {"format": "csv",
"path": importer.remote_path}
assert import_spec.entities == ["driver"]
schema = import_spec.schema
assert schema.entityIdColumn == "driver_id"
assert schema.timestampValue is not None
feature_columns = ["completed", "avg_distance_completed",
"avg_customer_distance_completed",
"avg_distance_cancelled"]
for col, field in zip(df.columns.values, schema.fields):
assert col == field.name
if col in feature_columns:
assert field.featureId == "driver.day." + col
def _validate_csv_importer(self,
importer, csv_path, entity_name,
feature_granularity, owner,
staging_location=None, id_column=None,
feature_columns=None,
timestamp_column=None, timestamp_value=None):
df = pd.read_csv(csv_path)
assert not importer.require_staging == is_gs_path(csv_path)
if importer.require_staging:
assert importer.remote_path == "{}/{}".format(staging_location,
ntpath.basename(
csv_path))
# check features created
for feature in importer.features.values():
assert feature.name in df.columns
assert feature.id == "{}.{}.{}".format(entity_name,
Granularity_pb2.Enum.Name(
feature_granularity.value).lower(),
feature.name)
import_spec = importer.spec
assert import_spec.type == "file.csv"
path = importer.remote_path if importer.require_staging else csv_path
assert import_spec.sourceOptions == {"path": path}
assert import_spec.entities == [entity_name]
schema = import_spec.schema
assert schema.entityIdColumn == id_column if id_column is not None else entity_name
if timestamp_column is not None:
assert schema.timestampColumn == timestamp_column
elif timestamp_value is not None:
assert schema.timestampValue == timestamp_value
if feature_columns is None:
feature_columns = list(df.columns.values)
feature_columns.remove(id_column)
feature_columns.remove(timestamp_column)
# check schema's field
for col, field in zip(df.columns.values, schema.fields):
assert col == field.name
if col in feature_columns:
assert field.featureId == \
"{}.{}.{}".format(entity_name,
Granularity_pb2.Enum.Name(
feature_granularity.value).lower(),
col)
class TestHelpers:
def test_create_feature(self):
col = pd.Series([1] * 3, dtype='int32', name="test")
expected = Feature(name="test",
entity="test",
granularity=Granularity.NONE,
owner="person",
value_type=ValueType.INT32)
actual = _create_feature(col, "test", Granularity.NONE, "person", None,
None)
assert actual.id == expected.id
assert actual.value_type == expected.value_type
assert actual.owner == expected.owner
def test_create_feature_with_stores(self):
col = pd.Series([1] * 3, dtype='int32', name="test")
expected = Feature(name="test",
entity="test",
granularity=Granularity.NONE,
owner="person",
value_type=ValueType.INT32,
serving_store=Datastore(id="SERVING"),
warehouse_store=Datastore(id="WAREHOUSE"))
actual = _create_feature(col, "test", Granularity.NONE, "person",
Datastore(id="SERVING"),
Datastore(id="WAREHOUSE"))
assert actual.id == expected.id
assert actual.value_type == expected.value_type
assert actual.owner == expected.owner
assert actual.serving_store == expected.serving_store
assert actual.warehouse_store == expected.warehouse_store