def test_object_indexing(self):
        idx = search.Indexer()

        # Create some test artists.
        art1 = models.Artist(name=u"Fall, The", parent=idx.transaction,
                             key_name="art1")
        art2 = models.Artist(name=u"Eno, Brian", parent=idx.transaction,
                             key_name="art2")
        # Create some test single-artist albums.
        alb1 = models.Album(title=u"This Nation's Saving Grace",
                            year=1985,
                            album_id=12345,
                            label=u"Some Label",
                            import_timestamp=datetime.datetime.now(),
                            album_artist=art1,
                            num_tracks=123,
                            parent=idx.transaction)
        trk1 = []
        for i, track_title in enumerate(
            (u"Mansion", u"Bombast", u"Cruiser's Creek", u"What You Need",
             u"Spoiled Victorian Child", u"L.A.")):
            trk1.append(models.Track(ufid="test1-%d" % i,
                                     album=alb1,
                                     sampling_rate_hz=44110,
                                     bit_rate_kbps=320,
                                     channels="stereo",
                                     duration_ms=123,
                                     title=track_title,
                                     track_num=i+1,
                                     parent=idx.transaction))
        alb2 = models.Album(title=u"Another Green World",
                            album_id=67890,
                            label=u"Some Label",
                            import_timestamp=datetime.datetime.now(),
                            album_artist=art2,
                            num_tracks=456,
                            parent=idx.transaction)
        trk2 = []
        for i, track_title in enumerate(
            (u"Sky Saw", u"Over Fire Island", u"St. Elmo's Fire",
             u"In Dark Trees", u"The Big Ship")):
            trk2.append(models.Track(ufid="test2-%d" % i,
                                     album=alb2,
                                     sampling_rate_hz=44110,
                                     bit_rate_kbps=192,
                                     channels="joint_stereo",
                                     duration_ms=456,
                                     title=track_title,
                                     track_num=i+1,
                                     parent=idx.transaction))
        # Create a test album that is a compilation.
        alb3 = models.Album(title=u"R&B Gold: 1976",
                            album_id=76543,
                            label=u"Some Label",
                            import_timestamp=datetime.datetime.now(),
                            is_compilation=True,
                            num_tracks=789,
                            parent=idx.transaction)
        trk3_art = []
        trk3 = []
        for i, (artist_name, track_title) in enumerate(
            ((u"Earth, Wind & Fire", u"Sing A Song"),
             (u"Diana Ross", u"Love Hangover"),
             (u"Aretha Franklin", u"Something He Can Feel"),
             (u"KC & the Sunshine Band",
              u"(Shake, Shake, Shake) Shake Your Booty"))):
            art = models.Artist(name=artist_name,
                                key_name=artist_name,
                                parent=idx.transaction)
            trk3_art.append(art)
            trk3.append(models.Track(ufid="test3-%d" % i,
                                     album=alb3,
                                     sampling_rate_hz=44110,
                                     bit_rate_kbps=128,
                                     channels="mono",
                                     duration_ms=789,
                                     title=track_title,
                                     track_artist=art,
                                     track_num=i+1,
                                     parent=idx.transaction))

        # Now index everything we just created.
        idx.add_artist(art1)
        idx.add_artist(art2)
        for art in trk3_art:
            idx.add_artist(art)

        idx.add_album(alb1)
        idx.add_album(alb2)
        idx.add_album(alb3)

        for trk in trk1 + trk2 + trk3:
            idx.add_track(trk)

        idx.save()  # This also saves all of the objects.

        # Now do some test searches.

        # This query matches the album and all of the tracks.
        expected = {alb1.key(): set(["title"])}
        self.assertEqual(
            expected,
            search.fetch_keys_for_query_string(u"nations",
                                               entity_kind="Album"))
        for t in trk1:
            expected[t.key()] = set(["album"])
        self.assertEqual(
            expected,
            search.fetch_keys_for_query_string(u"nations"))

        # The query "fire" should match:
        #   * Two of the songs from "Another Green World"
        #   * The band "Earth, Wind & Fire"
        #   * The EW&F track from the compilation.
        expected = {
            trk2[1].key(): set(["title"]),
            trk2[2].key(): set(["title"]),
            trk3_art[0].key(): set(["name"]),
            trk3[0].key(): set(["artist"]),
            }
        self.assertEqual(
            expected,
            search.fetch_keys_for_query_string(u"fire"))
