Source

spotimeta / tests / test_parsers.py

# -*- coding: utf-8 -*-


from os.path import join, dirname
from spotimeta import parser


# Path to the xml directory that contains lots of canned api responses
xmldir = join(dirname(__file__), "xml")


def test_artist():
    path = join(xmldir, "artist.xml")
    fp = open(path)

    res = parser.parse_lookup_doc(fp)

    assert res["type"] == "artist"
    artist = res["result"]

    assert artist
    assert "name" in artist
    assert artist["name"] == "Basement Jaxx"


def test_artist_album():
    path = join(xmldir, "artist_album.xml")
    fp = open(path)
    res = parser.parse_lookup_doc(fp)

    assert res["type"] == "artist"
    artist = res["result"]

    assert artist
    assert "name" in artist
    assert artist["name"] == "Basement Jaxx"
    assert "albums" in artist
    assert len(artist["albums"]) == 149

    # fixme: assert more stuff

def test_artist_albumdetail():
    path = join(xmldir, "artist_albumdetail.xml")
    fp = open(path)
    res = parser.parse_lookup_doc(fp)

    assert res["type"] == "artist"
    artist = res["result"]

    assert artist
    assert "name" in artist
    assert artist["name"] == "Basement Jaxx"
    assert "albums" in artist
    assert len(artist["albums"]) == 149
    # fixme: assert more stuff


def test_album():
    path = join(xmldir, "album.xml")
    fp = open(path)
    res = parser.parse_lookup_doc(fp)

    assert res["type"] == "album"
    album = res["result"]

    assert album
    assert "name" in album
    assert album["name"] == "Remedy"
    assert "artist" in album
    assert album["artist"]["name"] == "Basement Jaxx"
    assert "released" in album
    assert album["released"] == 1999
    assert "artist" in album
    assert album["artist"]["name"] == "Basement Jaxx"
    assert album["artist"]["href"] == "spotify:artist:4YrKBkKSVeqDamzBPWVnSJ"

    # fixme: assert ID stuff

def test_album_track():
    path = join(xmldir, "album_track.xml")
    fp = open(path)
    res = parser.parse_lookup_doc(fp)

    assert res["type"] == "album"
    album = res["result"]

    assert album
    assert "name" in album
    assert album["name"] == "Remedy"
    assert "artist" in album
    assert album["artist"]["name"] == "Basement Jaxx"
    assert "released" in album
    assert album["released"] == 1999
    # fixme: assert ID stuff

    assert "tracks" in album
    assert len(album["tracks"]) == 15

    assert album["tracks"][12]["name"] == "Stop 4 Love"
    assert "href" in album["tracks"][12]
    assert album["tracks"][12]["href"] == "spotify:track:2cC8NxlxmxbYLiUMpIdKtx"



def test_album_trackdetail():
    path = join(xmldir, "album_trackdetail.xml")
    fp = open(path)
    res = parser.parse_lookup_doc(fp)

    assert res["type"] == "album"
    album = res["result"]

    assert album
    assert "name" in album
    assert album["name"] == "Remedy"
    assert "artist" in album
    assert album["artist"]["name"] == "Basement Jaxx"
    assert "released" in album
    assert album["released"] == 1999
    # fixme: assert ID stuff

    assert "tracks" in album
    assert len(album["tracks"]) == 15

    track = album["tracks"][12]

    assert track["name"] == "Stop 4 Love"
    assert "href" in track
    assert track["href"] == "spotify:track:2cC8NxlxmxbYLiUMpIdKtx"

    assert track["disc-number"] == 1
    assert track["track-number"] == 13
    assert int(track["length"]) == 293
    assert track["popularity"] == 0.43011


def test_track():
    path = join(xmldir, "track.xml")
    fp = open(path)
    res = parser.parse_lookup_doc(fp)

    assert res["type"] == "track"
    track = res["result"]

    assert track
    assert "name" in track
    assert track["name"] == u"Bøn Fra Helvete - Live"

    # fixme: more asserts


def test_album_search():
    path = join(xmldir, "search_album_foo.xml")
    fp = open(path)
    res = parser.parse_search_doc(fp)

    assert res

    assert "term" in res
    assert res["term"] == "foo"

    assert "total_results" in res
    assert res["total_results"] == 6

    assert "result" in res
    assert len(res["result"]) == 6, "Expected 6, got %s" % len(res["result"])

    assert "items_per_page" in res
    assert res["items_per_page"] == 100

    assert "start_index" in res
    assert res["start_index"] == 0

def test_artist_search():
    path = join(xmldir, "search_artist_foo.xml")
    fp = open(path)
    res = parser.parse_search_doc(fp)

    assert res

    assert "term" in res
    assert res["term"] == "foo"

    assert "total_results" in res
    assert res["total_results"] == 9

    assert "result" in res
    assert len(res["result"]) == 9, "Expected 9, got %s" % len(res["result"])

    assert "items_per_page" in res
    assert res["items_per_page"] == 100

    assert "start_index" in res
    assert res["start_index"] == 0


def test_track_search():
    path = join(xmldir, "search_track_foo.xml")
    fp = open(path)
    res = parser.parse_search_doc(fp)

    assert res

    assert "term" in res
    assert res["term"] == "foo"


    assert "total_results" in res
    assert res["total_results"] == 503

    assert "result" in res
    assert len(res["result"]) == 100, "Expected 100, got %s" % len(res["result"])

    assert "items_per_page" in res
    assert res["items_per_page"] == 100

    assert "start_index" in res
    assert res["start_index"] == 0

    assert "result" in res

    hits = res["result"]
    track = hits[0]

    assert "album" in track


def test_track_multiple_artists():
    path = join(xmldir, "tc_track_multiple_artists.xml")
    fp = open(path)
    res = parser.parse_search_doc(fp)
    assert res
    assert "result" in res
    track = res["result"][0]

    assert "artists" in track
    assert len(track["artists"]) == 4


    assert track["artists"][0]["name"] == "100.00"
    assert track["artists"][-1]["name"] == u"Niël Schoombee"
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.