Skip to content

Commit

Permalink
Replace browser-search.txt with equivalent webtest tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
disko committed Dec 11, 2015
1 parent 769ef34 commit 8be754f
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 171 deletions.
88 changes: 0 additions & 88 deletions kotti/tests/browser-search.txt

This file was deleted.

199 changes: 116 additions & 83 deletions kotti/tests/test_search.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
from kotti.testing import DummyRequest


def create_contents(root):
def _create_contents(root):
from kotti.resources import Content, File
doc1 = root['doc1'] = Content(title=u'First Document')
doc11 = root['doc1']['doc11'] = Content(title=u'Second Document')
doc12 = root['doc1']['doc12'] = Content(title=u'Third Document')
file1 = root['doc1']['file1'] = File(title=u'First File',
description=u'this is a file')
doc1 = root['doc1'] = Content(
title=u'First Document',
description=u'I am the first.')
doc11 = root['doc1']['doc11'] = Content(
title=u'Second Document',
description=u'And I am the second.')
doc12 = root['doc1']['doc12'] = Content(
title=u'Third Document')
file1 = root['doc1']['file1'] = File(
title=u'First File',
description=u'this is a file')
return doc1, doc11, doc12, file1


def create_contents_with_tags(root=None):
def _create_contents_with_tags(root=None):
from kotti.resources import get_root
from kotti.resources import Content, File
if root is None:
Expand All @@ -35,79 +41,106 @@ def create_contents_with_tags(root=None):
return animals, cat, dog, monkey, gorilla, monkey_file


class TestSearch:

def test_search_empty_content(self, db_session):
from kotti.views.util import search_content
request = DummyRequest()
results = search_content(u'teststring', request)
assert results == []

def test_search_content(self, root):
from kotti.views.util import search_content
from kotti import DBSession
from kotti.resources import Tag

request = DummyRequest()
doc1, doc11, doc12, file1 = create_contents(root)
results = search_content(u'First Document', request)
assert len(results) == 1
assert results[0]['name'] == u'doc1'
assert results[0]['title'] == u'First Document'
results = search_content(u'Document', request)
# The frontpage contains 'Documentation' in its body!
assert len(results) == 4
assert results[1]['name'] == u'doc11'
assert results[1]['title'] == u'Second Document'
assert results[1]['path'] == '/doc1/doc11/'
assert results[1]['path'][-1] == '/'

animals, cat, dog, \
monkey, gorilla, monkey_file = create_contents_with_tags()

tags = DBSession.query(Tag).all()
assert len(tags) == 6
results = search_content(u'Animals', request)
assert len(results) == 6
results = search_content(u'Cat', request)
assert len(results) == 1
results = search_content(u'Primate', request)
assert len(results) == 3

# Tags were included in general search by modifying the pre-existing
# approach, wherein searching first worked on title and description,
# then on body, so that the search order became:
#
# first on title + description
# then on tags
# then on body
#
# So we test here to assure search results come back in that order.
# Searching on 'Animals', we should find all 6 content items, and the
# first item should be the Animals folder, found with a title hit, and
# the other items were found via tags.
#
# Note: this ordering is done to have some method, but it does not
# necessarily constitute a specification.
#
results = search_content(u'Animals', request)
assert len(results) == 6
assert results[0]['name'] == 'animals'

def test_search_file_description(self, root):
from kotti.views.util import search_content
request = DummyRequest()
doc1, doc11, doc12, file1 = create_contents(root)
results = search_content(u'this is a file', request)
assert len(results) == 1
assert results[0]['name'] == 'file1'
assert results[0]['title'] == 'First File'
assert results[0]['path'] == '/doc1/file1/'

def test_search_content_without_permission(self, config, root):
from kotti.views.util import search_content
request = DummyRequest()
create_contents(root)
config.testing_securitypolicy(permissive=False)
results = search_content(u'Document', request)
assert len(results) == 0
def test_search_empty_content(db_session):
from kotti.views.util import search_content
request = DummyRequest()
results = search_content(u'teststring', request)
assert results == []


def test_search_content(root):
from kotti.views.util import search_content
from kotti import DBSession
from kotti.resources import Tag

request = DummyRequest()
doc1, doc11, doc12, file1 = _create_contents(root)
results = search_content(u'First Document', request)
assert len(results) == 1
assert results[0]['name'] == u'doc1'
assert results[0]['title'] == u'First Document'
results = search_content(u'Document', request)
# The frontpage contains 'Documentation' in its body!
assert len(results) == 4
assert results[1]['name'] == u'doc11'
assert results[1]['title'] == u'Second Document'
assert results[1]['path'] == '/doc1/doc11/'
assert results[1]['path'][-1] == '/'

animals, cat, dog, \
monkey, gorilla, monkey_file = _create_contents_with_tags()

tags = DBSession.query(Tag).all()
assert len(tags) == 6
results = search_content(u'Animals', request)
assert len(results) == 6
results = search_content(u'Cat', request)
assert len(results) == 1
results = search_content(u'Primate', request)
assert len(results) == 3

# Tags were included in general search by modifying the pre-existing
# approach, wherein searching first worked on title and description,
# then on body, so that the search order became:
#
# first on title + description
# then on tags
# then on body
#
# So we test here to assure search results come back in that order.
# Searching on 'Animals', we should find all 6 content items, and the
# first item should be the Animals folder, found with a title hit, and
# the other items were found via tags.
#
# Note: this ordering is done to have some method, but it does not
# necessarily constitute a specification.
#
results = search_content(u'Animals', request)
assert len(results) == 6
assert results[0]['name'] == 'animals'


def test_search_file_description(root):
from kotti.views.util import search_content
request = DummyRequest()
doc1, doc11, doc12, file1 = _create_contents(root)
results = search_content(u'this is a file', request)
assert len(results) == 1
assert results[0]['name'] == 'file1'
assert results[0]['title'] == 'First File'
assert results[0]['path'] == '/doc1/file1/'


def test_search_content_without_permission(config, root):
from kotti.views.util import search_content
request = DummyRequest()
_create_contents(root)
config.testing_securitypolicy(permissive=False)
results = search_content(u'Document', request)
assert len(results) == 0


def test_search_functional(webtest, root):

doc1, doc11, doc12, file1 = _create_contents(root)

resp = webtest.app.get('/')

search_form = resp.forms['form-search']
search_form['search-term'] = u'First Document'
resp = search_form.submit()
assert 'I am the first' in resp.body
assert 'And I am the second' not in resp.body

search_form = resp.forms['form-search']
search_form['search-term'] = u'Document'
resp = search_form.submit()
assert 'I am the first' in resp.body
assert 'And I am the second' in resp.body

search_form = resp.forms['form-search']
search_form['search-term'] = u'is a file'
resp = search_form.submit()
assert 'And I am the second' not in resp.body
assert 'Third' not in resp.body
assert 'First File' in resp.body

0 comments on commit 8be754f

Please sign in to comment.