[autoqueue commit] r275 - moving stuff around to make the tests work

1 view
Skip to first unread message

codesite...@google.com

unread,
Jul 13, 2009, 5:50:56 PM7/13/09
to auto...@googlegroups.com
Author: this...@gmail.com
Date: Mon Jul 13 14:49:40 2009
New Revision: 275

Added:
branch/couch_backend/tests/
branch/couch_backend/tests/__init__.py
branch/couch_backend/tests/test_autoqueue.py (contents, props changed)
- copied, changed from r274, /branch/couch_backend/test_autoqueue.py
branch/couch_backend/tests/test_mirage.py (props changed)
- copied unchanged from r274, /branch/couch_backend/test_mirage.py
branch/couch_backend/tests/testfiles/ (props changed)
- copied from r274, /branch/couch_backend/testfiles/
Removed:
branch/couch_backend/test_autoqueue.py
branch/couch_backend/test_mirage.py
branch/couch_backend/testfiles/
Modified:
branch/couch_backend/autoqueue.py

Log:
moving stuff around to make the tests work

Modified: branch/couch_backend/autoqueue.py
==============================================================================
--- branch/couch_backend/autoqueue.py (original)
+++ branch/couch_backend/autoqueue.py Mon Jul 13 14:49:40 2009
@@ -28,11 +28,12 @@
from xml.dom import minidom
from cPickle import Pickler, Unpickler

-try:
- import sqlite3
- SQL = True
-except ImportError:
- SQL = False
+## try:
+# from desktopcouch.records.record import Record
+from desktopcouch.records.server import CouchDatabase
+DB = True
+## except ImportError:
+## DB = False

try:
from mirage import Mir, Db, MatrixDimensionMismatchException
@@ -108,6 +109,30 @@
# subseq has been exhausted, therefore remove it from the queue
heapq.heappop(heap)

+DOC_TYPES = {
+ 'track': 'http://thisfred.blogspot.com/2009-07/track_schema.html',
+ }
+
+DESIGN_DOCS = {
+ 'tracks': {'views': {'get_tracks': {
+ 'map': """function(doc) {
+ if (doc.record_type == '%s') {
+ if (doc.album && doc.track_number) {
+ emit(
+ [2, doc.artist, doc.album, doc.track_number,
doc.name],
+ null)}
+ else if (doc.track_number) {
+ emit(
+ [2, doc.artist, null, doc.track_number, doc.name],
+ null)}
+ else if (doc.album) {
+ emit([2, doc.artist, doc.album, null, doc.name], null)}
+ else {
+ emit([2, doc.artist, null, null, doc.name],
null)}}}""" %
+ DOC_TYPES['track']}}},
+ }
+
+

class Throttle(object):
def __init__(self, wait):
@@ -171,8 +196,10 @@
use_db = False
store_blocked_artists = False
in_memory = False
- def __init__(self):
- self.connection = None
+ def __init__(self, uri='http://localhost:5984'):
+ self.db = CouchDatabase(
+ 'music', uri=uri, create=True)
+ self.init_db()
self.max_track_match = 10000
self.max_artist_match = 10000
self.artist_block_time = 1
@@ -201,15 +228,15 @@
self.player_set_variables_from_config()
if self.store_blocked_artists:
self.get_blocked_artists_pickle()
- if self.use_db:
- self.check_db()
if MIRAGE:
self.mir = Mir()

- def close_database_connection(self, connection):
- if self.in_memory:
- return
- connection.close()
+ def init_db(self):
+ """initialize database, if necessary"""
+ for doc in DESIGN_DOCS:
+ if self.db.has_design_document(doc):
+ continue
+ self.db.add_design_document(doc, DESIGN_DOCS[doc])

def player_get_userdir(self):
"""get the application user directory to store files"""
@@ -255,16 +282,6 @@
for dummy in method(*args, **kwargs):
pass

- def check_db(self):
- if self.in_memory:
- self.create_db()
- return
- try:
- os.stat(self.get_db_path())
- except OSError:
- self.create_db()
- self.create_indices()
-
def get_blocked_artists_pickle(self):
dump = os.path.join(
self.player_get_userdir(), "autoqueue_block_cache")
@@ -284,24 +301,6 @@
except IOError:
pass

- def get_db_path(self):
- if self.in_memory:
- return ":memory:"
- return os.path.join(self.player_get_userdir(), "similarity.db")
-
- def get_database_connection(self):
- """get database reference"""
- if self.in_memory:
- if self.connection:
- return self.connection
- self.connection = sqlite3.connect(":memory:")
- self.connection.text_factory = str
- return self.connection
- connection = sqlite3.connect(
- self.get_db_path(), timeout=5.0, isolation_level="immediate")
- connection.text_factory = str
- return connection
-
def disallowed(self, song):
if song.get_artist() in self.get_blocked_artists():
return True
@@ -588,28 +587,12 @@
except:
return None

- def get_artist(self, artist_name, with_connection=None):
+ def get_artist(self, artist_name):
"""get artist information from the database"""
- if with_connection:
- connection = with_connection
- else:
- connection = self.get_database_connection()
- artist_name = artist_name.encode("UTF-8")
- rows = connection.execute(
- "SELECT * FROM artists WHERE name = ?", (artist_name,))
- for row in rows:
- return row
- connection.execute(
- "INSERT INTO artists (name) VALUES (?)", (artist_name,))
- connection.commit()
- rows = connection.execute(
- "SELECT * FROM artists WHERE name = ?", (artist_name,))
+ rows = self.db.view(
+ 'artists', 'get_artists', startkey=artist_name,
endkey=artist_name)
for row in rows:
- if not with_connection:
- self.close_database_connection(connection)
return row
- if not with_connection:
- self.close_database_connection(connection)

def get_track(self, artist_name, title, with_connection=None):
"""get track information from the database"""

Added: branch/couch_backend/tests/__init__.py
==============================================================================
--- (empty file)
+++ branch/couch_backend/tests/__init__.py Mon Jul 13 14:49:40 2009
@@ -0,0 +1 @@
+# tests

Copied: branch/couch_backend/tests/test_autoqueue.py (from r274,
/branch/couch_backend/test_autoqueue.py)
==============================================================================
--- /branch/couch_backend/test_autoqueue.py (original)
+++ branch/couch_backend/tests/test_autoqueue.py Mon Jul 13 14:49:40 2009
@@ -1,9 +1,7 @@
# -*- coding: utf-8 -*-
-import random
-from collections import deque
from datetime import datetime, timedelta
from xml.dom import minidom
-from nose.tools import assert_equals, assert_not_equals
+from nose.tools import assert_equals
from autoqueue import SongBase, AutoQueueBase, Throttle


Reply all
Reply to author
Forward
0 new messages