<commit_before>from __future__ import unicode_literals

import unittest

import spotify
from spotify import utils
import tests
from tests import mock


@mock.patch('spotify.artist.lib', spec=spotify.lib)
class ArtistTest(unittest.TestCase):

    def setUp(self):
        self.session = tests.create_session()

    def test_create_without_uri_or_sp_artist_fails(self, lib_mock):
        with self.assertRaises(AssertionError):
            spotify.Artist(self.session)

    @mock.patch('spotify.Link', spec=spotify.Link)
    def test_create_from_uri(self, link_mock, lib_mock):
        sp_artist = spotify.ffi.new('int *')
        link_instance_mock = link_mock.return_value
        link_instance_mock.as_artist.return_value = spotify.Artist(
            self.session, sp_artist=sp_artist)
        lib_mock.sp_link_as_artist.return_value = sp_artist
        uri = 'spotify:artist:foo'

        result = spotify.Artist(self.session, uri=uri)

        link_mock.assert_called_with(self.session, uri=uri)
        link_instance_mock.as_artist.assert_called_with()
        lib_mock.sp_artist_add_ref.assert_called_with(sp_artist)
        self.assertEqual(result._sp_artist, sp_artist)

    @mock.patch('spotify.Link', spec=spotify.Link)
    def test_create_from_uri_fail_raises_error(self, link_mock, lib_mock):
        link_instance_mock = link_mock.return_value
        link_instance_mock.as_artist.return_value = None
        lib_mock.sp_link_as_artist.return_value = spotify.ffi.NULL
        uri = 'spotify:artist:foo'

        with self.assertRaises(ValueError):
            spotify.Artist(self.session, uri=uri)

    def test_adds_ref_to_sp_artist_when_created(self, lib_mock):
        sp_artist = spotify.ffi.new('int *')

        spotify.Artist(self.session, sp_artist=sp_artist)

        lib_mock.sp_artist_add_ref.assert_called_with(sp_artist)

    def test_releases_sp_artist_when_artist_dies(self, lib_mock):
        sp_artist = spotify.ffi.new('int *')

        artist = spotify.Artist(self.session, sp_artist=sp_artist)
        artist = None  # noqa
        tests.gc_collect()

        lib_mock.sp_artist_release.assert_called_with(sp_artist)

    @mock.patch('spotify.Link', spec=spotify.Link)
    def test_repr(self, link_mock, lib_mock):
        link_instance_mock = link_mock.return_value
        link_instance_mock.uri = 'foo'
        sp_artist = spotify.ffi.new('int *')
        artist = spotify.Artist(self.session, sp_artist=sp_artist)

        result = repr(artist)

        self.assertEqual(result, 'Artist(%r)' % 'foo')

    def test_name(self, lib_mock):
        lib_mock.sp_artist_name.return_value = spotify.ffi.new(
            'char[]', b'Foo Bar Baz')
        sp_artist = spotify.ffi.new('int *')
        artist = spotify.Artist(self.session, sp_artist=sp_artist)

        result = artist.name

        lib_mock.sp_artist_name.assert_called_once_with(sp_artist)
        self.assertEqual(result, 'Foo Bar Baz')

    def test_name_is_none_if_unloaded(self, lib_mock):
        lib_mock.sp_artist_name.return_value = spotify.ffi.new('char[]', b'')
        sp_artist = spotify.ffi.new('int *')
        artist = spotify.Artist(self.session, sp_artist=sp_artist)

        result = artist.name

        lib_mock.sp_artist_name.assert_called_once_with(sp_artist)
        self.assertIsNone(result)

    def test_is_loaded(self, lib_mock):
        lib_mock.sp_artist_is_loaded.return_value = 1
        sp_artist = spotify.ffi.new('int *')
        artist = spotify.Artist(self.session, sp_artist=sp_artist)

        result = artist.is_loaded

        lib_mock.sp_artist_is_loaded.assert_called_once_with(sp_artist)
        self.assertTrue(result)

    @mock.patch('spotify.utils.load')
    def test_load(self, load_mock, lib_mock):
        sp_artist = spotify.ffi.new('int *')
        artist = spotify.Artist(self.session, sp_artist=sp_artist)

        artist.load(10)

        load_mock.assert_called_with(self.session, artist, timeout=10)

    @mock.patch('spotify.image.lib', spec=spotify.lib)
    def test_portrait(self, image_lib_mock, lib_mock):
        sp_image_id = spotify.ffi.new('char[]', b'portrait-id')
        lib_mock.sp_artist_portrait.return_value = sp_image_id
        sp_image = spotify.ffi.new('int *')
        lib_mock.sp_image_create.return_value = sp_image
        sp_artist = spotify.ffi.new('int *')
        artist = spotify.Artist(self.session, sp_artist=sp_artist)
        image_size = spotify.ImageSize.SMALL

        result = artist.portrait(image_size)

        lib_mock.sp_artist_portrait.assert_called_with(
            sp_artist, int(image_size))
        lib_mock.sp_image_create.assert_called_with(
            self.session._sp_session, sp_image_id)

        self.assertIsInstance(result, spotify.Image)
        self.assertEqual(result._sp_image, sp_image)

        # Since we *created* the sp_image, we already have a refcount of 1 and
        # shouldn't increase the refcount when wrapping this sp_image in an
        # Image object
        self.assertEqual(image_lib_mock.sp_image_add_ref.call_count, 0)

    def test_portrait_is_none_if_null(self, lib_mock):
        lib_mock.sp_artist_portrait.return_value = spotify.ffi.NULL
        sp_artist = spotify.ffi.new('int *')
        artist = spotify.Artist(self.session, sp_artist=sp_artist)

        result = artist.portrait()

        lib_mock.sp_artist_portrait.assert_called_with(
            sp_artist, int(spotify.ImageSize.NORMAL))
        self.assertIsNone(result)

    @mock.patch('spotify.Link', spec=spotify.Link)
    def test_portrait_link_creates_link_to_portrait(self, link_mock, lib_mock):
        sp_artist = spotify.ffi.new('int *')
        artist = spotify.Artist(self.session, sp_artist=sp_artist)
        sp_link = spotify.ffi.new('int *')
        lib_mock.sp_link_create_from_artist_portrait.return_value = sp_link
        link_mock.return_value = mock.sentinel.link

        result = artist.portrait_link(spotify.ImageSize.NORMAL)

        lib_mock.sp_link_create_from_artist_portrait.assert_called_once_with(
            sp_artist, spotify.ImageSize.NORMAL)
        link_mock.assert_called_once_with(
            self.session, sp_link=sp_link, add_ref=False)
        self.assertEqual(result, mock.sentinel.link)

    @mock.patch('spotify.Link', spec=spotify.Link)
    def test_link_creates_link_to_artist(self, link_mock, lib_mock):
        sp_artist = spotify.ffi.new('int *')
        artist = spotify.Artist(self.session, sp_artist=sp_artist)
        sp_link = spotify.ffi.new('int *')
        lib_mock.sp_link_create_from_artist.return_value = sp_link
        link_mock.return_value = mock.sentinel.link

        result = artist.link

        link_mock.assert_called_once_with(
            self.session, sp_link=sp_link, add_ref=False)
        self.assertEqual(result, mock.sentinel.link)


