<commit_before>from django.core.urlresolvers import reverse
from django.contrib.auth.models import User

from rest_framework.test import APIRequestFactory, APITestCase, \
    force_authenticate, APIClient
from rest_framework.authtoken.models import Token

from ..models.protocols import Protocol, ProtocolDataSource
from ..views.protocol import ProtocolViewSet, ProtocolDataSourceView, \
    ProtocolOrganizationView, ProtocolSubjectsView, ProtocolSubjectDetailView
from ..views import PDSViewSet, PDSSubjectView, PDSSubjectRecordsView, \
    PDSSubjectRecordDetailView, PDSRecordLinkDetailView, PDSAvailableLinksView

factory = APIRequestFactory()


class BRPTestCase(APITestCase):

    fixtures = ['demo_data']

    def setUp(self):
        self.test_user = User.objects.get(username='admin')
        self.test_protocol = Protocol.objects.get(pk=1)
        self.test_pds = ProtocolDataSource.objects.get(pk=1)

    def cleanUp(self):
        token = Token.objects.get(user__username='admin')
        client = APIClient()
        client.credentials(HTTP_AUTHORIZATION='Token ' + token.key)
        url = reverse(
            'protocol-subject-view',
            kwargs={
                'pk': self.test_protocol.id,
                'subject': 2
            })
        client.delete(url)


class ProtocolViewTests(BRPTestCase):

    def test_retrieve_all_protocols(self):
        '''
        Ensure we can retrieve all protocols
        '''
        view = ProtocolViewSet.as_view({'get': 'list'})
        url = reverse('protocol-list')
        request = factory.get(url)
        force_authenticate(request, user=self.test_user)
        response = view(request)
        response.render()
        self.assertEqual(response.status_code, 200)
        self.assertEqual(len(response.data), 1)
        self.assertEqual(response.data[0]['name'], 'Demonstration Protocol')

    def test_retrieve_pds_from_protocol(self):
        '''
        Ensure we can retrieve all ProtocolDataSources associated with a
        Protocol
        '''
        view = ProtocolDataSourceView.as_view()
        url = reverse(
            'protocol-datasources-list',
            kwargs={'pk': self.test_protocol.id})
        request = factory.get(url)
        force_authenticate(request, user=self.test_user)
        response = view(request, pk=self.test_protocol.id)
        response.render()
        self.assertEqual(response.status_code, 200)
        self.assertEqual(len(response.data), 3)

    def test_retrieve_pds_from_protocol_badpk(self):
        '''
        Ensure we get a proper error code when trying to retrieve
        ProtocolDatasources associated with a non-existent Protocol
        '''
        view = ProtocolDataSourceView.as_view()
        url = reverse(
            'protocol-datasources-list',
            kwargs={'pk': 99})
        request = factory.get(url)
        force_authenticate(request, user=self.test_user)
        response = view(request, pk=99)
        response.render()
        self.assertEqual(response.status_code, 404)
        self.assertEqual(response.data['error'], 'Protocol requested not found')

    def test_retrieve_orgs_from_protocol(self):
        '''
        Ensure we can retrieve all Organizations associated with a
        Protocol
        '''
        view = ProtocolOrganizationView.as_view()
        url = reverse(
            'protocol-organization-list',
            kwargs={'pk': self.test_protocol.id})
        request = factory.get(url)
        force_authenticate(request, user=self.test_user)
        response = view(request, pk=self.test_protocol.id)
        response.render()
        self.assertEqual(response.status_code, 200)
        self.assertEqual(len(response.data), 1)

    def test_retrieve_orgs_from_protocol_badpk(self):
        '''
        Ensure we get a proper error code when trying to retrieve
        Organizations associated with a non-existent Protocol
        '''
        view = ProtocolOrganizationView.as_view()
        url = reverse(
            'protocol-organization-list',
            kwargs={'pk': 99})
        request = factory.get(url)
        force_authenticate(request, user=self.test_user)
        response = view(request, pk=99)
        response.render()
        self.assertEqual(response.status_code, 404)
        self.assertEqual(response.data['error'], 'Protocol requested not found')

    def test_retrieve_subjects_from_protocol(self):
        '''
        Ensure we can retrieve all Subjects associated with a Protocol
        '''
        view = ProtocolSubjectsView.as_view()
        url = reverse(
            'protocol-subject-list',
            kwargs={'pk': self.test_protocol.id})
        request = factory.get(url)
        force_authenticate(request, user=self.test_user)
        response = view(request, pk=self.test_protocol.id)
        response.render()
        self.assertEqual(response.status_code, 200)
        self.assertEqual(len(response.data), 1)

    def test_retrieve_subjects_from_protocol_badpk(self):
        '''
        Ensure we get a proper error code when trying to retrieve
        Subjects associated with a Protocol with a non-existent Protocol
        '''
        view = ProtocolSubjectsView.as_view()
        url = reverse(
            'protocol-subject-list',
            kwargs={'pk': 99})
        request = factory.get(url)
        force_authenticate(request, user=self.test_user)
        response = view(request, pk=99)
        response.render()
        self.assertEqual(response.status_code, 404)
        self.assertEqual(response.data['error'], 'Protocol requested not found')

    def test_create_and_delete_subject_on_protocol(self):
        '''
        Ensure that we can create a subject on a Protocol
        '''
        subject = {
            'first_name': 'John',
            'last_name': 'Doe',
            'organization_subject_id': '1234',
            'organization': '1',
            'dob': '2000-01-01'
        }
        url = reverse(
            'protocol-subject-create',
            kwargs={
                'pk': self.test_protocol.id
            })
        token = Token.objects.get(user__username='admin')
        client = APIClient()
        client.credentials(HTTP_AUTHORIZATION='Token ' + token.key)
        response = client.post(url, subject, format='json')
        success, subject, errors = response.data
        if errors:
            print errors
        self.assertTrue(success)
        self.assertEqual(len(errors), 0)
        url = reverse(
            'protocol-subject-view',
            kwargs={
                'pk': self.test_protocol.id,
                'subject': subject['id']
            })
        response = client.delete(url)

    def test_update_subject_on_protocol(self):
        '''
        Ensure that we can create a subject on a Protocol
        '''
        subject = {
            'id': 1,
            'first_name': 'Johnny',
            'last_name': 'Sample',
            'organization_subject_id': '42424242',
            'organization_id': '1',
            'dob': '2000-01-01'
        }
        url = reverse(
            'protocol-subject-view',
            kwargs={
                'pk': self.test_protocol.id,
                'subject': self.test_user.id
            })
        token = Token.objects.get(user__username='admin')
        client = APIClient()
        client.credentials(HTTP_AUTHORIZATION='Token ' + token.key)<commit_msg>Update tests<commit_after>from django.core.urlresolvers import reverse
