def testUnicode(self):
        control = 'Il <a href="github.com">était</a> une fois…'

        text = 'Il était une fois…'
        self.assertEqual(replace_terms(text), control)

        html = 'Il &eacute;tait une fois&hellip;'
        self.assertEqual(replace_terms(html), control)
示例#2
0
    def testUnicode(self):
        control = 'Il <a href="github.com">était</a> une fois…'

        text = 'Il était une fois…'
        self.assertEqual(replace_terms(text), control)

        html = 'Il &eacute;tait une fois&hellip;'
        self.assertEqual(replace_terms(html), control)
示例#3
0
    def assertCachedRegex(self):
        """
        Checks if adding links hits the database.

        The database should be hit during the first replacement after a change
        in the terms table.  After that, a regular expression is cached to
        avoid hitting the database.
        """
        with self.assertNumQueries(0):
            for before_template, after_template in self.templates:
                replace_terms(read_file(before_template))
示例#4
0
    def assertCachedRegex(self):
        """
        Checks if adding links hits the database.

        The database should be hit during the first replacement after a change
        in the terms table.  After that, a regular expression is cached to
        avoid hitting the database.
        """
        with self.assertNumQueries(0):
            for before_template, after_template in self.templates:
                replace_terms(read_file(before_template))
示例#5
0
    def test5(self):
        """
        Tests case sensitiveness.
        """
        with self.assertNumQueries(1):
            self.assertHTMLEqual(
                replace_terms(read_file('5_before.html')),
                read_file('5_after1.html', {'term': self.term5}))

        self.term5.case_sensitive = True
        self.term5.save()

        self.assertHTMLEqual(replace_terms(read_file('5_before.html')),
                             read_file('5_after2.html', {'term': self.term5}))
示例#6
0
    def test(self):
        """
        After being reconstructed, valid_html should be exactly the same.
        And after being reconstructed, valid_html_with_extra_spaces should
        be exactly the same as valid_html (since extra whitespaces within tags
        are stripped).
        """
        html = read_file('valid_html.html')
        html_w_extra_spaces = read_file('valid_html_with_extra_spaces.html')

        new_html = replace_terms(html)
        self.assertHTMLEqual(html, new_html)

        new_html_w_extra_spaces = replace_terms(html_w_extra_spaces)
        self.assertHTMLEqual(html, new_html_w_extra_spaces)
示例#7
0
    def test(self):
        """
        After being reconstructed, valid_html should be exactly the same.
        And after being reconstructed, valid_html_with_extra_spaces should
        be exactly the same as valid_html (since extra whitespaces within tags
        are stripped).
        """
        html = read_file('valid_html.html')
        html_w_extra_spaces = read_file('valid_html_with_extra_spaces.html')

        new_html = replace_terms(html)
        self.assertHTMLEqual(html, new_html)

        new_html_w_extra_spaces = replace_terms(html_w_extra_spaces)
        self.assertHTMLEqual(html, new_html_w_extra_spaces)
示例#8
0
    def test5(self):
        """
        Tests case sensitiveness.
        """
        with self.assertNumQueries(1):
            self.assertHTMLEqual(
                replace_terms(read_file('5_before.html')),
                read_file('5_after1.html', {'term': self.term5}))

        self.term5.case_sensitive = True
        self.term5.save()

        self.assertHTMLEqual(
            replace_terms(read_file('5_before.html')),
            read_file('5_after2.html', {'term': self.term5}))
示例#9
0
 def testMultipleAttributedParagraphs(self):
     html = ('<p id="test1">First paragraph</p>'
             '<p id="test2">Il était une fois</p>')
     control = (
         '<p id="test1">First paragraph</p>'
         '<p id="test2">Il <a href="github.com">était</a> une fois</p>')
     self.assertHTMLEqual(replace_terms(html), control)
示例#10
0
 def test4(self):
     """
     Tests isolated term without even a single HTML tag.
     """
     with self.assertNumQueries(1):
         self.assertHTMLEqual(
             replace_terms(read_file('4_before.html')),
             read_file('4_after.html', {'term': self.term4}))
示例#11
0
 def test4(self):
     """
     Tests isolated term without even a single HTML tag.
     """
     with self.assertNumQueries(1):
         self.assertHTMLEqual(
             replace_terms(read_file('4_before.html')),
             read_file('4_after.html', {'term': self.term4}))
示例#12
0
 def testMultipleAttributedParagraphs(self):
     html = (
         '<p id="test1">First paragraph</p>'
         '<p id="test2">Il était une fois</p>')
     control = (
         '<p id="test1">First paragraph</p>'
         '<p id="test2">Il <a href="github.com">était</a> une fois</p>')
     self.assertHTMLEqual(replace_terms(html), control)
示例#13
0
 def test3(self):
     """
     Tests term variants.
     """
     with self.assertNumQueries(1):
         self.assertHTMLEqual(
             replace_terms(read_file('3_before.html')),
             read_file('3_after.html', {'term': self.term3}))
     self.assertDetailView(self.term3, status_code=404)
示例#14
0
 def test3(self):
     """
     Tests term variants.
     """
     with self.assertNumQueries(1):
         self.assertHTMLEqual(
             replace_terms(read_file('3_before.html')),
             read_file('3_after.html', {'term': self.term3}))
     self.assertDetailView(self.term3, status_code=404)
示例#15
0
 def test6(self):
     """
     Tests if a term starting with another shorter term is correctly linked.
     """
     with self.assertNumQueries(1):
         self.assertHTMLEqual(
             replace_terms(read_file('6_before.html')),
             read_file('6_after.html', {'bw1': self.term6_1,
                                        'bw2': self.term6_2}))
