Skip to content

Commit

Permalink
some tests for searching
Browse files Browse the repository at this point in the history
  • Loading branch information
amol- committed Feb 6, 2014
1 parent 0b14842 commit d959f24
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 3 deletions.
3 changes: 2 additions & 1 deletion tests/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ class FakePackage(object):

class CrudTest(object):
def setUp(self):
conf = AppConfig(minimal=True, root_controller=self.controller_factory())
self.root_controller = self.controller_factory()
conf = AppConfig(minimal=True, root_controller=self.root_controller)
conf.package = FakePackage()
conf.model = conf.package.model
conf.use_dotted_templatenames = True
Expand Down
80 changes: 78 additions & 2 deletions tests/test_crud_html.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from tg import TGController
import transaction
from tgext.crud import EasyCrudRestController
from .base import CrudTest, Movie, DBSession, metadata, Actor
from .base import CrudTest, Movie, DBSession, metadata, Actor, Genre


class TestCrudHTML(CrudTest):
Expand Down Expand Up @@ -33,4 +34,79 @@ def test_post_validation_dberror(self):

result = self.app.post('/movies/', params={'title':'Movie Test'})
assert '<form' in result, result
assert '(OperationalError)' in result, result
assert '(OperationalError)' in result, result

def test_search(self):
result = self.app.get('/movies/')
assert 'id="crud_search_field"' in result, result

def test_search_disabled(self):
self.root_controller.movies.search_fields = False
result = self.app.get('/movies/')
assert 'id="crud_search_field"' not in result, result

def test_search_some(self):
self.root_controller.movies.search_fields = ['title']
result = self.app.get('/movies/')
assert 'id="crud_search_field"' in result, result
assert 'value="title"' in result, result
assert 'value="genre"' not in result, result


class TestCrudHTMLSearch(CrudTest):
def controller_factory(self):
class MovieController(EasyCrudRestController):
model = Movie

class CrudHTMLController(TGController):
movies = MovieController(DBSession)

return CrudHTMLController()

def setUp(self):
super(TestCrudHTMLSearch, self).setUp()
genre = Genre(name='action')
DBSession.add(genre)

actors = [Actor(name='James Who'), Actor(name='John Doe'), Actor(name='Man Alone')]
list(map(DBSession.add, actors))

DBSession.add(Movie(title='First Movie', genre=genre, actors=actors[:2]))
DBSession.add(Movie(title='Second Movie', genre=genre))
DBSession.add(Movie(title='Third Movie', genre=genre))
DBSession.add(Movie(title='Fourth Movie', genre=genre))
DBSession.add(Movie(title='Fifth Movie'))
DBSession.add(Movie(title='Sixth Movie'))
DBSession.flush()
transaction.commit()

def test_search_no_filters(self):
result = self.app.get('/movies/')
assert 'First Movie' in result
assert 'Second Movie' in result

def test_search_by_text(self):
result = self.app.get('/movies/?title=First%20Movie')
assert 'First Movie' in result
assert 'Second Movie' not in result

def test_search_by_substring(self):
self.root_controller.movies.substring_filters = ['title']
result = self.app.get('/movies/?title=d%20Movie')
assert 'First Movie' not in result
assert 'Second Movie' in result
assert 'Third Movie' in result
assert 'Fourth Movie' not in result

def test_search_relation_by_id(self):
result = self.app.get('/movies/?actors=1')
assert 'First Movie' in result
assert 'Second Movie' not in result

"""
def test_search_relation_by_text(self):
result = self.app.get('/movies/?genre=action')
assert 'First Movie' in result
assert 'Second Movie' in result
assert 'Fifth Movie' not in result
"""
5 changes: 5 additions & 0 deletions tgext/crud/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ class CrudRestController(RestController):
Enables searching on some fields, can be ``True``, ``False`` or a list
of fields for which searching should be enabled.
**substring_filters**
Enable substring filtering for some fields, by default is disabled.
Pass ``True`` to enable it on all fields or pass a list of field
names to enable it only on some fields.
**json_dictify**
``True`` or ``False``, enables advanced dictification of retrieved models
when providing JSON responses. This also enables JSON encoding of related entities
Expand Down

0 comments on commit d959f24

Please sign in to comment.