<commit_before>from __future__ import absolute_import

from unittest import TestCase
import imghdr
import tempfile
import os
import itertools

from nose.plugins.attrib import attr

from plotly.plotly import plotly as py


@attr('slow')
class TestImage(TestCase):
    def setUp(self):
        py.sign_in('PlotlyImageTest', '<PASSWORD>',
                   plotly_domain='https://plot.ly',
                   plotly_api_domain='https://api.plot.ly')

        self.data = [{'x': [1, 2, 3], 'y': [3, 1, 6]}]


def _generate_image_get_returns_valid_image_test(image_format,
                                                 width, height, scale):
    def test(self):
        image = py.image.get(self.data, image_format, width, height, scale)
        if image_format in ['png', 'jpeg']:
            assert imghdr.what('', image) == image_format

    return test


def _generate_image_save_as_saves_valid_image(image_format,
                                              width, height, scale):
    def _test(self):
        f, filename = tempfile.mkstemp('.{}'.format(image_format))
        py.image.save_as(self.data, filename, format=image_format,
                         width=width, height=height, scale=scale)
        if image_format in ['png', 'jpeg']:
            assert imghdr.what(filename) == image_format
        else:
            assert os.path.getsize(filename) > 0

        os.remove(filename)

    return _test

kwargs = {
    'format': ['png', 'jpeg', 'pdf', 'svg'],
    'width': [None, 300],<commit_msg>Quick fix for intermittent image server tests.<commit_after>from __future__ import absolute_import

from unittest import TestCase
import imghdr
import tempfile
import os
import itertools
import warnings

from nose.plugins.attrib import attr

from plotly import exceptions
from plotly.plotly import plotly as py


@attr('slow')
class TestImage(TestCase):
    def setUp(self):
        py.sign_in('PlotlyImageTest', '786r5mecv0',
                   plotly_domain='https://plot.ly',
                   plotly_api_domain='https://api.plot.ly')

        self.data = [{'x': [1, 2, 3], 'y': [3, 1, 6]}]


def _generate_image_get_returns_valid_image_test(image_format,
                                                 width, height, scale):
    def test(self):
        # TODO: better understand why this intermittently fails. See #649
        num_attempts = 5
        for i in range(num_attempts):
            if i > 0:
                warnings.warn('image test intermittently failed, retrying...')
            try:
                image = py.image.get(self.data, image_format, width, height,
                                     scale)
                if image_format in ['png', 'jpeg']:
                    assert imghdr.what('', image) == image_format
                return
            except (KeyError, exceptions.PlotlyError):
                if i == num_attempts - 1:
                    raise

    return test


def _generate_image_save_as_saves_valid_image(image_format,
                                              width, height, scale):
    def _test(self):
        f, filename = tempfile.mkstemp('.{}'.format(image_format))
        py.image.save_as(self.data, filename, format=image_format,
                         width=width, height=height, scale=scale)
        if image_format in ['png', 'jpeg']:
            assert imghdr.what(filename) == image_format
        else:
            assert os.path.getsize(filename) > 0

        os.remove(filename)

    return _test

kwargs = {
    'format': ['png', 'jpeg', 'pdf', 'svg'],
    'width': [None, 300],
{'fraction_non_alphanumeric': 0.11101766190075694, 'fraction_numerical': 0.008690776562938042, 'mean_word_length': 2.113438045375218, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 7, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 9, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3130531', 'n_tokens_mistral': 1071, 'n_tokens_neox': 1004, 'n_words': 260}