Skip to content

Commit

Permalink
added hyp3 instance as fixture
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewPlayer3 committed May 14, 2024
1 parent 2d83008 commit 0984f6a
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 82 deletions.
16 changes: 16 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,27 @@
import requests
import shutil
from datetime import datetime
from pathlib import Path
from unittest.mock import patch
from uuid import uuid4

import pytest

from hyp3_sdk import Job
from hyp3_sdk.hyp3 import HyP3


@pytest.fixture(autouse=True)
def get_mock_hyp3():
def mock_my_info(self):
return {'application_status': 'APPROVED'}
def mock_get_authenticated_session(username, password):
return requests.Session()
def default_hyp3():
with patch('hyp3_sdk.hyp3.HyP3.my_info', mock_my_info):
with patch('hyp3_sdk.util.get_authenticated_session', mock_get_authenticated_session):
return HyP3()
return default_hyp3


@pytest.fixture(autouse=True)
Expand Down
124 changes: 42 additions & 82 deletions tests/test_hyp3.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,8 @@ def mock_get_authenticated_session(username, password):


@responses.activate
def test_session_headers():
with patch('hyp3_sdk.hyp3.HyP3.my_info', mock_my_info):
with patch('hyp3_sdk.util.get_authenticated_session', mock_get_authenticated_session):
api = HyP3()
def test_session_headers(get_mock_hyp3):
api = get_mock_hyp3()

responses.add(responses.GET, urljoin(api.url, '/user'), json={'foo': 'bar'})

Expand All @@ -35,7 +33,7 @@ def test_session_headers():


@responses.activate
def test_find_jobs(get_mock_job):
def test_find_jobs(get_mock_hyp3, get_mock_job):
api_response_mock = {
'jobs': [
get_mock_job(name='job1').to_dict(),
Expand All @@ -47,9 +45,7 @@ def test_find_jobs(get_mock_job):
]
}

with patch('hyp3_sdk.hyp3.HyP3.my_info', mock_my_info):
with patch('hyp3_sdk.util.get_authenticated_session', mock_get_authenticated_session):
api = HyP3()
api = get_mock_hyp3()

responses.add(responses.GET, urljoin(api.url, '/jobs'), json=api_response_mock)
responses.add(responses.GET, urljoin(api.url, '/jobs'), json={'jobs': []})
Expand All @@ -62,10 +58,8 @@ def test_find_jobs(get_mock_job):


@responses.activate
def test_find_jobs_paging(get_mock_job):
with patch('hyp3_sdk.hyp3.HyP3.my_info', mock_my_info):
with patch('hyp3_sdk.util.get_authenticated_session', mock_get_authenticated_session):
api = HyP3()
def test_find_jobs_paging(get_mock_hyp3, get_mock_job):
api = get_mock_hyp3()

