<commit_before># -*- coding: utf-8 -*-
from __future__ import unicode_literals

from rest_framework.test import APITestCase
from rest_framework import status
from django.contrib.auth import get_user_model
from api.models import Book, Issue

User = get_user_model()

class UserTestCase(APITestCase): 
    def setUp(self):
        User.objects.create(
            email='<EMAIL>', first_name='Test', 
            last_name='Admin', password='<PASSWORD>', 
            username='test.admin', is_superuser=True)
        super(UserTestCase, self).setUp()

    def test_can_get_users(self):
        response = self.client.get('/api/v1/users/')
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        
        data = response.data
        self.assertEqual(type(data), list)


class BookTestCase(APITestCase): 
    def setUp(self):
        self.admin = User.objects.create(
            email='<EMAIL>', first_name='Test', 
            last_name='Admin', password='<PASSWORD>',
            username='test.admin', is_superuser=True)
        super(BookTestCase, self).setUp()
    
    def test_can_get_books(self):
        response = self.client.get('/api/v1/books/')
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        
        data = response.data
        self.assertEqual(type(data), list)
    
    def test_can_create_book(self):
        self.client.force_authenticate(self.admin)
        response = self.client.post('/api/v1/books/', data={
            'title': 'Book 1',
            'category': 'Arts',
            'isbn': '12344242',
        })
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        
        data = response.data
        self.assertEqual(data['title'], 'Book 1')

class IssuesTestCase(APITestCase):
    def setUp(self):
        self.book = Book.objects.create(**{
            'title': 'Book 1',
            'category': 'Arts',
            'isbn': '12344242',
        })
        self.user = User.objects.create(
            email='<EMAIL>', first_name='Test',
            last_name='User', password='<PASSWORD>',
            username='test.user',
        )
        self.admin = User.objects.create(
            email='<EMAIL>', first_name='Test', 
            last_name='Admin', password='<PASSWORD>', 
            username='test.admin', is_superuser=True
        )
        super(IssuesTestCase, self).setUp()
    
    def test_can_get_issues(self):
        self.client.force_authenticate(self.admin)
        response = self.client.get('/api/v1/issues/')
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        
        data = response.data
        self.assertEqual(type(data), list)
    
    def test_can_request_unapproved_book(self):
        self.client.force_authenticate(self.user)
        response = self.client.post('/api/v1/issues/', {
            'book': self.book.id
        })
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
    
    def test_cannot_request_approved_book(self):
        self.client.force_authenticate(self.user)

        book = self.book
        book.approved = True
        book.save()

        response = self.client.post('/api/v1/issues/', {
            'book': self.book.id
        })
        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)

    def test_can_approve_issue(self):
        issue = Issue.objects.create(book=self.book, user=self.user)
        response = self.client.put('/api/v1/issues/{0.id}'.format(issue), {
            'approved': True,
        })
    
        self.assertEqual(response.status_code, status.HTTP_200_OK)

        data = response.data
        self.assertTrue(data['approved'])<commit_msg>(fix) relax reponse data type checking<commit_after># -*- coding: utf-8 -*-
from __future__ import unicode_literals

from rest_framework.test import APITestCase
from rest_framework import status
from django.contrib.auth import get_user_model
from api.models import Book, Issue

User = get_user_model()

class UserTestCase(APITestCase): 
    def setUp(self):
        User.objects.create(
            email='<EMAIL>', first_name='Test', 
            last_name='Admin', password='<PASSWORD>', 
            username='test.admin', is_superuser=True)
        super(UserTestCase, self).setUp()

    def test_can_get_users(self):
        response = self.client.get('/api/v1/users/')
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        
        data = response.data
        self.assertIsInstance(data, list)


class BookTestCase(APITestCase): 
    def setUp(self):
        self.admin = User.objects.create(
            email='<EMAIL>', first_name='Test', 
            last_name='Admin', password='<PASSWORD>',
            username='test.admin', is_superuser=True)
        super(BookTestCase, self).setUp()
    
    def test_can_get_books(self):
        response = self.client.get('/api/v1/books/')
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        
        data = response.data
        self.assertIsInstance(data, list)
    
    def test_can_create_book(self):
        self.client.force_authenticate(self.admin)
        response = self.client.post('/api/v1/books/', data={
            'title': 'Book 1',
            'category': 'Arts',
            'isbn': '12344242',
        })
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
        
        data = response.data
        self.assertEqual(data['title'], 'Book 1')

class IssuesTestCase(APITestCase):
    def setUp(self):
        self.book = Book.objects.create(**{
            'title': 'Book 1',
            'category': 'Arts',
            'isbn': '12344242',
        })
        self.user = User.objects.create(
            email='<EMAIL>', first_name='Test',
            last_name='User', password='<PASSWORD>',
            username='test.user',
        )
        self.admin = User.objects.create(
            email='<EMAIL>', first_name='Test', 
            last_name='Admin', password='<PASSWORD>', 
            username='test.admin', is_superuser=True
        )
        super(IssuesTestCase, self).setUp()
    
    def test_can_get_issues(self):
        self.client.force_authenticate(self.admin)
        response = self.client.get('/api/v1/issues/')
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        
        data = response.data
        self.assertIsInstance(data, list)
    
    def test_can_request_unapproved_book(self):
        self.client.force_authenticate(self.user)
        response = self.client.post('/api/v1/issues/', {
            'book': self.book.id
        })
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
    
    def test_cannot_request_approved_book(self):
        self.client.force_authenticate(self.user)

        book = self.book
        book.approved = True
        book.save()

        response = self.client.post('/api/v1/issues/', {
            'book': self.book.id
        })
        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)

    def test_can_approve_issue(self):
        issue = Issue.objects.create(book=self.book, user=self.user)
        response = self.client.put('/api/v1/issues/{0.id}'.format(issue), {
            'approved': True,
        })
    
        self.assertEqual(response.status_code, status.HTTP_200_OK)

        data = response.data
        self.assertTrue(data['approved'])
{'fraction_non_alphanumeric': 0.12960712839206157, 'fraction_numerical': 0.013230727690022951, 'mean_word_length': 2.4138248847926267, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, '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': '1969825', 'n_tokens_mistral': 2305, 'n_tokens_neox': 2216, 'n_words': 346}