review/tests/test_comment.py @ 9030dc9517cf

web: add basic tests

This patch adds a new test module `test_web` to automate testing of web
requests. For now the tests are rather simple and only check for
expected status codes.

To set up the flask app within the tests, it has to be configured
properly. This is the reason why the app configuration part in `web.py`
has been moved into an own function - now it may also be used by the
test module.
author Oben Sonne <obensonne@googlemail.com>
date Mon, 02 Jul 2012 22:32:48 +0200
parents bef3dce04be6
children (none)
import os

from nose import with_setup
from util import setup_reviewed_sandbox, teardown_sandbox, review, should_fail_with
from util import get_datastore_repo, get_sandbox_repo, get_ui
from util import check_comment_exists_on_line

from .. import api, messages

from mercurial.node import hex

# TODO: Figure out how to handle external editors nicely with nose.

a1, a2 = (messages.REVIEW_LOG_COMMENT_AUTHOR % '|').split('|')

@with_setup(setup_reviewed_sandbox(), teardown_sandbox)
def test_no_comments():
    output = review()
    assert messages.REVIEW_LOG_COMMENTS % (0, 0) in output

@with_setup(setup_reviewed_sandbox(), teardown_sandbox)
def test_comment_formatting():
    review(comment=True, message=' \tTest comment one.\t ')
    output = review()

    assert messages.REVIEW_LOG_COMMENT_LINE % 'Test comment one.' in output
    assert messages.REVIEW_LOG_COMMENT_LINE % ' \tTest comment one.' not in output
    assert messages.REVIEW_LOG_COMMENT_LINE % 'Test comment one.\t ' not in output
    assert messages.REVIEW_LOG_COMMENT_LINE % ' \tTest comment one.\t ' not in output

    review(rev=0, comment=True,
           message=' \tTest\n  indented\n\ttabindented\noutdented  \ndone\t ')
    output = review(rev=0)

    assert messages.REVIEW_LOG_COMMENT_LINE % 'Test' in output
    assert messages.REVIEW_LOG_COMMENT_LINE % '  indented' in output
    assert messages.REVIEW_LOG_COMMENT_LINE % '\ttabindented' in output
    assert messages.REVIEW_LOG_COMMENT_LINE % 'outdented  ' in output
    assert messages.REVIEW_LOG_COMMENT_LINE % 'done' in output

@with_setup(setup_reviewed_sandbox(), teardown_sandbox)
def test_comment_styles():
    review(comment=True, message='Test comment one.', mdown=True)
    output = review()

    assert messages.REVIEW_LOG_COMMENT_LINE % 'Test comment one.' in output

@with_setup(setup_reviewed_sandbox(), teardown_sandbox)
def test_add_comments_to_parent_rev():
    review(comment=True, message='Test comment one.')

    output = review()
    assert messages.REVIEW_LOG_COMMENTS % (1, 1) in output

    assert a1 in output
    assert a2 in output

    assert messages.REVIEW_LOG_COMMENT_LINE % 'Test comment one.' in output

    review(comment=True, message='Test comment two.')

    output = review()
    assert messages.REVIEW_LOG_COMMENTS % (2, 1) in output
    assert messages.REVIEW_LOG_COMMENT_LINE % 'Test comment one.' in output
    assert messages.REVIEW_LOG_COMMENT_LINE % 'Test comment two.' in output

@with_setup(setup_reviewed_sandbox(), teardown_sandbox)
def test_add_comments_to_specific_rev():
    review(comment=True, message='Test comment one.', rev='0')

    output = review(rev='0')
    assert messages.REVIEW_LOG_COMMENTS % (1, 1) in output

    assert a1 in output
    assert a2 in output

    assert messages.REVIEW_LOG_COMMENT_LINE % 'Test comment one.' in output

    output = review()
    assert messages.REVIEW_LOG_COMMENTS % (0, 0) in output
    assert messages.REVIEW_LOG_COMMENT_LINE % 'Test comment one.' not in output

    review(comment=True, message='Test comment two.', rev='0')

    output = review(rev='0')
    assert messages.REVIEW_LOG_COMMENTS % (2, 1) in output
    assert messages.REVIEW_LOG_COMMENT_LINE % 'Test comment one.' in output
    assert messages.REVIEW_LOG_COMMENT_LINE % 'Test comment two.' in output

@with_setup(setup_reviewed_sandbox(), teardown_sandbox)
def test_add_comments_to_file():
    review(comment=True, message='Test comment one.', rev='1', args=['file_one'])

    output = review(rev='1', args=['file_one'])
    assert a1 in output
    assert a2 in output
    assert messages.REVIEW_LOG_COMMENT_LINE % 'Test comment one.' in output

    output = review(rev='1', args=['file_two'])
    assert messages.REVIEW_LOG_COMMENT_LINE % 'Test comment one.' not in output

    output = review(rev='0', args=['file_one'])
    assert a1 not in output
    assert a2 not in output
    assert messages.REVIEW_LOG_COMMENT_LINE % 'Test comment one.' not in output

