<commit_before># -*- coding: utf-8 -*-
"""
TailorDev Bibliography

Test commands.
"""
import os.path
import pytest

from django.core.management.base import CommandError
from django.test import TestCase

from ..management.commands import bibtex_import
from ..models import Author, Entry, Journal


@pytest.mark.django_db
class BibTexImportCommandTests(TestCase):
    """
    Tests for the bibtex_import admin command
    """
    def setUp(self):
        """
        Set object level vars
        """
        self.bibtex_file = os.path.abspath(
            os.path.dirname(__file__) + '/fixtures/biblio.bib'
        )
        self.cmd = bibtex_import.Command()

    def test_command(self):
        """
        Test python manage.py bibtex_import biblio.bib
        """
        # Without an input file to handle, cmd shoud assert a CommandError
        with self.assertRaises(CommandError):
            self.cmd.handle()

        # Execute the command
        self.cmd.handle(self.bibtex_file)

        # How many entries did we successfully import?
        self.assertEqual(Entry.objects.count(), 9)

        # How many journals?
        self.assertEqual(Journal.objects.count(), 5)

        # How many authors?
        self.assertEqual(Author.objects.count(), 31)

    def _test_entry_authors(self, entry, expected_authors):
        for rank, author in enumerate(entry.get_authors()):
            self.assertEqual(
                str(author),
                expected_authors[rank]
            )

    def test_author_rank(self):
        """
        Test if author rank is respected
        """
        # Execute the command
        self.cmd.handle(self.bibtex_file)

        # Case 1
        entry = Entry.objects.get(
            title='Mobyle: a new full web bioinformatics framework'
        )
        expected_authors = [
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
        ]
        self._test_entry_authors(entry, expected_authors)

        # Case 2
        entry = Entry.objects.get(title__startswith='fpocket')
        expected_authors = [
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>'
        ]
        self._test_entry_authors(entry, expected_authors)

    def test_partial_publication_date(self):
        """Test if partial publication date flag"""
        # Execute the command
        self.cmd.handle(self.bibtex_file)

        qs = Entry.objects.filter(is_partial_publication_date=False)
        self.assertEqual(qs.count(), 1)

        qs = Entry.objects.filter(is_partial_publication_date=True)
        self.assertEqual(qs.count(), 8)
<commit_msg>Fix author repr for py2/3 compat<commit_after># -*- coding: utf-8 -*-
"""
TailorDev Bibliography

Test commands.
"""
from __future__ import unicode_literals

import os.path
import pytest

from django.core.management.base import CommandError
from django.test import TestCase

from ..management.commands import bibtex_import
from ..models import Author, Entry, Journal


@pytest.mark.django_db
class BibTexImportCommandTests(TestCase):
    """
    Tests for the bibtex_import admin command
    """
    def setUp(self):
        """
        Set object level vars
        """
        self.bibtex_file = os.path.abspath(
            os.path.dirname(__file__) + '/fixtures/biblio.bib'
        )
        self.cmd = bibtex_import.Command()

    def test_command(self):
        """
        Test python manage.py bibtex_import biblio.bib
        """
        # Without an input file to handle, cmd shoud assert a CommandError
        with self.assertRaises(CommandError):
            self.cmd.handle()

        # Execute the command
        self.cmd.handle(self.bibtex_file)

        # How many entries did we successfully import?
        self.assertEqual(Entry.objects.count(), 9)

        # How many journals?
        self.assertEqual(Journal.objects.count(), 5)

        # How many authors?
        self.assertEqual(Author.objects.count(), 31)

    def _test_entry_authors(self, entry, expected_authors):
        for rank, author in enumerate(entry.get_authors()):
            self.assertEqual(
                author.get_formatted_name(),
                expected_authors[rank]
            )

    def test_author_rank(self):
        """
        Test if author rank is respected
        """
        # Execute the command
        self.cmd.handle(self.bibtex_file)

        # Case 1
        entry = Entry.objects.get(
            title='Mobyle: a new full web bioinformatics framework'
        )
        expected_authors = [
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
        ]
        self._test_entry_authors(entry, expected_authors)

        # Case 2
        entry = Entry.objects.get(title__startswith='fpocket')
        expected_authors = [
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>'
        ]
        self._test_entry_authors(entry, expected_authors)

    def test_partial_publication_date(self):
        """Test if partial publication date flag"""
        # Execute the command
        self.cmd.handle(self.bibtex_file)

        qs = Entry.objects.filter(is_partial_publication_date=False)
        self.assertEqual(qs.count(), 1)

        qs = Entry.objects.filter(is_partial_publication_date=True)
        self.assertEqual(qs.count(), 8)

{'fraction_non_alphanumeric': 0.10872675250357654, 'fraction_numerical': 0.00357653791130186, 'mean_word_length': 2.0281537628586896, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, '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': '7298683', 'n_tokens_mistral': 1617, 'n_tokens_neox': 1564, 'n_words': 353}