from django.contrib.auth.models import User

from rest_framework.test import APIRequestFactory, APITestCase, \
    force_authenticate, APIClient
from rest_framework.authtoken.models import Token

from ..models.protocols import Protocol, ProtocolDataSource
from ..views.protocol import ProtocolViewSet, ProtocolDataSourceView, \
    ProtocolOrganizationView, ProtocolSubjectsView, ProtocolSubjectDetailView
from ..views import PDSViewSet, PDSSubjectView, PDSSubjectRecordsView, \
    PDSSubjectRecordDetailView, PDSRecordLinkDetailView, PDSAvailableLinksView

factory = APIRequestFactory()


class BRPTestCase(APITestCase):

    fixtures = ['demo_data']

    def setUp(self):
        self.test_user = User.objects.get(username='admin')
        self.test_protocol = Protocol.objects.get(pk=1)
        self.test_pds = ProtocolDataSource.objects.get(pk=1)

    def cleanUp(self):
        token = Token.objects.get(user__username='admin')
        client = APIClient()
        client.credentials(HTTP_AUTHORIZATION='Token ' + token.key)
        url = reverse(
            'protocol-subject-view',
            kwargs={
                'pk': self.test_protocol.id,
                'subject': 2
            })
        client.delete(url)


class ProtocolViewTests(BRPTestCase):

    def test_retrieve_all_protocols(self):
        '''
        Ensure we can retrieve all protocols
        '''
        view = ProtocolViewSet.as_view({'get': 'list'})
        url = reverse('protocol-list')
        request = factory.get(url)
        force_authenticate(request, user=self.test_user)
        response = view(request)
        response.render()
        self.assertEqual(response.status_code, 200)
        self.assertEqual(len(response.data), 1)
        self.assertEqual(response.data[0]['name'], 'Demonstration Protocol')

    def test_retrieve_pds_from_protocol(self):
        '''
        Ensure we can retrieve all ProtocolDataSources associated with a
        Protocol
        '''
        view = ProtocolDataSourceView.as_view()
        url = reverse(
            'protocol-datasources-list',
            kwargs={'pk': self.test_protocol.id})
        request = factory.get(url)
        force_authenticate(request, user=self.test_user)
        response = view(request, pk=self.test_protocol.id)
        response.render()
        self.assertEqual(response.status_code, 200)
        self.assertEqual(len(response.data), 3)

    def test_retrieve_pds_from_protocol_badpk(self):
        '''
        Ensure we get a proper error code when trying to retrieve
        ProtocolDatasources associated with a non-existent Protocol
        '''
        view = ProtocolDataSourceView.as_view()
        url = reverse(
            'protocol-datasources-list',
            kwargs={'pk': 99})
        request = factory.get(url)
        force_authenticate(request, user=self.test_user)
        response = view(request, pk=99)
        response.render()
        self.assertEqual(response.status_code, 404)
        self.assertEqual(response.data['error'], 'Protocol requested not found')

    def test_retrieve_orgs_from_protocol(self):
        '''
        Ensure we can retrieve all Organizations associated with a
        Protocol
        '''
        view = ProtocolOrganizationView.as_view()
        url = reverse(
            'protocol-organization-list',
            kwargs={'pk': self.test_protocol.id})
        request = factory.get(url)
        force_authenticate(request, user=self.test_user)
        response = view(request, pk=self.test_protocol.id)
        response.render()
        self.assertEqual(response.status_code, 200)
        self.assertEqual(len(response.data), 1)

    def test_retrieve_orgs_from_protocol_badpk(self):
        '''
        Ensure we get a proper error code when trying to retrieve
        Organizations associated with a non-existent Protocol
        '''
        view = ProtocolOrganizationView.as_view()
        url = reverse(
            'protocol-organization-list',
            kwargs={'pk': 99})
        request = factory.get(url)
        force_authenticate(request, user=self.test_user)
        response = view(request, pk=99)
        response.render()
        self.assertEqual(response.status_code, 404)
        self.assertEqual(response.data['error'], 'Protocol requested not found')

    def test_retrieve_subjects_from_protocol(self):
        '''
        Ensure we can retrieve all Subjects associated with a Protocol
        '''
        view = ProtocolSubjectsView.as_view()
        url = reverse(
            'protocol-subject-list',
            kwargs={'pk': self.test_protocol.id})
        request = factory.get(url)
        force_authenticate(request, user=self.test_user)
        response = view(request, pk=self.test_protocol.id)
        response.render()
        self.assertEqual(response.status_code, 200)
        self.assertEqual(len(response.data), 1)

    def test_retrieve_subjects_from_protocol_badpk(self):
        '''
        Ensure we get a proper error code when trying to retrieve
        Subjects associated with a Protocol with a non-existent Protocol
        '''
        view = ProtocolSubjectsView.as_view()
        url = reverse(
            'protocol-subject-list',
            kwargs={'pk': 99})
        request = factory.get(url)
        force_authenticate(request, user=self.test_user)
        response = view(request, pk=99)
        response.render()
        self.assertEqual(response.status_code, 404)
        self.assertEqual(response.data['error'], 'Protocol requested not found')

    def test_create_and_delete_subject_on_protocol(self):
        '''
        Ensure that we can create a subject on a Protocol
        '''
        subject = {
            'first_name': 'John',
            'last_name': 'Doe',
            'organization_subject_id': '1234',
            'organization': '1',
            'dob': '2000-01-01'
        }
        url = reverse(
            'protocol-subject-create',
            kwargs={
                'pk': self.test_protocol.id
            })
        token = Token.objects.get(user__username='admin')
        client = APIClient()
        client.credentials(HTTP_AUTHORIZATION='Token ' + token.key)
        response = client.post(url, subject, format='json')
        success, subject, errors = response.data
        if errors:
            print errors
        self.assertTrue(success)
        self.assertEqual(len(errors), 0)
        url = reverse(
            'protocol-subject-view',
            kwargs={
                'pk': self.test_protocol.id,
                'subject': subject['id']
            })
        response = client.delete(url)

    def test_update_subject_on_protocol(self):
        '''
        Ensure that we can create a subject on a Protocol
        '''
        subject = {
            'id': 1,
            'first_name': 'Johnny',
            'last_name': 'Sample',
            'organization_subject_id': '42424242',
            'organization': '1',
            'dob': '2000-01-01'
        }
        url = reverse(
            'protocol-subject-view',
            kwargs={
                'pk': self.test_protocol.id,
                'subject': self.test_user.id
            })
        token = Token.objects.get(user__username='admin')
        client = APIClient()
        client.credentials(HTTP_AUTHORIZATION='Token ' + token.key)
{'fraction_non_alphanumeric': 0.10352750371671847, 'fraction_numerical': 0.009866198134883092, 'mean_word_length': 2.3527412777526053, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '4002060', 'n_tokens_mistral': 4146, 'n_tokens_neox': 4016, 'n_words': 838}