File: __init__.py

package info (click to toggle)
deejayd 0.9.0-4
  • links: PTS, VCS
  • area: main
  • in suites: squeeze, wheezy
  • size: 2,596 kB
  • ctags: 2,514
  • sloc: python: 11,904; xml: 594; sh: 338; makefile: 42
file content (177 lines) | stat: -rw-r--r-- 6,508 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# Deejayd, a media player daemon
# Copyright (C) 2007-2009 Mickael Royer <mickael.royer@gmail.com>
#                         Alexandre Rossi <alexandre.rossi@gmail.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

import sys, os, shutil
import unittest

from testdeejayd.databuilder import TestData, TestAudioCollection,\
                                    TestVideoCollection
from testdeejayd.server import TestServer
from deejayd.ui.config import DeejaydConfig


class DeejaydTest(unittest.TestCase):

    def setUp(self):
        from deejayd.ui.i18n import DeejaydTranslations
        t = DeejaydTranslations()
        t.install()


class TestCaseWithData(DeejaydTest):

    def setUp(self):
        super(TestCaseWithData, self).setUp()
        self.testdata = TestData()

    def assert_filter_matches_sample(self, retrieved_filter):
        self.assertEqual(retrieved_filter.__class__.__name__, 'And')
        anded = retrieved_filter.filterlist
        self.assertEqual(anded[0].__class__.__name__, 'Contains')
        self.assertEqual(anded[0].tag, 'artist')
        self.assertEqual(anded[0].pattern, 'Britney')
        self.assertEqual(anded[1].__class__.__name__, 'Or')
        ored = anded[1].filterlist
        self.assertEqual(ored[0].__class__.__name__, 'Equals')
        self.assertEqual(ored[0].tag, 'genre')
        self.assertEqual(ored[0].pattern, 'Classical')
        self.assertEqual(ored[1].__class__.__name__, 'Equals')
        self.assertEqual(ored[1].tag, 'genre')
        self.assertEqual(ored[1].pattern, 'Disco')


class _TestCaseWithMediaData(DeejaydTest):

    def setUp(self):
        super(_TestCaseWithMediaData, self).setUp()
        self.testdata = self.collection_class()
        self.testdata.buildLibraryDirectoryTree()

    def tearDown(self):
        self.testdata.cleanLibraryDirectoryTree()


class TestCaseWithAudioData(_TestCaseWithMediaData):
    collection_class = TestAudioCollection


class TestCaseWithVideoData(_TestCaseWithMediaData):
    collection_class = TestVideoCollection


class TestCaseWithAudioAndVideoData(DeejaydTest):
    video_support = True

    def setUp(self):
        super(TestCaseWithAudioAndVideoData, self).setUp()

        self.testdata = TestData()
        # audio library
        self.test_audiodata = TestAudioCollection()
        self.test_audiodata.buildLibraryDirectoryTree()

        # video library
        self.test_videodata = TestVideoCollection()
        self.test_videodata.buildLibraryDirectoryTree()

    def tearDown(self):
        self.test_audiodata.cleanLibraryDirectoryTree()
        self.test_videodata.cleanLibraryDirectoryTree()


class TestCaseWithServer(TestCaseWithAudioAndVideoData):
    profiles = "default"

    def setUp(self):
        super(TestCaseWithServer, self).setUp()
        # create custom configuration files
        current_dir = os.path.dirname(__file__)
        DeejaydConfig.custom_conf = os.path.join(current_dir,\
                "profiles", self.profiles)
        config = DeejaydConfig()
        config.set('mediadb', 'music_directory',\
                self.test_audiodata.getRootDir())
        config.set('mediadb', 'video_directory',\
                self.test_videodata.getRootDir())
        self.dbfilename = None
        if config.get('database', 'db_type') == 'sqlite':
            self.dbfilename = '/tmp/testdeejayddb-' +\
                    self.testdata.getRandomString() + '.db'
            config.set('database', 'db_name', self.dbfilename)
        elif config.get('database', 'db_type') == 'mysql':
            import MySQLdb
            try:
                connection = MySQLdb.connect(\
                    db=config.get('database', 'db_name'),\
                    user=config.get('database', 'db_user'),\
                    passwd=config.get('database', 'db_password'),\
                    host=config.get('database', 'db_host'),\
                    port=config.getint('database', 'db_port'),\
                    use_unicode=True, charset="utf8")
            except MySQLdb.DatabaseError:
                print "Unable to connect to mysql db"
                sys.exit(1)
            cursor = connection.cursor()
           # drop all table
            from deejayd.database.schema import db_schema
            for table in db_schema:
                try:
                    cursor.execute("DROP TABLE `%s`" % table.name)
                except MySQLdb.DatabaseError:
                    pass
           # commit changes and close
            connection.commit()
            connection.close()

        self.tmp_dir = None
        if config.getboolean("webui","enabled"):
            # define a tmp directory
            self.tmp_dir = '/tmp/testdeejayd-tmpdir-'+\
                    self.testdata.getRandomString()
            config.set("webui", "tmp_dir", self.tmp_dir)

        # record config to be used by the test server
        self.conf = os.path.join(current_dir, "profiles", "current")
        fp = open(self.conf, "w")
        config.write(fp)
        fp.close()
        os.chmod(self.conf, 0644)

        # launch server
        self.testserver = TestServer(self.conf)
        self.testserver.start()

        # record port for clients
        self.serverPort = config.getint('net', 'port')
        self.webServerPort = config.getint('webui', 'port')

        # update video_support var
        self.video_support = config.get("general","media_backend")!="gstreamer"

    def tearDown(self):
        self.testserver.stop()
        if self.dbfilename is not None: # Clean up temporary db file
            os.unlink(self.dbfilename)
        if self.tmp_dir is not None:
             try: shutil.rmtree(self.tmp_dir)
             except (IOError, OSError):
                 pass
        os.unlink(self.conf)
        super(TestCaseWithServer, self).tearDown()

# vim: ts=4 sw=4 expandtab