@with_setup(setup_reviewed_sandbox(), teardown_sandbox)
def test_add_comments_to_multiple_files():
    review(comment=True, message='Test comment.', rev='1',
        args=['file_one', 'always_changing'])

    output = review(rev='1')
    assert output.count(messages.REVIEW_LOG_COMMENT_LINE % 'Test comment.') == 2

    should_fail_with(messages.COMMENT_LINES_REQUIRE_FILE,
                     comment=True, rev='1', message='Test bad comment.', lines='1',
                     args=['file_one', 'always_changing'])

@with_setup(setup_reviewed_sandbox(), teardown_sandbox)
def test_add_comments_to_bad_file():
    should_fail_with(messages.COMMENT_FILE_DOES_NOT_EXIST % ('bad', '2'),
                     comment=True, message='Test comment one.', args=['bad'])

@with_setup(setup_reviewed_sandbox(), teardown_sandbox)
def test_add_comments_to_file_line():
    should_fail_with(messages.COMMENT_LINES_REQUIRE_FILE,
                     comment=True, rev='1', message='Test bad comment.', lines='1')

    review(comment=True, rev='1', message='Test comment one.',
        args=['file_one'], lines='1')

    output = review(rev='1', args=['file_one'])

    # Make sure the comment is present at all.
    assert a1 in output
    assert a2 in output
    assert messages.REVIEW_LOG_COMMENT_LINE % 'Test comment one.' in output

    check_comment_exists_on_line(1, files=['file_one'], rev='1')

@with_setup(setup_reviewed_sandbox(), teardown_sandbox)
def test_add_comments_to_file_lines():
    review(comment=True, rev='1', message='Test comment one.',
        args=['file_one'], lines='1,2')

    output = review(rev='1', args=['file_one'])

    # Make sure the comment is present at all.
    assert a1 in output
    assert a2 in output
    assert messages.REVIEW_LOG_COMMENT_LINE % 'Test comment one.' in output

    check_comment_exists_on_line(2, files=['file_one'], rev='1')

@with_setup(setup_reviewed_sandbox(), teardown_sandbox)
def test_add_comments_to_file_in_subdir():
    filename = os.path.join('test_dir', 'test_file')

    review(comment=True, message='Test comment one.', rev='1', args=[filename])

    output = review(rev='1', args=[filename])
    assert a1 in output
    assert a2 in output
    assert messages.REVIEW_LOG_COMMENT_LINE % 'Test comment one.' in output

    output = review(rev='1', args=['file_two'])
    assert messages.REVIEW_LOG_COMMENT_LINE % 'Test comment one.' not in output

    output = review(rev='0', args=[filename])
    assert a1 not in output
    assert a2 not in output
    assert messages.REVIEW_LOG_COMMENT_LINE % 'Test comment one.' not in output

@with_setup(setup_reviewed_sandbox(), teardown_sandbox)
def test_add_comments_to_file_in_cwd():
    os.chdir('test_dir')
    review(comment=True, message='Test comment one.', rev='1', args=['test_file'])

    output = review(rev='1', args=['test_file'])
    assert a1 in output
    assert a2 in output
    assert messages.REVIEW_LOG_COMMENT_LINE % 'Test comment one.' in output

    output = review(rev='1', args=['file_two'])
    assert messages.REVIEW_LOG_COMMENT_LINE % 'Test comment one.' not in output

    output = review(rev='0', args=['test_file'])
    assert a1 not in output
    assert a2 not in output
    assert messages.REVIEW_LOG_COMMENT_LINE % 'Test comment one.' not in output

@with_setup(setup_reviewed_sandbox(), teardown_sandbox)
def test_add_comments_to_file_in_reldir():
    filename = os.path.join('..', 'file_three')

    os.chdir('test_dir')
    review(comment=True, message='Test comment one.', rev='1', args=[filename])

    output = review(rev='1', args=[filename])
    assert a1 in output
    assert a2 in output
    assert messages.REVIEW_LOG_COMMENT_LINE % 'Test comment one.' in output

    output = review(rev='1', args=['file_two'])
    assert messages.REVIEW_LOG_COMMENT_LINE % 'Test comment one.' not in output

    output = review(rev='0', args=[filename])
    assert a1 not in output
    assert a2 not in output
    assert messages.REVIEW_LOG_COMMENT_LINE % 'Test comment one.' not in output

@with_setup(setup_reviewed_sandbox(), teardown_sandbox)
def test_comment_identifiers():
    review(comment=True, message='Test comment one.', rev='1', args=['file_one'])

    rd = api.ReviewDatastore(get_ui(), get_sandbox_repo())
    dsr = get_datastore_repo()

    comment = rd['1'].comments[0]

    identifier = comment.identifier
    short_identifier = identifier[:12]

    comment_filename = api._split_path_dammit(dsr['tip'].files()[0])[-1]
    comment_cset = hex(dsr['tip'].node())

    assert identifier == comment_filename
    assert identifier != comment_cset

    verbose_identifier = messages.REVIEW_LOG_IDENTIFIER % identifier[:12]
    debug_identifier = messages.REVIEW_LOG_IDENTIFIER % identifier

    normal_output = review(rev='1')
    assert verbose_identifier not in normal_output
    assert debug_identifier not in normal_output

    verbose_output = review(rev='1', verbose=True)
    assert verbose_identifier in verbose_output
    assert debug_identifier not in verbose_output

    debug_output = review(rev='1', debug=True)
    assert verbose_identifier not in debug_output
    assert debug_identifier in debug_output