<s> '''<STR_LIT>''' <EOL> from contextlib import contextmanager <EOL> import json <EOL> import os <EOL> import sys <EOL> try : <EOL> from StringIO import StringIO as Buffy <EOL> except ImportError : <EOL> from io import BytesIO as Buffy <EOL> from click import ClickException <EOL> from click . testing import CliRunner <EOL> from mock import MagicMock <EOL> from mock import patch <EOL> import planet <EOL> from planet import api <EOL> from planet . api import models <EOL> from planet import scripts <EOL> from _common import read_fixture <EOL> from _common import clone <EOL> if api . auth . ENV_KEY in os . environ : <EOL> os . environ . pop ( api . auth . ENV_KEY ) <EOL> client = MagicMock ( name = '<STR_LIT>' , spec = api . Client ) <EOL> def run_cli ( * args , ** kw ) : <EOL> runner = CliRunner ( ) <EOL> with patch ( '<STR_LIT>' , lambda : client ) : <EOL> return runner . invoke ( scripts . cli , * args , ** kw ) <EOL> def assert_success ( result , expected ) : <EOL> assert result . exit_code == <NUM_LIT:0> <EOL> assert json . loads ( result . output ) == json . loads ( expected ) <EOL> def assert_cli_exception ( cause , expected ) : <EOL> def thrower ( ) : <EOL> raise cause <EOL> try : <EOL> scripts . call_and_wrap ( thrower ) <EOL> assert False , '<STR_LIT>' <EOL> except ClickException as ex : <EOL> assert str ( ex ) == expected <EOL> @ contextmanager <EOL> def stdin ( content ) : <EOL> saved = sys . stdin <EOL> sys . stdin = Buffy ( content . encode ( '<STR_LIT:utf-8>' ) ) <EOL> yield <EOL> sys . stdin = saved <EOL> def test_read ( tmpdir ) : <EOL> assert None is scripts . read ( None ) <EOL> assert '<STR_LIT:foo>' == scripts . read ( '<STR_LIT:foo>' ) <EOL> assert ( <NUM_LIT:1> , ) == scripts . read ( ( <NUM_LIT:1> , ) ) <EOL> assert None is scripts . read ( None , split = True ) <EOL> assert [ '<STR_LIT:foo>' ] == scripts . read ( '<STR_LIT:foo>' , split = True ) <EOL> assert ( <NUM_LIT:1> , ) == scripts . read ( ( <NUM_LIT:1> , ) , split = True ) <EOL> with stdin ( '<STR_LIT:text>' ) : <EOL> assert '<STR_LIT:text>' == scripts . read ( '<STR_LIT:->' ) <EOL> with stdin ( '<STR_LIT:text>' ) : <EOL> assert '<STR_LIT:text>' == scripts . read ( '<STR_LIT>' ) <EOL> infile = tmpdir . join ( '<STR_LIT>' ) <EOL> infile . write ( '<STR_LIT>' ) <EOL> assert '<STR_LIT>' == scripts . read ( '<STR_LIT>' % infile ) <EOL> assert '<STR_LIT>' == scripts . read ( '<STR_LIT:%s>' % infile ) <EOL> try : <EOL> noexist = '<STR_LIT>' <EOL> scripts . read ( '<STR_LIT>' % noexist ) <EOL> assert False <EOL> except ClickException as ex : <EOL> assert str ( ex ) == "<STR_LIT>" % noexist <EOL> xs = scripts . read ( '<STR_LIT>' , split = True ) <EOL> assert [ '<STR_LIT:x>' ] * <NUM_LIT:5> == xs <EOL> def test_exception_translation ( ) : <EOL> assert_cli_exception ( api . exceptions . BadQuery ( '<STR_LIT>' ) , '<STR_LIT>' ) <EOL> assert_cli_exception ( api . exceptions . APIException ( '<STR_LIT>' ) , <EOL> "<STR_LIT>" ) <EOL> def test_version_flag ( ) : <EOL> results = run_cli ( [ '<STR_LIT>' ] ) <EOL> assert results . output == "<STR_LIT>" % planet . __version__ <EOL> def test_workers_flag ( ) : <EOL> assert '<STR_LIT>' not in scripts . client_params <EOL> run_cli ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> assert '<STR_LIT>' in scripts . client_params <EOL> assert scripts . client_params [ '<STR_LIT>' ] == <NUM_LIT> <EOL> def test_api_key_flag ( ) : <EOL> run_cli ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> assert '<STR_LIT>' in scripts . client_params <EOL> assert scripts . client_params [ '<STR_LIT>' ] == '<STR_LIT>' <EOL> def test_no_api_key ( ) : <EOL> runner = CliRunner ( ) <EOL> def assert_no_api_key ( * args ) : <EOL> result = runner . invoke ( scripts . cli , * args ) <EOL> assert result . exit_code != <NUM_LIT:0> <EOL> assert '<STR_LIT>' == result . output <EOL> return True <EOL> assert all ( map ( assert_no_api_key , [ <EOL> ( '<STR_LIT>' , ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , ) , <EOL> ] ) ) <EOL> def test_search ( ) : <EOL> expected = read_fixture ( '<STR_LIT>' ) <EOL> response = MagicMock ( spec = models . JSON ) <EOL> response . get_raw . return_value = expected <EOL> client . get_scenes_list . return_value = response <EOL> result = run_cli ( [ '<STR_LIT>' ] ) <EOL> assert_success ( result , expected ) <EOL> def test_aoi_id_flag ( ) : <EOL> run_cli ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> client . get_scenes_list . assert_called_with ( <EOL> aoi_id = '<STR_LIT>' , count = <NUM_LIT:1000> , intersects = None , scene_type = '<STR_LIT>' ) <EOL> def test_search_by_aoi ( ) : <EOL> aoi = read_fixture ( '<STR_LIT>' ) <EOL> expected = read_fixture ( '<STR_LIT>' ) <EOL> response = MagicMock ( spec = models . JSON ) <EOL> response . get_raw . return_value = expected <EOL> client . get_scenes_list . return_value = response <EOL> result = run_cli ( [ '<STR_LIT>' ] , input = aoi ) <EOL> assert_success ( result , expected ) <EOL> def test_metadata ( ) : <EOL> expected = read_fixture ( '<STR_LIT>' ) <EOL> response = MagicMock ( spec = models . JSON ) <EOL> response . get_raw . return_value = expected <EOL> client . get_scene_metadata . return_value = response <EOL> result = run_cli ( [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> assert_success ( result , expected ) <EOL> def test_download ( ) : <EOL> response = MagicMock ( spec = models . Image ) <EOL> client . fetch_scene_geotiffs . return_value = response <EOL> def test ( ids , as_std_in = False ) : <EOL> args , input = [ '<STR_LIT>' ] , None <EOL> if as_std_in : <EOL> input = '<STR_LIT:\n>' . join ( ids ) <EOL> else : <EOL> args . extend ( ids ) <EOL> assert_correct ( run_cli ( args , input = input ) , ids ) <EOL> def assert_correct ( result , expected_ids ) : <EOL> assert result . exit_code == <NUM_LIT:0> <EOL> called_with = client . fetch_scene_geotiffs . call_args [ <NUM_LIT:0> ] <EOL> assert list ( expected_ids ) == list ( called_with [ <NUM_LIT:0> ] ) <EOL> assert '<STR_LIT>' == called_with [ <NUM_LIT:1> ] <EOL> assert '<STR_LIT>' == called_with [ <NUM_LIT:2> ] <EOL> test ( [ '<STR_LIT>' ] ) <EOL> test ( [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> test ( [ '<STR_LIT>' ] , as_std_in = True ) <EOL> test ( [ '<STR_LIT>' , '<STR_LIT>' ] , as_std_in = True ) <EOL> def test_thumbs ( ) : <EOL> response = MagicMock ( spec = models . Image ) <EOL> client . fetch_scene_thumbnails . return_value = response <EOL> def test ( ids , as_std_in = False ) : <EOL> args , input = [ '<STR_LIT>' ] , None <EOL> if as_std_in : <EOL> input = '<STR_LIT:\n>' . join ( ids ) <EOL> else : <EOL> args . extend ( ids ) <EOL> assert_correct ( run_cli ( args , input = input ) , ids ) <EOL> def assert_correct ( result , expected_ids ) : <EOL> assert result . exit_code == <NUM_LIT:0> <EOL> called_with = client . fetch_scene_thumbnails . call_args [ <NUM_LIT:0> ] <EOL> assert list ( expected_ids ) == list ( called_with [ <NUM_LIT:0> ] ) <EOL> assert '<STR_LIT>' == called_with [ <NUM_LIT:1> ] <EOL> assert '<STR_LIT>' == called_with [ <NUM_LIT:2> ] <EOL> test ( [ '<STR_LIT>' ] ) <EOL> test ( [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> test ( [ '<STR_LIT>' ] , as_std_in = True ) <EOL> test ( [ '<STR_LIT>' , '<STR_LIT>' ] , as_std_in = True ) <EOL> def test_init ( ) : <EOL> test_file = '<STR_LIT>' <EOL> api . utils . _planet_json_file = lambda : test_file <EOL> client . login . return_value = { <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> try : <EOL> result = run_cli ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> assert result . exit_code == <NUM_LIT:0> <EOL> assert os . path . exists ( test_file ) <EOL> with open ( test_file ) as fp : <EOL> data = json . loads ( fp . read ( ) ) <EOL> assert data [ '<STR_LIT:key>' ] == '<STR_LIT>' <EOL> finally : <EOL> if os . path . exists ( test_file ) : <EOL> os . unlink ( test_file ) <EOL> def test_mosaic_quads ( ) : <EOL> response = MagicMock ( spec = models . JSON ) <EOL> response . get_raw . return_value = '<STR_LIT>' <EOL> client . get_mosaic_quads . return_value = response <EOL> result = run_cli ( [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> assert result . exit_code == <NUM_LIT:0> <EOL> assert result . output == '<STR_LIT>' <EOL> called_with = client . get_mosaic_quads . call_args [ <NUM_LIT:0> ] <EOL> assert called_with [ <NUM_LIT:0> ] == '<STR_LIT>' <EOL> def _set_workspace ( workspace , * args , ** kw ) : <EOL> response = MagicMock ( spec = models . JSON ) <EOL> response . get_raw . return_value = '<STR_LIT>' <EOL> client . set_workspace . return_value = response <EOL> client . set_workspace . reset_mock ( ) <EOL> args = [ '<STR_LIT>' ] + list ( args ) <EOL> if workspace is not None : <EOL> args += [ json . dumps ( workspace ) ] <EOL> result = run_cli ( args , input = kw . get ( '<STR_LIT:input>' , None ) ) <EOL> assert result . exit_code == kw . get ( '<STR_LIT>' , <NUM_LIT:0> ) <EOL> def test_workspace_create_no_id ( ) : <EOL> workspace = json . loads ( read_fixture ( '<STR_LIT>' ) ) <EOL> workspace . pop ( '<STR_LIT:id>' ) <EOL> expected = clone ( workspace ) <EOL> _set_workspace ( workspace ) <EOL> client . set_workspace . assert_called_once_with ( expected , None ) <EOL> def test_workspace_create_from_existing ( ) : <EOL> workspace = json . loads ( read_fixture ( '<STR_LIT>' ) ) <EOL> expected = clone ( workspace ) <EOL> _set_workspace ( workspace , '<STR_LIT>' ) <EOL> client . set_workspace . assert_called_once_with ( expected , None ) <EOL> def test_workspace_update_from_existing_with_id ( ) : <EOL> workspace = json . loads ( read_fixture ( '<STR_LIT>' ) ) <EOL> expected = clone ( workspace ) <EOL> _set_workspace ( workspace , '<STR_LIT>' , '<STR_LIT>' ) <EOL> client . set_workspace . assert_called_once_with ( expected , '<STR_LIT>' ) <EOL> def test_workspace_update_stdin ( ) : <EOL> workspace = json . loads ( read_fixture ( '<STR_LIT>' ) ) <EOL> expected = clone ( workspace ) <EOL> _set_workspace ( workspace ) <EOL> client . set_workspace . assert_called_once_with ( expected , workspace [ '<STR_LIT:id>' ] ) <EOL> def test_workspace_create_aoi_stdin ( ) : <EOL> geometry = { '<STR_LIT:type>' : '<STR_LIT>' } <EOL> expected = { <EOL> '<STR_LIT:name>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : geometry <EOL> } <EOL> } <EOL> } <EOL> _set_workspace ( { } , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , json . dumps ( geometry ) ) <EOL> client . set_workspace . assert_called_once_with ( expected , None ) <EOL> _set_workspace ( { } , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> input = json . dumps ( geometry ) ) <EOL> client . set_workspace . assert_called_once_with ( expected , None ) <EOL> def test_set_workspace_filters ( ) : <EOL> _set_workspace ( { } , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:0>' ) <EOL> expected = { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : '<STR_LIT:0>' <EOL> } <EOL> } <EOL> } <EOL> client . set_workspace . assert_called_once_with ( expected , None ) </s>
