Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor integration test "arrange" step for auth objects #752

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions tests/integration/conftest.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import logging
import os
import random
import unittest

import earthaccess
import pytest
from earthaccess import Auth, Store

ACCEPTABLE_FAILURE_RATE = 10

Expand All @@ -10,3 +17,58 @@ def pytest_sessionfinish(session, exitstatus):
failure_rate = (100.0 * session.testsfailed) / session.testscollected
if failure_rate <= ACCEPTABLE_FAILURE_RATE:
session.exitstatus = 0


@pytest.fixture(scope="module")
def authenticated_store():
logger = logging.getLogger(__name__)
assertions = unittest.TestCase("__init__")

# we need to use a valid EDL credential

assert "EARTHDATA_USERNAME" in os.environ
assert "EARTHDATA_PASSWORD" in os.environ

auth = Auth().login(strategy="environment")
assert auth.authenticated is True
logger.info(f"Current username: {os.environ['EARTHDATA_USERNAME']}")
logger.info(f"earthaccess version: {earthaccess.__version__}")

store = Store(auth)

return store, logger, assertions


@pytest.fixture()
def get_sample_granules(granules, sample_size, max_granule_size):
"""Returns a list with sample granules and their size in MB if
the total size is less than the max_granule_size.
"""
files_to_download = []
total_size = 0
max_tries = sample_size * 2
tries = 0

while tries <= max_tries:
g = random.sample(granules, 1)[0]
if g.size() > max_granule_size:
# print(f"G: {g['meta']['concept-id']} exceded max size: {g.size()}")
tries += 1
continue
else:
# print(f"Adding : {g['meta']['concept-id']} size: {g.size()}")
files_to_download.append(g)
total_size += g.size()
if len(files_to_download) >= sample_size:
break
return files_to_download, round(total_size)


@pytest.fixture()
def granules():
granules = earthaccess.search_data(
count=2,
short_name="SEA_SURFACE_HEIGHT_ALT_GRIDS_L4_2SATS_5DAY_6THDEG_V_JPL2205",
cloud_hosted=True,
)
return granules
10 changes: 5 additions & 5 deletions tests/integration/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
assertions = unittest.TestCase("__init__")


assertions.assertTrue("EARTHDATA_USERNAME" in os.environ)
assertions.assertTrue("EARTHDATA_PASSWORD" in os.environ)
assert "EARTHDATA_USERNAME" in os.environ
assert "EARTHDATA_PASSWORD" in os.environ

logger.info(f"Current username: {os.environ['EARTHDATA_USERNAME']}")
logger.info(f"earthaccess version: {earthaccess.__version__}")
Expand Down Expand Up @@ -46,13 +46,13 @@ def test_auth_returns_valid_auth_class():
auth = earthaccess.login(strategy="environment")
assertions.assertIsInstance(auth, earthaccess.Auth)
assertions.assertIsInstance(earthaccess.__auth__, earthaccess.Auth)
assertions.assertTrue(earthaccess.__auth__.authenticated)
assert earthaccess.__auth__.authenticated is True


def test_dataset_search_returns_none_with_no_parameters():
results = earthaccess.search_datasets()
assertions.assertIsInstance(results, list)
assertions.assertTrue(len(results) == 0)
assert (len(results) == 0) is True


@pytest.mark.parametrize("kwargs", dataset_valid_params)
Expand All @@ -66,7 +66,7 @@ def test_dataset_search_returns_valid_results(kwargs):
def test_granules_search_returns_valid_results(kwargs):
results = earthaccess.search_data(count=10, **kwargs)
assertions.assertIsInstance(results, list)
assertions.assertTrue(len(results) <= 10)
assert (len(results) <= 10) is True


@pytest.mark.parametrize("selection", [0, slice(None)])
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/test_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def test_auth_can_create_authenticated_requests_sessions():
def test_auth_can_fetch_s3_credentials():
activate_environment()
auth = earthaccess.login(strategy="environment")
assertions.assertTrue(auth.authenticated)
assert auth.authenticated is True
for daac in earthaccess.daac.DAACS:
if len(daac["s3-credentials"]) > 0:
try:
Expand Down
52 changes: 6 additions & 46 deletions tests/integration/test_cloud_download.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
# package imports
import logging
import os
import random
import shutil
import unittest
from pathlib import Path

import earthaccess
import pytest
from earthaccess import Auth, DataCollections, DataGranules, Store

logger = logging.getLogger(__name__)

from earthaccess import DataCollections, DataGranules

daac_list = [
{
Expand Down Expand Up @@ -56,47 +50,11 @@
},
]

assertions = unittest.TestCase("__init__")

# we need to use a valid EDL credential

assertions.assertTrue("EARTHDATA_USERNAME" in os.environ)
assertions.assertTrue("EARTHDATA_PASSWORD" in os.environ)

auth = Auth().login(strategy="environment")
assertions.assertTrue(auth.authenticated)
logger.info(f"Current username: {os.environ['EARTHDATA_USERNAME']}")
logger.info(f"earthaccess version: {earthaccess.__version__}")

store = Store(auth)


def get_sample_granules(granules, sample_size, max_granule_size):
"""Returns a list with sample granules and their size in MB if
the total size is less than the max_granule_size.
"""
files_to_download = []
total_size = 0
max_tries = sample_size * 2
tries = 0

while tries <= max_tries:
g = random.sample(granules, 1)[0]
if g.size() > max_granule_size:
# print(f"G: {g['meta']['concept-id']} exceded max size: {g.size()}")
tries += 1
continue
else:
# print(f"Adding : {g['meta']['concept-id']} size: {g.size()}")
files_to_download.append(g)
total_size += g.size()
if len(files_to_download) >= sample_size:
break
return files_to_download, round(total_size)


