<commit_before>import unittest
import sqlalchemy as sa
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
from tikplay.database import interface
from datetime import datetime


SHA1 = 'a' * 40


class DBInterfaceTestCase(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        cls.engine = create_engine('sqlite:///:memory:', echo=True)
        _base = declarative_base()

        class _Song(_base):
            __tablename__ = 'test_songs'
            song_hash = sa.Column(sa.String(40), primary_key=True)
            filename = sa.Column(sa.Text, nullable=False)
            play_count = sa.Column(sa.Integer, nullable=False)
            artist = sa.Column(sa.Text, nullable=True)
            title = sa.Column(sa.Text, nullable=True)
            length = sa.Column(sa.Integer, nullable=True)
            last_played = sa.Column(sa.DateTime, nullable=True)
            date_added = sa.Column(sa.DateTime, nullable=True)

        cls.model = _Song

    def setUp(self):
        self.model.metadata.create_all(self.engine)
        self.session = sessionmaker(bind=self.engine)()
        self.dbi = interface(self.session, self.model)

    def tearDown(self):
        self.model.drop(self.engine, checkfirst=True)
        self.session.close()
        del(self.dbi)

    def test_add_song_metadata_full(self):
        self.assertTrue(self.dbi.add_song_metadata(SHA1, 'filename.ext', 'artist', 'title', 0, 0,
                                                   datetime(1970, 1, 1), datetime(1970, 1, 1)))

        self.assertEqual(1, self.session.query(self.model).count())
        self.assertEqual(SHA1, self.session.query(self.model.song_hash).all()[0])
        self.assertEqual('filename.ext', self.session.query(self.model.filename).all()[0])
        self.assertEqual('artist', self.session.query(self.model.artist).all()[0])
        self.assertEqual('title', self.session.query(self.model.title).all()[0])
        self.assertEqual(0, self.session.query(self.model.length).all()[0])
        self.assertEqual(0, self.session.query(self.model.play_count).all()[0])
        self.assertEqual(0, self.session.query(self.model.date_added).all()[0])
        self.assertEqual(0, self.session.query(self.model.last_played).all()[0])

    def test_add_song_metadata_partial(self):
        now = datetime.datetime.now()
        self.assertTrue(self.dbi.add_song_metadata(SHA1, 'filename.ext'))

        self.assertEqual(1, self.session.query(self.model).count())
        self.assertEqual(SHA1, self.session.query(self.model.song_hash).all()[0])
        self.assertEqual('filename.ext', self.session.query(self.model.filename).all()[0])
        self.assertEqual(None, self.session.query(self.model.artist).all()[0])
        self.assertEqual(None, self.session.query(self.model.title).all()[0])
        self.assertEqual(None, self.session.query(self.model.length).all()[0])
        self.assertEqual(1, self.session.query(self.model.play_count).all()[0])
        self.assertEqual(now, self.session.query(self.model.date_added).all()[0])
        self.assertEqual(now, self.session.query(self.model.last_played).all()[0])

    def test_get_song_metadata(self):
        self.test_add_song_metadata_full()
        result = self.dbi.get_song_metadata()
        expected = {'filename': 'filename.ext',
                    'artist': 'artist',
                    'title': 'title',
                    'length': 0,
                    'play_count': 0,
                    'date_added': datetime(1970, 1, 1),
                    'last_played': datetime(1970, 1, 1)}
        self.assertEqual(expected, result)

    def test_get_song_hashes_by_artist(self):
        self.test_add_song_metadata_full()
        result = self.dbi.get_song_hashes_by_artist('artist')
        expected = [SHA1]
        self.assertEqual(expected, result)

    def test_get_song_hashes_by_filename(self):
        self.test_add_song_metadata_full()
        result = self.dbi.get_song_hashes_by_filename('filename.ext')
        expected = [SHA1]
        self.assertEqual(expected, result)

    def test_get_song_hashes_by_length(self):
        self.test_add_song_metadata_full()
        result = self.dbi.get_song_hashes_by_length(0)
        expected = [SHA1]
        self.assertEqual(expected, result)

    def test_get_song_hashes_by_title(self):
        self.test_add_song_metadata_full()
        result = self.dbi.get_song_hashes_by_title('title')
        expected = [SHA1]
        self.assertEqual(expected, result)

    def test_increment_play_count(self):
        self.test_add_song_metadata_full()
        self.assertTrue(self.dbi.increment_play_count(SHA1))
        self.assertEqual(1, self.session.query(self.model.play_count).all()[0])

    def test_set_last_played(self):
        self.test_add_song_metadata_full()
        self.assertTrue(self.dbi.set_last_played(SHA1, datetime(2013, 9, 8)))
        self.assertEqual(datetime(2013, 9, 8), self.session.query(self.model.last_played).all()[0])
<commit_msg>Add tests for DatabaseInterface and DB<commit_after>import unittest
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from tikplay.database import Base
from tikplay.database.interface import DatabaseInterface
from tikplay.database.models import Song
from datetime import datetime


SHA1 = u'a' * 40


class DBInterfaceTestCase(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        cls.engine = create_engine('sqlite:///:memory:', echo=True)
        cls._base = Base
        cls.model = Song

    def setUp(self):
        self._base.metadata.create_all(self.engine)
        self.session = sessionmaker(bind=self.engine)()
        self.dbi = DatabaseInterface(self.session, self.model)

    def tearDown(self):
        self._base.metadata.drop_all(self.engine)
        self.session.close()
        del(self.dbi)

    def test_add_song_metadata_full(self):
        self.assertTrue(self.dbi.add_song_metadata(SHA1, 'filename.ext', 'artist', 'title', 0, 1,
                                                   datetime(1970, 1, 2), datetime(1970, 1, 2)))
        self.assertEqual(1, self.session.query(self.model).count())
        query = self.session.query(self.model).one()

        self.assertEqual(SHA1, query.song_hash)
        self.assertEqual('filename.ext', query.filename)
        self.assertEqual('artist', query.artist)
        self.assertEqual('title', query.title)
        self.assertEqual(0, query.length)
        self.assertEqual(1, query.play_count)
        self.assertEqual(datetime(1970, 1, 2), query.date_added)
        self.assertEqual(datetime(1970, 1, 2), query.last_played)

    def test_add_song_metadata_partial(self):
        self.assertTrue(self.dbi.add_song_metadata(SHA1, 'filename.ext'))

        self.assertEqual(1, self.session.query(self.model).count())
        query = self.session.query(self.model).one()

        self.assertEqual(SHA1, query.song_hash)
        self.assertEqual('filename.ext', query.filename)
        self.assertEqual(None, query.artist)
        self.assertEqual(None, query.title)
        self.assertEqual(None, query.length)
        self.assertEqual(0, query.play_count)
        # self.assertEqual(now, query.date_added)  Is not tested since the time doesn't match in millis
        self.assertEqual(None, query.last_played)

    def test_get_song_metadata(self):
        self.test_add_song_metadata_full()
        result = self.dbi.get_song_metadata(SHA1)
        expected = {'song_hash': SHA1,
                    'filename': u'filename.ext',
                    'artist': u'artist',
                    'title': u'title',
                    'length': 0,
                    'play_count': 1,
                    'date_added': datetime(1970, 1, 2),
                    'last_played': datetime(1970, 1, 2)}
        self.assertEqual(expected, result)

    def test_get_song_hashes_by_artist(self):
        self.test_add_song_metadata_full()
        result = self.dbi.get_song_hashes_by_artist('artist')
        expected = [{'song_hash': SHA1,
                    'filename': u'filename.ext',
                    'artist': u'artist',
                    'title': u'title',
                    'length': 0,
                    'play_count': 1,
                    'date_added': datetime(1970, 1, 2),
                    'last_played': datetime(1970, 1, 2)}]
        self.assertEqual(expected, result)

    def test_get_song_hashes_by_filename(self):
        self.test_add_song_metadata_full()
        result = self.dbi.get_song_hashes_by_filename('filename.ext')
        expected = [{'song_hash': SHA1,
                    'filename': u'filename.ext',
                    'artist': u'artist',
                    'title': u'title',
                    'length': 0,
                    'play_count': 1,
                    'date_added': datetime(1970, 1, 2),
                    'last_played': datetime(1970, 1, 2)}]
        self.assertEqual(expected, result)

    def test_get_song_hashes_by_length(self):
        self.test_add_song_metadata_full()
        result = self.dbi.get_song_hashes_by_length(0)
        expected = [{'song_hash': SHA1,
                    'filename': u'filename.ext',
                    'artist': u'artist',
                    'title': u'title',
                    'length': 0,
                    'play_count': 1,
                    'date_added': datetime(1970, 1, 2),
                    'last_played': datetime(1970, 1, 2)}]
        self.assertEqual(expected, result)

    def test_get_song_hashes_by_title(self):
        self.test_add_song_metadata_full()
        result = self.dbi.get_song_hashes_by_title('title')
        expected = [{'song_hash': SHA1,
                    'filename': u'filename.ext',
                    'artist': u'artist',
                    'title': u'title',
                    'length': 0,
                    'play_count': 1,
                    'date_added': datetime(1970, 1, 2),
                    'last_played': datetime(1970, 1, 2)}]
        self.assertEqual(expected, result)

    def test_increment_play_count(self):
        self.test_add_song_metadata_full()
        self.assertTrue(self.dbi.increment_play_count(SHA1))
        query = self.session.query(self.model.play_count).one()
        self.assertEqual(2, query.play_count)

    def test_set_last_played(self):
        self.test_add_song_metadata_full()
        self.assertTrue(self.dbi.set_last_played(SHA1, datetime(2013, 9, 8)))
        query = self.session.query(self.model.last_played).one()
        self.assertEqual(datetime(2013, 9, 8), query.last_played)

{'fraction_non_alphanumeric': 0.12579857196542654, 'fraction_numerical': 0.019917324314167605, 'mean_word_length': 2.550700466977985, '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': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6182584', 'n_tokens_mistral': 3460, 'n_tokens_neox': 3187, 'n_words': 510}