From 75183b0c969c50d2036ce814b5c493a9d800c8ff Mon Sep 17 00:00:00 2001
From: Robbie Luijben <luijben@astron.nl>
Date: Thu, 16 Mar 2023 15:04:51 +0100
Subject: [PATCH] Added a few unit tests for beam and connectors

---
 scripts/beam.py                               | 15 ++++++++
 scripts/convert_alta_to_adex_cache_django.py  | 20 ++---------
 scripts/convert_alta_to_adex_cache_fastapi.py | 20 ++---------
 tests/__init__.py                             |  0
 tests/test.py                                 |  9 -----
 tests/test_beam.py                            | 20 +++++++++++
 tests/test_connectors_postgres.py             | 35 +++++++++++++++++++
 tests/test_connectors_vo.py                   | 16 +++++++++
 8 files changed, 90 insertions(+), 45 deletions(-)
 create mode 100644 scripts/beam.py
 create mode 100644 tests/__init__.py
 delete mode 100644 tests/test.py
 create mode 100644 tests/test_beam.py
 create mode 100644 tests/test_connectors_postgres.py
 create mode 100644 tests/test_connectors_vo.py

diff --git a/scripts/beam.py b/scripts/beam.py
new file mode 100644
index 0000000..a005e63
--- /dev/null
+++ b/scripts/beam.py
@@ -0,0 +1,15 @@
+def calc_beam(name):
+    # the beam number can be found by parsing the number in the string pattern "_B012."
+    beam = 0
+    try:
+        position = name.find("_B")
+        if position>=0:
+           beam_string = name[position:position+6]
+           if beam_string.find(".") == 5:
+               beam = int(beam_string[2:5])
+
+           return beam
+    except:
+        pass
+
+    return 0
\ No newline at end of file
diff --git a/scripts/convert_alta_to_adex_cache_django.py b/scripts/convert_alta_to_adex_cache_django.py
index 84c0e1a..65b0a9d 100644
--- a/scripts/convert_alta_to_adex_cache_django.py
+++ b/scripts/convert_alta_to_adex_cache_django.py
@@ -6,6 +6,8 @@ from psycopg2 import Error
 import argparse
 
 import sql_scripts
+from scripts.beam import calc_beam
+
 
 def parse_database_url(url):
     # parse database url like: postgres:postgres@localhost:5432/altadb_1sept2022
@@ -26,24 +28,6 @@ def parse_database_url(url):
 
     return user, password, host, database, db_port
 
-
-def calc_beam(name):
-    # the beam number can be found by parsing the number in the string pattern "_B012."
-    beam = 0
-    try:
-        position = name.find("_B")
-        if position>=0:
-           beam_string = name[position:position+6]
-           if beam_string.find(".") == 5:
-               beam = int(beam_string[2:5])
-
-           return beam
-    except:
-        pass
-
-    return 0
-
-
 # conversion algorithm to extract a bit more metadata from an ALTA record
 def convert_dataproduct(row):
     name, pid, access_url, ra, dec, dt, dst, observation = row
diff --git a/scripts/convert_alta_to_adex_cache_fastapi.py b/scripts/convert_alta_to_adex_cache_fastapi.py
index 2bcb2bf..7d6fa69 100644
--- a/scripts/convert_alta_to_adex_cache_fastapi.py
+++ b/scripts/convert_alta_to_adex_cache_fastapi.py
@@ -6,6 +6,8 @@ from psycopg2 import Error
 import argparse
 
 import sql_scripts
+from scripts.beam import calc_beam
+
 
 def parse_database_url(url):
 
@@ -27,24 +29,6 @@ def parse_database_url(url):
 
     return user, password, host, database, db_port
 
-
-def calc_beam(name):
-    # the beam number can be found by parsing the number in the string pattern "_B012."
-    beam = 0
-    try:
-        position = name.find("_B")
-        if position>=0:
-           beam_string = name[position:position+6]
-           if beam_string.find(".") == 5:
-               beam = int(beam_string[2:5])
-
-           return beam
-    except:
-        pass
-
-    return 0
-
-
 # conversion algorithm to extract a bit more metadata from an ALTA record
 def convert_dataproduct(row):
     name, pid, access_url, ra, dec, dt, dst, observation = row
diff --git a/tests/__init__.py b/tests/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/tests/test.py b/tests/test.py
deleted file mode 100644
index 2563626..0000000
--- a/tests/test.py
+++ /dev/null
@@ -1,9 +0,0 @@
-import unittest
-from scraper.adex_io import ADEX
-class TestConnectors(unittest.TestCase):
-
-    def test_connector(self):
-        pass
-
-if __name__ == '__main__':
-    unittest.main()
\ No newline at end of file
diff --git a/tests/test_beam.py b/tests/test_beam.py
new file mode 100644
index 0000000..fa028bf
--- /dev/null
+++ b/tests/test_beam.py
@@ -0,0 +1,20 @@
+import unittest
+
+from scripts.beam import calc_beam
+
+
+class TestBeam(unittest.TestCase):
+    def test_calc_beam_no_beam_number_at_all(self):
+        actual = calc_beam("abc")
+        expected = 0
+        self.assertEqual(actual, expected)
+
+    def test_calc_beam_no_beam_number_but_partial_match_no_dot(self):
+        actual = calc_beam("_BXXXXX")
+        expected = 0
+        self.assertEqual(actual, expected)
+
+    def test_calc_beam_valid_beam_number(self):
+        actual = calc_beam("_B099.")
+        expected = 99
+        self.assertEqual(actual, expected)
\ No newline at end of file
diff --git a/tests/test_connectors_postgres.py b/tests/test_connectors_postgres.py
new file mode 100644
index 0000000..39504d5
--- /dev/null
+++ b/tests/test_connectors_postgres.py
@@ -0,0 +1,35 @@
+import types
+import unittest
+from scraper.postgres.connectors.apertif import Inspectionplots
+
+
+class TestConnectorPostgresApertif(unittest.TestCase):
+    def test_translate(self):
+        connector = Inspectionplots()
+
+        name = 'my_name'
+        pid = 5
+        url = 'some_access_url'
+        dataset_id = 23
+
+        row = (name, pid, url, '', '', dataset_id)
+
+        args_collection = [1, 2, 3]
+        args = types.SimpleNamespace(collection=args_collection)
+
+        actual = connector.translate(row=row, args=args)
+
+        expected = {
+            'pid': pid,
+            'name': name,
+            'dp_type': 'diagnostic_plots',
+            'format': 'png',
+            'locality': 'online',
+            'access_url': url,
+            'dataset_id': str(dataset_id),
+            'parent': None,
+            'collection': args_collection,
+        }
+
+        self.assertDictEqual(actual, expected)
+
diff --git a/tests/test_connectors_vo.py b/tests/test_connectors_vo.py
new file mode 100644
index 0000000..ea5dad4
--- /dev/null
+++ b/tests/test_connectors_vo.py
@@ -0,0 +1,16 @@
+import unittest
+
+
+# TODO: implement
+class TestConnectorVOApertif(unittest.TestCase):
+    pass
+
+
+# TODO: implement
+class TestConnectorVOFocus(unittest.TestCase):
+    pass
+
+
+# TODO: implement
+class TestConnectorVOLotssDR2(unittest.TestCase):
+    pass
-- 
GitLab