<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""A test harness for checking that api calls mutate the server
in the expected fashion. Unit testing is also performed.

A successful test run should not appear to mutate the library
when it is finished, but no guarantees are made."""


import copy
from glob import glob
import logging
import os
import random
import string
import sys
import time
import unittest

from gmusicapi.protocol.metadata import md_expectations
from ..test import utils as test_utils

#unittest.main() will set __name__ to '__main__'; use a static name
log = logging.getLogger('gmusicapi.test.integration_test_api')

#Test files are located in the same directory as this test runner.
cwd = os.getcwd()
os.chdir(os.path.dirname(sys.argv[0]))
test_filenames = glob(u'audiotest*')
os.chdir(cwd)


class TestWCApiCalls(test_utils.BaseTest):

    @classmethod
    def setUpClass(cls):
        super(TestWCApiCalls, cls).setUpClass()

        #Get the full path of the test files.
        #Can't use abspath since this is relative to where _this_ file is,
        # not necessarily the calling curdir.
        path = os.path.realpath(__file__)
        real_path = lambda lp: path[:string.rfind(path, os.sep)] + os.sep + lp
        cls.test_filenames = map(real_path, test_filenames)

    #---
    #   Monolithic tests:
    #   (messy, but less likely to destructively modify the library)
    #   Modified from http://stackoverflow.com/questions/5387299/python-unittest-testcase-execution-order
    #---

    def pl_1_create(self):
        """Create a playlist."""
        self.api.create_playlist('test playlist')

        #Need to reload playlists so it appears.
        self.playlists = self.api.get_all_playlist_ids()['user']


    def pl_2_add_song(self):
        """Add a random song to the playlist."""
        p_id = self.playlists['test playlist'][-1]

        self.api.add_songs_to_playlist(p_id, self.r_song_id)

        #Verify the playlist has it.
        tracks = self.api.get_playlist_songs(p_id)

        self.assertEqual(tracks[0]["id"], self.r_song_id)


    def pl_2a_remove_song(self):
        """Remove a song from the playlist."""
        p_id = self.playlists['test playlist'][-1]

        sid = self.api.get_playlist_songs(p_id)[0]["id"]

        self.api.remove_songs_from_playlist(p_id, sid)

        #Verify.
        tracks = self.api.get_playlist_songs(p_id)

        self.assertEqual(len(tracks), 0)

    def pl_3_change_name(self):
        """Change the playlist's name."""
        p_id = self.playlists['test playlist'][-1]

        self.api.change_playlist_name(p_id, 'modified playlist')

        self.playlists = self.api.get_all_playlist_ids()['user']

    def pl_4_delete(self):
        """Delete the playlist."""
        self.api.delete_playlist(self.playlists['modified playlist'][-1])

        self.playlists = self.api.get_all_playlist_ids()['user']


    def test_playlists(self):
        self.run_steps("pl")

    def cpl_1_create(self):
        """Create and populate a random playlist."""
        self.api.create_playlist('playlist to change')

        #Need to reload playlists so it appears.
        self.playlists = self.api.get_all_playlist_ids()['user']

        p_id = self.playlists['playlist to change'][-1]

        self.api.add_songs_to_playlist(p_id, [s["id"] for s in random.sample(self.library, 10)])

    def cpl_2_change(self):
        """Change the playlist with random deletions, additions and reordering."""
        p_id = self.playlists['playlist to change'][-1]
        tracks = self.api.get_playlist_songs(p_id)

        #Apply random modifications.
        delete, add_dupe, add_blank, reorder = [random.choice([True, False]) for i in xrange(4)]

        if tracks and delete:
            log.debug("deleting tracks")
            track_is = range(len(tracks))
            #Select a random number of indices to delete.
            del_is = set(random.sample(track_is, random.choice(track_is)))
            tracks = [track for i, track in enumerate(tracks) if not i in del_is]

        if add_dupe:
            log.debug("adding dupe tracks from same playlist")
            tracks.extend(random.sample(tracks, random.randrange(len(tracks))))

        if add_blank:
            log.debug("adding random tracks with no eid")
            tracks.extend(random.sample(self.library, random.randrange(len(tracks))))

        if reorder:
            log.debug("shuffling tracks")
            random.shuffle(tracks)

        self.api.change_playlist(p_id, tracks)

        server_tracks = self.api.get_playlist_songs(p_id)

        self.assertEqual(len(tracks), len(server_tracks))

        for local_t, server_t in zip(tracks, server_tracks):
            self.assertEqual(local_t['id'], server_t['id'])

    def cpl_3_delete(self):
        """Delete the playlist."""
        self.api.delete_playlist(self.playlists['playlist to change'][-1])

        self.playlists = self.api.get_all_playlist_ids()['user']

    def test_change_playlist(self):
        self.run_steps("cpl")

    def updel_1_upload(self):
        """Upload test files."""

        uploaded, matched, not_uploaded = self.api.upload(self.test_filenames)
        if not_uploaded:
            self.fail("upload failed: %s" % not_uploaded)

        #A bit messy; need to pass the ids on to the next step.
        self.uploaded_ids = uploaded.values() + matched.values()

    def updel_1a_get_dl_info(self):
        """Check how many times the newly uploaded songs have been
        downloaded."""
        #gross - upload servers need time to sync
        time.sleep(5)

        info_tuples = [self.api.get_song_download_info(sid) for sid in
                       self.uploaded_ids]

        for info_tuple in info_tuples:
            self.assertNotEqual(info_tuple[0], None, 'should be available for download')

    def updel_2_delete(self):
        """Delete the uploaded test files."""
        self.api.delete_songs(self.uploaded_ids)
        del self.uploaded_ids
