Source

bugs.python.org / extensions / test / test_local_replace.py

import sys
import unittest
import os.path

if len(sys.argv) != 2:
    sys.exit('Error: You have to provide the path of Roundup in order to run '
             'the tests (e.g. /opt/tracker-roundup/lib/python2.7/site-packages/).')
# add to sys.path the dir where roundup is installed (local_replace will try
# to import it)
sys.path.append(sys.argv.pop())

testdir = os.path.dirname(os.path.abspath(__file__))
dirs = testdir.split(os.path.sep)
# add the dir where local_replace is (i.e. one level up)
sys.path.append(os.path.sep.join(dirs[:-1]))
# add the dir where the roundup tests are
sys.path.append(os.path.sep.join(dirs[:-3] + ['roundup', 'test']))


from local_replace import PyDevStringHTMLProperty
from test_templating import MockDatabase, TemplatingTestCase


class MockDBItem(object):
    def __init__(self, values):
        # *values* is a list of ids that are supposed to exist in the db
        # (note that there's no difference here between issues, msg, etc.)
        self.ids = map(str, values)

    def hasnode(self, id):
        return id in self.ids

    def get(self, id, value):
        if value == 'title':
            return 'Mock title'
        if value == 'status':
            return 1
        if value == 'name':
            return 'open'

class PyDevMockDatabase(MockDatabase):
    def __init__(self, values):
        self.issue = self.msg = self.status = MockDBItem(values)
    def getclass(self, cls):
        return self.issue


class TestPyDevStringHTMLProperty(TemplatingTestCase):
    def test_replacement(self):
        self.maxDiff = None
        # create a db with a few issue/msg/file ids
        self.client.db = self._db = PyDevMockDatabase(
                [1000, 5555, 555555, 1999999, 2000000, 1234567890123])
        self.client.db.security.hasPermission = lambda *args, **kw: True
        # the test file contains the text on odd lines and the expected
        # result on even ones, with comments starting with '##'
        f = open(os.path.join(testdir, 'local_replace_data.txt'))
        for text, expected_result in zip(f, f):
            if text.startswith('##') and expected_result.startswith('##'):
                continue  # skip the comments
            p = PyDevStringHTMLProperty(self.client, 'test', '1',
                                        None, 'test', text)
            # decode the str -- Unicode strings have a better diff
            self.assertEqual(p.pydev_hyperlinked().decode(),
                             expected_result.decode())

# run the tests
unittest.main()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.