<commit_before>from daisyproducer.documents.models import Document, Version, StateError
from django.conf import settings
from django.core.urlresolvers import reverse
from django.test import TestCase
import django.test.client
import os

TEST_DATA_DIR= os.path.abspath(os.path.join(os.path.dirname(os.path.dirname(__file__)), 'data'))

def get_file_encoder(content_type):
    def encode_pdf_file(boundary, key, file):
        from django.utils.encoding import smart_str
        import os
        
        to_str = lambda s: smart_str(s, settings.DEFAULT_CHARSET)
        return [
            '--' + boundary,
            'Content-Disposition: form-data; name="%s"; filename="%s"' \
                % (to_str(key), to_str(os.path.basename(file.name))),
            'Content-Type: %s' % content_type,
            '',
            file.read()
            ]
    return encode_pdf_file

class TodoViewTest(TestCase):
    fixtures = ['state.yaml', 'user.yaml']

    def setUp(self):
        self.document = Document()
        self.document.save()

    def test_todo_index(self):
        """Check if login succeeds"""
        self.client.login(username='testuser', password='<PASSWORD>')
        response = self.client.get(reverse('todo_index'))
        self.assertTemplateUsed(response, 'documents/todo_index.html')
        self.failUnlessEqual(response.status_code, 200)

    def test_todo_details(self):
        """Check if login succeeds"""
        self.client.login(username='testuser', password='<PASSWORD>')
        response = self.client.get(reverse('todo_detail', args=[self.document.pk]))
        self.assertTemplateUsed(response, 'documents/todo_detail.html')
        self.failUnlessEqual(response.status_code, 200)<commit_msg>Add some comments about the monkey patching of django.test.client.encode_file<commit_after>from daisyproducer.documents.models import Document, Version, StateError
from django.conf import settings
from django.core.urlresolvers import reverse
from django.test import TestCase
import django.test.client
import os

TEST_DATA_DIR= os.path.abspath(os.path.join(os.path.dirname(os.path.dirname(__file__)), 'data'))

# We have to do some monkey patching here since the default django
# test client encodes files always as an octet stream. However as We
# want to be able to specify the content-type we overwrite the
# default django functionality 
def get_file_encoder(content_type):
    def local_encode_file(boundary, key, file):
        from django.utils.encoding import smart_str
        import os
        
        to_str = lambda s: smart_str(s, settings.DEFAULT_CHARSET)
        return [
            '--' + boundary,
            'Content-Disposition: form-data; name="%s"; filename="%s"' \
                % (to_str(key), to_str(os.path.basename(file.name))),
            'Content-Type: %s' % content_type,
            '',
            file.read()
            ]
    return local_encode_file

class TodoViewTest(TestCase):
    fixtures = ['state.yaml', 'user.yaml']

    def setUp(self):
        self.document = Document()
        self.document.save()

    def test_todo_index(self):
        """Check if login succeeds"""
        self.client.login(username='testuser', password='<PASSWORD>')
        response = self.client.get(reverse('todo_index'))
        self.assertTemplateUsed(response, 'documents/todo_index.html')
        self.failUnlessEqual(response.status_code, 200)

    def test_todo_details(self):
        """Check if login succeeds"""
        self.client.login(username='testuser', password='<PASSWORD>')
        response = self.client.get(reverse('todo_detail', args=[self.document.pk]))
        self.assertTemplateUsed(response, 'documents/todo_detail.html')
        self.failUnlessEqual(response.status_code, 200)
{'fraction_non_alphanumeric': 0.118508655126498, 'fraction_numerical': 0.003195739014647137, 'mean_word_length': 3.2249718785151855, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, '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': '7299344', 'n_tokens_mistral': 1130, 'n_tokens_neox': 1086, 'n_words': 235}