chiark / gitweb /
test search
authorRichard Kettlewell <rjk@greenend.org.uk>
Thu, 22 Nov 2007 13:21:16 +0000 (13:21 +0000)
committerRichard Kettlewell <rjk@greenend.org.uk>
Thu, 22 Nov 2007 13:21:16 +0000 (13:21 +0000)
tests/Makefile.am
tests/dtest.py
tests/search.py [new file with mode: 0755]

index 7573b40..e79c021 100644 (file)
@@ -21,4 +21,5 @@
 check:
        ${PYTHON} ${srcdir}/alltests
 
-EXTRA_DIST=alltests dtest.py nothing.py version.py dbversion.py
+EXTRA_DIST=alltests dtest.py nothing.py version.py dbversion.py search.py
+
index ab76991..68a130d 100644 (file)
@@ -66,6 +66,17 @@ def copyfile(a,b):
 Copy A to B."""
     open(b,"w").write(open(a).read())
 
+def to_unicode(s):
+    """Convert UTF-8 to unicode.  A no-op if already unicode."""
+    if type(s) == unicode:
+        return s
+    else:
+        return unicode(s, "UTF-8")
+
+def nfc(s):
+    """Convert UTF-8 string or unicode to NFC unicode."""
+    return  unicodedata.normalize("NFC", to_unicode(s))
+
 def maketrack(s):
     """maketrack(S)
 
@@ -78,8 +89,7 @@ Make track with relative path S exist"""
     # We record the tracks we created so they can be tested against
     # server responses.  We put them into NFC since that's what the server
     # uses internally.
-    bits = unicodedata.normalize("NFC",
-                                 unicode(s, "UTF-8")).split('/')
+    bits = nfc(s).split('/')
     dp = tracks
     for d in bits [0:-1]:
         dd = "%s/%s" % (dp,  d)
@@ -127,11 +137,11 @@ def stdtracks():
     maketrack("Joe Bloggs/Second Album/03:Third track.ogg")
     maketrack("Joe Bloggs/Second Album/04:Fourth track.ogg")
     maketrack("Joe Bloggs/Second Album/05:Fifth track.ogg")
-    maketrack("Joe Bloggs/Third Album/01:First track.ogg")
-    maketrack("Joe Bloggs/Third Album/02:Second track.ogg")
-    maketrack("Joe Bloggs/Third Album/03:Third track.ogg")
-    maketrack("Joe Bloggs/Third Album/04:Fourth track.ogg")
-    maketrack("Joe Bloggs/Third Album/05:Fifth track.ogg")
+    maketrack("Joe Bloggs/Third Album/01:First_track.ogg")
+    maketrack("Joe Bloggs/Third Album/02:Second_track.ogg")
+    maketrack("Joe Bloggs/Third Album/03:Third_track.ogg")
+    maketrack("Joe Bloggs/Third Album/04:Fourth_track.ogg")
+    maketrack("Joe Bloggs/Third Album/05:Fifth_track.ogg")
     maketrack("Fred Smith/Boring/01:Dull.ogg")
     maketrack("Fred Smith/Boring/02:Tedious.ogg")
     maketrack("Fred Smith/Boring/03:Drum Solo.ogg")
diff --git a/tests/search.py b/tests/search.py
new file mode 100755 (executable)
index 0000000..832a6d9
--- /dev/null
@@ -0,0 +1,72 @@
+#! /usr/bin/env python
+#
+# This file is part of DisOrder.
+# Copyright (C) 2007 Richard Kettlewell
+#
+# 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+# USA
+#
+import dtest,time,disorder,sys
+
+failures = 0
+
+def check_search_results(terms, expected):
+    global failures
+    # We want a consistent encoding and ordering
+    print "terms:    %s" % terms
+    got = client.search(terms)
+    got = map(dtest.nfc, got)
+    expected = map(lambda s: "%s/%s" % (dtest.tracks, s), expected)
+    expected = map(dtest.nfc, expected)
+    got.sort()
+    expected.sort()
+    if got != expected:
+        print "expected: %s" % expected
+        print "got:      %s" % got
+        print
+        failures += 1
+
+def test():
+    """Check that the search produces the right results"""
+    dtest.start_daemon()
+    time.sleep(2)                       # give rescan a chance
+    global client
+    client = disorder.client()
+    # ASCII matches
+    check_search_results(["first"],
+                         ["Joe Bloggs/First Album/01:F\xC3\x8Crst track.ogg",
+                          "Joe Bloggs/First Album/02:Second track.ogg",
+                          "Joe Bloggs/First Album/03:ThI\xCC\x81rd track.ogg",
+                          "Joe Bloggs/First Album/04:Fourth track.ogg",
+                          "Joe Bloggs/First Album/05:Fifth track.ogg",
+                          "Joe Bloggs/Second Album/01:First track.ogg",
+                          "Joe Bloggs/Third Album/01:First_track.ogg"])
+    check_search_results(["second"],
+                         ["Joe Bloggs/First Album/02:Second track.ogg",
+                          "Joe Bloggs/Second Album/01:First track.ogg",
+                          "Joe Bloggs/Second Album/02:Second track.ogg",
+                          "Joe Bloggs/Second Album/03:Third track.ogg",
+                          "Joe Bloggs/Second Album/04:Fourth track.ogg",
+                          "Joe Bloggs/Second Album/05:Fifth track.ogg",
+                          "Joe Bloggs/Third Album/02:Second_track.ogg"])
+    # ASCII Conjunctions
+    check_search_results(["first", "second"],
+                         ["Joe Bloggs/First Album/02:Second track.ogg",
+                          "Joe Bloggs/Second Album/01:First track.ogg"])
+    if failures > 0:
+        sys.exit(1)
+
+if __name__ == '__main__':
+    dtest.run()