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 était une fois…' self.assertEqual(replace_terms(html), control)
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))
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}))
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)
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}))
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)
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}))
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)
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)
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}))
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)
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)
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()
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()
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 }))
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)
def testText(self): self.assertHTMLEqual(replace_terms('était'), '<a href="github.com">était</a>')
def testSingleParagraph(self): self.assertHTMLEqual(replace_terms('<p>était</p>'), '<p><a href="github.com">était</a></p>')
def testTextAndNode(self): self.assertHTMLEqual( replace_terms('était <em>une fois</em>'), '<a href="github.com">était</a> <em>une fois</em>')
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))
def testSingleAttributedParagraph(self): self.assertHTMLEqual( replace_terms('<p id="test">était</p>'), '<p id="test"><a href="github.com">était</a></p>')
def testTextAndNode(self): self.assertHTMLEqual(replace_terms('était <em>une fois</em>'), '<a href="github.com">était</a> <em>une fois</em>')
def testEmptyString(self): self.assertHTMLEqual(replace_terms(''), '')
def testSingleAttributedParagraph(self): self.assertHTMLEqual(replace_terms('<p id="test">était</p>'), '<p id="test"><a href="github.com">était</a></p>')