<commit_before>from django.test import TransactionTestCase, TestCase
from django.contrib import comments
from django.contrib.sites.models import Site
from django.template import loader
from django.conf import settings

from threadedcomments.util import annotate_tree_properties

PATH_DIGITS = getattr(settings, 'COMMENT_PATH_DIGITS', 10)

def sanitize_html(html):
    return '\n'.join(( i.strip() for i in html.split('\n') if i.strip() != '' ))

class SanityTests(TransactionTestCase):
    BASE_DATA = {
        'name': u'<NAME>',
        'email': u'<EMAIL>',
        'comment': u'This is my favorite Django app ever!',
    }
    
    def _post_comment(self, data=None, parent=None):
        Comment = comments.get_model()
        body = (data or self.BASE_DATA).copy()
        url = comments.get_form_target()
        args = [Site.objects.all()[0]]
        kwargs = {}
        if parent is not None:
            kwargs['parent'] = unicode(parent.pk)
            body['parent'] = unicode(parent.pk)
        form = comments.get_form()(*args, **kwargs)
        body.update(form.generate_security_data())
        self.client.post(url, body, follow=True)
        return Comment.objects.order_by('-id')[0]
    
    def test_post_comment(self):
        Comment = comments.get_model()
        self.assertEqual(Comment.objects.count(), 0)
        comment = self._post_comment()
        self.assertEqual(comment.tree_path, str(comment.pk).zfill(PATH_DIGITS))
        self.assertEqual(Comment.objects.count(), 1)
    
    def test_post_comment_child(self):
        comment = self._post_comment()
        self.assertEqual(comment.tree_path, str(comment.pk).zfill(PATH_DIGITS))
        child_comment = self._post_comment(parent=comment)
        comment_pk = str(comment.pk).zfill(PATH_DIGITS)
        child_comment_pk = str(child_comment.pk).zfill(PATH_DIGITS)
        self.assertEqual(child_comment.tree_path, '%s/%s' % (comment_pk,
            child_comment_pk))
        self.assertEqual(comment.pk, child_comment.parent.pk)


