forked from feast-dev/feast
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_key_encoding_utils.py
More file actions
99 lines (80 loc) · 3.16 KB
/
Copy pathtest_key_encoding_utils.py
File metadata and controls
99 lines (80 loc) · 3.16 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
import pytest
from feast.infra.key_encoding_utils import (
_deserialize_value,
_serialize_val,
deserialize_entity_key,
serialize_entity_key,
)
from feast.protos.feast.types.EntityKey_pb2 import EntityKey as EntityKeyProto
from feast.protos.feast.types.Value_pb2 import Value as ValueProto
from feast.protos.feast.types.Value_pb2 import ValueType
def test_serialize_entity_key():
# Should be fine
serialize_entity_key(
EntityKeyProto(
join_keys=["user"], entity_values=[ValueProto(int64_val=int(2**15))]
),
entity_key_serialization_version=2,
)
# True int64, but should also be fine.
serialize_entity_key(
EntityKeyProto(
join_keys=["user"], entity_values=[ValueProto(int64_val=int(2**31))]
),
entity_key_serialization_version=2,
)
# Old serialization scheme, should fail.
with pytest.raises(BaseException):
serialize_entity_key(
EntityKeyProto(
join_keys=["user"], entity_values=[ValueProto(int64_val=int(2**31))]
),
)
def test_deserialize_entity_key():
serialized_entity_key = serialize_entity_key(
EntityKeyProto(
join_keys=["user"], entity_values=[ValueProto(int64_val=int(2**15))]
),
entity_key_serialization_version=3,
)
deserialized_entity_key = deserialize_entity_key(
serialized_entity_key, entity_key_serialization_version=3
)
assert deserialized_entity_key == EntityKeyProto(
join_keys=["user"], entity_values=[ValueProto(int64_val=int(2**15))]
)
def test_serialize_value():
v, t = _serialize_val("string_val", ValueProto(string_val="test"))
assert t == ValueType.STRING
assert v == b"test"
v, t = _serialize_val("bytes_val", ValueProto(bytes_val=b"test"))
assert t == ValueType.BYTES
assert v == b"test"
v, t = _serialize_val("int32_val", ValueProto(int32_val=1))
assert t == ValueType.INT32
assert v == b"\x01\x00\x00\x00"
# default entity_key_serialization_version is 1, so the result should be 4 bytes
v, t = _serialize_val("int64_val", ValueProto(int64_val=1))
assert t == ValueType.INT64
assert v == b"\x01\x00\x00\x00"
# current entity_key_serialization_version is 2, so the result should be 8 bytes
v, t = _serialize_val(
"int64_val", ValueProto(int64_val=1), entity_key_serialization_version=2
)
assert t == ValueType.INT64
assert v == b"\x01\x00\x00\x00\x00\x00\x00\x00"
# new entity_key_serialization_version is 3, the result should be same as version 2
v, t = _serialize_val(
"int64_val", ValueProto(int64_val=1), entity_key_serialization_version=3
)
assert t == ValueType.INT64
assert v == b"\x01\x00\x00\x00\x00\x00\x00\x00"
def test_deserialize_value():
v = _deserialize_value(ValueType.STRING, b"test")
assert v.string_val == "test"
v = _deserialize_value(ValueType.BYTES, b"test")
assert v.bytes_val == b"test"
v = _deserialize_value(ValueType.INT32, b"\x01\x00\x00\x00")
assert v.int32_val == 1
v = _deserialize_value(ValueType.INT64, b"\x01\x00\x00\x00\x00\x00\x00\x00")
assert v.int64_val == 1