<commit_before># coding: utf-8

from django.test import TestCase
from django.core.urlresolvers import reverse
from rest_framework.test import APIClient
from rest_framework import status
from ..models import NoteBook, User
import logging

# Get an instance of a logger
logger = logging.getLogger(__name__)


class TestNoteBookApi(TestCase):
    def setUp(self):
        self.client = APIClient()
        self.user = User.objects.create(username='user', password='<PASSWORD>')
        self.client.force_authenticate(user=self.user)

    def test_create_without_params(self):
        self.assertEquals(status.HTTP_400_BAD_REQUEST, self.client.post(reverse('notebook-list')).status_code)
        self.assertEquals(0, NoteBook.objects.count())

    def test_create(self):
        response = self.client.post(
            reverse('notebook-list'),
            {'title': 'a title', 'created_by': reverse('user-detail', args=[self.user.id])}
        )
        self.assertEquals(status.HTTP_201_CREATED, response.status_code, response.content)
        self.assertEquals(1, NoteBook.objects.count())

    def test_update(self):
        notebook = NoteBook.objects.create(title='a title', created_by=self.user)
        self.assertEquals(1, NoteBook.objects.count())
        response = self.client.put(
            reverse('notebook-detail', args=[notebook.id]),
            {'title': 'new title', 'created_by': reverse('user-detail', args=[self.user.id])}
        )
        self.assertEquals(status.HTTP_200_OK, response.status_code, response.content)
        self.assertEquals(1, NoteBook.objects.count())
        self.assertEquals('new title', NoteBook.objects.all()[0].title)

    def test_delete(self):
        notebook = NoteBook.objects.create(title='a title', created_by=self.user)
        self.assertEquals(1, NoteBook.objects.count())
        response = self.client.delete(reverse('notebook-detail', args=[notebook.id]))
        self.assertEquals(status.HTTP_204_NO_CONTENT, response.status_code, response.content)
        self.assertEquals(0, NoteBook.objects.count())

    def test_get(self):
        notebook = NoteBook.objects.create(title='a title', created_by=self.user)
        self.assertEquals(1, NoteBook.objects.count())
        response = self.client.get(reverse('notebook-detail', args=[notebook.id]))
        self.assertEquals(status.HTTP_200_OK, response.status_code, response.content)
        self.assertEquals('a title', response.data['title'], response.data)

    def test_get_all(self):
        NoteBook.objects.create(title='notebook 1', created_by=self.user)
        NoteBook.objects.create(title='notebook 2', created_by=self.user)
        response = self.client.get(reverse('notebook-list'))
        self.assertEquals(status.HTTP_200_OK, response.status_code, response.content)
        self.assertEquals(2, len(response.data))
        self.assertEquals('notebook 1', response.data[0]['title'])
        self.assertEquals('notebook 2', response.data[1]['title'])


class TestNoteBookApiWithDifferentUser(TestCase):
    def setUp(self):
        self.client = APIClient()
        self.user1 = User.objects.create(username='user1', password='<PASSWORD>')
        self.user2 = User.objects.create(username='user2', password='<PASSWORD>')
        self.client.force_authenticate(user=self.user1)
        self.url = reverse('notebook-list')

    def test_get_all(self):
        NoteBook.objects.create(title='notebook 1', created_by=self.user1)
        NoteBook.objects.create(title='notebook 2', created_by=self.user1)
        NoteBook.objects.create(title='notebook 3', created_by=self.user2)
        response = self.client.get(reverse('notebook-list'))
        self.assertEquals(status.HTTP_200_OK, response.status_code, response.content)
        self.assertEquals(2, len(response.data))
        self.assertEquals('notebook 1', response.data[0]['title'])
        self.assertEquals('notebook 2', response.data[1]['title'])

    def test_get(self):
        notebook = NoteBook.objects.create(title='notebook 1', created_by=self.user2)
        response = self.client.get(reverse('notebook-detail', args=[notebook.id]))
        self.assertEquals(status.HTTP_403_FORBIDDEN, response.status_code, response.content)

    def test_delete(self):
        notebook = NoteBook.objects.create(title='notebook 1', created_by=self.user2)
        response = self.client.delete(reverse('notebook-detail', args=[notebook.id]))
        self.assertEquals(status.HTTP_403_FORBIDDEN, response.status_code, response.content)<commit_msg>add test<commit_after># coding: utf-8

from django.test import TestCase
from django.core.urlresolvers import reverse
from rest_framework.test import APIClient
from rest_framework import status
from ..models import NoteBook, User
import logging

# Get an instance of a logger
logger = logging.getLogger(__name__)


