forked from PrefectHQ/prefect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_variables.py
212 lines (162 loc) · 5.24 KB
/
test_variables.py
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
import pytest
from prefect import flow
from prefect.variables import Variable
@pytest.fixture
async def variable():
var = await Variable.set(name="my_variable", value="my-value", tags=["123", "456"])
return var
async def test_set_sync(variable):
@flow
def test_flow():
# confirm variable doesn't exist
variable_doesnt_exist = Variable.get("my_new_variable")
assert variable_doesnt_exist is None
# create new
created = Variable.set(
name="my_new_variable",
value="my_value",
tags=["123", "456"],
)
assert created.value == "my_value"
assert created.tags == ["123", "456"]
# try to overwrite
with pytest.raises(
ValueError,
match="Variable 'my_new_variable' already exists. Use `overwrite=True` to update it.",
):
Variable.set(name="my_new_variable", value="other_value")
# actually overwrite
updated = Variable.set(
name="my_new_variable",
value="other_value",
tags=["new", "tags"],
overwrite=True,
)
assert updated.value == "other_value"
assert updated.tags == ["new", "tags"]
test_flow()
async def test_set_async(variable):
# confirm variable doesn't exist
variable_doesnt_exist = await Variable.get("my_new_variable")
assert variable_doesnt_exist is None
# create new
created = await Variable.set(
name="my_new_variable", value="my_value", tags=["123", "456"]
)
assert created.value == "my_value"
assert created.tags == ["123", "456"]
# try to overwrite
with pytest.raises(
ValueError,
match="Variable 'my_new_variable' already exists. Use `overwrite=True` to update it.",
):
await Variable.set(name="my_new_variable", value="other_value")
# actually overwrite
updated = await Variable.set(
name="my_new_variable",
value="other_value",
tags=["new", "tags"],
overwrite=True,
)
assert updated.value == "other_value"
assert updated.tags == ["new", "tags"]
@pytest.mark.parametrize(
"value",
[
"string-value",
'"string-value"',
123,
12.3,
True,
False,
None,
{"key": "value"},
["value1", "value2"],
{"key": ["value1", "value2"]},
],
)
async def test_json_types(value):
set_value = await Variable.set("json_variable", value=value)
assert set_value.value == value
async def test_get(variable):
# get value
value = await Variable.get(variable.name)
assert value == variable.value
# get value of a variable that doesn't exist
doesnt_exist = await Variable.get("doesnt_exist")
assert doesnt_exist is None
# default is respected if it doesn't exist
doesnt_exist_default = await Variable.get("doesnt_exist", 42)
assert doesnt_exist_default == 42
async def test_get_async(variable):
# get value
value = await Variable.get(variable.name)
assert value == variable.value
# get value of a variable that doesn't exist
doesnt_exist = await Variable.get("doesnt_exist")
assert doesnt_exist is None
# default is respected if it doesn't exist
doesnt_exist_default = await Variable.get("doesnt_exist", 42)
assert doesnt_exist_default == 42
async def test_unset(variable):
# unset a variable
unset = await Variable.unset(variable.name)
assert unset is True
# confirm it's gone
doesnt_exist = await Variable.get(variable.name)
assert doesnt_exist is None
# unset a variable that doesn't exist
unset_doesnt_exist = await Variable.unset("doesnt_exist")
assert unset_doesnt_exist is False
async def test_unset_async(variable):
# unset a variable
unset = await Variable.unset(variable.name)
assert unset is True
# confirm it's gone
doesnt_exist = await Variable.get(variable.name)
assert doesnt_exist is None
# unset a variable that doesn't exist
unset_doesnt_exist = await Variable.unset("doesnt_exist")
assert unset_doesnt_exist is False
def test_get_in_sync_flow(variable):
@flow
def foo():
var = Variable.get("my_variable")
return var
value = foo()
assert value == variable.value
async def test_get_in_async_flow(variable):
@flow
async def foo():
var = await Variable.get("my_variable")
return var
value = await foo()
assert value == variable.value
def test_set_in_sync_flow():
@flow
def foo():
return Variable.set("my_variable", value="my-value")
res = foo()
assert res
assert res.value == "my-value"
async def test_set_in_async_flow():
@flow
async def foo():
return await Variable.set("my_variable", value="my-value")
res = await foo()
assert res
assert res.value == "my-value"
async def test_unset_in_sync_flow(variable):
@flow
def foo():
Variable.unset(variable.name)
foo()
value = await Variable.get(variable.name)
assert value is None
async def test_unset_in_async_flow(variable):
@flow
async def foo():
await Variable.unset(variable.name)
await foo()
value = await Variable.get(variable.name)
assert value is None