This is an automated email from the ASF dual-hosted git repository.

tloubrieu pushed a commit to branch add_metadata_to_remote_coll
in repository https://gitbox.apache.org/repos/asf/incubator-sdap-nexus.git


The following commit(s) were added to refs/heads/add_metadata_to_remote_coll by 
this push:
     new 5c7a4e2  add unit tests for remote collection cache
5c7a4e2 is described below

commit 5c7a4e2f968bfc33cfb2d63cfca5ff51fed9d6f3
Author: Thomas Loubrieu <[email protected]>
AuthorDate: Thu Jul 7 23:13:12 2022 -0700

    add unit tests for remote collection cache
---
 analysis/tests/redirect/test_RemoteSDAPCache.py  | 64 ++++++++++++++++++++++--
 analysis/webservice/algorithms/DataSeriesList.py |  3 +-
 analysis/webservice/redirect/RemoteSDAPCache.py  | 19 ++++---
 3 files changed, 72 insertions(+), 14 deletions(-)

diff --git a/analysis/tests/redirect/test_RemoteSDAPCache.py 
b/analysis/tests/redirect/test_RemoteSDAPCache.py
index 237659f..7a40e2f 100644
--- a/analysis/tests/redirect/test_RemoteSDAPCache.py
+++ b/analysis/tests/redirect/test_RemoteSDAPCache.py
@@ -1,9 +1,12 @@
 import unittest
 from unittest import mock
 import requests
+from datetime import datetime
+from datetime import timedelta
 
 from webservice.redirect import RemoteSDAPCache
-
+from webservice.redirect import CollectionNotFound
+from webservice.redirect.RemoteSDAPCache import RemoteSDAPList
 
 class MockResponse:
     def __init__(self, json_data, status_code):
@@ -13,8 +16,7 @@ class MockResponse:
     def json(self):
         return self.json_data
 
-def mocked_requests_get(*asgs, **kwargs):
-    json_data = [
+LIST_CONTENT = [
       {
         "shortName": "PM25",
         "title": "PM25",
@@ -25,13 +27,23 @@ def mocked_requests_get(*asgs, **kwargs):
         "iso_end": "2021-12-31T23:00:00+0000"
       }
     ]
-    return MockResponse(json_data, 200)
+
+LIST_CONTENT_FORMER = [LIST_CONTENT[0].copy()]
+LIST_CONTENT_FORMER[0]['start'] = 0
+
+def mocked_requests_get(*asgs, **kwargs):
+    return MockResponse(LIST_CONTENT, 200)
 
 
 def mocked_requests_get_timeout(*asgs, **kwargs):
     raise requests.exceptions.ConnectTimeout()
 
 
+def mocked_requests_get_not_found(*asgs, **kwargs):
+    return MockResponse({}, 404)
+
+
+
 class MyTestCase(unittest.TestCase):
 
     @mock.patch('requests.get', side_effect=mocked_requests_get)
@@ -41,6 +53,50 @@ class MyTestCase(unittest.TestCase):
         collection = 
remote_sdap_cache.get('https://aq-sdap.stcenter.net/nexus/', 'PM25')
         self.assertEqual(collection["start"], 1514818800.0)
 
+    @mock.patch('requests.get', side_effect=mocked_requests_get_timeout)
+    def test_get_timeout(self, mock_get):
+        remote_sdap_cache = RemoteSDAPCache()
+        with self.assertRaises(CollectionNotFound):
+            remote_sdap_cache.get('https://aq-sdap.stcenter.net/nexus/', 
'PM25')
+
+
+    @mock.patch('requests.get', side_effect=mocked_requests_get_not_found)
+    def test_get_not_found(self, mock_get):
+        remote_sdap_cache = RemoteSDAPCache()
+        with self.assertRaises(CollectionNotFound):
+            remote_sdap_cache.get('https://aq-sdap.stcenter.net/nexus/', 
'PM25')
+
+    @mock.patch('requests.get', side_effect=mocked_requests_get)
+    def test_get_expired(self, mock_get):
+        remote_sdap_cache = RemoteSDAPCache()
+
+        remote_sdap_cache.sdap_lists['https://aq-sdap.stcenter.net/nexus/'] = 
RemoteSDAPList(
+            list=LIST_CONTENT_FORMER,
+            outdated_at=datetime.now() - timedelta(seconds=3600*25)
+        )
+
+        collection = 
remote_sdap_cache.get('https://aq-sdap.stcenter.net/nexus/', 'PM25')
+
+        # check requests.get is called once
+        self.assertEqual(mock_get.call_count, 1)
+        self.assertEqual(collection["start"], 1514818800.0)
+
+    @mock.patch('requests.get', side_effect=mocked_requests_get)
+    def test_get_cached_valid(self, mock_get):
+        remote_sdap_cache = RemoteSDAPCache()
+
+        remote_sdap_cache.sdap_lists['https://aq-sdap.stcenter.net/nexus'] = 
RemoteSDAPList(
+            list=LIST_CONTENT_FORMER,
+            outdated_at=datetime.now() - timedelta(seconds=3600 * 23)
+        )
+
+        collection = 
remote_sdap_cache.get('https://aq-sdap.stcenter.net/nexus/', 'PM25')
+
+        # check requests.get is called once
+        self.assertEqual(mock_get.call_count, 0)
+        self.assertEqual(collection["start"], 0)
+
+
 
 if __name__ == '__main__':
     unittest.main()
diff --git a/analysis/webservice/algorithms/DataSeriesList.py 
b/analysis/webservice/algorithms/DataSeriesList.py
index 09c5a2a..e247bb6 100644
--- a/analysis/webservice/algorithms/DataSeriesList.py
+++ b/analysis/webservice/algorithms/DataSeriesList.py
@@ -49,8 +49,7 @@ class DataSeriesListCalcHandlerImpl(NexusCalcHandler):
             def toJson(self):
                 return json.dumps(self.result)
 
-        #collection_list = self._get_tile_service().get_dataseries_list()
-        collection_list = []
+        collection_list = self._get_tile_service().get_dataseries_list()
 
         # add remote collections
         if self._remote_collections:
diff --git a/analysis/webservice/redirect/RemoteSDAPCache.py 
b/analysis/webservice/redirect/RemoteSDAPCache.py
index 17adb6f..25d41b8 100644
--- a/analysis/webservice/redirect/RemoteSDAPCache.py
+++ b/analysis/webservice/redirect/RemoteSDAPCache.py
@@ -20,14 +20,17 @@ class RemoteSDAPCache:
 
     def _add(self, url, timeout=2, max_age=3600*24):
         list_url = f"{url}/list"
-        r = requests.get(list_url, timeout=timeout)
-        if r.status_code == 200:
-            self.sdap_lists[url] = RemoteSDAPList(
-                list=r.json(),
-                outdated_at=datetime.now()+timedelta(seconds=max_age)
-            )
-        else:
-            raise CollectionNotFound("url %s was not reachable, responded with 
status %s", list_url, r.status_code)
+        try:
+            r = requests.get(list_url, timeout=timeout)
+            if r.status_code == 200:
+                self.sdap_lists[url] = RemoteSDAPList(
+                    list=r.json(),
+                    outdated_at=datetime.now()+timedelta(seconds=max_age)
+                )
+            else:
+                raise CollectionNotFound("url %s was not reachable, responded 
with status %s", list_url, r.status_code)
+        except requests.exceptions.ConnectTimeout as e:
+            raise CollectionNotFound("url %s was not reachable in %i s",  
list_url, timeout)
 
     def get(self, url, short_name):
         stripped_url = url.strip('/')

Reply via email to