Skip to content

Commit

Permalink
Added mocking in fido_search for test_noaa (sunpy#2900)
Browse files Browse the repository at this point in the history
Added mock tests for test_noaa.py
  • Loading branch information
yashrsharma44 authored and ishanisri committed Jan 28, 2019
1 parent e8d41eb commit ff10e9e
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 19 deletions.
1 change: 1 addition & 0 deletions changelog/2900.trivial.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Used `unittest.mock` for creating offline tests for simulating online tests for `test_noaa.py`
98 changes: 79 additions & 19 deletions sunpy/net/dataretriever/tests/test_noaa.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from unittest import mock

import pytest

from sunpy.time import parse_time
Expand All @@ -8,9 +10,66 @@
from sunpy.net.fido_factory import UnifiedResponse
from sunpy.net import Fido
from sunpy.net import attrs as a

LCClient = noaa.NOAAIndicesClient()



def mock_query_object(start_date, end_date):
"""
Creation of a QueryResponse object, and prefill some
downloaded data from noaa.NOAAIndicesClient().fetch(Time('20 ..)
"""
# Create a mock QueryResponse object
map_ = {
'TimeRange' : TimeRange(parse_time(start_date), parse_time(end_date)),
'Time_start': parse_time(start_date),
'Time_end': parse_time(end_date),
'source': 'sdic',
'instrument': 'noaa-indices',
'physobs':'sunspot number',
'provider':'swpc'
}

resp = QueryResponse.create(map_, LCClient._get_default_uri())
# Attach the client with the QueryResponse
resp.client = LCClient
return resp


@pytest.mark.remote_data
def test_fetch_working():
"""
Tests if the online server for noaa is working.
Uses the url : ftp://ftp.swpc.noaa.gov/pub/weekly/RecentIndices.txt
"""
qr1 = LCClient.search(Time('2012/10/4', '2012/10/6'),
Instrument('noaa-indices'))

# Mock QueryResponse object
mock_qr = mock_query_object('2012/10/4', '2012/10/6')

# Compare if two objects have the same attribute

mock_qr = mock_qr[0]
qr = qr1[0]

assert mock_qr.source == qr.source
assert mock_qr.provider == qr.provider
assert mock_qr.physobs == qr.physobs
assert mock_qr.instrument == qr.instrument
assert mock_qr.url == qr.url
assert mock_qr.time == qr.time

# Assert if the timerange is same
assert qr1.time_range() == TimeRange('2012/10/4', '2012/10/6')

res = LCClient.fetch(qr1)
download_list = res.wait(progress=False)
assert len(download_list) == len(qr1)
assert download_list[0].split('/')[-1] == 'RecentIndices.txt'


@pytest.mark.parametrize(
"timerange,url_start,url_end",
[(TimeRange('1995/06/03', '1995/06/04'),
Expand All @@ -29,17 +88,18 @@ def test_get_url_for_time_range(timerange, url_start, url_end):
def test_can_handle_query():
ans1 = noaa.NOAAIndicesClient._can_handle_query(
Time('2012/8/9', '2012/8/10'), Instrument('noaa-indices'))
assert ans1 == True
assert ans1
ans2 = noaa.NOAAIndicesClient._can_handle_query(
Time('2012/7/7', '2012/7/7'))
assert ans2 == False
assert not ans2
ans3 = noaa.NOAAIndicesClient._can_handle_query(
Time('2012/8/9', '2012/8/10'), Instrument('eve'))
assert ans3 == False
assert not ans3


@pytest.mark.remote_data
def test_query():
@mock.patch('sunpy.net.dataretriever.sources.noaa.NOAAIndicesClient.search',
return_value=mock_query_object('2012/8/9', '2012/8/10'))
def test_query(mock_search):
qr1 = LCClient.search(
Time('2012/8/9', '2012/8/10'), Instrument('noaa-indices'))
assert isinstance(qr1, QueryResponse)
Expand All @@ -48,25 +108,25 @@ def test_query():
assert qr1.time_range().end == parse_time('2012/08/10')


@pytest.mark.remote_data
@pytest.mark.parametrize("time, instrument", [
(Time('2012/11/27', '2012/11/27'), Instrument('noaa-indices')),
(Time('2012/10/4', '2012/10/6'), Instrument('noaa-indices')),
])
def test_fetch(time, instrument):
qr1 = LCClient.search(time, instrument)
@mock.patch('sunpy.net.dataretriever.sources.noaa.NOAAIndicesClient.search',
return_value=mock_query_object('2012/10/4', '2012/10/6'))
@mock.patch('sunpy.net.download.Results.wait',
return_value=['some/path/extension/RecentIndices.txt'])
def test_fetch(mock_wait, mock_search):
qr1 = LCClient.search(Time('2012/10/4', '2012/10/6'),
Instrument('noaa-indices'))
res = LCClient.fetch(qr1)
download_list = res.wait(progress=False)
assert len(download_list) == len(qr1)


@pytest.mark.remote_data
@pytest.mark.parametrize(
"time, instrument",
[(a.Time("2012/10/4", "2012/10/6"), a.Instrument('noaa-indices')),
(a.Time('2013/10/5', '2013/10/7'), a.Instrument('noaa-indices'))])
def test_fido(time, instrument):
qr = Fido.search(time, instrument)
@mock.patch('sunpy.net.fido_factory.Fido.fetch',
side_effect=(UnifiedResponse(
mock_query_object("2012/10/4", "2012/10/6"))))
def test_fido(mock_fetch):
qr = Fido.search(a.Time("2012/10/4", "2012/10/6"),
a.Instrument('noaa-indices'))
assert isinstance(qr, UnifiedResponse)

response = Fido.fetch(qr)
assert len(response) == qr._numfile

0 comments on commit ff10e9e

Please sign in to comment.