<commit_msg>increase wait time in tests for upload server syncing<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""A test harness for checking that api calls mutate the server
in the expected fashion. Unit testing is also performed.

A successful test run should not appear to mutate the library
when it is finished, but no guarantees are made."""


import copy
from glob import glob
import logging
import os
import random
import string
import sys
import time
import unittest

from gmusicapi.protocol.metadata import md_expectations
from ..test import utils as test_utils

#unittest.main() will set __name__ to '__main__'; use a static name
log = logging.getLogger('gmusicapi.test.integration_test_api')

#Test files are located in the same directory as this test runner.
cwd = os.getcwd()
os.chdir(os.path.dirname(sys.argv[0]))
test_filenames = glob(u'audiotest*')
os.chdir(cwd)


class TestWCApiCalls(test_utils.BaseTest):

    @classmethod
    def setUpClass(cls):
        super(TestWCApiCalls, cls).setUpClass()

        #Get the full path of the test files.
        #Can't use abspath since this is relative to where _this_ file is,
        # not necessarily the calling curdir.
        path = os.path.realpath(__file__)
        real_path = lambda lp: path[:string.rfind(path, os.sep)] + os.sep + lp
        cls.test_filenames = map(real_path, test_filenames)

    #---
    #   Monolithic tests:
    #   (messy, but less likely to destructively modify the library)
    #   Modified from http://stackoverflow.com/questions/5387299/python-unittest-testcase-execution-order
    #---

    def pl_1_create(self):
        """Create a playlist."""
        self.api.create_playlist('test playlist')

        #Need to reload playlists so it appears.
        self.playlists = self.api.get_all_playlist_ids()['user']


    def pl_2_add_song(self):
        """Add a random song to the playlist."""
        p_id = self.playlists['test playlist'][-1]

        self.api.add_songs_to_playlist(p_id, self.r_song_id)

        #Verify the playlist has it.
        tracks = self.api.get_playlist_songs(p_id)

        self.assertEqual(tracks[0]["id"], self.r_song_id)


    def pl_2a_remove_song(self):
        """Remove a song from the playlist."""
        p_id = self.playlists['test playlist'][-1]

        sid = self.api.get_playlist_songs(p_id)[0]["id"]

        self.api.remove_songs_from_playlist(p_id, sid)

        #Verify.
        tracks = self.api.get_playlist_songs(p_id)

        self.assertEqual(len(tracks), 0)

    def pl_3_change_name(self):
        """Change the playlist's name."""
        p_id = self.playlists['test playlist'][-1]

        self.api.change_playlist_name(p_id, 'modified playlist')

        self.playlists = self.api.get_all_playlist_ids()['user']

    def pl_4_delete(self):
        """Delete the playlist."""
        self.api.delete_playlist(self.playlists['modified playlist'][-1])

        self.playlists = self.api.get_all_playlist_ids()['user']


    def test_playlists(self):
        self.run_steps("pl")

    def cpl_1_create(self):
        """Create and populate a random playlist."""
        self.api.create_playlist('playlist to change')

        #Need to reload playlists so it appears.
        self.playlists = self.api.get_all_playlist_ids()['user']

        p_id = self.playlists['playlist to change'][-1]

        self.api.add_songs_to_playlist(p_id, [s["id"] for s in random.sample(self.library, 10)])

    def cpl_2_change(self):
        """Change the playlist with random deletions, additions and reordering."""
        p_id = self.playlists['playlist to change'][-1]
        tracks = self.api.get_playlist_songs(p_id)

        #Apply random modifications.
        delete, add_dupe, add_blank, reorder = [random.choice([True, False]) for i in xrange(4)]

        if tracks and delete:
            log.debug("deleting tracks")
            track_is = range(len(tracks))
            #Select a random number of indices to delete.
            del_is = set(random.sample(track_is, random.choice(track_is)))
            tracks = [track for i, track in enumerate(tracks) if not i in del_is]

        if add_dupe:
            log.debug("adding dupe tracks from same playlist")
            tracks.extend(random.sample(tracks, random.randrange(len(tracks))))

        if add_blank:
            log.debug("adding random tracks with no eid")
            tracks.extend(random.sample(self.library, random.randrange(len(tracks))))

        if reorder:
            log.debug("shuffling tracks")
            random.shuffle(tracks)

        self.api.change_playlist(p_id, tracks)

        server_tracks = self.api.get_playlist_songs(p_id)

        self.assertEqual(len(tracks), len(server_tracks))

        for local_t, server_t in zip(tracks, server_tracks):
            self.assertEqual(local_t['id'], server_t['id'])

    def cpl_3_delete(self):
        """Delete the playlist."""
        self.api.delete_playlist(self.playlists['playlist to change'][-1])

        self.playlists = self.api.get_all_playlist_ids()['user']

    def test_change_playlist(self):
        self.run_steps("cpl")

    def updel_1_upload(self):
        """Upload test files."""

        uploaded, matched, not_uploaded = self.api.upload(self.test_filenames)
        if not_uploaded:
            self.fail("upload failed: %s" % not_uploaded)

        #A bit messy; need to pass the ids on to the next step.
        self.uploaded_ids = uploaded.values() + matched.values()

    def updel_1a_get_dl_info(self):
        """Check how many times the newly uploaded songs have been
        downloaded."""
        #gross - upload servers need time to sync
        time.sleep(15)

        info_tuples = [self.api.get_song_download_info(sid) for sid in
                       self.uploaded_ids]

        for info_tuple in info_tuples:
            self.assertNotEqual(info_tuple[0], None, 'should be available for download')

    def updel_2_delete(self):
        """Delete the uploaded test files."""
        self.api.delete_songs(self.uploaded_ids)
        del self.uploaded_ids

{'fraction_non_alphanumeric': 0.11058765187205555, 'fraction_numerical': 0.005868253574675593, 'mean_word_length': 2.8522763451130215, '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': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3297144', 'n_tokens_mistral': 3927, 'n_tokens_neox': 3730, 'n_words': 948}