-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathtest_data_sources.py
More file actions
308 lines (268 loc) · 10.3 KB
/
test_data_sources.py
File metadata and controls
308 lines (268 loc) · 10.3 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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
import pytest
from feast.data_format import ProtoFormat
from feast.data_source import (
DataSource,
KafkaSource,
KinesisSource,
PushSource,
RequestSource,
)
from feast.field import Field
from feast.infra.offline_stores.bigquery_source import BigQuerySource
from feast.infra.offline_stores.file_source import FileSource
from feast.infra.offline_stores.redshift_source import RedshiftSource
from feast.infra.offline_stores.snowflake_source import SnowflakeSource
from feast.types import Bool, Float32, Int64
def test_push_with_batch():
push_source = PushSource(
name="test",
batch_source=BigQuerySource(table="test.test"),
)
push_source_proto = push_source.to_proto()
assert push_source_proto.HasField("batch_source")
push_source_unproto = PushSource.from_proto(push_source_proto)
assert push_source.name == push_source_unproto.name
assert push_source.batch_source.name == push_source_unproto.batch_source.name
def test_push_source_without_batch_source():
# Create PushSource with no batch_source
push_source = PushSource(name="test_push_source")
# Convert to proto
push_source_proto = push_source.to_proto()
# Assert batch_source is not present in proto
assert not push_source_proto.HasField("batch_source")
# Deserialize and check again
push_source_unproto = PushSource.from_proto(push_source_proto)
assert push_source_unproto.batch_source is None
assert push_source_unproto.name == "test_push_source"
def test_request_source_primitive_type_to_proto():
schema = [
Field(name="f1", dtype=Float32),
Field(name="f2", dtype=Bool),
]
request_source = RequestSource(
name="source",
schema=schema,
description="desc",
tags={},
owner="feast",
)
request_proto = request_source.to_proto()
deserialized_request_source = RequestSource.from_proto(request_proto)
assert deserialized_request_source == request_source
def test_hash():
push_source_1 = PushSource(
name="test",
batch_source=BigQuerySource(table="test.test"),
)
push_source_2 = PushSource(
name="test",
batch_source=BigQuerySource(table="test.test"),
)
push_source_3 = PushSource(
name="test",
batch_source=BigQuerySource(table="test.test2"),
)
push_source_4 = PushSource(
name="test",
batch_source=BigQuerySource(table="test.test2"),
description="test",
)
s1 = {push_source_1, push_source_2}
assert len(s1) == 1
s2 = {push_source_1, push_source_3}
assert len(s2) == 2
s3 = {push_source_3, push_source_4}
assert len(s3) == 2
s4 = {push_source_1, push_source_2, push_source_3, push_source_4}
assert len(s4) == 3
def test_proto_conversion():
bigquery_source = BigQuerySource(
name="test_source",
table="test_table",
timestamp_field="event_timestamp",
created_timestamp_column="created_timestamp",
field_mapping={"foo": "bar"},
description="test description",
tags={"test": "test"},
owner="test@gmail.com",
)
file_source = FileSource(
name="test_source",
path="test_path",
timestamp_field="event_timestamp",
created_timestamp_column="created_timestamp",
field_mapping={"foo": "bar"},
description="test description",
tags={"test": "test"},
owner="test@gmail.com",
)
redshift_source = RedshiftSource(
name="test_source",
database="test_database",
schema="test_schema",
table="test_table",
timestamp_field="event_timestamp",
created_timestamp_column="created_timestamp",
field_mapping={"foo": "bar"},
description="test description",
tags={"test": "test"},
owner="test@gmail.com",
)
snowflake_source = SnowflakeSource(
name="test_source",
database="test_database",
schema="test_schema",
table="test_table",
timestamp_field="event_timestamp",
created_timestamp_column="created_timestamp",
field_mapping={"foo": "bar"},
description="test description",
tags={"test": "test"},
owner="test@gmail.com",
)
kafka_source = KafkaSource(
name="test_source",
kafka_bootstrap_servers="test_servers",
message_format=ProtoFormat("class_path"),
topic="test_topic",
timestamp_field="event_timestamp",
created_timestamp_column="created_timestamp",
field_mapping={"foo": "bar"},
description="test description",
tags={"test": "test"},
owner="test@gmail.com",
batch_source=file_source,
)
kinesis_source = KinesisSource(
name="test_source",
region="test_region",
record_format=ProtoFormat("class_path"),
stream_name="test_stream",
timestamp_field="event_timestamp",
created_timestamp_column="created_timestamp",
field_mapping={"foo": "bar"},
description="test description",
tags={"test": "test"},
owner="test@gmail.com",
batch_source=file_source,
)
push_source = PushSource(
name="test_source",
batch_source=file_source,
description="test description",
tags={"test": "test"},
owner="test@gmail.com",
)
request_source = RequestSource(
name="test_source",
schema=[Field(name="test1", dtype=Float32), Field(name="test1", dtype=Int64)],
description="test description",
tags={"test": "test"},
owner="test@gmail.com",
)
assert DataSource.from_proto(bigquery_source.to_proto()) == bigquery_source
assert DataSource.from_proto(file_source.to_proto()) == file_source
assert DataSource.from_proto(redshift_source.to_proto()) == redshift_source
assert DataSource.from_proto(snowflake_source.to_proto()) == snowflake_source
assert DataSource.from_proto(kafka_source.to_proto()) == kafka_source
assert DataSource.from_proto(kinesis_source.to_proto()) == kinesis_source
assert DataSource.from_proto(push_source.to_proto()) == push_source
assert DataSource.from_proto(request_source.to_proto()) == request_source
# Test that timestamps are properly included in protobuf conversion
# Test FileSource timestamps
file_proto = file_source.to_proto()
assert file_proto.HasField("meta")
assert file_proto.meta.HasField("created_timestamp")
assert file_proto.meta.HasField("last_updated_timestamp")
assert file_source.created_timestamp is not None
assert file_source.last_updated_timestamp is not None
# Test BigQuerySource timestamps
bigquery_proto = bigquery_source.to_proto()
assert bigquery_proto.HasField("meta")
assert bigquery_proto.meta.HasField("created_timestamp")
assert bigquery_proto.meta.HasField("last_updated_timestamp")
assert bigquery_source.created_timestamp is not None
assert bigquery_source.last_updated_timestamp is not None
# Test RedshiftSource timestamps
redshift_proto = redshift_source.to_proto()
assert redshift_proto.HasField("meta")
assert redshift_proto.meta.HasField("created_timestamp")
assert redshift_proto.meta.HasField("last_updated_timestamp")
assert redshift_source.created_timestamp is not None
assert redshift_source.last_updated_timestamp is not None
# Test KafkaSource timestamps
kafka_proto = kafka_source.to_proto()
assert kafka_proto.HasField("meta")
assert kafka_proto.meta.HasField("created_timestamp")
assert kafka_proto.meta.HasField("last_updated_timestamp")
assert kafka_source.created_timestamp is not None
assert kafka_source.last_updated_timestamp is not None
# Test KinesisSource timestamps
kinesis_proto = kinesis_source.to_proto()
assert kinesis_proto.HasField("meta")
assert kinesis_proto.meta.HasField("created_timestamp")
assert kinesis_proto.meta.HasField("last_updated_timestamp")
assert kinesis_source.created_timestamp is not None
assert kinesis_source.last_updated_timestamp is not None
# Test PushSource timestamps
push_proto = push_source.to_proto()
assert push_proto.HasField("meta")
assert push_proto.meta.HasField("created_timestamp")
assert push_proto.meta.HasField("last_updated_timestamp")
assert push_source.created_timestamp is not None
assert push_source.last_updated_timestamp is not None
# Test RequestSource timestamps
request_proto = request_source.to_proto()
assert request_proto.HasField("meta")
assert request_proto.meta.HasField("created_timestamp")
assert request_proto.meta.HasField("last_updated_timestamp")
assert request_source.created_timestamp is not None
assert request_source.last_updated_timestamp is not None
def test_column_conflict():
with pytest.raises(ValueError):
_ = FileSource(
name="test_source",
path="test_path",
timestamp_field="event_timestamp",
created_timestamp_column="event_timestamp",
)
@pytest.mark.parametrize(
"source_kwargs,expected_name",
[
(
{
"database": "test_database",
"schema": "test_schema",
"table": "test_table",
},
"test_database.test_schema.test_table",
),
(
{"database": "test_database", "table": "test_table"},
"test_database.public.test_table",
),
({"table": "test_table"}, "public.test_table"),
({"database": "test_database", "table": "b.c"}, "test_database.b.c"),
({"database": "test_database", "table": "a.b.c"}, "a.b.c"),
(
{
"database": "test_database",
"schema": "test_schema",
"query": "select * from abc",
},
"",
),
],
)
def test_redshift_fully_qualified_table_name(source_kwargs, expected_name):
redshift_source = RedshiftSource(
name="test_source",
timestamp_field="event_timestamp",
created_timestamp_column="created_timestamp",
field_mapping={"foo": "bar"},
description="test description",
tags={"test": "test"},
owner="test@gmail.com",
**source_kwargs,
)
assert redshift_source.redshift_options.fully_qualified_table_name == expected_name