示例#16
0
    def test_end_tag(self):
        """
        After being reconstructed, invalid missing end tags should be there.
        """
        valid = read_file('valid.html')
        invalid = read_file('missing_end_tag.html')

        new_html = replace_terms(invalid)
        self.assertHTMLEqual(valid, new_html)
示例#17
0
    def test_start_tag(self):
        """
        After being reconstructed, invalid missing start tags should be
        stripped.
        """
        valid = read_file('valid.html')
        invalid = read_file('missing_start_tag.html')

        new_html = replace_terms(invalid)
        self.assertHTMLEqual(valid, new_html)
示例#18
0
    def test1(self):
        """
        Tests regular case, with a complete HTML structure.
        """
        with self.assertNumQueries(1):
            self.assertHTMLEqual(
                replace_terms(read_file('1_before.html')),
                read_file('1_after.html', {'term': self.term1}))
        self.assertDetailView(self.term1, status_code=404)

        self.assertCachedRegex()
示例#19
0
    def test1(self):
        """
        Tests regular case, with a complete HTML structure.
        """
        with self.assertNumQueries(1):
            self.assertHTMLEqual(
                replace_terms(read_file('1_before.html')),
                read_file('1_after.html', {'term': self.term1}))
        self.assertDetailView(self.term1, status_code=404)

        self.assertCachedRegex()
示例#20
0
    def test2(self):
        """
        Tests links with definitions.
        """
        with self.assertNumQueries(1):
            self.assertHTMLEqual(
                replace_terms(read_file('2_before.html')),
                read_file('2_after.html', {'term': self.term2}))
        self.assertDetailView(self.term2)

        self.assertCachedRegex()
示例#21
0
    def test2(self):
        """
        Tests links with definitions.
        """
        with self.assertNumQueries(1):
            self.assertHTMLEqual(
                replace_terms(read_file('2_before.html')),
                read_file('2_after.html', {'term': self.term2}))
        self.assertDetailView(self.term2)

        self.assertCachedRegex()
示例#22
0
 def test6(self):
     """
     Tests if a term starting with another shorter term is correctly linked.
     """
     with self.assertNumQueries(1):
         self.assertHTMLEqual(
             replace_terms(read_file('6_before.html')),
             read_file('6_after.html', {
                 'bw1': self.term6_1,
                 'bw2': self.term6_2
             }))
示例#23
0
    def testPerformance(self):
        self.assertHTMLEqual(
            replace_terms(read_file('performance_test_before.html')),
            read_file('performance_test_after.html'))

        # Parsing & rebuilding should take less than 50 ms
        # on this complex page, even if your computer is a bit slow.
        # On my laptop it takes 11 ms.
        self.assertLess(
            timeit("replace_terms(test_page)",
                   setup='test_page = """%s"""\n'
                   'from terms.html import '
                   'replace_terms' % self.performance_test_page,
                   number=100) / 100.0, 0.05)
示例#24
0
    def testPerformance(self):
        self.assertHTMLEqual(
            replace_terms(read_file('performance_test_before.html')),
            read_file('performance_test_after.html'))

        # Parsing & rebuilding should take less than 50 ms
        # on this complex page, even if your computer is a bit slow.
        # On my laptop it takes 11 ms.
        self.assertLess(
            timeit("replace_terms(test_page)",
                   setup='test_page = """%s"""\n'
                         'from terms.html import '
                         'replace_terms' % self.performance_test_page,
                   number=100) / 100.0,
            0.05)
示例#25
0
 def testText(self):
     self.assertHTMLEqual(replace_terms('était'),
                          '<a href="github.com">était</a>')
示例#26
0
 def testSingleParagraph(self):
     self.assertHTMLEqual(replace_terms('<p>était</p>'),
                          '<p><a href="github.com">était</a></p>')
示例#27
0
 def testTextAndNode(self):
     self.assertHTMLEqual(
         replace_terms('était <em>une fois</em>'),
         '<a href="github.com">était</a> <em>une fois</em>')
示例#28
0
 def testSingleParagraph(self):
     self.assertHTMLEqual(replace_terms('<p>était</p>'),
                          '<p><a href="github.com">était</a></p>')
示例#29
0
    def testUnicode(self):
        text = 'Il était une fois…'
        self.assertEqual(text, replace_terms(text))

        html = 'Il &eacute;tait une fois&hellip;'
        self.assertEqual(text, replace_terms(html))
示例#30
0
 def testSingleAttributedParagraph(self):
     self.assertHTMLEqual(
         replace_terms('<p id="test">était</p>'),
         '<p id="test"><a href="github.com">était</a></p>')
示例#31
0
 def testTextAndNode(self):
     self.assertHTMLEqual(replace_terms('était <em>une fois</em>'),
                          '<a href="github.com">était</a> <em>une fois</em>')
示例#32
0
 def testEmptyString(self):
     self.assertHTMLEqual(replace_terms(''), '')
示例#33
0
 def testText(self):
     self.assertHTMLEqual(replace_terms('était'),
                          '<a href="github.com">était</a>')
示例#34
0
 def testEmptyString(self):
     self.assertHTMLEqual(replace_terms(''), '')
示例#35
0
 def testSingleAttributedParagraph(self):
     self.assertHTMLEqual(replace_terms('<p id="test">était</p>'),
                          '<p id="test"><a href="github.com">était</a></p>')