mirror of
https://github.com/sphinx-doc/sphinx.git
synced 2025-02-25 18:55:22 -06:00
Automated merge with ssh://bitbucket.org/DasIch/sphinx-version-tracking
This commit is contained in:
83
tests/test_searchadapters.py
Normal file
83
tests/test_searchadapters.py
Normal file
@@ -0,0 +1,83 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
test_searchadapters
|
||||
~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
Test the Web Support Package search adapters.
|
||||
|
||||
:copyright: Copyright 2007-2010 by the Sphinx team, see AUTHORS.
|
||||
:license: BSD, see LICENSE for details.
|
||||
"""
|
||||
|
||||
import os, sys
|
||||
from StringIO import StringIO
|
||||
|
||||
from util import *
|
||||
from sphinx.websupport import WebSupport
|
||||
|
||||
|
||||
def clear_builddir():
|
||||
(test_root / 'websupport').rmtree(True)
|
||||
|
||||
|
||||
def teardown_module():
|
||||
(test_root / 'generated').rmtree(True)
|
||||
clear_builddir()
|
||||
|
||||
|
||||
def search_adapter_helper(adapter):
|
||||
clear_builddir()
|
||||
|
||||
settings = {'builddir': os.path.join(test_root, 'websupport'),
|
||||
'status': StringIO(),
|
||||
'warning': StringIO()}
|
||||
settings.update({'srcdir': test_root,
|
||||
'search': adapter})
|
||||
support = WebSupport(**settings)
|
||||
support.build()
|
||||
|
||||
s = support.search
|
||||
|
||||
# Test the adapters query method. A search for "Epigraph" should return
|
||||
# one result.
|
||||
results = s.query(u'Epigraph')
|
||||
assert len(results) == 1, \
|
||||
'%s search adapter returned %s search result(s), should have been 1'\
|
||||
% (adapter, len(results))
|
||||
|
||||
# Make sure documents are properly updated by the search adapter.
|
||||
s.init_indexing(changed=['markup'])
|
||||
s.add_document(u'markup', u'title', u'SomeLongRandomWord')
|
||||
s.finish_indexing()
|
||||
# Now a search for "Epigraph" should return zero results.
|
||||
results = s.query(u'Epigraph')
|
||||
assert len(results) == 0, \
|
||||
'%s search adapter returned %s search result(s), should have been 0'\
|
||||
% (adapter, len(results))
|
||||
# A search for "SomeLongRandomWord" should return one result.
|
||||
results = s.query(u'SomeLongRandomWord')
|
||||
assert len(results) == 1, \
|
||||
'%s search adapter returned %s search result(s), should have been 1'\
|
||||
% (adapter, len(results))
|
||||
# Make sure it works through the WebSupport API
|
||||
html = support.get_search_results(u'SomeLongRandomWord')
|
||||
|
||||
|
||||
def test_xapian():
|
||||
# Don't run tests if xapian is not installed.
|
||||
try:
|
||||
import xapian
|
||||
search_adapter_helper('xapian')
|
||||
except ImportError:
|
||||
sys.stderr.write('info: not running xapian tests, ' \
|
||||
'xapian doesn\'t seem to be installed')
|
||||
|
||||
|
||||
def test_whoosh():
|
||||
# Don't run tests if whoosh is not installed.
|
||||
try:
|
||||
import whoosh
|
||||
search_adapter_helper('whoosh')
|
||||
except ImportError:
|
||||
sys.stderr.write('info: not running whoosh tests, ' \
|
||||
'whoosh doesn\'t seem to be installed')
|
||||
258
tests/test_websupport.py
Normal file
258
tests/test_websupport.py
Normal file
@@ -0,0 +1,258 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
test_websupport
|
||||
~~~~~~~~~~~~~~~
|
||||
|
||||
Test the Web Support Package
|
||||
|
||||
:copyright: Copyright 2007-2010 by the Sphinx team, see AUTHORS.
|
||||
:license: BSD, see LICENSE for details.
|
||||
"""
|
||||
|
||||
import os
|
||||
from StringIO import StringIO
|
||||
|
||||
from nose import SkipTest
|
||||
|
||||
from sphinx.websupport import WebSupport
|
||||
from sphinx.websupport.errors import *
|
||||
from sphinx.websupport.storage.differ import CombinedHtmlDiff
|
||||
from sphinx.websupport.storage.sqlalchemystorage import Session, \
|
||||
SQLAlchemyStorage, Comment, CommentVote
|
||||
from sphinx.websupport.storage.db import Node
|
||||
from util import *
|
||||
|
||||
try:
|
||||
from functools import wraps
|
||||
except ImportError:
|
||||
# functools is new in 2.4
|
||||
wraps = lambda f: (lambda w: w)
|
||||
|
||||
|
||||
default_settings = {'builddir': os.path.join(test_root, 'websupport'),
|
||||
'status': StringIO(),
|
||||
'warning': StringIO()}
|
||||
|
||||
def teardown_module():
|
||||
(test_root / 'generated').rmtree(True)
|
||||
(test_root / 'websupport').rmtree(True)
|
||||
|
||||
|
||||
def with_support(*args, **kwargs):
|
||||
"""Make a WebSupport object and pass it the test."""
|
||||
settings = default_settings.copy()
|
||||
settings.update(kwargs)
|
||||
|
||||
def generator(func):
|
||||
@wraps(func)
|
||||
def new_func(*args2, **kwargs2):
|
||||
support = WebSupport(**settings)
|
||||
func(support, *args2, **kwargs2)
|
||||
return new_func
|
||||
return generator
|
||||
|
||||
|
||||
@with_support()
|
||||
def test_no_srcdir(support):
|
||||
"""Make sure the correct exception is raised if srcdir is not given."""
|
||||
raises(SrcdirNotSpecifiedError, support.build)
|
||||
|
||||
|
||||
@with_support(srcdir=test_root)
|
||||
def test_build(support):
|
||||
support.build()
|
||||
|
||||
|
||||
@with_support()
|
||||
def test_get_document(support):
|
||||
raises(DocumentNotFoundError, support.get_document, 'nonexisting')
|
||||
|
||||
contents = support.get_document('contents')
|
||||
assert contents['title'] and contents['body'] \
|
||||
and contents['sidebar'] and contents['relbar']
|
||||
|
||||
|
||||
@with_support()
|
||||
def test_comments(support):
|
||||
session = Session()
|
||||
nodes = session.query(Node).all()
|
||||
first_node = nodes[0]
|
||||
second_node = nodes[1]
|
||||
|
||||
# Create a displayed comment and a non displayed comment.
|
||||
comment = support.add_comment('First test comment',
|
||||
node_id=first_node.id,
|
||||
username='user_one')
|
||||
hidden_comment = support.add_comment('Hidden comment',
|
||||
node_id=first_node.id,
|
||||
displayed=False)
|
||||
# Make sure that comments can't be added to a comment where
|
||||
# displayed == False, since it could break the algorithm that
|
||||
# converts a nodes comments to a tree.
|
||||
raises(CommentNotAllowedError, support.add_comment, 'Not allowed',
|
||||
parent_id=str(hidden_comment['id']))
|
||||
# Add a displayed and not displayed child to the displayed comment.
|
||||
support.add_comment('Child test comment', parent_id=str(comment['id']),
|
||||
username='user_one')
|
||||
support.add_comment('Hidden child test comment',
|
||||
parent_id=str(comment['id']), displayed=False)
|
||||
# Add a comment to another node to make sure it isn't returned later.
|
||||
support.add_comment('Second test comment',
|
||||
node_id=second_node.id,
|
||||
username='user_two')
|
||||
|
||||
# Access the comments as a moderator.
|
||||
data = support.get_data(first_node.id, moderator=True)
|
||||
comments = data['comments']
|
||||
children = comments[0]['children']
|
||||
assert len(comments) == 2
|
||||
assert comments[1]['text'] == 'Hidden comment'
|
||||
assert len(children) == 2
|
||||
assert children[1]['text'] == 'Hidden child test comment'
|
||||
|
||||
# Access the comments without being a moderator.
|
||||
data = support.get_data(first_node.id)
|
||||
comments = data['comments']
|
||||
children = comments[0]['children']
|
||||
assert len(comments) == 1
|
||||
assert comments[0]['text'] == 'First test comment'
|
||||
assert len(children) == 1
|
||||
assert children[0]['text'] == 'Child test comment'
|
||||
|
||||
|
||||
@with_support()
|
||||
def test_voting(support):
|
||||
session = Session()
|
||||
nodes = session.query(Node).all()
|
||||
node = nodes[0]
|
||||
|
||||
comment = support.get_data(node.id)['comments'][0]
|
||||
|
||||
def check_rating(val):
|
||||
data = support.get_data(node.id)
|
||||
comment = data['comments'][0]
|
||||
assert comment['rating'] == val, '%s != %s' % (comment['rating'], val)
|
||||
|
||||
support.process_vote(comment['id'], 'user_one', '1')
|
||||
support.process_vote(comment['id'], 'user_two', '1')
|
||||
support.process_vote(comment['id'], 'user_three', '1')
|
||||
check_rating(3)
|
||||
support.process_vote(comment['id'], 'user_one', '-1')
|
||||
check_rating(1)
|
||||
support.process_vote(comment['id'], 'user_one', '0')
|
||||
check_rating(2)
|
||||
|
||||
# Make sure a vote with value > 1 or < -1 can't be cast.
|
||||
raises(ValueError, support.process_vote, comment['id'], 'user_one', '2')
|
||||
raises(ValueError, support.process_vote, comment['id'], 'user_one', '-2')
|
||||
|
||||
# Make sure past voting data is associated with comments when they are
|
||||
# fetched.
|
||||
data = support.get_data(str(node.id), username='user_two')
|
||||
comment = data['comments'][0]
|
||||
assert comment['vote'] == 1, '%s != 1' % comment['vote']
|
||||
|
||||
|
||||
@with_support()
|
||||
def test_proposals(support):
|
||||
session = Session()
|
||||
node = session.query(Node).first()
|
||||
|
||||
data = support.get_data(node.id)
|
||||
|
||||
source = data['source']
|
||||
proposal = source[:5] + source[10:15] + 'asdf' + source[15:]
|
||||
|
||||
comment = support.add_comment('Proposal comment',
|
||||
node_id=node.id,
|
||||
proposal=proposal)
|
||||
|
||||
|
||||
@with_support()
|
||||
def test_user_delete_comments(support):
|
||||
def get_comment():
|
||||
session = Session()
|
||||
node = session.query(Node).first()
|
||||
session.close()
|
||||
return support.get_data(node.id)['comments'][0]
|
||||
|
||||
comment = get_comment()
|
||||
assert comment['username'] == 'user_one'
|
||||
# Make sure other normal users can't delete someone elses comments.
|
||||
raises(UserNotAuthorizedError, support.delete_comment,
|
||||
comment['id'], username='user_two')
|
||||
# Now delete the comment using the correct username.
|
||||
support.delete_comment(comment['id'], username='user_one')
|
||||
comment = get_comment()
|
||||
assert comment['username'] == '[deleted]'
|
||||
assert comment['text'] == '[deleted]'
|
||||
|
||||
|
||||
@with_support()
|
||||
def test_moderator_delete_comments(support):
|
||||
def get_comment():
|
||||
session = Session()
|
||||
node = session.query(Node).first()
|
||||
session.close()
|
||||
return support.get_data(node.id, moderator=True)['comments'][1]
|
||||
|
||||
comment = get_comment()
|
||||
support.delete_comment(comment['id'], username='user_two',
|
||||
moderator=True)
|
||||
comment = get_comment()
|
||||
assert comment['username'] == '[deleted]'
|
||||
assert comment['text'] == '[deleted]'
|
||||
|
||||
|
||||
@with_support()
|
||||
def test_update_username(support):
|
||||
support.update_username('user_two', 'new_user_two')
|
||||
session = Session()
|
||||
comments = session.query(Comment).\
|
||||
filter(Comment.username == 'user_two').all()
|
||||
assert len(comments) == 0
|
||||
votes = session.query(CommentVote).\
|
||||
filter(CommentVote.username == 'user_two')
|
||||
assert len(comments) == 0
|
||||
comments = session.query(Comment).\
|
||||
filter(Comment.username == 'new_user_two').all()
|
||||
assert len(comments) == 1
|
||||
votes = session.query(CommentVote).\
|
||||
filter(CommentVote.username == 'new_user_two')
|
||||
assert len(comments) == 1
|
||||
|
||||
|
||||
called = False
|
||||
def moderation_callback(comment):
|
||||
global called
|
||||
called = True
|
||||
|
||||
|
||||
@with_support(moderation_callback=moderation_callback)
|
||||
def test_moderation(support):
|
||||
raise SkipTest(
|
||||
'test is broken, relies on order of test execution and numeric ids')
|
||||
accepted = support.add_comment('Accepted Comment', node_id=3,
|
||||
displayed=False)
|
||||
rejected = support.add_comment('Rejected comment', node_id=3,
|
||||
displayed=False)
|
||||
# Make sure the moderation_callback is called.
|
||||
assert called == True
|
||||
# Make sure the user must be a moderator.
|
||||
raises(UserNotAuthorizedError, support.accept_comment, accepted['id'])
|
||||
raises(UserNotAuthorizedError, support.reject_comment, accepted['id'])
|
||||
support.accept_comment(accepted['id'], moderator=True)
|
||||
support.reject_comment(rejected['id'], moderator=True)
|
||||
comments = support.get_data(3)['comments']
|
||||
assert len(comments) == 1
|
||||
comments = support.get_data(3, moderator=True)['comments']
|
||||
assert len(comments) == 1
|
||||
|
||||
|
||||
def test_differ():
|
||||
differ = CombinedHtmlDiff()
|
||||
source = 'Lorem ipsum dolor sit amet,\nconsectetur adipisicing elit,\n' \
|
||||
'sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.'
|
||||
prop = 'Lorem dolor sit amet,\nconsectetur nihil adipisicing elit,\n' \
|
||||
'sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.'
|
||||
differ.make_html(source, prop)
|
||||
Reference in New Issue
Block a user