Skip to content

Commit

Permalink
wrote a better, less brittle test
Browse files Browse the repository at this point in the history
  • Loading branch information
christinahedges committed Jan 6, 2025
1 parent 7e07ec4 commit 6ae6618
Showing 1 changed file with 34 additions and 23 deletions.
57 changes: 34 additions & 23 deletions tests/test_missionsearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import os
import pytest
from time import time
import socket
from contextlib import contextmanager

from numpy.testing import assert_almost_equal, assert_array_equal
import numpy as np
Expand Down Expand Up @@ -580,32 +582,41 @@ def test_tess_return_clouduri_not_download():
def test_cached_files_no_filesize_check():
"""Test to see if turning off the file size check results in a faster return."""

conf.reload()
conf.CHECK_CACHED_FILE_SIZES = True
sr = KeplerSearch("Kepler-10", exptime=1800, quarter=1).timeseries
@contextmanager
def monitor_socket():
original_socket = socket.socket

# Download file once, shouldn't be cached, should be longest DL time
start = time()
sr.download(cloud=False, cache=False)
dur1 = time() - start
# Download second time, ensure it's in the cache
sr.download(cloud=False, cache=True)

# Download third time, check against cache, should be slow
start = time()
sr.download(cloud=False, cache=True)
dur2 = time() - start
class WrappedSocket(original_socket):
def connect(self, address):
print(f"Network call to: {address}")
raise RuntimeError("Function uses internet access.")

# Getting the cached data should be faster
assert dur2 < dur1
socket.socket = WrappedSocket
try:
yield
finally:
socket.socket = original_socket

# Set no file size checking, should be fastest.
conf.CHECK_CACHED_FILE_SIZES = True
conf.reload()
sr = KeplerSearch("Kepler-10", exptime=1800, quarter=1).timeseries

# Download third time, check against cache, should be slow
start = time()
# ensure file is in the cache
sr.download(cloud=False, cache=True)
dur3 = time() - start

# This should be the fastest time
assert dur3 < dur2
# if CHECK_CACHED_FILE_SIZES is True, this should check the internet for file size
# this should result in a RuntimeError
conf.CHECK_CACHED_FILE_SIZES = True
with pytest.raises(RuntimeError):
with monitor_socket():
sr.download(cloud=False, cache=True)

# if CHECK_CACHED_FILE_SIZES is False, this should NOT check the internet for file size
# this should NOT result in a RuntimeError
conf.CHECK_CACHED_FILE_SIZES = False
try:
with monitor_socket():
sr.download(cloud=False, cache=True)
except RuntimeError:
pytest.fail(
"`CHECK_CACHED_FILE_SIZES` set to `False` still results in a file size check."
)

0 comments on commit 6ae6618

Please sign in to comment.