Skip to content

Commit

Permalink
tests coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
dpgaspar committed May 26, 2014
1 parent cbc3086 commit 675a411
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 26 deletions.
11 changes: 9 additions & 2 deletions flask_appbuilder/filemanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@
from werkzeug import secure_filename
from werkzeug.datastructures import FileStorage

try:
from flask import _app_ctx_stack
except ImportError:
_app_ctx_stack = None

app_stack = _app_ctx_stack or _request_ctx_stack

log = logging.getLogger(__name__)

Expand All @@ -27,7 +33,7 @@ def __init__(self, base_path=None,
permission=0o666, **kwargs):


ctx = _request_ctx_stack.top
ctx = app_stack.top

if 'UPLOAD_FOLDER' in ctx.app.config and not base_path:
base_path = ctx.app.config['UPLOAD_FOLDER']
Expand Down Expand Up @@ -85,7 +91,7 @@ def __init__(self, base_path=None,
if Image is None:
raise Exception('PIL library was not found')

ctx = _request_ctx_stack.top
ctx = app_stack.top
if 'IMG_SIZE' in ctx.app.config and not max_size:
max_size = ctx.app.config['IMG_SIZE']
self.max_size = max_size or (300, 200, True)
Expand Down Expand Up @@ -198,6 +204,7 @@ def get_save_format(self, filename, image):
def uuid_namegen(file_data):
return str(uuid.uuid1()) + '_sep_' + file_data.filename


def get_file_original_name(name):
"""
Use this function to get the user's original filename.
Expand Down
75 changes: 51 additions & 24 deletions flask_appbuilder/tests/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from flask.ext.appbuilder import Model, SQLA
from flask_appbuilder.models.filters import FilterStartsWith, FilterEqual
from flask_appbuilder.models.mixins import FileColumn, ImageColumn
from flask_appbuilder.views import MasterDetailView
from flask_appbuilder.charts.views import ChartView, TimeChartView, DirectChartView

import logging
Expand All @@ -25,7 +26,6 @@
DEFAULT_ADMIN_USER = 'admin'
DEFAULT_ADMIN_PASSWORD = 'general'


log = logging.getLogger(__name__)


Expand Down Expand Up @@ -55,7 +55,6 @@ def __repr__(self):


class FlaskTestCase(unittest.TestCase):

def setUp(self):
from flask import Flask
from flask.ext.appbuilder import AppBuilder
Expand All @@ -72,17 +71,22 @@ def setUp(self):
self.db = SQLA(self.app)
self.appbuilder = AppBuilder(self.app, self.db.session)

class Model1View(ModelView):
datamodel = SQLAModel(Model1)

class Model2View(ModelView):
datamodel = SQLAModel(Model2)
related_views = [Model1View]

class Model1View(ModelView):
datamodel = SQLAModel(Model1)
related_views = [Model2View]
list_columns = ['field_string','field_file']

class Model1Filtered1View(ModelView):
datamodel = SQLAModel(Model1)
base_filters = [['field_string', FilterStartsWith, 'a']]

class Model1MasterView(MasterDetailView):
datamodel = SQLAModel(Model1)
related_views = [Model2View]

class Model1Filtered2View(ModelView):
datamodel = SQLAModel(Model1)
base_filters = [['field_integer', FilterEqual, 0]]
Expand All @@ -102,10 +106,20 @@ class Model2DirectChartView(DirectChartView):
chart_title = 'Test Model1 Chart'
direct_columns = {'stat1': ('group', 'field_integer')}

class Model1MasterView(MasterDetailView):
datamodel = SQLAModel(Model1)
related_views = [Model2View]

class Model1MasterChartView(MasterDetailView):
datamodel = SQLAModel(Model1)
related_views = [Model2ChartView]


self.appbuilder.add_view(Model1View, "Model1")
self.appbuilder.add_view(Model1Filtered1View, "Model1Filtered1")
self.appbuilder.add_view(Model1Filtered2View, "Model1Filtered2")
self.appbuilder.add_view(Model1View, "Model1", category='Model1')
self.appbuilder.add_view(Model1MasterView, "Model1Master", category='Model1')
self.appbuilder.add_view(Model1MasterChartView, "Model1MasterChart", category='Model1')
self.appbuilder.add_view(Model1Filtered1View, "Model1Filtered1", category='Model1')
self.appbuilder.add_view(Model1Filtered2View, "Model1Filtered2", category='Model1')

self.appbuilder.add_view(Model2View, "Model2")
self.appbuilder.add_view(Model2View, "Model2 Add", href='/model2view/add')
Expand All @@ -125,6 +139,7 @@ def tearDown(self):
TEST HELPER FUNCTIONS
---------------------------------
"""

def login(self, client, username, password):
# Login with default admin
return client.post('/login/', data=dict(
Expand All @@ -136,7 +151,7 @@ def logout(self, client):
return client.get('/logout/')

def insert_data(self):
for x,i in zip(string.ascii_letters[:23], range(23)):
for x, i in zip(string.ascii_letters[:23], range(23)):
model = Model1(field_string="%stest" % (x), field_integer=i)
self.db.session.add(model)
self.db.session.commit()
Expand All @@ -149,10 +164,10 @@ def insert_data2(self):
try:
self.db.session.add(model1)
self.db.session.commit()
for x,i in zip(string.ascii_letters[:10], range(10)):
for x, i in zip(string.ascii_letters[:10], range(10)):
model = Model2(field_string="%stest" % (x),
field_integer=random.randint(1, 10),
group = model1)
field_integer=random.randint(1, 10),
group=model1)
year = random.choice(range(1900, 2012))
month = random.choice(range(1, 12))
day = random.choice(range(1, 28))
Expand All @@ -164,12 +179,11 @@ def insert_data2(self):
self.db.session.rollback()



def test_fab_views(self):
"""
Test views creation and registration
"""
eq_(len(self.appbuilder.baseviews), 18) # current minimal views are 11
eq_(len(self.appbuilder.baseviews), 20) # current minimal views are 11


def test_model_creation(self):
Expand Down Expand Up @@ -260,17 +274,14 @@ def test_model_crud(self):
client = self.app.test_client()
rv = self.login(client, DEFAULT_ADMIN_USER, DEFAULT_ADMIN_PASSWORD)

#with open('test.txt', 'rb') as fp:
rv = client.post('/model1view/add',
data=dict(field_string='test1', field_integer='1',
field_float='0.12',
field_date='2014-01-01',
field_file=(StringIO('file test'), 'test_base.py'),
field_image='groups.png'), follow_redirects=True)
data=dict(field_string='test1', field_integer='1',
field_float='0.12',
field_date='2014-01-01'), follow_redirects=True)
eq_(rv.status_code, 200)

model = self.db.session.query(Model1).first()
print(rv.data.decode('utf-8'))
print model.field_string, model.field_float, model.field_date, model.field_file, model.field_image
eq_(model.field_string, u'test1')
eq_(model.field_integer, 1)

Expand Down Expand Up @@ -324,9 +335,9 @@ def test_model_edit_validation(self):
self.login(client, 'admin', 'general')

client.post('/model1view/add',
data=dict(field_string='test1', field_integer='1'), follow_redirects=True)
data=dict(field_string='test1', field_integer='1'), follow_redirects=True)
client.post('/model1view/add',
data=dict(field_string='test2', field_integer='1'), follow_redirects=True)
data=dict(field_string='test2', field_integer='1'), follow_redirects=True)
rv = client.post('/model1view/edit/1',
data=dict(field_string='test2', field_integer='2'), follow_redirects=True)
eq_(rv.status_code, 200)
Expand Down Expand Up @@ -375,3 +386,19 @@ def test_charts_view(self):
rv = client.get('/model2directchartview/chart/')
eq_(rv.status_code, 200)

def test_master_detail_view(self):
"""
Test Master detail view
"""
client = self.app.test_client()
self.login(client, DEFAULT_ADMIN_USER, DEFAULT_ADMIN_PASSWORD)
self.insert_data2()
rv = client.get('/model1masterview/list/')
eq_(rv.status_code, 200)
rv = client.get('/model1masterview/list/1')
eq_(rv.status_code, 200)

rv = client.get('/model1masterchartview/list/')
eq_(rv.status_code, 200)
rv = client.get('/model1masterchartview/list/1')
eq_(rv.status_code, 200)

0 comments on commit 675a411

Please sign in to comment.