Repository: dlr-eoc/ukis-pysat (language: python)

Issue:
Improve unit tests
We often combine several test cases into one, it would be better if we would split them up into new ones.


Discussion / hints:
When doing this we should also replace the `dn2toa` test, because #94 was not a long term solution

Fix patch:
diff --git a/CHANGELOG.rst b/CHANGELOG.rst
index 2e615ac..b1f2bdb 100644
--- a/CHANGELOG.rst
+++ b/CHANGELOG.rst
@@ -21,6 +21,7 @@ Changed
 - ``data``: change default scihup endpoint `/dhus` to `/apihub`#110
 - ``data``: mocked all interaction with Hubs for unit testing #116
 - ``data``: EarthExplorer changed to new M2M API due to dependency update #120
+- split unit tests #110
 
 Added
 ^^^^^


Test patch:
diff --git a/tests/test_file.py b/tests/test_file.py
index 48e01b9..b530a50 100644
--- a/tests/test_file.py
+++ b/tests/test_file.py
@@ -19,35 +19,43 @@ def test_env(self):
         os.environ["ISITWEEKENDYET"] = "0"
         self.assertFalse(psf.env_get("ISITWEEKENDYET", boolean=True))
 
-    def test_get_sentinel_scene_from_dir(self):
+    def test_get_sentinel_scene_from_path_testfiles(self):
         with psf.get_sentinel_scene_from_dir(path_testfiles) as (full_path, ident):
             self.assertEqual("S1M_hello_from_inside", ident)
 
+    def test_get_sentinel_scene_from_str_path(self):
         with psf.get_sentinel_scene_from_dir(str_path) as (full_path, ident):
             self.assertEqual("S1M_hello_from_inside", ident)
 
+    def test_get_sentinel_scene_from_dir_joinpath(self):
         with psf.get_sentinel_scene_from_dir(path_testfiles.joinpath("another_scene")) as (full_path, ident):
             self.assertEqual("S2__IN_FOLDER", ident)
 
-    def test_get_polarization_from_s1_filename(self):
+    def test_get_polarization_from_s1_filename_SDH(self):
         self.assertEqual(
             psf.get_polarization_from_s1_filename(
                 "MMM_BB_TTTR_1SDH_YYYYMMDDTHHMMSS_YYYYMMDDTHHMMSS_OOOOOO_DDDDDD_CCCC.SAFE.zip"
             ),
             "HH",
         )
+
+    def test_get_polarization_from_s1_filename_SSH(self):
         self.assertEqual(
             psf.get_polarization_from_s1_filename(
                 "MMM_BB_TTTR_1SSH_YYYYMMDDTHHMMSS_YYYYMMDDTHHMMSS_OOOOOO_DDDDDD_CCCC.SAFE.zip"
             ),
             "HH",
         )
+
+    def test_get_polarization_from_s1_filename_SSV(self):
         self.assertEqual(
             psf.get_polarization_from_s1_filename(
                 "MMM_BB_TTTR_2SSV_YYYYMMDDTHHMMSS_YYYYMMDDTHHMMSS_OOOOOO_DDDDDD_CCCC.SAFE.zip"
             ),
             "VV",
         )
