<commit_before>import unittest
import logging
import openphoto

try:
    import tokens
except ImportError:
    print ("********************************************************************\n"
           "You need to create a 'tokens.py' file containing the following:\n\n"
           "   host = \"<test_url>\"\n"
           "   consumer_key = \"<test_consumer_key>\"\n"
           "   consumer_secret = \"<test_consumer_secret>\"\n"
           "   token = \"<test_token>\"\n"
           "   token_secret = \"<test_token_secret>\"\n"
           "   host = \"<hostname>\"\n\n"
           "WARNING: Don't use a production OpenPhoto instance for this!\n"
           "********************************************************************\n")
    raise

class TestBase(unittest.TestCase):
    TEST_TITLE = "Test Image - delete me!"
    TEST_TAG = "test_tag"
    TEST_ALBUM = "test_album"
    MAXIMUM_TEST_PHOTOS = 4 # Never have more the 4 photos on the test server
    testcase_name = "(unknown testcase)"
    api_version = None

    def __init__(self, *args, **kwds):
        unittest.TestCase.__init__(self, *args, **kwds)
        self.photos = []

        LOG_FILENAME = "tests.log"
        logging.basicConfig(filename="tests.log",
                            filemode="w",
                            format="%(message)s",
                            level=logging.INFO)

    @classmethod
    def setUpClass(cls):
        """ Ensure there is nothing on the server before running any tests """
        if cls.api_version is None:
            print "\nTesting Latest %s" % cls.testcase_name
        else:
            print "\nTesting %s v%d" % (cls.testcase_name, cls.api_version)

        cls.client = openphoto.OpenPhoto(tokens.host,
                                         tokens.consumer_key, tokens.consumer_secret,
                                         tokens.token, tokens.token_secret,
                                         cls.api_version)

        if cls.client.photos.list() != []:
            raise ValueError("The test server (%s) contains photos. "
                             "Please delete them before running the tests"
                             % tokens.host)

        if cls.client.tags.list() != []:
            raise ValueError("The test server (%s) contains tags. "
                             "Please delete them before running the tests"
                             % tokens.host)

        if cls.client.albums.list() != []:
            raise ValueError("The test server (%s) contains albums. "
                             "Please delete them before running the tests"
                             % tokens.host)

    @classmethod
    def tearDownClass(cls):
        """ Once all tests have finished, delete all photos, tags and albums"""
        cls._delete_all()

    def setUp(self):
        """
        Ensure the three test photos are present before each test.
        Give them each a tag.
        Put them into an album.
        """
        self.photos = self.client.photos.list()
        if len(self.photos) != 3:
#            print self.photos
            print "[Regenerating Photos]"
            if len(self.photos) > 0:
                self._delete_all()
            self._create_test_photos()
            self.photos = self.client.photos.list()

        self.tags = self.client.tags.list()
        if (len(self.tags) != 1 or
                self.tags[0].id != self.TEST_TAG or
                self.tags[0].count != 3):
            print "[Regenerating Tags]"
            self._delete_all()
            self._create_test_photos()
            self.photos = self.client.photos.list()
            self.tags = self.client.tags.list()
        if len(self.tags) != 1:
            print "Tags: %s" % self.tags
            raise Exception("Tag creation failed")

        self.albums = self.client.albums.list()
        if (len(self.albums) != 1 or
                self.albums[0].name != self.TEST_ALBUM or
                self.albums[0].count != "3"):
            print "[Regenerating Albums]"
            self._delete_all()
            self._create_test_photos()
            self.photos = self.client.photos.list()
            self.tags = self.client.tags.list()
            self.albums = self.client.albums.list()
        if len(self.albums) != 1:
            print "Albums: %s" % self.albums
            raise Exception("Album creation failed")

        logging.info("\nRunning %s..." % self.id())

    def tearDown(self):
        logging.info("Finished %s\n" % self.id())

    @classmethod
    def _create_test_photos(cls):
        """ Upload three test photos """
        album = cls.client.album.create(cls.TEST_ALBUM)
        photos = [
            cls.client.photo.upload("tests/test_photo1.jpg",
                                    title=cls.TEST_TITLE,
                                    albums=album.id),
            cls.client.photo.upload("tests/test_photo2.jpg",
                                    title=cls.TEST_TITLE,
                                    albums=album.id),
            cls.client.photo.upload("tests/test_photo3.jpg",
                                    title=cls.TEST_TITLE,
                                    albums=album.id),
            ]
        # Add the test tag, removing any autogenerated tags
        for photo in photos:
            photo.update(tags=cls.TEST_TAG)

    @classmethod
    def _delete_all(cls):
        photos = cls.client.photos.list()
        if len(photos) > cls.MAXIMUM_TEST_PHOTOS:
            raise ValueError("There too many photos on the test server - must always be less than %d."
                             % cls.MAXIMUM_TEST_PHOTOS)
        for photo in photos:
            photo.delete()
        for tag in cls.client.tags.list():
            tag.delete()
        for album in cls.client.albums.list():
            album.delete()
<commit_msg>Handle case where tag count is returned as string (as was the case in APIv1)<commit_after>import unittest
import logging
import openphoto

try:
    import tokens