@mock.patch('spotify.artist.lib', spec=spotify.lib)
class ArtistBrowserTest(unittest.TestCase):

    def setUp(self):
        self.session = tests.create_session()
        spotify._session_instance = self.session

    def tearDown(self):
        spotify._session_instance = None

    def test_create_without_artist_or_sp_artistbrowse_fails(self, lib_mock):
        with self.assertRaises(AssertionError):
            spotify.ArtistBrowser(self.session)
<commit_msg>artist: Test default behavior of portrait() and portrait_link()<commit_after>from __future__ import unicode_literals

import unittest

import spotify
from spotify import utils
import tests
from tests import mock


@mock.patch('spotify.artist.lib', spec=spotify.lib)
class ArtistTest(unittest.TestCase):

    def setUp(self):
        self.session = tests.create_session()

    def test_create_without_uri_or_sp_artist_fails(self, lib_mock):
        with self.assertRaises(AssertionError):
            spotify.Artist(self.session)

    @mock.patch('spotify.Link', spec=spotify.Link)
    def test_create_from_uri(self, link_mock, lib_mock):
        sp_artist = spotify.ffi.new('int *')
        link_instance_mock = link_mock.return_value
        link_instance_mock.as_artist.return_value = spotify.Artist(
            self.session, sp_artist=sp_artist)
        lib_mock.sp_link_as_artist.return_value = sp_artist
        uri = 'spotify:artist:foo'

        result = spotify.Artist(self.session, uri=uri)

        link_mock.assert_called_with(self.session, uri=uri)
        link_instance_mock.as_artist.assert_called_with()
        lib_mock.sp_artist_add_ref.assert_called_with(sp_artist)
        self.assertEqual(result._sp_artist, sp_artist)

    @mock.patch('spotify.Link', spec=spotify.Link)
    def test_create_from_uri_fail_raises_error(self, link_mock, lib_mock):
        link_instance_mock = link_mock.return_value
        link_instance_mock.as_artist.return_value = None
        lib_mock.sp_link_as_artist.return_value = spotify.ffi.NULL
        uri = 'spotify:artist:foo'

        with self.assertRaises(ValueError):
            spotify.Artist(self.session, uri=uri)

    def test_adds_ref_to_sp_artist_when_created(self, lib_mock):
        sp_artist = spotify.ffi.new('int *')

        spotify.Artist(self.session, sp_artist=sp_artist)

        lib_mock.sp_artist_add_ref.assert_called_with(sp_artist)

    def test_releases_sp_artist_when_artist_dies(self, lib_mock):
        sp_artist = spotify.ffi.new('int *')

        artist = spotify.Artist(self.session, sp_artist=sp_artist)
        artist = None  # noqa
        tests.gc_collect()

        lib_mock.sp_artist_release.assert_called_with(sp_artist)

    @mock.patch('spotify.Link', spec=spotify.Link)
    def test_repr(self, link_mock, lib_mock):
        link_instance_mock = link_mock.return_value
        link_instance_mock.uri = 'foo'
        sp_artist = spotify.ffi.new('int *')
        artist = spotify.Artist(self.session, sp_artist=sp_artist)

        result = repr(artist)

        self.assertEqual(result, 'Artist(%r)' % 'foo')

    def test_name(self, lib_mock):
        lib_mock.sp_artist_name.return_value = spotify.ffi.new(
            'char[]', b'Foo Bar Baz')
        sp_artist = spotify.ffi.new('int *')
        artist = spotify.Artist(self.session, sp_artist=sp_artist)

        result = artist.name

        lib_mock.sp_artist_name.assert_called_once_with(sp_artist)
        self.assertEqual(result, 'Foo Bar Baz')

    def test_name_is_none_if_unloaded(self, lib_mock):
        lib_mock.sp_artist_name.return_value = spotify.ffi.new('char[]', b'')
        sp_artist = spotify.ffi.new('int *')
        artist = spotify.Artist(self.session, sp_artist=sp_artist)

        result = artist.name

        lib_mock.sp_artist_name.assert_called_once_with(sp_artist)
        self.assertIsNone(result)

    def test_is_loaded(self, lib_mock):
        lib_mock.sp_artist_is_loaded.return_value = 1
        sp_artist = spotify.ffi.new('int *')
        artist = spotify.Artist(self.session, sp_artist=sp_artist)

        result = artist.is_loaded

        lib_mock.sp_artist_is_loaded.assert_called_once_with(sp_artist)
        self.assertTrue(result)

    @mock.patch('spotify.utils.load')
    def test_load(self, load_mock, lib_mock):
        sp_artist = spotify.ffi.new('int *')
        artist = spotify.Artist(self.session, sp_artist=sp_artist)

        artist.load(10)

        load_mock.assert_called_with(self.session, artist, timeout=10)

    @mock.patch('spotify.image.lib', spec=spotify.lib)
    def test_portrait(self, image_lib_mock, lib_mock):
        sp_image_id = spotify.ffi.new('char[]', b'portrait-id')
        lib_mock.sp_artist_portrait.return_value = sp_image_id
        sp_image = spotify.ffi.new('int *')
        lib_mock.sp_image_create.return_value = sp_image
        sp_artist = spotify.ffi.new('int *')
        artist = spotify.Artist(self.session, sp_artist=sp_artist)
        image_size = spotify.ImageSize.SMALL

        result = artist.portrait(image_size)

        lib_mock.sp_artist_portrait.assert_called_with(
            sp_artist, int(image_size))
        lib_mock.sp_image_create.assert_called_with(
            self.session._sp_session, sp_image_id)

        self.assertIsInstance(result, spotify.Image)
        self.assertEqual(result._sp_image, sp_image)

        # Since we *created* the sp_image, we already have a refcount of 1 and
        # shouldn't increase the refcount when wrapping this sp_image in an
        # Image object
        self.assertEqual(image_lib_mock.sp_image_add_ref.call_count, 0)

    @mock.patch('spotify.image.lib', spec=spotify.lib)
    def test_portrait_defaults_to_normal_size(self, image_lib_mock, lib_mock):
        sp_image_id = spotify.ffi.new('char[]', b'portrait-id')
        lib_mock.sp_artist_portrait.return_value = sp_image_id
        sp_image = spotify.ffi.new('int *')
        lib_mock.sp_image_create.return_value = sp_image
        sp_artist = spotify.ffi.new('int *')
        artist = spotify.Artist(self.session, sp_artist=sp_artist)

        artist.portrait()

        lib_mock.sp_artist_portrait.assert_called_with(
            sp_artist, int(spotify.ImageSize.NORMAL))

    def test_portrait_is_none_if_null(self, lib_mock):
        lib_mock.sp_artist_portrait.return_value = spotify.ffi.NULL
        sp_artist = spotify.ffi.new('int *')
        artist = spotify.Artist(self.session, sp_artist=sp_artist)

        result = artist.portrait()

        lib_mock.sp_artist_portrait.assert_called_with(
            sp_artist, int(spotify.ImageSize.NORMAL))
        self.assertIsNone(result)

    @mock.patch('spotify.Link', spec=spotify.Link)
    def test_portrait_link_creates_link_to_portrait(self, link_mock, lib_mock):
        sp_artist = spotify.ffi.new('int *')
        artist = spotify.Artist(self.session, sp_artist=sp_artist)
        sp_link = spotify.ffi.new('int *')
        lib_mock.sp_link_create_from_artist_portrait.return_value = sp_link
        link_mock.return_value = mock.sentinel.link
        image_size = spotify.ImageSize.SMALL

        result = artist.portrait_link(image_size)

        lib_mock.sp_link_create_from_artist_portrait.assert_called_once_with(
            sp_artist, int(image_size))
        link_mock.assert_called_once_with(
            self.session, sp_link=sp_link, add_ref=False)
        self.assertEqual(result, mock.sentinel.link)

    @mock.patch('spotify.Link', spec=spotify.Link)
    def test_portrait_link_defaults_to_normal_size(self, link_mock, lib_mock):
        sp_artist = spotify.ffi.new('int *')
        artist = spotify.Artist(self.session, sp_artist=sp_artist)
        sp_link = spotify.ffi.new('int *')
        lib_mock.sp_link_create_from_artist_portrait.return_value = sp_link
        link_mock.return_value = mock.sentinel.link

        artist.portrait_link()

        lib_mock.sp_link_create_from_artist_portrait.assert_called_once_with(
            sp_artist, int(spotify.ImageSize.NORMAL))

    @mock.patch('spotify.Link', spec=spotify.Link)
    def test_link_creates_link_to_artist(self, link_mock, lib_mock):
        sp_artist = spotify.ffi.new('int *')
        artist = spotify.Artist(self.session, sp_artist=sp_artist)
        sp_link = spotify.ffi.new('int *')
        lib_mock.sp_link_create_from_artist.return_value = sp_link
        link_mock.return_value = mock.sentinel.link

        result = artist.link

        link_mock.assert_called_once_with(
            self.session, sp_link=sp_link, add_ref=False)
        self.assertEqual(result, mock.sentinel.link)


@mock.patch('spotify.artist.lib', spec=spotify.lib)
class ArtistBrowserTest(unittest.TestCase):

    def setUp(self):
        self.session = tests.create_session()
        spotify._session_instance = self.session

    def tearDown(self):
        spotify._session_instance = None

    def test_create_without_artist_or_sp_artistbrowse_fails(self, lib_mock):
        with self.assertRaises(AssertionError):
            spotify.ArtistBrowser(self.session)

{'fraction_non_alphanumeric': 0.09953235591506572, 'fraction_numerical': 0.000884732052578362, 'mean_word_length': 3.335616438356164, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6234024', 'n_tokens_mistral': 5564, 'n_tokens_neox': 5462, 'n_words': 718}