Skip to content

Commit

Permalink
Add exception and warning for a.helio.MaxRecords (#7540)
Browse files Browse the repository at this point in the history
* add error and warning for a.helio.MaxRecords

* reduce warn_user condition code

* add exception test for test_client_search

* add changelog

* Update sunpy/net/helio/hec.py

Co-authored-by: Nabil Freij <nabil.freij@gmail.com>

* match the message in pytest.raise

* make warn_user simpler

* add test for warn_user

* pre-commit changes

* fix changelog reference target

* add test_client_search_limit

* fix pre-commit

* add test_client_search_warning

* remove test_client_search_warning

* pre-commit

* add exception for MaxRecords class

* reduce test_client_search_limit

* reduced matched message

* change test name

* remove unnecessary import

* fix CI

---------

Co-authored-by: Nabil Freij <nabil.freij@gmail.com>
  • Loading branch information
ahmedhosssam and nabobalis committed Apr 6, 2024
1 parent 5dde579 commit b0cb336
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 4 deletions.
1 change: 1 addition & 0 deletions changelog/7540.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The maximum records in `~sunpy.net.helio.HECClient` now are 20000.
4 changes: 4 additions & 0 deletions sunpy/net/helio/attrs.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ class MaxRecords(SimpleAttr):
"""
The maximum number of desired records.
"""
def __init__(self, value):
super().__init__(value)
if self.value > 20000:
raise ValueError("Helio will only return a max of 20000 results.")


class TableName(SimpleAttr):
Expand Down
4 changes: 2 additions & 2 deletions sunpy/net/helio/hec.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,8 @@ def search(self, *args, **kwargs):
MAXRECORDS=max_records)
results = votable_handler(etree.tostring(results))
table = HECResponse(results.to_table(), client=self)
if len(table) == max_records == 500:
warn_user("Number of results is the same as the default `max_records` of 500. "
if len(table) == max_records:
warn_user("Number of results is the same as current limit. "
"It is possible your query has been truncated. "
"If you want to change this, set `a.helio.MaxRecords` to a higher value.")
return table
Expand Down
11 changes: 9 additions & 2 deletions sunpy/net/helio/tests/test_helio.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
webservice_parser,
wsdl_retriever,
)
from sunpy.util.exceptions import SunpyUserWarning

# Currently helio makes unverified requests - this filter should be removed when
# https://github.com/sunpy/sunpy/issues/4401 is fixed
Expand Down Expand Up @@ -290,16 +291,22 @@ def test_client_search(client):
start = '2005/01/03'
end = '2005/12/03'
table_name = 'rhessi_hxr_flare'
res = client.search(a.Time(start, end), a.helio.TableName(table_name), a.helio.MaxRecords(10))
with pytest.warns(SunpyUserWarning, match="Number of results is the same as current limit. "):
res = client.search(a.Time(start, end), a.helio.TableName(table_name), a.helio.MaxRecords(10))
assert len(res) == 10


def test_max_records_limit():
with pytest.raises(ValueError, match="Helio will only return a max of 20000 results."):
a.helio.MaxRecords(99999)


@pytest.mark.remote_data
def test_HECResponse_iter(client):
start = '2005/01/03'
end = '2005/12/03'
table_name = 'rhessi_hxr_flare'
res = client.search(a.Time(start, end), a.helio.TableName(table_name), a.helio.MaxRecords(10))
res = client.search(a.Time(start, end), a.helio.TableName(table_name), a.helio.MaxRecords(10000))
for i in res:
# Just to make sure iter still works, check number of columns
assert len(i) == 13

0 comments on commit b0cb336

Please sign in to comment.