From 16ab1dd143a5001d0108fade4abfac99f290c7a8 Mon Sep 17 00:00:00 2001
From: Niclas Esser <nesser@mpifr-bonn.mpg.de>
Date: Sun, 28 Jan 2024 12:43:30 +0100
Subject: [PATCH] Addtional CI

---
 .gitlab-ci.yml          | 53 +++++++++++++++++++++++++++++++++++------
 pafsim/chain.py         |  4 +---
 pafsim/vector.py        | 20 ++++++++++++----
 run_tests.sh            |  2 +-
 tests/__init__.py       |  0
 tests/test_connector.py | 19 +++++++++++++++
 tests/test_vector.py    | 34 ++++++++++++++++++++++++++
 7 files changed, 117 insertions(+), 15 deletions(-)
 create mode 100644 tests/__init__.py
 create mode 100644 tests/test_connector.py
 create mode 100644 tests/test_vector.py

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index 273d238..a1fe9f1 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -3,11 +3,50 @@ build:
   image: python:3.9.18-bookworm
   script:
     - pip install .
+  artifacts:
+    paths:
+      - build/
 
-# unittest:
-#   stage: test
-#   image: python:3.9.18-bookworm
-#   before_script:
-#     - apt-get install -y python3-coverage
-#   script:
-#     - bash run_tests.sh
\ No newline at end of file
+
+unittest:
+  stage: test
+  image: python:3.9.18-bookworm
+  before_script:
+    - pip install coverage
+  script:
+    - bash run_tests.sh
+
+coverage_python:
+  stage: stats
+  image: python:3.9.18-bookworm
+  script:
+    - pip install coverage
+    - coverage xml --include="dbbc/*"
+    - coverage html --include="dbbc/*"
+    - coverage report --include="dbbc/*"
+  dependencies:
+    - unittest
+  coverage: '/TOTAL.*\s+(\d+\%)/'
+  artifacts:
+    paths:
+      - htmlcov
+      - coverage.xml
+    reports:
+      coverage_report:
+        coverage_format: cobertura
+        path: coverage.xml
+
+pylint:
+  stage: stats
+  image: python:3.9.18-bookworm
+  before_script:
+    - pip install pylint==2.13.5 anybadge==1.9.0 pylint-exit==1.2.0
+  script:
+    - mkdir ./pylint
+    - pylint --output-format=colorized dbbc | tee ./pylint/pylint.log || pylint-exit $?
+    - PYLINT_SCORE=$(sed -n 's/^Your code has been rated at \([-0-9.]*\)\/.*/\1/p' ./pylint/pylint.log)
+    - anybadge --label=Pylint --file=pylint/pylint.svg --value=$PYLINT_SCORE 8.5=red 9=orange 9.5=yellow 9.75=green
+    - echo "Pylint score is $PYLINT_SCORE"
+  artifacts:
+    paths:
+      - ./pylint/
diff --git a/pafsim/chain.py b/pafsim/chain.py
index 250b6e9..e8e4a5b 100644
--- a/pafsim/chain.py
+++ b/pafsim/chain.py
@@ -106,7 +106,5 @@ class ProcessingChain():
             self.plotAll(directory + '/plot/')
 
         for tv_name in self.conf["store"]:
-            tv = Vector(tv_name)
-            tv.data = self.getProcessor(tv_name).output
-            tv.order = self.getProcessor(tv_name).oformat
+            tv = Vector(self.getProcessor(tv_name))
             tv.store(directory + '/data/')
\ No newline at end of file
diff --git a/pafsim/vector.py b/pafsim/vector.py
index a25c90b..4943e6d 100644
--- a/pafsim/vector.py
+++ b/pafsim/vector.py
@@ -1,13 +1,24 @@
 import pickle
 import numpy as np
+from pafsim.processor._processor import Processor
 
 class Vector():
 
-    def __init__(self, name: str):
-        self.name: str = name
+    def __init__(self, processor: Processor=None):
+        if processor is not None:
+            self.setProcessor(processor)
+            return
+        self.name: str = ""
         self.data: np.ndarray = np.zeros(0)
         self.order: list = []
-        self.dtype: np.dtype = None
+        self.dtype: np.dtype = np.int8
+        self.dir:str = ""
+
+    def setProcessor(self, processor: Processor):
+        self.name = processor.name
+        self.data = processor.output
+        self.order = processor.oformat
+        self.dtype = processor.dtype
 
     def __str__(self):
         s  = f"Vector of {self.name}"
@@ -17,7 +28,8 @@ class Vector():
         return s
 
     def store(self, dir):
-        with open(dir + self.name + '.pk1', 'wb') as f:
+        self.path = dir + self.name + '.pk1'
+        with open(self.path, 'wb') as f:
             pickle.dump(self, f, pickle.HIGHEST_PROTOCOL)
 
     def load(self, path: str):
diff --git a/run_tests.sh b/run_tests.sh
index d9a3f81..f43124c 100644
--- a/run_tests.sh
+++ b/run_tests.sh
@@ -1,3 +1,3 @@
 #!/bin/bash
-LOG_LEVEL=INFO python3-coverage run --source="pafsim" -m unittest tests/test*.py
+LOG_LEVEL=INFO coverage run --source="pafsim" -m unittest tests/test*.py
 exit $?
diff --git a/tests/__init__.py b/tests/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/tests/test_connector.py b/tests/test_connector.py
new file mode 100644
index 0000000..36df6f7
--- /dev/null
+++ b/tests/test_connector.py
@@ -0,0 +1,19 @@
+import unittest
+from pafsim.connector import Connector, ConnectionError
+import pafsim.processor as pp
+
+class TEST_Connector(unittest.TestCase):
+
+    def setUp(self) -> None:
+        return super().setUp()
+
+    def tearDown(self) -> None:
+        return super().tearDown()
+
+    def test_non_matching_nodes(self):
+        proc1 = pp.Correlator("dummy1")
+        proc2 = pp.Correlator("dummy2")
+        conn = Connector(proc1, proc2)
+        with self.assertRaises(ConnectionError):
+            conn.connect()
+
diff --git a/tests/test_vector.py b/tests/test_vector.py
new file mode 100644
index 0000000..c2cff58
--- /dev/null
+++ b/tests/test_vector.py
@@ -0,0 +1,34 @@
+import unittest
+import shutil
+import os
+import numpy as np
+from pafsim.vector import Vector
+import pafsim.processor as pp
+
+DIR = "tmp/"
+
+class TEST_Vector(unittest.TestCase):
+
+    def setUp(self) -> None:
+        if not os.path.exists(DIR):
+            os.makedirs(DIR)
+        return super().setUp()
+
+    def tearDown(self) -> None:
+        if os.path.exists(DIR):
+            shutil.rmtree(DIR)
+        return super().tearDown()
+
+    # def test_load_store(self):
+    #     proc = pp.Correlator("dummy")
+    #     proc.output = np.arange(10)
+    #     stored = Vector(proc)
+    #     stored.store(DIR)
+    #     loaded = Vector()
+    #     loaded.load(stored.path)
+    #     self.assertEqual(stored.data, loaded.data)
+    #     self.assertEqual(stored.order, loaded.order)
+    #     self.assertEqual(stored.dtype, loaded.dtype)
+    #     self.assertEqual(stored.name, loaded.name)
+    #     self.assertEqual(str(stored), str(loaded.name))
+
-- 
GitLab