# Path: wham/apis/lastfm/models.py
# class LastFmUser(WhamModel):
# 
#     name = WhamCharField(max_length=255, primary_key=True)
# 
#     top_artists = WhamManyToManyField(
#         #http://ws.audioscrobbler.com/2.0/?method=user.gettopartists&user=rj&api_key=a04961fe4330211ff149a949dfabef51&format=json
#         'LastFmArtist',
#         through='LastFmUserTopArtists',
#         wham_endpoint='',
#         wham_pk_param='user',
#         wham_params={'method': 'user.gettopartists'},
#         wham_results_path=('topartists', 'artist')
#     )
# 
#     class Meta:
#         db_table = 'lastfm_user'
# 
#     class WhamMeta(LastFmMeta):
#         endpoint = ''
#         detail_base_result_path = ('user',)
#         params = {'method': 'user.getInfo'}
#         url_pk_type = 'querystring'
#         url_pk_param = 'user'
# 
#     def __unicode__(self):
#         return self.name
# 
# class LastFmUserTopArtists(models.Model):
#     user = models.ForeignKey(LastFmUser, related_name='')
#     artist = models.ForeignKey(LastFmArtist)
#     playcount = models.CharField(max_length=255)
# 
#     class Meta:
#         ordering = ('-playcount',)
# 
# Path: wham/tests.py
# def build_httmock_functions(mock_response_dir):
#     print 'building mock functions'
#     functions = []
#     for filename in listdir(mock_response_dir):
#         filepath = join(mock_response_dir,filename)
#         if isfile(filepath):
#             method = None
#             for _method in ('GET', 'POST', 'PUT', 'DELETE', 'PATCH'):
#                 if filename.startswith(_method):
#                     filename = filename[len(_method):]
#                     method = _method
# 
#             url = urllib2.unquote(filename)
# 
#             parts = urlparse(url)
#             params = {}
#             if parts.query:
#                 print parts.query
#                 params = dict(parse_qsl(parts.query))
#                 print params
#             with open(filepath) as f:
#                 content = f.read()
#                 functions.append(build_httmock_function(
#                     parts.scheme, parts.netloc, parts.path, content, params=params, method=method))
#     return functions


from os.path import join
from django.test import TestCase
from httmock import HTTMock
from wham.apis.lastfm.models import LastFmUser, LastFmUserTopArtists
from wham.tests import build_httmock_functions
import os

APP_DIR = os.path.dirname(__file__)
MOCK_RESPONSES_DIR = join(APP_DIR, 'mock_responses')


mock_functions = build_httmock_functions(MOCK_RESPONSES_DIR)



class TestCase(TestCase):

    def setUp(self):
        pass

    def test_lastfm(self):

        with HTTMock(*mock_functions):
            user = LastFmUser.objects.get(pk='CarpetSquare')
            self.assertEquals(user.name, 'CarpetSquare')

            artists = user.top_artists.all()
            top_artist = artists[0]
            self.assertEqual(top_artist.name, "Ariel Pink's Haunted Graffiti")

# Path: wham/apis/lastfm/models.py
# class LastFmUser(WhamModel):
# 
#     name = WhamCharField(max_length=255, primary_key=True)
# 
#     top_artists = WhamManyToManyField(
#         #http://ws.audioscrobbler.com/2.0/?method=user.gettopartists&user=rj&api_key=a04961fe4330211ff149a949dfabef51&format=json
#         'LastFmArtist',
#         through='LastFmUserTopArtists',
#         wham_endpoint='',
#         wham_pk_param='user',
#         wham_params={'method': 'user.gettopartists'},
#         wham_results_path=('topartists', 'artist')
#     )
# 
#     class Meta:
#         db_table = 'lastfm_user'
# 
#     class WhamMeta(LastFmMeta):
#         endpoint = ''
#         detail_base_result_path = ('user',)
#         params = {'method': 'user.getInfo'}
#         url_pk_type = 'querystring'
#         url_pk_param = 'user'
# 
#     def __unicode__(self):
#         return self.name
# 
# class LastFmUserTopArtists(models.Model):
#     user = models.ForeignKey(LastFmUser, related_name='')
#     artist = models.ForeignKey(LastFmArtist)
#     playcount = models.CharField(max_length=255)
# 
#     class Meta:
#         ordering = ('-playcount',)
# 
# Path: wham/tests.py
# def build_httmock_functions(mock_response_dir):
#     print 'building mock functions'
#     functions = []
#     for filename in listdir(mock_response_dir):
#         filepath = join(mock_response_dir,filename)
#         if isfile(filepath):
#             method = None
#             for _method in ('GET', 'POST', 'PUT', 'DELETE', 'PATCH'):
#                 if filename.startswith(_method):
#                     filename = filename[len(_method):]
#                     method = _method
# 
#             url = urllib2.unquote(filename)
# 
#             parts = urlparse(url)
#             params = {}
#             if parts.query:
#                 print parts.query
#                 params = dict(parse_qsl(parts.query))
#                 print params
#             with open(filepath) as f:
#                 content = f.read()
#                 functions.append(build_httmock_function(
#                     parts.scheme, parts.netloc, parts.path, content, params=params, method=method))
#     return functions


# Path: wham/apis/lastfm/tests.py
from os.path import join
from django.test import TestCase
from httmock import HTTMock
from wham.apis.lastfm.models import LastFmUser, LastFmUserTopArtists
from wham.tests import build_httmock_functions
import os


APP_DIR = os.path.dirname(__file__)
MOCK_RESPONSES_DIR = join(APP_DIR, 'mock_responses')


mock_functions = build_httmock_functions(MOCK_RESPONSES_DIR)



class TestCase(TestCase):

    def setUp(self):
        pass

    def test_lastfm(self):

        with HTTMock(*mock_functions):
            user = LastFmUser.objects.get(pk='CarpetSquare')
            self.assertEquals(user.name, 'CarpetSquare')

            artists = user.top_artists.all()
            top_artist = artists[0]
            self.assertEqual(top_artist.name, "Ariel Pink's Haunted Graffiti")

            top_artist_through = LastFmUserTopArtists.objects.get(user=user, artist=top_artist)