<commit_before>import pytest

from django.http import Http404

from accession import admin_views

from .factories import DonorFactory, AccessionFactory

pytestmark = pytest.mark.django_db


class TestDuplicates():

    def test_returns_200_with_mapped_model(self, rf, admin_user):
        request = rf.get('/admin/accession/donor/duplicates/')
        request.user = admin_user

        response = admin_views.duplicates(request, 'donor')

        assert response.status_code == 200

    def test_returns_200_with_unmapped_model(self, rf, admin_user):
        request = rf.get('/admin/accession/city/duplicates/')
        request.user = admin_user

        response = admin_views.duplicates(request, 'city')

        assert response.status_code == 200

    @pytest.mark.xfail(reason='The view does not catch the exception raised'
                              'when the model does not exist')
    def test_returns_404_when_model_does_not_exist(self, rf, admin_user):
        request = rf.get('/admin/accession/donor/duplicates/')
        request.user = admin_user

        response = admin_views.duplicates(request, 'dne')

        assert response.status_code == 404

    def test_template_used(self, admin_client):
        response = admin_client.get('/admin/accession/donor/duplicates/')
        assert response.templates[0].name == 'admin/accession/duplicates.html'

    def test_with_duplicates(self, rf, admin_user):
        # Non-duplicate entry
        DonorFactory(last_name='Libman')
        # Duplicate entries
        DonorFactory.create_batch(4, last_name='Smith')

        request = rf.get('/admin/accession/donor/duplicates/')
        request.user = admin_user

        response = admin_views.duplicates(request, 'donor')

        assert 'Libman' not in response.content
        assert 'Smith' in response.content

    def test_without_duplicates(self, rf, admin_user):
        DonorFactory.create_batch(5)

        request = rf.get('/admin/accession/donor/duplicates/')
        request.user = admin_user

        response = admin_views.duplicates(request, 'donor')

        assert 'There are no duplicate entries' in response.content


class TestPrintView():

    def test_returns_200(self, rf, admin_user):
        DonorFactory()

        request = rf.get('/admin/accession/donor/1/print/')
        request.user = admin_user

        response = admin_views.print_view(request, 'accession', 'donor', 1)

        assert response.status_code == 200

    def test_raises_404_when_object_id_not_found(self, rf, admin_user):
        request = rf.get('/admin/accession/donor/8/print/')
        request.user = admin_user

        with pytest.raises(Http404):
            admin_views.print_view(request, 'accession', 'donor', 8)

    def test_correct_info_printed(self, rf, admin_user):
        accession = AccessionFactory()

        request = rf.get('/admin/accession/accession/1/print/')
        request.user = admin_user

        response = admin_views.print_view(request, 'accession', 'accession', 1)

        # Check a couple fields to make sure they're there.
        assert accession.description in response.content
        assert accession.acquisition_method in response.content
        # Make sure the related items are there too.
        assert str(accession.donor) in response.content


def test_query_to_tuple():
    DonorFactory.create_batch(3, first_name='Bob', last_name='Tucker')

    query = """SELECT first_name, last_name from accession_donor"""
    expected = [('Bob', 'Tucker'), ('Bob', 'Tucker'), ('Bob', 'Tucker')]
<commit_msg>Removed URIs from test requests.<commit_after>import pytest

from django.http import Http404

from accession import admin_views

from .factories import DonorFactory, AccessionFactory

pytestmark = pytest.mark.django_db


class TestDuplicates():

    def test_returns_200_with_mapped_model(self, rf, admin_user):
        request = rf.get('/')
        request.user = admin_user

        response = admin_views.duplicates(request, 'donor')

        assert response.status_code == 200

    def test_returns_200_with_unmapped_model(self, rf, admin_user):
        request = rf.get('/')
        request.user = admin_user

        response = admin_views.duplicates(request, 'city')

        assert response.status_code == 200

    @pytest.mark.xfail(reason='The view does not catch the exception raised'
                              'when the model does not exist')
    def test_returns_404_when_model_does_not_exist(self, rf, admin_user):
        request = rf.get('/')
        request.user = admin_user

        response = admin_views.duplicates(request, 'dne')

        assert response.status_code == 404

    def test_template_used(self, admin_client):
        response = admin_client.get('/admin/accession/donor/duplicates/')
        assert response.templates[0].name == 'admin/accession/duplicates.html'

    def test_with_duplicates(self, rf, admin_user):
        # Non-duplicate entry
        DonorFactory(last_name='Libman')
        # Duplicate entries
        DonorFactory.create_batch(4, last_name='Smith')

        request = rf.get('/')
        request.user = admin_user

        response = admin_views.duplicates(request, 'donor')

        assert 'Libman' not in response.content
        assert 'Smith' in response.content

    def test_without_duplicates(self, rf, admin_user):
        DonorFactory.create_batch(5)

        request = rf.get('/')
        request.user = admin_user

        response = admin_views.duplicates(request, 'donor')

        assert 'There are no duplicate entries' in response.content


class TestPrintView():

    def test_returns_200(self, rf, admin_user):
        DonorFactory()

        request = rf.get('/')
        request.user = admin_user

        response = admin_views.print_view(request, 'accession', 'donor', 1)

        assert response.status_code == 200

    def test_raises_404_when_object_id_not_found(self, rf, admin_user):
        request = rf.get('/')
        request.user = admin_user

        with pytest.raises(Http404):
            admin_views.print_view(request, 'accession', 'donor', 8)

    def test_correct_info_printed(self, rf, admin_user):
        accession = AccessionFactory()

        request = rf.get('/')
        request.user = admin_user

        response = admin_views.print_view(request, 'accession', 'accession', 1)

        # Check a couple fields to make sure they're there.
        assert accession.description in response.content
        assert accession.acquisition_method in response.content
        # Make sure the related items are there too.
        assert str(accession.donor) in response.content


def test_query_to_tuple():
    DonorFactory.create_batch(3, first_name='Bob', last_name='Tucker')

    query = """SELECT first_name, last_name from accession_donor"""
    expected = [('Bob', 'Tucker'), ('Bob', 'Tucker'), ('Bob', 'Tucker')]

{'fraction_non_alphanumeric': 0.10382434200959721, 'fraction_numerical': 0.012069216228006397, 'mean_word_length': 3.0722320899940794, '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': '326770', 'n_tokens_mistral': 2189, 'n_tokens_neox': 2049, 'n_words': 454}