def test_parse_result_set(self): result = ErosWrapper.parse_result_set(None) assert result == [] result = ErosWrapper.parse_result_set([]) assert result == [] result = ErosWrapper.parse_result_set([{ 'displayId': 'aaaa', 'foo': 'bar' }]) assert result == ['aaaa'] result = ErosWrapper.parse_result_set([ { 'displayId': 'a', 'foo': 'bar' }, { 'displayId': 'b', 'foo': 'bar' }, ]) assert result == ['a', 'b'] result = ErosWrapper.parse_result_set([{'displayId': 'a'}, {}]) assert result == ['a']
def test_eros_prepare(self): with mock.patch('theia.adapters.usgs.ErosWrapper.token') as mockToken: mockToken.return_value = 'aaaaaa' result = ErosWrapper.eros_prepare(None) assert result == { 'headers': { 'Content-Type': 'application/json', 'X-Auth-Token': 'aaaaaa' }, 'params': {} } result = ErosWrapper.eros_prepare({'key': 'value'}) assert result == { 'headers': { 'Content-Type': 'application/json', 'X-Auth-Token': 'aaaaaa' }, 'params': { 'jsonRequest': '{"key": "value"}' } } result = ErosWrapper.eros_prepare({'key': 'value'}, params={'foo': 'bar'}) assert result == { 'headers': { 'Content-Type': 'application/json', 'X-Auth-Token': 'aaaaaa' }, 'params': { 'foo': 'bar', 'jsonRequest': '{"key": "value"}' } } result = ErosWrapper.eros_prepare({'key': 'value'}, headers={'X-Foo': 'bar'}) assert result == { 'headers': { 'Content-Type': 'application/json', 'X-Auth-Token': 'aaaaaa', 'X-Foo': 'bar' }, 'params': { 'jsonRequest': '{"key": "value"}' } }
def test_successful_connect(self, mockPost, mockToken, mockPassword, mockUsername): result = ErosWrapper.connect() mockPost.assert_called_once_with('login', { 'username': '******', 'password': '******' })
def test_search(self, mockParse, mockSearchOnce): result = ErosWrapper.search({'search': 'foo'}) assert (result == ['parsed results']) mockSearchOnce.assert_called_once_with({ 'search': 'foo', 'startingNumber': 0 }) mockParse.assert_called_once_with(['a'])
def test_eros_get(self, mockPost, mockUrl, mockPrepare, mockConnect): ErosWrapper.auth_token = None with patch.object(mockPost.return_value, 'json', return_value='some json string') as mockJson: result = ErosWrapper.eros_get('endpoint', {'foo': 'bar'}, thing='thing') mockConnect.assert_called_once() mockPrepare.assert_called_once_with({'foo': 'bar'}, thing='thing') mockUrl.assert_called_once_with('endpoint') mockPost.assert_called_once_with('api_url', prepare='result') assert (result == 'some json string')
def test_send_search_request(self, mockRequestSend, mockPassword, mockUsername): fakeSearch = {'datasetName': 'LANDSAT_BAND'} ErosWrapper().search(search=fakeSearch) mockRequestSend.assert_has_calls([ call( 'https://m2m.cr.usgs.gov/api/api/json/stable/login', { 'username': '******', 'password': '******' }), call('https://m2m.cr.usgs.gov/api/api/json/stable/scene-search', {'datasetName': 'LANDSAT_BAND'}, {'results': ['LC01_FAKESCENE_007']}) ], any_order=False)
def test_token(self): assert (ErosWrapper.token() == None)
def test_access_level(self): with mock.patch('theia.adapters.usgs.ErosWrapper.connect'), \ mock.patch('theia.adapters.usgs.ErosWrapper.eros_post') as mocked: ErosWrapper.access_level() mocked.assert_called_once_with('', None)
def test_unsuccessful_connect(self, mockPost, mockToken): with pytest.raises(Exception) as connectError: result = ErosWrapper.connect()
def test_search_once(self, mockPost): result = ErosWrapper.search_once({'search': 'foo'}) assert (result == 'foo') mockPost.assert_called_once_with('search', {'search': 'foo'})
def test_api_url(self): assert (ErosWrapper.api_url('foo') == 'https://earthexplorer.usgs.gov/inventory/json/v/stable/foo')
def test_token_present(self, mockPost, mockToken): result = ErosWrapper.connect() assert (result == 'aaaaaa') mockPost.assert_not_called()