+
+    def test_get_polarization_from_s1_filename_SDV(self):
         self.assertEqual(
             psf.get_polarization_from_s1_filename(
                 "MMM_BB_TTTR_1SDV_YYYYMMDDTHHMMSS_YYYYMMDDTHHMMSS_OOOOOO_DDDDDD_CCCC.SAFE.zip",
@@ -56,13 +64,7 @@ def test_get_polarization_from_s1_filename(self):
             "VV,VH",
         )
 
-    def test_get_ts_from_sentinel_filename(self):
-        self.assertEqual(
-            psf.get_ts_from_sentinel_filename(
-                "S3M_OL_L_TTT____20200113T002219_YYYYMMDDTHHMMSS_YYYYMMDDTHHMMSS_i_GGG_c.SEN3"
-            ),
-            datetime(2020, 1, 13, 0, 22, 19, tzinfo=timezone.utc),
-        )
+    def test_get_ts_from_sentinel_filename_S1(self):
         self.assertEqual(
             psf.get_ts_from_sentinel_filename(
                 "S1M_BB_TTTR_LFPP_YYYYMMDDTHHMMSS_20200113T002219_OOOOOO_DDDDDD_CCCC.SAFE.zip",
@@ -76,12 +78,22 @@ def test_get_ts_from_sentinel_filename(self):
             ),
             datetime(2020, 1, 13, 7, 46, 19, tzinfo=timezone.utc),
         )
+
+    def test_get_ts_from_sentinel_filename_S2(self):
         self.assertEqual(
             psf.get_ts_from_sentinel_filename(
                 "S2AM_MSIXXX_20200113T002219_Nxxyy_ROOO_Txxxxx_<Product Discriminator>.SAFE"
             ),
             datetime(2020, 1, 13, 0, 22, 19, tzinfo=timezone.utc),
         )
+
+    def test_get_ts_from_sentinel_filename_S3(self):
+        self.assertEqual(
+            psf.get_ts_from_sentinel_filename(
+                "S3M_OL_L_TTT____20200113T002219_YYYYMMDDTHHMMSS_YYYYMMDDTHHMMSS_i_GGG_c.SEN3"
+            ),
+            datetime(2020, 1, 13, 0, 22, 19, tzinfo=timezone.utc),
+        )
         self.assertEqual(
             psf.get_ts_from_sentinel_filename(
                 "S3M_OL_L_TTTTTT_yyyymmddThhmmss_20200113T002219_YYYYMMDDTHHMMSS_i_GGG_c.SEN3", False
diff --git a/tests/test_raster.py b/tests/test_raster.py
index 7d8b0d8..f1f2dd0 100644
--- a/tests/test_raster.py
+++ b/tests/test_raster.py
@@ -49,9 +49,8 @@ def test_init_with_arry_fail_missing_transform(self):
             Image(data=self.img.arr, crs=self.img.dataset.crs)
 
     def test_init_with_array(self):
-        img = Image(self.img.arr, crs=self.img.dataset.crs, transform=self.img.dataset.transform)
-        self.assertTrue(np.array_equal(self.img.arr, img.arr))
-        img.close()
+        with Image(self.img.arr, crs=self.img.dataset.crs, transform=self.img.dataset.transform) as img:
+            self.assertTrue(np.array_equal(self.img.arr, img.arr))
 
     def test_init_2dim(self):
         with Image(np.ones(shape=(385, 502)), crs=self.img.dataset.crs, transform=self.img.dataset.transform) as img:
@@ -67,68 +66,67 @@ def test_dimorder_error(self):
         with self.assertRaises(TypeError):
             Image(TEST_FILE, dimorder="middle")
 
-    def test_arr(self):
-        img_first = Image(TEST_FILE, dimorder="first")
-        img_first.mask(
-            box(
-                11.9027457562112939,
-                51.4664152338322580,
-                11.9477435281016131,
-                51.5009522690838750,
-            )
-        )
-        self.assertEqual(img_first.arr.shape, (1, 385, 502))
-        self.assertEqual(
-            str(img_first.dataset.transform),
-            str(
-                from_bounds(
+    def test_file_dimorder_first(self):
+        with Image(TEST_FILE, dimorder="first") as img_first:
+            img_first.mask(
+                box(
                     11.9027457562112939,
                     51.4664152338322580,
                     11.9477435281016131,
                     51.5009522690838750,
-                    img_first.arr.shape[2],
-                    img_first.arr.shape[1],
                 )
-            ),
-        )
-        img_first.close()
-
-        img_last = Image(TEST_FILE, dimorder="last")
-        img_last.mask(
-            box(
-                11.9027457562112939,
-                51.4664152338322580,
-                11.9477435281016131,
-                51.5009522690838750,
             )
-        )
-        self.assertEqual(img_last.arr.shape, (385, 502, 1))
-        self.assertEqual(
-            str(img_last.dataset.transform),
-            str(
-                from_bounds(
+            self.assertEqual(img_first.arr.shape, (1, 385, 502))
+            self.assertEqual(
+                str(img_first.dataset.transform),
+                str(
+                    from_bounds(
+                        11.9027457562112939,
+                        51.4664152338322580,
+                        11.9477435281016131,
+                        51.5009522690838750,
+                        img_first.arr.shape[2],
+                        img_first.arr.shape[1],
+                    )
+                ),
+            )
+
+    def test_file_dimorder_last(self):
+        with Image(TEST_FILE, dimorder="last") as img_last:
+            img_last.mask(
+                box(
                     11.9027457562112939,
                     51.4664152338322580,
                     11.9477435281016131,
                     51.5009522690838750,
-                    img_last.arr.shape[1],
-                    img_last.arr.shape[0],
                 )
-            ),
-        )
-        img_last.close()
+            )
+            self.assertEqual(img_last.arr.shape, (385, 502, 1))
+            self.assertEqual(
+                str(img_last.dataset.transform),
+                str(
+                    from_bounds(
+                        11.9027457562112939,
+                        51.4664152338322580,
+                        11.9477435281016131,
+                        51.5009522690838750,
+                        img_last.arr.shape[1],
+                        img_last.arr.shape[0],
+                    )
+                ),
+            )
 
-        img_first = Image(
+    def test_arr_dimorder_first(self):
+        with Image(
             np.ones((1, 385, 502)), dimorder="first", crs=self.img.dataset.crs, transform=self.img.dataset.transform
-        )
-        self.assertEqual(img_first.arr.shape, (1, 385, 502))
-        img_first.close()
+        ) as img_first:
+            self.assertEqual(img_first.arr.shape, (1, 385, 502))
 
-        img_last = Image(
+    def test_arr_dimorder_last(self):
+        with Image(
             np.ones((385, 502, 1)), dimorder="last", crs=self.img.dataset.crs, transform=self.img.dataset.transform
-        )
-        self.assertEqual(img_last.arr.shape, (385, 502, 1))
-        img_last.close()
+        ) as img_last:
+            self.assertEqual(img_last.arr.shape, (385, 502, 1))
 
     def test_arr_nodata(self):
         array = np.ones((3, 385, 502))
@@ -144,7 +142,7 @@ def test_set_array(self):
 
             self.assertTrue(np.array_equal(im.arr, np.ones(shape=im.arr.shape)))
 
-    def test_set_array_error(self):
+    def test_set_array_error_dimorder_first(self):
         img_first = Image(TEST_FILE, dimorder="first")
 
         with self.assertRaises(TypeError):
@@ -153,6 +151,7 @@ def test_set_array_error(self):
         with self.assertRaises(ValueError):
             img_first.arr = np.ones(shape=(764, 679, 1))
 
+    def test_set_array_error_dimorder_last(self):
         with self.assertRaises(ValueError):
             img_last = Image(TEST_FILE, dimorder="last")
             img_last.arr = np.ones(shape=(1, 764, 679))