class HierarchyTest(TransactionTestCase):
    fixtures = ['simple_tree']
    
    EXPECTED_HTML = sanitize_html('''
    <ul>
        <li>
            0000000001
            <ul>
                <li>
                    0000000001/0000000002
                    <ul>
                        <li>
                            0000000001/0000000002/0000000003
                        </li>
                        <li class="last">
                            0000000001/0000000002/0000000005
                        </li>
                    </ul>
                </li>
                <li class="last">
                    0000000001/0000000004
                    <ul>
                        <li class="last">
                            0000000001/0000000004/0000000006
                        </li>
                    </ul>
                </li>
            </ul>
        </li>
    </ul>
    <ul>
        <li>
            0000000007
        </li>
    </ul>
    ''')

    def test_open_and_close_match(self):
        depth = 0
        for x in annotate_tree_properties(comments.get_model().objects.all()):
            depth += getattr(x, 'open', 0)
            self.assertEqual(x.depth, depth)
            depth -= len(getattr(x, 'close', []))

        self.assertEqual(0, depth)

    def test_last_flags_set_correctly_only_on_last_sibling(self):
        # construct the tree
        nodes = {}
        for x in comments.get_model().objects.all():
            nodes[x.pk] = (x, [])
            if x.parent_id:
                nodes[x.parent_id][1].append(x.pk)

        # check all the cmments
        for x in annotate_tree_properties(comments.get_model().objects.all()):
            if getattr(x, 'last', False):
                # last comments have a parent
                self.assertTrue(x.parent_id)
                par, siblings = nodes[x.parent_id]

                # and ar last in their child list
                self.assertTrue( x.pk in siblings )
                self.assertEqual(len(siblings)-1, siblings.index(x.pk) )

    def test_template(self):
        Comment = comments.get_model()
        output = loader.render_to_string(
            'sample_tree.html',
            {'comment_list': annotate_tree_properties(Comment.objects.all())}<commit_msg>Yak shaving<commit_after>from django.test import TransactionTestCase, TestCase
from django.contrib import comments
from django.contrib.sites.models import Site
from django.template import loader
from django.conf import settings

from threadedcomments.util import annotate_tree_properties

PATH_DIGITS = getattr(settings, 'COMMENT_PATH_DIGITS', 10)

def sanitize_html(html):
    return '\n'.join(( i.strip() for i in html.split('\n') if i.strip() != '' ))

class SanityTests(TransactionTestCase):
    BASE_DATA = {
        'name': u'<NAME>',
        'email': u'<EMAIL>',
        'comment': u'This is my favorite Django app ever!',
    }
    
    def _post_comment(self, data=None, parent=None):
        Comment = comments.get_model()
        body = (data or self.BASE_DATA).copy()
        url = comments.get_form_target()
        args = [Site.objects.all()[0]]
        kwargs = {}
        if parent is not None:
            kwargs['parent'] = unicode(parent.pk)
            body['parent'] = unicode(parent.pk)
        form = comments.get_form()(*args, **kwargs)
        body.update(form.generate_security_data())
        self.client.post(url, body, follow=True)
        return Comment.objects.order_by('-id')[0]
    
    def test_post_comment(self):
        Comment = comments.get_model()
        self.assertEqual(Comment.objects.count(), 0)
        comment = self._post_comment()
        self.assertEqual(comment.tree_path, str(comment.pk).zfill(PATH_DIGITS))
        self.assertEqual(Comment.objects.count(), 1)
    
    def test_post_comment_child(self):
        comment = self._post_comment()
        self.assertEqual(comment.tree_path, str(comment.pk).zfill(PATH_DIGITS))
        child_comment = self._post_comment(parent=comment)
        comment_pk = str(comment.pk).zfill(PATH_DIGITS)
        child_comment_pk = str(child_comment.pk).zfill(PATH_DIGITS)
        self.assertEqual(child_comment.tree_path, '%s/%s' % (comment_pk,
            child_comment_pk))
        self.assertEqual(comment.pk, child_comment.parent.pk)


class HierarchyTest(TransactionTestCase):
    fixtures = ['simple_tree']
    
    EXPECTED_HTML = sanitize_html('''
    <ul>
        <li>
            0000000001
            <ul>
                <li>
                    0000000001/0000000002
                    <ul>
                        <li>
                            0000000001/0000000002/0000000003
                        </li>
                        <li class="last">
                            0000000001/0000000002/0000000005
                        </li>
                    </ul>
                </li>
                <li class="last">
                    0000000001/0000000004
                    <ul>
                        <li class="last">
                            0000000001/0000000004/0000000006
                        </li>
                    </ul>
                </li>
            </ul>
        </li>
    </ul>
    <ul>
        <li>
            0000000007
        </li>
    </ul>
    ''')

    def test_open_and_close_match(self):
        depth = 0
        for x in annotate_tree_properties(comments.get_model().objects.all()):
            depth += getattr(x, 'open', 0)
            self.assertEqual(x.depth, depth)
            depth -= len(getattr(x, 'close', []))

        self.assertEqual(0, depth)

    def test_last_flags_set_correctly_only_on_last_sibling(self):
        # construct the tree
        nodes = {}
        for x in comments.get_model().objects.all():
            nodes[x.pk] = (x, [])
            if x.parent_id:
                nodes[x.parent_id][1].append(x.pk)

        # check all the comments
        for x in annotate_tree_properties(comments.get_model().objects.all()):
            if getattr(x, 'last', False):
                # last comments have a parent
                self.assertTrue(x.parent_id)
                par, siblings = nodes[x.parent_id]

                # and ar last in their child list
                self.assertTrue( x.pk in siblings )
                self.assertEqual(len(siblings)-1, siblings.index(x.pk) )

    def test_template(self):
        Comment = comments.get_model()
        output = loader.render_to_string(
            'sample_tree.html',
            {'comment_list': annotate_tree_properties(Comment.objects.all())}
{'fraction_non_alphanumeric': 0.11555970584802147, 'fraction_numerical': 0.037586086144507994, 'mean_word_length': 1.8540972684876749, 'pattern_counts': {'":': 0, '<': 55, '<?xml version=': 0, '>': 55, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 30, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1043666', 'n_tokens_mistral': 2743, 'n_tokens_neox': 2412, 'n_words': 470}