<s> """<STR_LIT>""" <EOL> from __future__ import absolute_import <EOL> import json <EOL> import requests <EOL> import six <EOL> from unittest import TestCase <EOL> from nose . plugins . attrib import attr <EOL> from nose . tools import raises <EOL> import plotly . tools as tls <EOL> from plotly . tests . utils import PlotlyTestCase <EOL> from plotly . plotly import plotly as py <EOL> from plotly . exceptions import PlotlyError , PlotlyEmptyDataError <EOL> from plotly . files import CONFIG_FILE <EOL> class TestPlot ( TestCase ) : <EOL> def setUp ( self ) : <EOL> py . sign_in ( '<STR_LIT>' , '<STR_LIT>' , <EOL> plotly_domain = '<STR_LIT>' ) <EOL> self . simple_figure = { '<STR_LIT:data>' : [ { '<STR_LIT:x>' : [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] , '<STR_LIT:y>' : [ <NUM_LIT:2> , <NUM_LIT:1> , <NUM_LIT:2> ] } ] } <EOL> super ( TestPlot , self ) . setUp ( ) <EOL> @ attr ( '<STR_LIT>' ) <EOL> def test_plot_valid ( self ) : <EOL> fig = { <EOL> '<STR_LIT:data>' : [ <EOL> { <EOL> '<STR_LIT:x>' : [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] , <EOL> '<STR_LIT:y>' : [ <NUM_LIT:2> , <NUM_LIT:1> , <NUM_LIT:2> ] <EOL> } <EOL> ] <EOL> } <EOL> py . plot ( fig , auto_open = False , filename = '<STR_LIT>' ) <EOL> @ raises ( PlotlyError ) <EOL> def test_plot_invalid ( self ) : <EOL> fig = { <EOL> '<STR_LIT:data>' : [ <EOL> { <EOL> '<STR_LIT:x>' : [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] , <EOL> '<STR_LIT:y>' : [ <NUM_LIT:2> , <NUM_LIT:1> , <NUM_LIT:2> ] , <EOL> '<STR_LIT:z>' : [ <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT:1> ] <EOL> } <EOL> ] <EOL> } <EOL> py . plot ( fig , auto_open = False , filename = '<STR_LIT>' ) <EOL> @ raises ( TypeError ) <EOL> def test_plot_invalid_args_1 ( self ) : <EOL> py . plot ( x = [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] , y = [ <NUM_LIT:2> , <NUM_LIT:1> , <NUM_LIT:2> ] , auto_open = False , filename = '<STR_LIT>' ) <EOL> @ raises ( PlotlyError ) <EOL> def test_plot_invalid_args_2 ( self ) : <EOL> py . plot ( [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] , [ <NUM_LIT:2> , <NUM_LIT:1> , <NUM_LIT:2> ] , auto_open = False , filename = '<STR_LIT>' ) <EOL> def test_plot_empty_data ( self ) : <EOL> self . assertRaises ( PlotlyEmptyDataError , py . plot , [ ] , <EOL> filename = '<STR_LIT>' ) <EOL> def test_plot_sharing_invalid_argument ( self ) : <EOL> kwargs = { '<STR_LIT:filename>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } <EOL> with self . assertRaisesRegexp ( <EOL> PlotlyError , <EOL> "<STR_LIT>" ) : <EOL> py . plot ( self . simple_figure , ** kwargs ) <EOL> def test_plot_world_readable_sharing_conflict_1 ( self ) : <EOL> kwargs = { '<STR_LIT:filename>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT>' : '<STR_LIT>' } <EOL> with self . assertRaisesRegexp ( <EOL> PlotlyError , <EOL> '<STR_LIT>' ) : <EOL> py . plot ( self . simple_figure , ** kwargs ) <EOL> def test_plot_world_readable_sharing_conflict_2 ( self ) : <EOL> kwargs = { '<STR_LIT:filename>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : '<STR_LIT>' } <EOL> with self . assertRaisesRegexp ( <EOL> PlotlyError , <EOL> '<STR_LIT>' ) : <EOL> py . plot ( self . simple_figure , ** kwargs ) <EOL> def test_plot_option_logic_only_world_readable_given ( self ) : <EOL> kwargs = { '<STR_LIT:filename>' : '<STR_LIT:test>' , <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : False } <EOL> plot_option_logic = py . _plot_option_logic ( kwargs ) <EOL> expected_plot_option_logic = { '<STR_LIT:filename>' : '<STR_LIT:test>' , <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT>' : '<STR_LIT>' } <EOL> self . assertEqual ( plot_option_logic , expected_plot_option_logic ) <EOL> def test_plot_option_logic_only_sharing_given ( self ) : <EOL> kwargs = { '<STR_LIT:filename>' : '<STR_LIT:test>' , <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : '<STR_LIT>' } <EOL> plot_option_logic = py . _plot_option_logic ( kwargs ) <EOL> expected_plot_option_logic = { '<STR_LIT:filename>' : '<STR_LIT:test>' , <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT>' : '<STR_LIT>' } <EOL> self . assertEqual ( plot_option_logic , expected_plot_option_logic ) <EOL> @ attr ( '<STR_LIT>' ) <EOL> def test_plot_url_given_sharing_key ( self ) : <EOL> validate = True <EOL> fig = tls . return_figure_from_figure_or_data ( self . simple_figure , <EOL> validate ) <EOL> kwargs = { '<STR_LIT:filename>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT>' : '<STR_LIT>' } <EOL> response = py . _send_to_plotly ( fig , ** kwargs ) <EOL> plot_url = response [ '<STR_LIT:url>' ] <EOL> self . assertTrue ( '<STR_LIT>' in plot_url ) <EOL> @ attr ( '<STR_LIT>' ) <EOL> def test_plot_url_response_given_sharing_key ( self ) : <EOL> kwargs = { '<STR_LIT:filename>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT>' : '<STR_LIT>' } <EOL> plot_url = py . plot ( self . simple_figure , ** kwargs ) <EOL> embed_url = plot_url . split ( '<STR_LIT:?>' ) [ <NUM_LIT:0> ] + '<STR_LIT>' + plot_url . split ( '<STR_LIT:?>' ) [ <NUM_LIT:1> ] <EOL> response = requests . get ( embed_url ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT:200> ) <EOL> @ attr ( '<STR_LIT>' ) <EOL> def test_private_plot_response_with_and_without_share_key ( self ) : <EOL> kwargs = { '<STR_LIT:filename>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT>' : '<STR_LIT>' } <EOL> private_plot_url = py . _send_to_plotly ( self . simple_figure , <EOL> ** kwargs ) [ '<STR_LIT:url>' ] <EOL> private_plot_response = requests . get ( private_plot_url + "<STR_LIT>" ) <EOL> self . assertEqual ( private_plot_response . status_code , <NUM_LIT> ) <EOL> secret_plot_url = py . add_share_key_to_url ( private_plot_url ) <EOL> urlsplit = six . moves . urllib . parse . urlparse ( secret_plot_url ) <EOL> secret_plot_json_file = six . moves . urllib . parse . urljoin ( <EOL> urlsplit . geturl ( ) , "<STR_LIT>" + urlsplit . query ) <EOL> secret_plot_response = requests . get ( secret_plot_json_file ) <EOL> self . assertTrue ( secret_plot_response . status_code , <NUM_LIT:200> ) <EOL> class TestPlotOptionLogic ( PlotlyTestCase ) : <EOL> conflicting_option_set = ( <EOL> { '<STR_LIT>' : True , '<STR_LIT>' : '<STR_LIT>' } , <EOL> { '<STR_LIT>' : True , '<STR_LIT>' : '<STR_LIT>' } , <EOL> { '<STR_LIT>' : False , '<STR_LIT>' : '<STR_LIT>' } <EOL> ) <EOL> def test_default_options ( self ) : <EOL> options = py . _plot_option_logic ( { } ) <EOL> config_options = tls . get_config_file ( ) <EOL> for key in options : <EOL> if key != '<STR_LIT>' and key in config_options : <EOL> self . assertEqual ( options [ key ] , config_options [ key ] ) <EOL> def test_conflicting_plot_options_in_plot_option_logic ( self ) : <EOL> for plot_options in self . conflicting_option_set : <EOL> self . assertRaises ( PlotlyError , py . _plot_option_logic , <EOL> plot_options ) <EOL> def test_set_config_updates_plot_options ( self ) : <EOL> original_config = tls . get_config_file ( ) <EOL> new_options = { <EOL> '<STR_LIT>' : not original_config [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : not original_config [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : ( '<STR_LIT>' if original_config [ '<STR_LIT>' ] is False <EOL> else '<STR_LIT>' ) <EOL> } <EOL> tls . set_config_file ( ** new_options ) <EOL> options = py . _plot_option_logic ( { } ) <EOL> for key in new_options : <EOL> self . assertEqual ( new_options [ key ] , options [ key ] ) <EOL> def generate_conflicting_plot_options_in_signin ( ) : <EOL> """<STR_LIT>""" <EOL> def gen_test ( plot_options ) : <EOL> def test ( self ) : <EOL> py . sign_in ( '<STR_LIT:username>' , '<STR_LIT:key>' , ** plot_options ) <EOL> self . assertRaises ( PlotlyError , py . _plot_option_logic , { } ) <EOL> return test <EOL> for i , plot_options in enumerate ( TestPlotOptionLogic . conflicting_option_set ) : <EOL> setattr ( TestPlotOptionLogic , <EOL> '<STR_LIT>' . format ( i ) , <EOL> gen_test ( plot_options ) ) <EOL> generate_conflicting_plot_options_in_signin ( ) <EOL> def generate_conflicting_plot_options_in_tools_dot_set_config ( ) : <EOL> """<STR_LIT>""" <EOL> def gen_test ( plot_options ) : <EOL> def test ( self ) : <EOL> self . assertRaises ( PlotlyError , tls . set_config_file , <EOL> ** plot_options ) <EOL> return test <EOL> for i , plot_options in enumerate ( TestPlotOptionLogic . conflicting_option_set ) : <EOL> setattr ( TestPlotOptionLogic , <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' . format ( i ) , <EOL> gen_test ( plot_options ) ) <EOL> generate_conflicting_plot_options_in_tools_dot_set_config ( ) <EOL> def generate_conflicting_plot_options_with_json_writes_of_config ( ) : <EOL> """<STR_LIT>""" <EOL> def gen_test ( plot_options ) : <EOL> def test ( self ) : <EOL> config = json . load ( open ( CONFIG_FILE ) ) <EOL> with open ( CONFIG_FILE , '<STR_LIT:w>' ) as f : <EOL> config . update ( plot_options ) <EOL> f . write ( json . dumps ( config ) ) <EOL> self . assertRaises ( PlotlyError , py . _plot_option_logic , { } ) <EOL> return test <EOL> for i , plot_options in enumerate ( TestPlotOptionLogic . conflicting_option_set ) : <EOL> setattr ( TestPlotOptionLogic , <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' . format ( i ) , <EOL> gen_test ( plot_options ) ) <EOL> generate_conflicting_plot_options_with_json_writes_of_config ( ) <EOL> def generate_private_sharing_and_public_world_readable_precedence ( ) : <EOL> """<STR_LIT>""" <EOL> plot_option_sets = ( <EOL> { <EOL> '<STR_LIT>' : { '<STR_LIT>' : True , '<STR_LIT>' : False } , <EOL> '<STR_LIT>' : { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : True } , <EOL> '<STR_LIT>' : { '<STR_LIT>' : False , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : True } <EOL> } , <EOL> { <EOL> '<STR_LIT>' : { '<STR_LIT>' : True , '<STR_LIT>' : True } , <EOL> '<STR_LIT>' : { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : False } , <EOL> '<STR_LIT>' : { '<STR_LIT>' : False , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : False } <EOL> } , <EOL> { <EOL> '<STR_LIT>' : { '<STR_LIT>' : False , '<STR_LIT>' : False } , <EOL> '<STR_LIT>' : { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : True } , <EOL> '<STR_LIT>' : { '<STR_LIT>' : True , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : True } <EOL> } <EOL> ) <EOL> def gen_test_signin ( plot_options ) : <EOL> def test ( self ) : <EOL> py . sign_in ( '<STR_LIT:username>' , '<STR_LIT:key>' , ** plot_options [ '<STR_LIT>' ] ) <EOL> options = py . _plot_option_logic ( plot_options [ '<STR_LIT>' ] ) <EOL> for option , value in plot_options [ '<STR_LIT>' ] . items ( ) : <EOL> self . assertEqual ( options [ option ] , value ) <EOL> return test <EOL> def gen_test_config ( plot_options ) : <EOL> def test ( self ) : <EOL> tls . set_config ( ** plot_options [ '<STR_LIT>' ] ) <EOL> options = py . _plot_option_logic ( plot_options [ '<STR_LIT>' ] ) <EOL> for option , value in plot_options [ '<STR_LIT>' ] . items ( ) : <EOL> self . assertEqual ( options [ option ] , value ) <EOL> for i , plot_options in enumerate ( plot_option_sets ) : <EOL> setattr ( TestPlotOptionLogic , <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' . format ( i ) , <EOL> gen_test_signin ( plot_options ) ) <EOL> setattr ( TestPlotOptionLogic , <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' . format ( i ) , <EOL> gen_test_config ( plot_options ) ) <EOL> generate_private_sharing_and_public_world_readable_precedence ( ) </s>
