2014-02-06 18:49:05 +00:00
|
|
|
import datetime
|
2014-02-06 09:10:04 +00:00
|
|
|
import redash.models
|
2014-03-19 11:48:11 +00:00
|
|
|
from redash.utils import gen_query_hash
|
2014-02-06 09:10:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
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 instance(self, **override_kwargs):
|
|
|
|
kwargs = self._get_kwargs(override_kwargs)
|
|
|
|
|
|
|
|
return self.model(**kwargs)
|
|
|
|
|
|
|
|
def create(self, **override_kwargs):
|
|
|
|
kwargs = self._get_kwargs(override_kwargs)
|
|
|
|
return self.model.create(**kwargs)
|
|
|
|
|
2014-02-22 12:42:08 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
|
2014-05-08 18:29:50 +00:00
|
|
|
|
2014-02-22 12:42:08 +00:00
|
|
|
user_factory = ModelFactory(redash.models.User,
|
|
|
|
name='John Doe', email=Sequence('test{}@example.com'),
|
2014-05-09 17:12:34 +00:00
|
|
|
groups=['default'])
|
2014-02-22 12:42:08 +00:00
|
|
|
|
|
|
|
|
2014-03-19 10:53:39 +00:00
|
|
|
data_source_factory = ModelFactory(redash.models.DataSource,
|
|
|
|
name='Test',
|
|
|
|
type='pg',
|
|
|
|
options='')
|
|
|
|
|
|
|
|
|
2014-02-06 09:10:04 +00:00
|
|
|
dashboard_factory = ModelFactory(redash.models.Dashboard,
|
2014-02-22 12:42:08 +00:00
|
|
|
name='test', user=user_factory.create, layout='[]')
|
|
|
|
|
2014-02-06 09:10:04 +00:00
|
|
|
|
|
|
|
query_factory = ModelFactory(redash.models.Query,
|
|
|
|
name='New Query',
|
|
|
|
description='',
|
|
|
|
query='SELECT 1',
|
|
|
|
ttl=-1,
|
2014-03-19 10:53:39 +00:00
|
|
|
user=user_factory.create,
|
|
|
|
data_source=data_source_factory.create)
|
2014-02-06 09:10:04 +00:00
|
|
|
|
2014-02-06 18:49:05 +00:00
|
|
|
query_result_factory = ModelFactory(redash.models.QueryResult,
|
2014-02-09 16:50:28 +00:00
|
|
|
data='{"columns":{}, "rows":[]}',
|
2014-02-06 18:49:05 +00:00
|
|
|
runtime=1,
|
2014-03-20 18:57:35 +00:00
|
|
|
retrieved_at=datetime.datetime.utcnow,
|
2014-03-19 11:48:11 +00:00
|
|
|
query="SELECT 1",
|
|
|
|
query_hash=gen_query_hash('SELECT 1'),
|
|
|
|
data_source=data_source_factory.create)
|
2014-02-06 18:49:05 +00:00
|
|
|
|
2014-02-06 09:10:04 +00:00
|
|
|
visualization_factory = ModelFactory(redash.models.Visualization,
|
|
|
|
type='CHART',
|
|
|
|
query=query_factory.create,
|
|
|
|
name='Chart',
|
|
|
|
description='',
|
|
|
|
options='{}')
|
|
|
|
|
|
|
|
widget_factory = ModelFactory(redash.models.Widget,
|
|
|
|
type='chart',
|
|
|
|
width=1,
|
|
|
|
options='{}',
|
|
|
|
dashboard=dashboard_factory.create,
|
2014-03-19 10:53:39 +00:00
|
|
|
visualization=visualization_factory.create)
|