<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-

import unittest

from spam.preprocess import PreProcess
from spam.common import params


class TestPreProcess(unittest.TestCase):
    """
    Class for testing the preprocces.
    """
    def setUp(self):
        self.preprocess = PreProcess(
            params.DATASET_PATH,
            params.DATASET_SUBDIRS,
        )

    def tearDown(self):
        pass

    def test_preprocess_instance(self):
        """
        Test if preprocess is creating a instance.
        """
        self.assertIsInstance(self.preprocess, PreProcess)

    def test_preprocess_open_email(self):
        """
        Test if preprocess can open email from the dataset.
        """
        pass

    def test_preprocess_read_email(self):
        """
        Test if preprocess can read email from the dataset.
        """
        pass

    def test_preprocess_regex_email(self):
        """<commit_msg>Update setup function for test preprocess.<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

import unittest
import os
import shutil
from datetime import datetime

from spam.preprocess import PreProcess
from spam.common import params


class TestPreProcess(unittest.TestCase):
    """
    Class for testing the preprocces.
    """
    def setUp(self):
        self.preprocess = PreProcess(
            params.DATASET_PATH,
            params.DATASET_SUBDIRS,
        )

        self.dataset_path = os.path.join(
            os.path.dirname(__file__),
            'test_dataset',
        )
        self.dataset_subdirs = [
            {
                'name': 'enron1',
                'total_count': 5,
                'ham_count': 3,
                'spam_count': 2,
                'path': os.path.join(self.dataset_path, 'enron1'),
                'ham_path': os.path.join(
                    self.dataset_path,
                    'enron1',
                    'ham'
                ),
                'spam_path': os.path.join(
                    self.dataset_path,
                    'enron1',
                    'spam'
                ),
            },
            {
                'name': 'enron2',
                'total_count': 6,
                'ham_count': 2,
                'spam_count': 4,
                'path': os.path.join(self.dataset_path, 'enron2'),
                'ham_path': os.path.join(
                    self.dataset_path,
                    'enron2',
                    'ham'
                ),
                'spam_path': os.path.join(
                    self.dataset_path,
                    'enron2',
                    'spam'
                ),
            },
        ]

        # create test dataset dir
        self.mkdir(self.dataset_path)

        # get current date YYYY-MM-DD
        current_date = datetime.now().strftime('%Y-%m-%d')

        # create subdirs and email create files
        for subdirs in self.dataset_subdirs:
            # create subdirs
            self.mkdir(subdirs['path'])
            self.mkdir(subdirs['ham_path'])
            self.mkdir(subdirs['spam_path'])

            # create spam email files
            for id in range(1, subdirs['spam_count'] + 1):
                self.mkfile(os.path.join(
                    subdirs['spam_path'],
                    '{}.{}.TEST.spam.txt'.format(id, current_date)
                ))

            # create ham email files
            for id in range(1, subdirs['ham_count'] + 1):
                self.mkfile(os.path.join(
                    subdirs['ham_path'],
                    '{}.{}.TEST.ham.txt'.format(id, current_date)
                ))

    def tearDown(self):
        self.preprocess = None

        # remove test dataset if it does exist.
        if os.path.exists(self.dataset_path):
            shutil.rmtree(self.dataset_path)

    def mkdir(self, dir):
        """
        Check if the directory exist and create if it doesn't.
        """
        if not os.path.exists(dir):
            os.mkdir(dir)

    def mkfile(self, file_path):
        """
        Check if the file exist and create if it doesn't.
        """
        if not os.path.isfile(file_path):
            file = open(file_path, 'w')
            file.close()

    def test_preprocess_instance(self):
        """
        Test if preprocess is creating a instance.
        """
        self.assertIsInstance(self.preprocess, PreProcess)

    def test_preprocess_open_email(self):
        """
        Test if preprocess can open email from the dataset.
        """
        pass

    def test_preprocess_read_email(self):
        """
        Test if preprocess can read email from the dataset.
        """
        pass

    def test_preprocess_regex_email(self):
        """
{'fraction_non_alphanumeric': 0.1043956043956044, 'fraction_numerical': 0.00422654268808115, 'mean_word_length': 1.4638209266007287, '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': '2242526', 'n_tokens_mistral': 1390, 'n_tokens_neox': 1305, 'n_words': 296}