Skip to content

Commit

Permalink
Added Fixtures to conftest.py
Browse files Browse the repository at this point in the history
  • Loading branch information
Sherwin-14 committed Jul 8, 2024
1 parent e7cb5bc commit b5ed7bf
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 207 deletions.
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 is True
assert "EARTHDATA_PASSWORD" in os.environ is True

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
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
54 changes: 7 additions & 47 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 @@ -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
52 changes: 6 additions & 46 deletions tests/integration/test_onprem_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

daacs_list = [
{
Expand Down Expand Up @@ -48,44 +42,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 @@ -95,7 +51,9 @@ def supported_collection(data_links):


@pytest.mark.parametrize("daac", daacs_list)
def test_earthaccess_can_download_onprem_collection_granules(daac):
def test_earthaccess_can_download_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 @@ -104,6 +62,8 @@ def test_earthaccess_can_download_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(False)
hits = collection_query.hits()
logger.info(f"Cloud hosted collections for {daac_shortname}: {hits}")
Expand Down
Loading

0 comments on commit b5ed7bf

Please sign in to comment.