except ImportError:
    print ("********************************************************************\n"
           "You need to create a 'tokens.py' file containing the following:\n\n"
           "   host = \"<test_url>\"\n"
           "   consumer_key = \"<test_consumer_key>\"\n"
           "   consumer_secret = \"<test_consumer_secret>\"\n"
           "   token = \"<test_token>\"\n"
           "   token_secret = \"<test_token_secret>\"\n"
           "   host = \"<hostname>\"\n\n"
           "WARNING: Don't use a production OpenPhoto instance for this!\n"
           "********************************************************************\n")
    raise

class TestBase(unittest.TestCase):
    TEST_TITLE = "Test Image - delete me!"
    TEST_TAG = "test_tag"
    TEST_ALBUM = "test_album"
    MAXIMUM_TEST_PHOTOS = 4 # Never have more the 4 photos on the test server
    testcase_name = "(unknown testcase)"
    api_version = None

    def __init__(self, *args, **kwds):
        unittest.TestCase.__init__(self, *args, **kwds)
        self.photos = []

        LOG_FILENAME = "tests.log"
        logging.basicConfig(filename="tests.log",
                            filemode="w",
                            format="%(message)s",
                            level=logging.INFO)

    @classmethod
    def setUpClass(cls):
        """ Ensure there is nothing on the server before running any tests """
        if cls.api_version is None:
            print "\nTesting Latest %s" % cls.testcase_name
        else:
            print "\nTesting %s v%d" % (cls.testcase_name, cls.api_version)

        cls.client = openphoto.OpenPhoto(tokens.host,
                                         tokens.consumer_key, tokens.consumer_secret,
                                         tokens.token, tokens.token_secret,
                                         cls.api_version)

        if cls.client.photos.list() != []:
            raise ValueError("The test server (%s) contains photos. "
                             "Please delete them before running the tests"
                             % tokens.host)

        if cls.client.tags.list() != []:
            raise ValueError("The test server (%s) contains tags. "
                             "Please delete them before running the tests"
                             % tokens.host)

        if cls.client.albums.list() != []:
            raise ValueError("The test server (%s) contains albums. "
                             "Please delete them before running the tests"
                             % tokens.host)

    @classmethod
    def tearDownClass(cls):
        """ Once all tests have finished, delete all photos, tags and albums"""
        cls._delete_all()

    def setUp(self):
        """
        Ensure the three test photos are present before each test.
        Give them each a tag.
        Put them into an album.
        """
        self.photos = self.client.photos.list()
        if len(self.photos) != 3:
#            print self.photos
            print "[Regenerating Photos]"
            if len(self.photos) > 0:
                self._delete_all()
            self._create_test_photos()
            self.photos = self.client.photos.list()

        self.tags = self.client.tags.list()
        if (len(self.tags) != 1 or
                self.tags[0].id != self.TEST_TAG or
                str(self.tags[0].count) != "3"):
            print "[Regenerating Tags]"
            self._delete_all()
            self._create_test_photos()
            self.photos = self.client.photos.list()
            self.tags = self.client.tags.list()
        if len(self.tags) != 1:
            print "Tags: %s" % self.tags
            raise Exception("Tag creation failed")

        self.albums = self.client.albums.list()
        if (len(self.albums) != 1 or
                self.albums[0].name != self.TEST_ALBUM or
                self.albums[0].count != "3"):
            print "[Regenerating Albums]"
            self._delete_all()
            self._create_test_photos()
            self.photos = self.client.photos.list()
            self.tags = self.client.tags.list()
            self.albums = self.client.albums.list()
        if len(self.albums) != 1:
            print "Albums: %s" % self.albums
            raise Exception("Album creation failed")

        logging.info("\nRunning %s..." % self.id())

    def tearDown(self):
        logging.info("Finished %s\n" % self.id())

    @classmethod
    def _create_test_photos(cls):
        """ Upload three test photos """
        album = cls.client.album.create(cls.TEST_ALBUM)
        photos = [
            cls.client.photo.upload("tests/test_photo1.jpg",
                                    title=cls.TEST_TITLE,
                                    albums=album.id),
            cls.client.photo.upload("tests/test_photo2.jpg",
                                    title=cls.TEST_TITLE,
                                    albums=album.id),
            cls.client.photo.upload("tests/test_photo3.jpg",
                                    title=cls.TEST_TITLE,
                                    albums=album.id),
            ]
        # Add the test tag, removing any autogenerated tags
        for photo in photos:
            photo.update(tags=cls.TEST_TAG)

    @classmethod
    def _delete_all(cls):
        photos = cls.client.photos.list()
        if len(photos) > cls.MAXIMUM_TEST_PHOTOS:
            raise ValueError("There too many photos on the test server - must always be less than %d."
                             % cls.MAXIMUM_TEST_PHOTOS)
        for photo in photos:
            photo.delete()
        for tag in cls.client.tags.list():
            tag.delete()
        for album in cls.client.albums.list():
            album.delete()

{'fraction_non_alphanumeric': 0.13026494565217392, 'fraction_numerical': 0.002972146739130435, 'mean_word_length': 1.6015020985199913, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 24, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4695809', 'n_tokens_mistral': 3258, 'n_tokens_neox': 3154, 'n_words': 760}