Ejemplo n.º 1
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))
Ejemplo n.º 2
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}))
Ejemplo n.º 3
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)
Ejemplo n.º 4
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}))
Ejemplo n.º 5
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)
Ejemplo n.º 6
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)
Ejemplo n.º 7
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}))
Ejemplo n.º 8
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)
Ejemplo n.º 9
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()
Ejemplo n.º 10
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()
Ejemplo n.º 11
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 200 ms
        # on this complex page, even if your computer is a bit slow.
        # On my laptop it takes 45 ms.
        self.assertLess(
            timeit("replace_terms(test_page)",
                   setup='test_page = """%s"""\n'
                         'from terms.templatetags.terms import '
                         'replace_terms' % self.performance_test_page,
                   number=100) / 100.0,
            0.2)
Ejemplo n.º 12
0
    def testUnicode(self):
        text = 'Il était une fois…'
        self.assertEqual(text, replace_terms(text))

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