@pytest.mark.parametrize("daac", daac_list)
def test_earthaccess_can_download_cloud_collection_granules(daac):
def test_earthaccess_can_download_cloud_collection_granules(
authenticated_store, get_sample_granules, daac
):
"""Tests that we can download cloud collections using HTTPS links."""
daac_shortname = daac["short_name"]
collections_count = daac["collections_count"]
Expand All @@ -105,6 +63,8 @@ def test_earthaccess_can_download_cloud_collection_granules(daac):
granules_sample_size = daac["granules_sample_size"]
granules_max_size = daac["granules_max_size_mb"]

store, logger, assertions = authenticated_store

collection_query = DataCollections().data_center(daac_shortname).cloud_hosted(True)
hits = collection_query.hits()
logger.info(f"Cloud hosted collections for {daac_shortname}: {hits}")
Expand Down
56 changes: 8 additions & 48 deletions tests/integration/test_cloud_open.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
# package imports
import logging
import os
import random
import unittest

import earthaccess
import magic
import pytest
from earthaccess import Auth, DataCollections, DataGranules, Store

logger = logging.getLogger(__name__)

from earthaccess import DataCollections, DataGranules

daacs_list = [
{
Expand Down Expand Up @@ -55,44 +49,6 @@
},
]

assertions = unittest.TestCase("__init__")

# we need to use a valid EDL credential

assertions.assertTrue("EARTHDATA_USERNAME" in os.environ)
assertions.assertTrue("EARTHDATA_PASSWORD" in os.environ)

auth = Auth().login(strategy="environment")
assertions.assertTrue(auth.authenticated)
logger.info(f"Current username: {os.environ['EARTHDATA_USERNAME']}")
logger.info(f"earthaccess version: {earthaccess.__version__}")

store = Store(auth)


def get_sample_granules(granules, sample_size, max_granule_size):
"""Returns a list with sample granules and their size in MB if
the total size is less than the max_granule_size.
"""
files_to_download = []
total_size = 0
max_tries = sample_size * 2
tries = 0

while tries <= max_tries:
g = random.sample(granules, 1)[0]
if g.size() > max_granule_size:
# print(f"G: {g['meta']['concept-id']} exceded max size: {g.size()}")
tries += 1
continue
else:
# print(f"Adding : {g['meta']['concept-id']} size: {g.size()}")
files_to_download.append(g)
total_size += g.size()
if len(files_to_download) >= sample_size:
break
return files_to_download, round(total_size, 2)


def supported_collection(data_links):
for url in data_links:
Expand All @@ -102,7 +58,9 @@ def supported_collection(data_links):


@pytest.mark.parametrize("daac", daacs_list)
def test_earthaccess_can_open_onprem_collection_granules(daac):
def test_earthaccess_can_open_onprem_collection_granules(
authenticated_store, get_sample_granules, daac
):
"""Tests that we can download cloud collections using HTTPS links."""
daac_shortname = daac["short_name"]
collections_count = daac["collections_count"]
Expand All @@ -111,6 +69,8 @@ def test_earthaccess_can_open_onprem_collection_granules(daac):
granules_sample_size = daac["granules_sample_size"]
granules_max_size = daac["granules_max_size_mb"]

store, logger, assertions = authenticated_store

collection_query = DataCollections().data_center(daac_shortname).cloud_hosted(True)
hits = collection_query.hits()
logger.info(f"Cloud hosted collections for {daac_shortname}: {hits}")
Expand Down Expand Up @@ -146,7 +106,7 @@ def test_earthaccess_can_open_onprem_collection_granules(daac):
# We are testing this method
fileset = store.open(granules_to_open)

assertions.assertTrue(isinstance(fileset, list))
assert isinstance(fileset, list) is True

# we test that we can read some bytes and get the file type
for file in fileset:
Expand All @@ -156,7 +116,7 @@ def test_earthaccess_can_open_onprem_collection_granules(daac):
logger.warning(f"File could not be open: {file}")


def test_multi_file_granule():
def test_multi_file_granule(authenticated_store):
# Ensure granules that contain multiple files are handled correctly
granules = earthaccess.search_data(short_name="HLSL30", count=1)
assert len(granules) == 1
Expand Down
22 changes: 0 additions & 22 deletions tests/integration/test_kerchunk.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import logging
import os
import unittest
from pathlib import Path

import earthaccess
Expand All @@ -10,25 +7,6 @@
kerchunk = pytest.importorskip("kerchunk")
pytest.importorskip("dask")

logger = logging.getLogger(__name__)
assertions = unittest.TestCase("__init__")

assertions.assertTrue("EARTHDATA_USERNAME" in os.environ)
assertions.assertTrue("EARTHDATA_PASSWORD" in os.environ)

logger.info(f"Current username: {os.environ['EARTHDATA_USERNAME']}")
logger.info(f"earthaccess version: {earthaccess.__version__}")


@pytest.fixture(scope="module")
def granules():
granules = earthaccess.search_data(
count=2,
short_name="SEA_SURFACE_HEIGHT_ALT_GRIDS_L4_2SATS_5DAY_6THDEG_V_JPL2205",
cloud_hosted=True,
)
return granules


@pytest.mark.parametrize("protocol", ["", "file://"])
def test_consolidate_metadata_outfile(tmp_path, granules, protocol):
Expand Down
Loading
Loading