From 696b3e231ebb81680c2f17f2fd8eb88b54867ebb Mon Sep 17 00:00:00 2001
From: Cyril Danilevski <cyril.danilevski@xfel.eu>
Date: Thu, 1 Sep 2022 12:11:26 +0200
Subject: [PATCH] Update tests to take beamtime dates into account

---
 tests/test_webservice.py | 52 +++++++++++++++++++++++++++++++---------
 1 file changed, 41 insertions(+), 11 deletions(-)

diff --git a/tests/test_webservice.py b/tests/test_webservice.py
index 97bb33f38..73f015d82 100644
--- a/tests/test_webservice.py
+++ b/tests/test_webservice.py
@@ -21,6 +21,21 @@ from webservice.webservice import (  # noqa: import not at top of file
     config,
 )
 
+VALID_BEAMTIME = {
+        "begin_at": (dt.datetime.today() - dt.timedelta(days=1)).isoformat(),
+        "description": None,
+        "end_at": (dt.datetime.today() + dt.timedelta(days=1)).isoformat(),
+        "flg_available": True,
+        "id": 772,
+    }
+
+INVALID_BEAMTIME = {
+        "begin_at": "1818-05-05T00:00:00.000+02:00",
+        "description": "Karl",
+        "end_at": "1883-03-14T00:00:00.000+02:00",
+        "flg_available": True,
+        "id": 773,
+}
 
 @pytest.mark.requires_gpfs
 def test_check_files():
@@ -243,22 +258,33 @@ async def test_run_action(mode, cmd, retcode, expected, monkeypatch):
 
 @pytest.mark.asyncio
 @pytest.mark.parametrize(
-    'proposal_number, action, mock_proposal_status, expected_result',
+    'proposal_number, action, mock_proposal_status, mock_beamtimes, expected_result',
     [
-        (42, 'correct', 'A', 'upex-middle'),  # active
-        ('42', 'dark', 'R', 'upex-high'),  # active
-        (404, 'correct', 'FR', 'exfel'),  # finished and reviewed
-        (404, 'dark', 'CLO', 'exfel'),  # closed
+        (42, 'correct', 'A', [INVALID_BEAMTIME, VALID_BEAMTIME], 'upex-middle'),  # active
+        ('42', 'dark', 'R', [INVALID_BEAMTIME, VALID_BEAMTIME], 'upex-high'),  # active
+        (404, 'correct', 'FR', [INVALID_BEAMTIME, VALID_BEAMTIME], 'exfel'),  # finished and reviewed
+        (404, 'dark', 'CLO', [INVALID_BEAMTIME, VALID_BEAMTIME], 'exfel'),  # closed
+        (42, 'correct', 'A', [INVALID_BEAMTIME, INVALID_BEAMTIME], 'exfel'),  # active
+        (42, 'correct', 'A', [INVALID_BEAMTIME], 'exfel'),  # active but not in beamtime
+        (42, 'correct', 'A', [VALID_BEAMTIME], 'upex-middle'),  # active
+        ('42', 'dark', 'R', [VALID_BEAMTIME, {}], 'upex-high'),  # active
+        (42, 'correct', 'A', [], 'exfel'),  # active, no beatime?
+        (42, 'correct', 'A', None, 'exfel'),  # active, no beatime?
     ],
 )
-async def test_get_slurm_partition(proposal_number,
-                                   action,
-                                   mock_proposal_status,
-                                   expected_result):
+async def test_get_slurm_partition(
+    proposal_number, action, mock_proposal_status, mock_beamtimes, expected_result
+):
+
+    return_value = {
+        "flg_beamtime_status": mock_proposal_status,
+    }
+    if mock_beamtimes is not None:  # Test that we handle missing field
+        return_value["beamtimes"] = mock_beamtimes
 
     response = mock.Mock()
     response.status_code = 200
-    response.json = mock.Mock(return_value={'flg_beamtime_status': mock_proposal_status})
+    response.json = mock.Mock(return_value=return_value)
     client = mock.Mock()
     client.get_proposal_by_number_api = mock.Mock(
         return_value=response)
@@ -295,7 +321,11 @@ async def test_get_slurm_partition_run_age(
     # response for `get_proposal_by_number_api` AND `get_run_by_id_api`
     response = mock.Mock()
     response.status_code = 200
-    response.json = lambda: {"flg_beamtime_status": "A", "begin_at": run_str}
+    response.json = lambda: {
+        "flg_beamtime_status": "A",
+        "begin_at": run_str,
+        "beamtimes": [INVALID_BEAMTIME, VALID_BEAMTIME],
+    }
 
     client = mock.Mock()
     client.get_proposal_by_number_api = mock.Mock(return_value=response)
-- 
GitLab