示例#2
0
    def test_object_indexing(self):
        idx = search.Indexer()

        # Create some test artists.
        art1 = models.Artist(name=u"Fall, The",
                             parent=idx.transaction,
                             key_name="art1")
        art2 = models.Artist(name=u"Eno, Brian",
                             parent=idx.transaction,
                             key_name="art2")
        # Create some test single-artist albums.
        alb1 = models.Album(title=u"This Nation's Saving Grace",
                            year=1985,
                            album_id=12345,
                            label=u"Some Label",
                            import_timestamp=datetime.datetime.now(),
                            album_artist=art1,
                            num_tracks=123,
                            parent=idx.transaction)
        trk1 = []
        for i, track_title in enumerate(
            (u"Mansion", u"Bombast", u"Cruiser's Creek", u"What You Need",
             u"Spoiled Victorian Child", u"L.A.")):
            trk1.append(
                models.Track(ufid="test1-%d" % i,
                             album=alb1,
                             sampling_rate_hz=44110,
                             bit_rate_kbps=320,
                             channels="stereo",
                             duration_ms=123,
                             title=track_title,
                             track_num=i + 1,
                             parent=idx.transaction))
        alb2 = models.Album(title=u"Another Green World",
                            album_id=67890,
                            label=u"Some Label",
                            import_timestamp=datetime.datetime.now(),
                            album_artist=art2,
                            num_tracks=456,
                            parent=idx.transaction)
        trk2 = []
        for i, track_title in enumerate(
            (u"Sky Saw", u"Over Fire Island", u"St. Elmo's Fire",
             u"In Dark Trees", u"The Big Ship")):
            trk2.append(
                models.Track(ufid="test2-%d" % i,
                             album=alb2,
                             sampling_rate_hz=44110,
                             bit_rate_kbps=192,
                             channels="joint_stereo",
                             duration_ms=456,
                             title=track_title,
                             track_num=i + 1,
                             parent=idx.transaction))
        # Create a test album that is a compilation.
        alb3 = models.Album(title=u"R&B Gold: 1976",
                            album_id=76543,
                            label=u"Some Label",
                            import_timestamp=datetime.datetime.now(),
                            is_compilation=True,
                            num_tracks=789,
                            parent=idx.transaction)
        trk3_art = []
        trk3 = []
        for i, (artist_name, track_title) in enumerate(
            ((u"Earth, Wind & Fire", u"Sing A Song"),
             (u"Diana Ross", u"Love Hangover"), (u"Aretha Franklin",
                                                 u"Something He Can Feel"),
             (u"KC & the Sunshine Band",
              u"(Shake, Shake, Shake) Shake Your Booty"))):
            art = models.Artist(name=artist_name,
                                key_name=artist_name,
                                parent=idx.transaction)
            trk3_art.append(art)
            trk3.append(
                models.Track(ufid="test3-%d" % i,
                             album=alb3,
                             sampling_rate_hz=44110,
                             bit_rate_kbps=128,
                             channels="mono",
                             duration_ms=789,
                             title=track_title,
                             track_artist=art,
                             track_num=i + 1,
                             parent=idx.transaction))

        # Now index everything we just created.
        idx.add_artist(art1)
        idx.add_artist(art2)
        for art in trk3_art:
            idx.add_artist(art)

        idx.add_album(alb1)
        idx.add_album(alb2)
        idx.add_album(alb3)

        for trk in trk1 + trk2 + trk3:
            idx.add_track(trk)

        idx.save()  # This also saves all of the objects.

        # Now do some test searches.

        # This query matches the album and all of the tracks.
        expected = {alb1.key(): set(["title"])}
        self.assertEqual(
            expected,
            search.fetch_keys_for_query_string(u"nations",
                                               entity_kind="Album"))
        for t in trk1:
            expected[t.key()] = set(["album"])
        self.assertEqual(expected,
                         search.fetch_keys_for_query_string(u"nations"))

        # The query "fire" should match:
        #   * Two of the songs from "Another Green World"
        #   * The band "Earth, Wind & Fire"
        #   * The EW&F track from the compilation.
        expected = {
            trk2[1].key(): set(["title"]),
            trk2[2].key(): set(["title"]),
            trk3_art[0].key(): set(["name"]),
            trk3[0].key(): set(["artist"]),
        }
        self.assertEqual(expected, search.fetch_keys_for_query_string(u"fire"))
    def test_search_using_queries(self):
        key1 = db.Key.from_path("kind_Foo", "key1")
        key2 = db.Key.from_path("kind_Foo", "key2")
        key3 = db.Key.from_path("kind_Foo", "key3")
        key4 = db.Key.from_path("kind_Bar", "key4")
        key5 = db.Key.from_path("kind_Bar", "key5")
        key6 = db.Key.from_path("kind_Bar", "key6")
        key7 = db.Key.from_path("kind_Bar", "key7")

        idx = search.Indexer()
        idx.add_key(key1, "f1", u"alpha beta")
        idx.add_key(key2, "f2", u"alpha delta")
        idx.add_key(key3, "f1", u"alaska beta")
        idx.add_key(key4, "f2", u"beta delta")
        idx.add_key(key5, "f1", u"alpha alaska")
        idx.add_key(key6, "f2", u"delta gamma")
        # an indexed value ending in a stop word:
        idx.add_key(key7, "stop-word-prefix", u"something in")
        idx.save()

        # Check that some simple queries are handled correctly.
        self.assertEqual(
            {key1: set(["f1"]), key2: set(["f2"]), key5: set(["f1"])},
            search.fetch_keys_for_query_string(u"alpha"))
        self.assertEqual(
            {key2: set(["f2"]), key4: set(["f2"]), key6: set(["f2"])},
            search.fetch_keys_for_query_string(u"delta"))
        self.assertEqual(
            {key1: set(["f1"]), key2: set(["f2"]), key3: set(["f1"]),
             key5: set(["f1"])},
            search.fetch_keys_for_query_string(u"al*"))
        self.assertEqual(
            {key1: set(["f1"])},
            search.fetch_keys_for_query_string(u"beta alpha"))
        self.assertEqual(
            {key1: set(["f1"]), key3: set(["f1"])},
            search.fetch_keys_for_query_string(u"al* beta"))
        self.assertEqual(
            {key2: set(["f2"]), key5: set(["f1"])},
            search.fetch_keys_for_query_string(u"al* -beta"))
        self.assertEqual(
            {key4: set(["f2"]), key6: set(["f2"])},
            search.fetch_keys_for_query_string(u"delta -al*"))
        # Make sure we can run a prefix search on a stop word
        # (this is necessary for autocomplete searches)
        self.assertEqual(
            {key7: set(["stop-word-prefix"])},
            search.fetch_keys_for_query_string(u"something i*"))

        # Check that entity kind restrictions are respected.
        self.assertEqual(
            {key1: set(["f1"]), key2: set(["f2"])},
            search.fetch_keys_for_query_string(u"alpha",
                                               entity_kind="kind_Foo"))
        self.assertEqual(
            {key5: set(["f1"])},
            search.fetch_keys_for_query_string(u"al*", entity_kind="kind_Bar"))
        self.assertEqual(
            {key2: set(["f2"])},
            search.fetch_keys_for_query_string(u"al* -beta",
                                               entity_kind="kind_Foo"))

        # Check that searches against unknown terms are handled properly.
        self.assertEqual(
            {},
            search.fetch_keys_for_query_string(u"nosuchterm"))
        self.assertEqual(
            {},
            search.fetch_keys_for_query_string(u"nosuchterm*"))
        self.assertEqual(
            {},
            search.fetch_keys_for_query_string(u"alpha nosuchterm"))
        self.assertEqual(
            {},
            search.fetch_keys_for_query_string(u"alpha nosuchterm*"))
        self.assertEqual(
            {key1: set(["f1"]), key2: set(["f2"]), key5: set(["f1"])},
            search.fetch_keys_for_query_string(u"alpha -nosuchterm"))
        self.assertEqual(
            {key1: set(["f1"]), key2: set(["f2"]), key5: set(["f1"])},
            search.fetch_keys_for_query_string(u"alpha -nosuchterm*"))

        # Check that None is returned for various invalid/bogus queries.
        self.assertEqual(None, search.fetch_keys_for_query_string(u""))
        self.assertEqual(None, search.fetch_keys_for_query_string(u"+,,,*"))
        self.assertEqual(None, search.fetch_keys_for_query_string(u"-foo"))
