<commit_before>"""Tests for the 'ihate' plugin"""
import os
import shutil

from _common import unittest
from beets import importer, config
from beets.library import Item
from beets.mediafile import MediaFile
from beetsplug.ihate import IHatePlugin
from test import _common
from test.helper import capture_log
from test.test_importer import ImportHelper


class IHatePluginTest(_common.TestCase, ImportHelper):
    def setUp(self):
        super(IHatePluginTest, self).setUp()
        self.setup_beets()
        self.__create_import_dir(2)
        self._setup_import_session()
        config['import']['pretend'] = True

        self.all_paths = [self.artist_paths[0], self.artist_paths[1],
                          self.album_paths[0], self.album_paths[1],
                          self.misc_paths[0], self.misc_paths[1]]

    def tearDown(self):
        self.teardown_beets()

    def __copy_file(self, dest_path, metadata):
        # Copy files
        resource_path = os.path.join(_common.RSRC, 'full.mp3')
        shutil.copy(resource_path, dest_path)
        medium = MediaFile(dest_path)
        # Set metadata
        for attr in metadata:
            setattr(medium, attr, metadata[attr])
        medium.save()

    def __create_import_dir(self, count):
        self.import_dir = os.path.join(self.temp_dir, 'testsrcdir')
        if os.path.isdir(self.import_dir):
            shutil.rmtree(self.import_dir)

        artist_path = os.path.join(self.import_dir, 'artist')
        album_path = os.path.join(artist_path, 'album')
        misc_path = os.path.join(self.import_dir, 'misc')
        os.makedirs(album_path)
        os.makedirs(misc_path)

        metadata = {
            'artist': 'Tag Artist',
            'album':  'Tag Album',
            'albumartist':  None,
            'mb_trackid': None,
            'mb_albumid': None,
            'comp': None
        }
        self.album_paths = []
        for i in range(count):
            metadata['track'] = i + 1
            metadata['title'] = 'Tag Title Album %d' % (i + 1)
            dest_path = os.path.join(album_path, '%02d - track.mp3' % (i + 1))
            self.__copy_file(dest_path, metadata)
            self.album_paths.append(dest_path)

        self.artist_paths = []
        metadata['album'] = None
        for i in range(count):
            metadata['track'] = i + 10
            metadata['title'] = 'Tag Title Artist %d' % (i + 1)
            dest_path = os.path.join(artist_path, 'track_%d.mp3' % (i + 1))
            self.__copy_file(dest_path, metadata)
            self.artist_paths.append(dest_path)

        self.misc_paths = []
        for i in range(count):
            metadata['artist'] = 'Artist %d' % (i + 42)
            metadata['track'] = i + 5
            metadata['title'] = 'Tag Title Misc %d' % (i + 1)
            dest_path = os.path.join(misc_path, 'track_%d.mp3' % (i + 1))
            self.__copy_file(dest_path, metadata)
            self.misc_paths.append(dest_path)

    def __run(self, expected_lines, singletons=False):
        import beetsplug
        path = beetsplug.__path__
        print path
        self.load_plugins('ihate')

        import_files = [self.import_dir]
        self._setup_import_session(singletons=singletons)
        self.importer.paths = import_files

        with capture_log() as logs:
            self.importer.run()
        self.unload_plugins()
        IHatePlugin.listeners = None

        logs = [line for line in logs if not line.startswith('Sending event:')]

        self.assertEqual(logs, expected_lines)

    def __reset_config(self):
        config['ihate'] = {}

    def test_hate(self):

        match_pattern = {}
        test_item = Item(
            genre='TestGenre',
            album=u'TestAlbum',
            artist=u'TestArtist')
        task = importer.SingletonImportTask(None, test_item)<commit_msg>Removed some testing code which was checked in accidentally<commit_after>"""Tests for the 'ihate' plugin"""
import os
import shutil

from _common import unittest
from beets import importer, config
from beets.library import Item
from beets.mediafile import MediaFile
from beetsplug.ihate import IHatePlugin
from test import _common
from test.helper import capture_log
from test.test_importer import ImportHelper


class IHatePluginTest(_common.TestCase, ImportHelper):
    def setUp(self):
        super(IHatePluginTest, self).setUp()
        self.setup_beets()
        self.__create_import_dir(2)
        self._setup_import_session()
        config['import']['pretend'] = True

        self.all_paths = [self.artist_paths[0], self.artist_paths[1],
                          self.album_paths[0], self.album_paths[1],
                          self.misc_paths[0], self.misc_paths[1]]

    def tearDown(self):
        self.teardown_beets()

    def __copy_file(self, dest_path, metadata):
        # Copy files
        resource_path = os.path.join(_common.RSRC, 'full.mp3')
        shutil.copy(resource_path, dest_path)
        medium = MediaFile(dest_path)
        # Set metadata
        for attr in metadata:
            setattr(medium, attr, metadata[attr])
        medium.save()

    def __create_import_dir(self, count):
        self.import_dir = os.path.join(self.temp_dir, 'testsrcdir')
        if os.path.isdir(self.import_dir):
            shutil.rmtree(self.import_dir)

        artist_path = os.path.join(self.import_dir, 'artist')
        album_path = os.path.join(artist_path, 'album')
        misc_path = os.path.join(self.import_dir, 'misc')
        os.makedirs(album_path)
        os.makedirs(misc_path)

        metadata = {
            'artist': 'Tag Artist',
            'album':  'Tag Album',
            'albumartist':  None,
            'mb_trackid': None,
            'mb_albumid': None,
            'comp': None
        }
        self.album_paths = []
        for i in range(count):
            metadata['track'] = i + 1
            metadata['title'] = 'Tag Title Album %d' % (i + 1)
            dest_path = os.path.join(album_path, '%02d - track.mp3' % (i + 1))
            self.__copy_file(dest_path, metadata)
            self.album_paths.append(dest_path)

        self.artist_paths = []
        metadata['album'] = None
        for i in range(count):
            metadata['track'] = i + 10
            metadata['title'] = 'Tag Title Artist %d' % (i + 1)
            dest_path = os.path.join(artist_path, 'track_%d.mp3' % (i + 1))
            self.__copy_file(dest_path, metadata)
            self.artist_paths.append(dest_path)

        self.misc_paths = []
        for i in range(count):
            metadata['artist'] = 'Artist %d' % (i + 42)
            metadata['track'] = i + 5
            metadata['title'] = 'Tag Title Misc %d' % (i + 1)
            dest_path = os.path.join(misc_path, 'track_%d.mp3' % (i + 1))
            self.__copy_file(dest_path, metadata)
            self.misc_paths.append(dest_path)

    def __run(self, expected_lines, singletons=False):
        self.load_plugins('ihate')

        import_files = [self.import_dir]
        self._setup_import_session(singletons=singletons)
        self.importer.paths = import_files

        with capture_log() as logs:
            self.importer.run()
        self.unload_plugins()
        IHatePlugin.listeners = None

        logs = [line for line in logs if not line.startswith('Sending event:')]

        self.assertEqual(logs, expected_lines)

    def __reset_config(self):
        config['ihate'] = {}

    def test_hate(self):

        match_pattern = {}
        test_item = Item(
            genre='TestGenre',
            album=u'TestAlbum',
            artist=u'TestArtist')
        task = importer.SingletonImportTask(None, test_item)
{'fraction_non_alphanumeric': 0.11526479750778816, 'fraction_numerical': 0.006490134994807892, 'mean_word_length': 2.2689859991514636, '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': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3904443', 'n_tokens_mistral': 2456, 'n_tokens_neox': 2374, 'n_words': 499}