Skip to content

Commit

Permalink
[generator] test cases for find_template_dir
Browse files Browse the repository at this point in the history
ref #230
  • Loading branch information
iblislin committed May 3, 2016
1 parent 0939f25 commit df94391
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 2 deletions.
5 changes: 4 additions & 1 deletion couchapp/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ def copy_helper(path, directory, tname="templates"):
(path))


def find_template_dir(tmpl_name, tmpl_type, raise_error=False):
def find_template_dir(tmpl_name='', tmpl_type='', raise_error=False):
'''
Find template dir for different platform
Expand Down Expand Up @@ -208,6 +208,9 @@ def find_template_dir(tmpl_name, tmpl_type, raise_error=False):
- <module dir path>/../
- <python prefix>/Lib/site-packages/couchapp/
'''
if tmpl_type and tmpl_type not in TEMPLATE_TYPES:
raise AppError('invalid template type "{0}"'.format(tmpl_type))

modpath = os.path.dirname(__file__)
search_paths = user_path() + [
modpath,
Expand Down
50 changes: 49 additions & 1 deletion tests/test_generator.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# -*- coding: utf-8 -*-

from couchapp.generator import init_basic, save_id
from couchapp.errors import AppError
from couchapp.generator import find_template_dir, init_basic, save_id

from mock import patch
from nose.tools import raises


@patch('couchapp.generator.save_id')
Expand All @@ -23,3 +25,49 @@ def test_save_id(open_):
save_id('/mock/app', 'someid')

open_.assert_called_with('/mock/app/_id', 'wb')


@patch('couchapp.generator.os.path.isdir', return_value=False)
def test_find_template_dir_not_found(isdir):
assert find_template_dir() is None
assert isdir.called


@patch('couchapp.generator.os.path.isdir', return_value=False)
def test_find_template_dir_not_found_raise(isdir):
@raises(AppError)
def f():
find_template_dir(raise_error=True)

f()
assert isdir.called


@patch('couchapp.generator.os.path.isdir', return_value=False)
def test_find_template_dir_template_type_error(isdir):
@raises(AppError)
def f():
find_template_dir(tmpl_type='mock_type')

f()
assert not isdir.called


@patch('couchapp.generator.user_path', return_value=['/mock/.couchapp'])
@patch('couchapp.generator.os.path.isdir', return_value=True)
def test_find_template_dir_user_dir_first(isdir, user_path):
ret = find_template_dir()

assert ret == '/mock/.couchapp/templates/', ret
assert user_path.called
assert isdir.called


@patch('couchapp.generator.user_path', return_value=['/mock/.couchapp'])
@patch('couchapp.generator.os.path.isdir', return_value=True)
def test_find_template_dir_user_dir_first_with_type(isdir, user_path):
ret = find_template_dir(tmpl_type='app')

assert ret == '/mock/.couchapp/templates/app', ret
assert user_path.called
assert isdir.called

0 comments on commit df94391

Please sign in to comment.