示例#4
0
    def test_search_using_queries(self):
        key1 = db.Key.from_path("kind_Foo", "key1")
        key2 = db.Key.from_path("kind_Foo", "key2")
        key3 = db.Key.from_path("kind_Foo", "key3")
        key4 = db.Key.from_path("kind_Bar", "key4")
        key5 = db.Key.from_path("kind_Bar", "key5")
        key6 = db.Key.from_path("kind_Bar", "key6")
        key7 = db.Key.from_path("kind_Bar", "key7")

        idx = search.Indexer()
        idx.add_key(key1, "f1", u"alpha beta")
        idx.add_key(key2, "f2", u"alpha delta")
        idx.add_key(key3, "f1", u"alaska beta")
        idx.add_key(key4, "f2", u"beta delta")
        idx.add_key(key5, "f1", u"alpha alaska")
        idx.add_key(key6, "f2", u"delta gamma")
        # an indexed value ending in a stop word:
        idx.add_key(key7, "stop-word-prefix", u"something in")
        idx.save()

        # Check that some simple queries are handled correctly.
        self.assertEqual(
            {
                key1: set(["f1"]),
                key2: set(["f2"]),
                key5: set(["f1"])
            }, search.fetch_keys_for_query_string(u"alpha"))
        self.assertEqual(
            {
                key2: set(["f2"]),
                key4: set(["f2"]),
                key6: set(["f2"])
            }, search.fetch_keys_for_query_string(u"delta"))
        self.assertEqual(
            {
                key1: set(["f1"]),
                key2: set(["f2"]),
                key3: set(["f1"]),
                key5: set(["f1"])
            }, search.fetch_keys_for_query_string(u"al*"))
        self.assertEqual({key1: set(["f1"])},
                         search.fetch_keys_for_query_string(u"beta alpha"))
        self.assertEqual({
            key1: set(["f1"]),
            key3: set(["f1"])
        }, search.fetch_keys_for_query_string(u"al* beta"))
        self.assertEqual({
            key2: set(["f2"]),
            key5: set(["f1"])
        }, search.fetch_keys_for_query_string(u"al* -beta"))
        self.assertEqual({
            key4: set(["f2"]),
            key6: set(["f2"])
        }, search.fetch_keys_for_query_string(u"delta -al*"))
        # Make sure we can run a prefix search on a stop word
        # (this is necessary for autocomplete searches)
        self.assertEqual({key7: set(["stop-word-prefix"])},
                         search.fetch_keys_for_query_string(u"something i*"))

        # Check that entity kind restrictions are respected.
        self.assertEqual({
            key1: set(["f1"]),
            key2: set(["f2"])
        }, search.fetch_keys_for_query_string(u"alpha",
                                              entity_kind="kind_Foo"))
        self.assertEqual({key5: set(["f1"])},
                         search.fetch_keys_for_query_string(
                             u"al*", entity_kind="kind_Bar"))
        self.assertEqual({key2: set(["f2"])},
                         search.fetch_keys_for_query_string(
                             u"al* -beta", entity_kind="kind_Foo"))

        # Check that searches against unknown terms are handled properly.
        self.assertEqual({}, search.fetch_keys_for_query_string(u"nosuchterm"))
        self.assertEqual({},
                         search.fetch_keys_for_query_string(u"nosuchterm*"))
        self.assertEqual(
            {}, search.fetch_keys_for_query_string(u"alpha nosuchterm"))
        self.assertEqual(
            {}, search.fetch_keys_for_query_string(u"alpha nosuchterm*"))
        self.assertEqual(
            {
                key1: set(["f1"]),
                key2: set(["f2"]),
                key5: set(["f1"])
            }, search.fetch_keys_for_query_string(u"alpha -nosuchterm"))
        self.assertEqual(
            {
                key1: set(["f1"]),
                key2: set(["f2"]),
                key5: set(["f1"])
            }, search.fetch_keys_for_query_string(u"alpha -nosuchterm*"))

        # Check that None is returned for various invalid/bogus queries.
        self.assertEqual(None, search.fetch_keys_for_query_string(u""))
        self.assertEqual(None, search.fetch_keys_for_query_string(u"+,,,*"))
        self.assertEqual(None, search.fetch_keys_for_query_string(u"-foo"))