forked from feast-dev/feast
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest_feature_set.py
More file actions
169 lines (159 loc) · 5.48 KB
/
test_feature_set.py
File metadata and controls
169 lines (159 loc) · 5.48 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
# Copyright 2019 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.
from datetime import datetime
import pytz
from feast.entity import Entity
from feast.feature_set import FeatureSet, Feature
from feast.value_type import ValueType
from feast.client import Client
import pandas as pd
import pytest
from concurrent import futures
import grpc
from feast_core_server import CoreServicer
import feast.core.CoreService_pb2_grpc as Core
import dataframes
CORE_URL = "core.feast.local"
SERVING_URL = "serving.feast.local"
class TestFeatureSet:
@pytest.fixture(scope="function")
def server(self):
server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
Core.add_CoreServiceServicer_to_server(CoreServicer(), server)
server.add_insecure_port("[::]:50051")
server.start()
yield server
server.stop(0)
@pytest.fixture
def client(self, server):
return Client(core_url="localhost:50051")
def test_add_remove_features_success(self):
fs = FeatureSet("my-feature-set")
fs.add(Feature(name="my-feature-1", dtype=ValueType.INT64))
fs.add(Feature(name="my-feature-2", dtype=ValueType.INT64))
fs.drop(name="my-feature-1")
assert len(fs.features) == 1 and fs.features[0].name == "my-feature-2"
def test_remove_feature_failure(self):
with pytest.raises(ValueError):
fs = FeatureSet("my-feature-set")
fs.drop(name="my-feature-1")
def test_update_from_source_failure(self):
with pytest.raises(Exception):
df = pd.DataFrame()
fs = FeatureSet("driver-feature-set")
fs.infer_fields_from_df(df)
@pytest.mark.parametrize(
"dataframe,feature_count,entity_count,discard_unused_fields,features,entities",
[
(
dataframes.GOOD,
3,
1,
True,
[],
[Entity(name="entity_id", dtype=ValueType.INT64)],
),
(
dataframes.GOOD_FIVE_FEATURES,
5,
1,
True,
[],
[Entity(name="entity_id", dtype=ValueType.INT64)],
),
(
dataframes.GOOD_FIVE_FEATURES,
6,
1,
True,
[Feature(name="feature_6", dtype=ValueType.INT64)],
[Entity(name="entity_id", dtype=ValueType.INT64)],
),
(
dataframes.GOOD_FIVE_FEATURES_TWO_ENTITIES,
5,
2,
True,
[],
[
Entity(name="entity_1_id", dtype=ValueType.INT64),
Entity(name="entity_2_id", dtype=ValueType.INT64),
],
),
(
dataframes.GOOD_FIVE_FEATURES_TWO_ENTITIES,
6,
3,
False,
[],
[
Entity(name="entity_1_id", dtype=ValueType.INT64),
Entity(name="entity_2_id", dtype=ValueType.INT64),
],
),
(
dataframes.NO_FEATURES,
0,
1,
True,
[],
[Entity(name="entity_id", dtype=ValueType.INT64)],
),
(
pd.DataFrame(
{
"datetime": [
datetime.utcnow().replace(tzinfo=pytz.utc) for _ in range(3)
]
}
),
0,
0,
True,
[],
[],
),
],
ids=[
"Test small dataframe update with hardcoded entity",
"Test larger dataframe update with hardcoded entity",
"Test larger dataframe update with hardcoded entity and feature",
"Test larger dataframe update with two hardcoded entities and discarding of existing fields",
"Test larger dataframe update with two hardcoded entities and retention of existing fields",
"Test dataframe with no featuresdataframe",
"Test empty dataframe",
],
)
def test_add_features_from_df_success(
self,
dataframe,
feature_count,
entity_count,
discard_unused_fields,
features,
entities,
):
my_feature_set = FeatureSet(
name="my_feature_set",
features=[Feature(name="dummy_f1", dtype=ValueType.INT64)],
entities=[Entity(name="dummy_entity_1", dtype=ValueType.INT64)],
)
my_feature_set.infer_fields_from_df(
dataframe,
discard_unused_fields=discard_unused_fields,
features=features,
entities=entities,
)
assert len(my_feature_set.features) == feature_count
assert len(my_feature_set.entities) == entity_count