<commit_before>"""Unit tests for the utils helper functions."""

import hashlib
import mock
import os
import shutil
import subprocess
import tempfile
import unittest
import time
import couchdb
from collections import defaultdict
from taca.utils import misc, filesystem, transfer, config, bioinfo_tab, statusdb
from six.moves import map


class TestMisc(unittest.TestCase):
    """Test class for the misc functions."""

    @classmethod
    def setUpClass(self):
        self.rootdir = tempfile.mkdtemp(prefix='test_taca_misc')
        self.hashfile = os.path.join(self.rootdir, 'test_hashfile')
        with open(self.hashfile, 'w') as fh:
            fh.write('This is some contents\n')
        self.hashfile_digests = {
            'SHA256':
                '4f075ae76b480bb0200dab01cd304f4045e04cd2b73e88b89549e5ac1627f222',
            'MD5':
                'c8498fc299bc3e22690045f1b62ce4e9',
            'SHA1':
                '098fb272dfdae2ea1ba57c795dd325fa70e3c3fb'}

    @classmethod
    def tearDownClass(self):
        shutil.rmtree(self.rootdir)

    def test_hashfile(self):
        """Test different hashing algorithms."""
        for alg, obj in self.hashfile_digests.items():
            self.assertEqual(misc.hashfile(self.hashfile, hasher=alg), obj)

    def test_hashfile_dir(self):
        """Hash digest for a directory should be None."""
        assert misc.hashfile(self.rootdir) is None

    def test_multiple_hashfile_calls(self):
        """Ensure that the hasher object is cleared between subsequent calls."""
        assert misc.hashfile(self.hashfile, hasher='sha1') == misc.hashfile(self.hashfile, 'sha1')

    @mock.patch('taca.utils.misc.smtplib.SMTP')
    def test_send_mail(self, mock_smtplib):
        """Test send email."""
        mock_smtplib.sendmail()
        mock_smtplib.quit()
        assert misc.send_mail('subject', 'content', 'receiver') is None
        with self.assertRaises(SystemExit):
            misc.send_mail('subject', 'content', None)
<commit_msg>Touch-ups of the email test function<commit_after>"""Unit tests for the utils helper functions."""

import hashlib
import mock
import os
import shutil
import subprocess
import tempfile
import unittest
import time
import couchdb
from collections import defaultdict
from taca.utils import misc, filesystem, transfer, config, bioinfo_tab, statusdb
from six.moves import map


class TestMisc(unittest.TestCase):
    """Test class for the misc functions."""

    @classmethod
    def setUpClass(self):
        self.rootdir = tempfile.mkdtemp(prefix='test_taca_misc')
        self.hashfile = os.path.join(self.rootdir, 'test_hashfile')
        with open(self.hashfile, 'w') as fh:
            fh.write('This is some contents\n')
        self.hashfile_digests = {
            'SHA256':
                '4f075ae76b480bb0200dab01cd304f4045e04cd2b73e88b89549e5ac1627f222',
            'MD5':
                'c8498fc299bc3e22690045f1b62ce4e9',
            'SHA1':
                '098fb272dfdae2ea1ba57c795dd325fa70e3c3fb'}

    @classmethod
    def tearDownClass(self):
        shutil.rmtree(self.rootdir)

    def test_hashfile(self):
        """Test different hashing algorithms."""
        for alg, obj in self.hashfile_digests.items():
            self.assertEqual(misc.hashfile(self.hashfile, hasher=alg), obj)

    def test_hashfile_dir(self):
        """Hash digest for a directory should be None."""
        assert misc.hashfile(self.rootdir) is None

    def test_multiple_hashfile_calls(self):
        """Ensure that the hasher object is cleared between subsequent calls."""
        assert misc.hashfile(self.hashfile, hasher='sha1') == misc.hashfile(self.hashfile, 'sha1')

    @mock.patch('taca.utils.misc.smtplib.SMTP')
    def test_send_mail(self, mock_smtplib):
        """Test send email."""
        assert misc.send_mail('subject', 'content', 'receiver') is None
        mock_smtplib.assert_called_with('localhost')
        mock_smtplib().sendmail.assert_called_with('TACA', ['receiver'], mock.ANY)
        with self.assertRaises(SystemExit):
            misc.send_mail('subject', 'content', None)

{'fraction_non_alphanumeric': 0.10576221735959154, 'fraction_numerical': 0.0437636761487965, 'mean_word_length': 3.1766497461928935, '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': '1262718', 'n_tokens_mistral': 1415, 'n_tokens_neox': 1288, 'n_words': 293}