class TestNoteBookApi(TestCase):
    def setUp(self):
        self.client = APIClient()
        self.user = User.objects.create(username='user', password='<PASSWORD>')
        self.client.force_authenticate(user=self.user)

    def test_create_without_params(self):
        self.assertEquals(status.HTTP_400_BAD_REQUEST, self.client.post(reverse('notebook-list')).status_code)
        self.assertEquals(0, NoteBook.objects.count())

    def test_create(self):
        response = self.client.post(
            reverse('notebook-list'),
            {'title': 'a title', 'created_by': reverse('user-detail', args=[self.user.id])}
        )
        self.assertEquals(status.HTTP_201_CREATED, response.status_code, response.content)
        self.assertEquals(1, NoteBook.objects.count())

    def test_update(self):
        notebook = NoteBook.objects.create(title='a title', created_by=self.user)
        self.assertEquals(1, NoteBook.objects.count())
        response = self.client.put(
            reverse('notebook-detail', args=[notebook.id]),
            {'title': 'new title', 'created_by': reverse('user-detail', args=[self.user.id])}
        )
        self.assertEquals(status.HTTP_200_OK, response.status_code, response.content)
        self.assertEquals(1, NoteBook.objects.count())
        self.assertEquals('new title', NoteBook.objects.all()[0].title)

    def test_delete(self):
        notebook = NoteBook.objects.create(title='a title', created_by=self.user)
        self.assertEquals(1, NoteBook.objects.count())
        response = self.client.delete(reverse('notebook-detail', args=[notebook.id]))
        self.assertEquals(status.HTTP_204_NO_CONTENT, response.status_code, response.content)
        self.assertEquals(0, NoteBook.objects.count())

    def test_get(self):
        notebook = NoteBook.objects.create(title='a title', created_by=self.user)
        self.assertEquals(1, NoteBook.objects.count())
        response = self.client.get(reverse('notebook-detail', args=[notebook.id]))
        self.assertEquals(status.HTTP_200_OK, response.status_code, response.content)
        self.assertEquals('a title', response.data['title'], response.data)

    def test_get_all(self):
        NoteBook.objects.create(title='notebook 1', created_by=self.user)
        NoteBook.objects.create(title='notebook 2', created_by=self.user)
        response = self.client.get(reverse('notebook-list'))
        self.assertEquals(status.HTTP_200_OK, response.status_code, response.content)
        self.assertEquals(2, len(response.data))
        self.assertEquals('notebook 1', response.data[0]['title'])
        self.assertEquals('notebook 2', response.data[1]['title'])


class TestNoteBookApiWithDifferentUser(TestCase):
    def setUp(self):
        self.client = APIClient()
        self.user1 = User.objects.create(username='user1', password='<PASSWORD>')
        self.user2 = User.objects.create(username='user2', password='<PASSWORD>')
        self.client.force_authenticate(user=self.user1)
        self.url = reverse('notebook-list')

    def test_get_all(self):
        NoteBook.objects.create(title='notebook 1', created_by=self.user1)
        NoteBook.objects.create(title='notebook 2', created_by=self.user1)
        NoteBook.objects.create(title='notebook 3', created_by=self.user2)
        response = self.client.get(reverse('notebook-list'))
        self.assertEquals(status.HTTP_200_OK, response.status_code, response.content)
        self.assertEquals(2, len(response.data))
        self.assertEquals('notebook 1', response.data[0]['title'])
        self.assertEquals('notebook 2', response.data[1]['title'])

    def test_get(self):
        notebook = NoteBook.objects.create(title='notebook 1', created_by=self.user2)
        response = self.client.get(reverse('notebook-detail', args=[notebook.id]))
        self.assertEquals(status.HTTP_403_FORBIDDEN, response.status_code, response.content)

    def test_delete(self):
        notebook = NoteBook.objects.create(title='notebook 1', created_by=self.user2)
        response = self.client.delete(reverse('notebook-detail', args=[notebook.id]))
        self.assertEquals(status.HTTP_403_FORBIDDEN, response.status_code, response.content)

    def test_update(self):
        notebook = NoteBook.objects.create(title='notebook 1', created_by=self.user2)
        response = self.client.put(
            reverse('notebook-detail', args=[notebook.id]),
            {'title': 'new title', 'created_by': reverse('user-detail', args=[self.user1.id])}
        )
        self.assertEquals(status.HTTP_403_FORBIDDEN, response.status_code, response.content)
{'fraction_non_alphanumeric': 0.13574373612432603, 'fraction_numerical': 0.013954963526799874, 'mean_word_length': 3.8537711646998463, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '2735168', 'n_tokens_mistral': 2838, 'n_tokens_neox': 2766, 'n_words': 439}