Пример #1
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)
Пример #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>')