forked from getredash/redash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfactories.py
333 lines (267 loc) · 9.11 KB
/
factories.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
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
from passlib.apps import custom_app_context as pwd_context
import redash.models
from redash.models import db
from redash.permissions import ACCESS_TYPE_MODIFY
from redash.utils import gen_query_hash, utcnow
from redash.utils.configuration import ConfigurationContainer
class ModelFactory(object):
def __init__(self, model, **kwargs):
self.model = model
self.kwargs = kwargs
def _get_kwargs(self, override_kwargs):
kwargs = self.kwargs.copy()
kwargs.update(override_kwargs)
for key, arg in kwargs.items():
if callable(arg):
kwargs[key] = arg()
return kwargs
def create(self, **override_kwargs):
kwargs = self._get_kwargs(override_kwargs)
obj = self.model(**kwargs)
db.session.add(obj)
db.session.commit()
return obj
class Sequence(object):
def __init__(self, string):
self.sequence = 0
self.string = string
def __call__(self):
self.sequence += 1
return self.string.format(self.sequence)
user_factory = ModelFactory(
redash.models.User,
name="John Doe",
email=Sequence("test{}@example.com"),
password_hash=pwd_context.encrypt("test1234"),
group_ids=[2],
org_id=1,
)
org_factory = ModelFactory(
redash.models.Organization,
name=Sequence("Org {}"),
slug=Sequence("org{}.example.com"),
settings={},
)
data_source_factory = ModelFactory(
redash.models.DataSource,
name=Sequence("Test {}"),
type="pg",
# If we don't use lambda here it will reuse the same options between tests:
options=lambda: ConfigurationContainer.from_json('{"dbname": "test"}'),
org_id=1,
)
dashboard_factory = ModelFactory(
redash.models.Dashboard,
name="test",
user=user_factory.create,
layout="[]",
is_draft=False,
org=1,
)
api_key_factory = ModelFactory(redash.models.ApiKey, object=dashboard_factory.create)
query_factory = ModelFactory(
redash.models.Query,
name="Query",
description="",
query_text="SELECT 1",
user=user_factory.create,
is_archived=False,
is_draft=False,
schedule=None,
data_source=data_source_factory.create,
org_id=1,
)
query_with_params_factory = ModelFactory(
redash.models.Query,
name="New Query with Params",
description="",
query_text="SELECT {{param1}}",
user=user_factory.create,
is_archived=False,
is_draft=False,
schedule={},
data_source=data_source_factory.create,
org_id=1,
)
access_permission_factory = ModelFactory(
redash.models.AccessPermission,
object_id=query_factory.create,
object_type=redash.models.Query.__name__,
access_type=ACCESS_TYPE_MODIFY,
grantor=user_factory.create,
grantee=user_factory.create,
)
alert_factory = ModelFactory(
redash.models.Alert,
name=Sequence("Alert {}"),
query_rel=query_factory.create,
user=user_factory.create,
options={},
)
query_result_factory = ModelFactory(
redash.models.QueryResult,
data='{"columns":{}, "rows":[]}',
runtime=1,
retrieved_at=utcnow,
query_text="SELECT 1",
query_hash=gen_query_hash("SELECT 1"),
data_source=data_source_factory.create,
org_id=1,
)
visualization_factory = ModelFactory(
redash.models.Visualization,
type="CHART",
query_rel=query_factory.create,
name="Chart",
description="",
options="{}",
)
widget_factory = ModelFactory(
redash.models.Widget,
width=1,
options="{}",
dashboard=dashboard_factory.create,
visualization=visualization_factory.create,
)
destination_factory = ModelFactory(
redash.models.NotificationDestination,
org_id=1,
user=user_factory.create,
name=Sequence("Destination {}"),
type="slack",
options=lambda: ConfigurationContainer.from_json('{"url": "https://www.slack.com"}'),
)
alert_subscription_factory = ModelFactory(
redash.models.AlertSubscription,
user=user_factory.create,
destination=destination_factory.create,
alert=alert_factory.create,
)
query_snippet_factory = ModelFactory(
redash.models.QuerySnippet,
trigger=Sequence("trigger {}"),
description="description",
snippet="snippet",
)
class Factory(object):
def __init__(self):
self.org, self.admin_group, self.default_group = redash.models.init_db()
self._data_source = None
self._user = None
@property
def user(self):
if self._user is None:
self._user = self.create_user()
# Test setup creates users, they need to be in the db by the time
# the handler's db transaction starts.
db.session.commit()
return self._user
@property
def data_source(self):
if self._data_source is None:
self._data_source = data_source_factory.create(org=self.org)
db.session.add(redash.models.DataSourceGroup(group=self.default_group, data_source=self._data_source))
return self._data_source
def create_org(self, **kwargs):
org = org_factory.create(**kwargs)
self.create_group(org=org, type=redash.models.Group.BUILTIN_GROUP, name="default")
self.create_group(
org=org,
type=redash.models.Group.BUILTIN_GROUP,
name="admin",
permissions=["admin"],
)
return org
def create_user(self, **kwargs):
args = {"org": self.org, "group_ids": [self.default_group.id]}
if "org" in kwargs:
args["group_ids"] = [kwargs["org"].default_group.id]
args.update(kwargs)
return user_factory.create(**args)
def create_admin(self, **kwargs):
args = {
"org": self.org,
"group_ids": [self.admin_group.id, self.default_group.id],
}
if "org" in kwargs:
args["group_ids"] = [
kwargs["org"].default_group.id,
kwargs["org"].admin_group.id,
]
args.update(kwargs)
return user_factory.create(**args)
def create_group(self, **kwargs):
args = {"name": "Group", "org": self.org}
args.update(kwargs)
g = redash.models.Group(**args)
return g
def create_alert(self, **kwargs):
args = {"user": self.user, "query_rel": self.create_query()}
args.update(**kwargs)
return alert_factory.create(**args)
def create_alert_subscription(self, **kwargs):
args = {"user": self.user, "alert": self.create_alert()}
args.update(**kwargs)
return alert_subscription_factory.create(**args)
def create_data_source(self, **kwargs):
group = None
if "group" in kwargs:
group = kwargs.pop("group")
args = {"org": self.org}
args.update(kwargs)
if group and "org" not in kwargs:
args["org"] = group.org
view_only = args.pop("view_only", False)
data_source = data_source_factory.create(**args)
if group:
db.session.add(redash.models.DataSourceGroup(group=group, data_source=data_source, view_only=view_only))
return data_source
def create_dashboard(self, **kwargs):
args = {"user": self.user, "org": self.org}
args.update(kwargs)
return dashboard_factory.create(**args)
def create_query(self, **kwargs):
args = {"user": self.user, "data_source": self.data_source, "org": self.org}
args.update(kwargs)
return query_factory.create(**args)
def create_query_with_params(self, **kwargs):
args = {"user": self.user, "data_source": self.data_source, "org": self.org}
args.update(kwargs)
return query_with_params_factory.create(**args)
def create_access_permission(self, **kwargs):
args = {"grantor": self.user}
args.update(kwargs)
return access_permission_factory.create(**args)
def create_query_result(self, **kwargs):
args = {"data_source": self.data_source}
args.update(kwargs)
if "data_source" in args and "org" not in args:
args["org"] = args["data_source"].org
return query_result_factory.create(**args)
def create_visualization(self, **kwargs):
args = {"query_rel": self.create_query()}
args.update(kwargs)
return visualization_factory.create(**args)
def create_visualization_with_params(self, **kwargs):
args = {"query_rel": self.create_query_with_params()}
args.update(kwargs)
return visualization_factory.create(**args)
def create_widget(self, **kwargs):
args = {
"dashboard": self.create_dashboard(),
"visualization": self.create_visualization(),
}
args.update(kwargs)
return widget_factory.create(**args)
def create_api_key(self, **kwargs):
args = {"org": self.org}
args.update(kwargs)
return api_key_factory.create(**args)
def create_destination(self, **kwargs):
args = {"org": self.org}
args.update(kwargs)
return destination_factory.create(**args)
def create_query_snippet(self, **kwargs):
args = {"user": self.user, "org": self.org}
args.update(kwargs)
return query_snippet_factory.create(**args)