From 70898457b9b3b03bd45a2dc29e9805bbd9e69c2b Mon Sep 17 00:00:00 2001
From: lukken <lukken@astron.nl>
Date: Thu, 18 Aug 2022 11:15:05 +0000
Subject: [PATCH] L2SS-572: Raise exceptions instead of using assert

---
 .../tangostationcontrol/beam/delays.py            |  8 ++++----
 .../tangostationcontrol/test/beam/test_delays.py  | 15 ++++++++++++---
 2 files changed, 16 insertions(+), 7 deletions(-)

diff --git a/tangostationcontrol/tangostationcontrol/beam/delays.py b/tangostationcontrol/tangostationcontrol/beam/delays.py
index da649342d..7c4b13ba1 100644
--- a/tangostationcontrol/tangostationcontrol/beam/delays.py
+++ b/tangostationcontrol/tangostationcontrol/beam/delays.py
@@ -14,8 +14,8 @@ class Delays:
         measure = casacore.measures.measures()
         frame_location = measure.position("ITRF", *[f"{x}m" for x in itrf])
 
-        result = measure.do_frame(frame_location)
-        assert result == True , f"measure.do_frame failed for ITRF location {itrf}"
+        if not measure.do_frame(frame_location):
+            raise ValueError(f"measure.do_frame failed for ITRF location {itrf}")
 
         self.reference_itrf = itrf
         self.measure = measure
@@ -26,8 +26,8 @@ class Delays:
         utc_time_str = utc_time.isoformat(' ')
         frame_time = self.measure.epoch("UTC", utc_time_str)
 
-        result = self.measure.do_frame(frame_time)
-        assert result == True , f"measure.do_frame failed for UTC time {utc_time_str}"
+        if not self.measure.do_frame(frame_time):
+            raise ValueError(f"measure.do_frame failed for UTC time {utc_time_str}")
 
     def get_direction_vector(self, pointing: numpy.ndarray) -> numpy.ndarray:
         """ Compute direction vector for a given pointing, relative to the measure. """
diff --git a/tangostationcontrol/tangostationcontrol/test/beam/test_delays.py b/tangostationcontrol/tangostationcontrol/test/beam/test_delays.py
index fd4e96628..0a779f980 100644
--- a/tangostationcontrol/tangostationcontrol/test/beam/test_delays.py
+++ b/tangostationcontrol/tangostationcontrol/test/beam/test_delays.py
@@ -1,8 +1,11 @@
 import datetime
 import time
 import logging
+import mock
+
 import numpy
 import numpy.testing
+import casacore
 
 from tangostationcontrol.beam.delays import Delays
 from tangostationcontrol.test import base
@@ -10,15 +13,21 @@ from tangostationcontrol.test import base
 
 class TestDelays(base.TestCase):
     def test_init(self):
-        """
-        Fail condition is simply the object creation failing
-        """
+        """Fail condition is simply the object creation failing"""
 
         reference_itrf = [3826577.066, 461022.948, 5064892.786]  # CS002LBA, in ITRF2005 epoch 2012.5
         d = Delays(reference_itrf)
 
         self.assertIsNotNone(d)
 
+    def test_init_fails(self):
+        """Test do_measure returning false is correctly caught"""
+
+        with mock.patch.object(casacore.measures, "measures") as m_measure:
+            m_measure.return_value.do_frame.return_value = False
+
+            self.assertRaises(ValueError, Delays, [0, 0, 0])
+
     def test_is_valid_direction(self):
         d = Delays([0, 0, 0])
 
-- 
GitLab