api_response_mock_1 = {
'jobs': [
Expand All @@ -90,10 +84,8 @@ def test_find_jobs_paging(get_mock_job):


@responses.activate
def test_find_jobs_user_id(get_mock_job):
with patch('hyp3_sdk.hyp3.HyP3.my_info', mock_my_info):
with patch('hyp3_sdk.util.get_authenticated_session', mock_get_authenticated_session):
api = HyP3()
def test_find_jobs_user_id(get_mock_hyp3, get_mock_job):
api = get_mock_hyp3()

responses.add(responses.GET, urljoin(api.url, '/jobs?user_id=foo'),
json={'jobs': []}, match_querystring=True)
Expand All @@ -109,10 +101,8 @@ def test_find_jobs_user_id(get_mock_job):


@responses.activate
def test_find_jobs_start():
with patch('hyp3_sdk.hyp3.HyP3.my_info', mock_my_info):
with patch('hyp3_sdk.util.get_authenticated_session', mock_get_authenticated_session):
api = HyP3()
def test_find_jobs_start(get_mock_hyp3):
api = get_mock_hyp3()

responses.add(responses.GET, urljoin(api.url, '/jobs?start=2021-01-01T00%3A00%3A00%2B00%3A00'),
json={'jobs': []}, match_querystring=True)
Expand All @@ -125,10 +115,8 @@ def test_find_jobs_start():


@responses.activate
def test_find_jobs_end():
with patch('hyp3_sdk.hyp3.HyP3.my_info', mock_my_info):
with patch('hyp3_sdk.util.get_authenticated_session', mock_get_authenticated_session):
api = HyP3()
def test_find_jobs_end(get_mock_hyp3):
api = get_mock_hyp3()

responses.add(responses.GET, urljoin(api.url, '/jobs?end=2021-01-02T00%3A00%3A00%2B00%3A00'),
json={'jobs': []}, match_querystring=True)
Expand All @@ -141,10 +129,8 @@ def test_find_jobs_end():


@responses.activate
def test_find_jobs_status_code():
with patch('hyp3_sdk.hyp3.HyP3.my_info', mock_my_info):
with patch('hyp3_sdk.util.get_authenticated_session', mock_get_authenticated_session):
api = HyP3()
def test_find_jobs_status_code(get_mock_hyp3):
api = get_mock_hyp3()

responses.add(responses.GET, urljoin(api.url, '/jobs?status_code=RUNNING'),
json={'jobs': []}, match_querystring=True)
Expand All @@ -158,24 +144,20 @@ def test_find_jobs_status_code():


@responses.activate
def test_get_job_by_id(get_mock_job):
def test_get_job_by_id(get_mock_hyp3, get_mock_job):
job = get_mock_job()
with patch('hyp3_sdk.hyp3.HyP3.my_info', mock_my_info):
with patch('hyp3_sdk.util.get_authenticated_session', mock_get_authenticated_session):
api = HyP3()
api = get_mock_hyp3()
responses.add(responses.GET, urljoin(api.url, f'/jobs/{job.job_id}'), json=job.to_dict())
response = api.get_job_by_id(job.job_id)
assert response == job


@responses.activate
def test_watch(get_mock_job):
def test_watch(get_mock_hyp3, get_mock_job):
incomplete_job = get_mock_job()
complete_job = Job.from_dict(incomplete_job.to_dict())
complete_job.status_code = 'SUCCEEDED'
with patch('hyp3_sdk.hyp3.HyP3.my_info', mock_my_info):
with patch('hyp3_sdk.util.get_authenticated_session', mock_get_authenticated_session):
api = HyP3()
api = get_mock_hyp3()
for ii in range(3):
responses.add(responses.GET, urljoin(api.url, f'/jobs/{incomplete_job.job_id}'),
json=incomplete_job.to_dict())
Expand All @@ -187,22 +169,20 @@ def test_watch(get_mock_job):


@responses.activate
def test_refresh(get_mock_job):
def test_refresh(get_mock_hyp3, get_mock_job):
job = get_mock_job()
new_job = Job.from_dict(job.to_dict())
new_job.status_code = 'SUCCEEDED'

with patch('hyp3_sdk.hyp3.HyP3.my_info', mock_my_info):
with patch('hyp3_sdk.util.get_authenticated_session', mock_get_authenticated_session):
api = HyP3()
api = get_mock_hyp3()

responses.add(responses.GET, urljoin(api.url, f'/jobs/{job.job_id}'), json=new_job.to_dict())
response = api.refresh(job)
assert response == new_job


@responses.activate
def test_submit_prepared_jobs(get_mock_job):
def test_submit_prepared_jobs(get_mock_hyp3, get_mock_job):
rtc_job = get_mock_job('RTC_GAMMA', job_parameters={'granules': ['g1']})
insar_job = get_mock_job('INSAR_GAMMA', job_parameters={'granules': ['g1', 'g2']})
api_response = {
Expand All @@ -212,9 +192,7 @@ def test_submit_prepared_jobs(get_mock_job):
]
}

with patch('hyp3_sdk.hyp3.HyP3.my_info', mock_my_info):
with patch('hyp3_sdk.util.get_authenticated_session', mock_get_authenticated_session):
api = HyP3()
api = get_mock_hyp3()

responses.add(responses.POST, urljoin(api.url, '/jobs'), json=api_response)

Expand Down Expand Up @@ -346,87 +324,77 @@ def test_deprecated_warning():


@responses.activate
def test_submit_autorift_job(get_mock_job):
def test_submit_autorift_job(get_mock_hyp3, get_mock_job):
job = get_mock_job('AUTORIFT', job_parameters={'granules': ['g1', 'g2']})
api_response = {
'jobs': [
job.to_dict()
]
}
with patch('hyp3_sdk.hyp3.HyP3.my_info', mock_my_info):
with patch('hyp3_sdk.util.get_authenticated_session', mock_get_authenticated_session):
api = HyP3()
api = get_mock_hyp3()
responses.add(responses.POST, urljoin(api.url, '/jobs'), json=api_response)
batch = api.submit_autorift_job('g1', 'g2')
assert batch.jobs[0] == job


@responses.activate
def test_submit_rtc_job(get_mock_job):
def test_submit_rtc_job(get_mock_hyp3, get_mock_job):
job = get_mock_job('RTC_GAMMA', job_parameters={'granules': ['g1']})
api_response = {
'jobs': [
job.to_dict()
]
}
with patch('hyp3_sdk.hyp3.HyP3.my_info', mock_my_info):
with patch('hyp3_sdk.util.get_authenticated_session', mock_get_authenticated_session):
api = HyP3()
api = get_mock_hyp3()
responses.add(responses.POST, urljoin(api.url, '/jobs'), json=api_response)
batch = api.submit_rtc_job('g1')
assert batch.jobs[0] == job


@responses.activate
def test_submit_insar_job(get_mock_job):
def test_submit_insar_job(get_mock_hyp3, get_mock_job):
job = get_mock_job('INSAR_GAMMA', job_parameters={'granules': ['g1', 'g2']})
api_response = {
'jobs': [
job.to_dict()
]
}
with patch('hyp3_sdk.hyp3.HyP3.my_info', mock_my_info):
with patch('hyp3_sdk.util.get_authenticated_session', mock_get_authenticated_session):
api = HyP3()
api = get_mock_hyp3()
responses.add(responses.POST, urljoin(api.url, '/jobs'), json=api_response)
batch = api.submit_insar_job('g1', 'g2')
assert batch.jobs[0] == job


@responses.activate
def test_submit_insar_isce_burst_job(get_mock_job):
def test_submit_insar_isce_burst_job(get_mock_hyp3, get_mock_job):
job = get_mock_job('INSAR_ISCE_BURST', job_parameters={'granules': ['g1', 'g2']})
api_response = {
'jobs': [
job.to_dict()
]
}
with patch('hyp3_sdk.hyp3.HyP3.my_info', mock_my_info):
with patch('hyp3_sdk.util.get_authenticated_session', mock_get_authenticated_session):
api = HyP3()
api = get_mock_hyp3()
responses.add(responses.POST, urljoin(api.url, '/jobs'), json=api_response)
batch = api.submit_insar_isce_burst_job('g1', 'g2')
assert batch.jobs[0] == job


@responses.activate
def test_resubmit_previous_job(get_mock_job):
def test_resubmit_previous_job(get_mock_hyp3, get_mock_job):
job = get_mock_job()
api_response = {
'jobs': [
job.to_dict()
]
}
with patch('hyp3_sdk.hyp3.HyP3.my_info', mock_my_info):
with patch('hyp3_sdk.util.get_authenticated_session', mock_get_authenticated_session):
api = HyP3()
api = get_mock_hyp3()
responses.add(responses.POST, urljoin(api.url, '/jobs'), json=api_response)
batch = api.submit_prepared_jobs(job.to_dict(for_resubmit=True))
assert batch.jobs[0] == job


@responses.activate
def test_my_info():
def test_my_info(get_mock_hyp3):
api_response = {
'job_names': [
'name1',
Expand All @@ -435,16 +403,14 @@ def test_my_info():
'remaining_credits': 25.,
'user_id': 'someUser'
}
with patch('hyp3_sdk.hyp3.HyP3.my_info', mock_my_info):
with patch('hyp3_sdk.util.get_authenticated_session', mock_get_authenticated_session):
api = HyP3()
api = get_mock_hyp3()
responses.add(responses.GET, urljoin(api.url, '/user'), json=api_response)
response = api.my_info()
assert response == api_response


@responses.activate
def test_check_credits():
def test_check_credits(get_mock_hyp3):
api_response = {
'job_names': [
'name1',
Expand All @@ -453,39 +419,33 @@ def test_check_credits():
'remaining_credits': 25.,
'user_id': 'someUser'
}
with patch('hyp3_sdk.hyp3.HyP3.my_info', mock_my_info):
with patch('hyp3_sdk.util.get_authenticated_session', mock_get_authenticated_session):
api = HyP3()
api = get_mock_hyp3()
responses.add(responses.GET, urljoin(api.url, '/user'), json=api_response)

assert math.isclose(api.check_credits(), 25.)


@responses.activate
def test_check_application_status_approved():
def test_check_application_status_approved(get_mock_hyp3):
with warnings.catch_warnings(record=True) as w:
with patch('hyp3_sdk.hyp3.HyP3.my_info', lambda x: {'user_id': 'someUser', 'application_status': 'APPROVED'}):
with patch('hyp3_sdk.util.get_authenticated_session', mock_get_authenticated_session):
_ = HyP3()
_ = get_mock_hyp3()
assert len(w) == 0


@responses.activate
def test_check_application_status_not_approved():
def test_check_application_status_not_approved(get_mock_hyp3):
with warnings.catch_warnings(record=True) as w:
with patch('hyp3_sdk.hyp3.HyP3.my_info', lambda x: {'user_id': 'someUser', 'application_status': 'PENDING'}):
with patch('hyp3_sdk.util.get_authenticated_session', mock_get_authenticated_session):
with patch('hyp3_sdk.util.get_authenticated_session', lambda username, password: requests.Session()):
_ = HyP3()
assert len(w) == 1
assert 'not yet applied for a monthly credit allotment' in str(w[0].message)


@responses.activate
def test_costs():
def test_costs(get_mock_hyp3):
api_response = {'foo': 5}
with patch('hyp3_sdk.hyp3.HyP3.my_info', mock_my_info):
with patch('hyp3_sdk.util.get_authenticated_session', mock_get_authenticated_session):
api = HyP3()
api = get_mock_hyp3()
responses.add(responses.GET, urljoin(api.url, '/costs'), json=api_response)

assert api.costs() == {'foo': 5}

0 comments on commit 0984f6a

Please sign in to comment.