From 3e1b3ea11cfa9ba4751b69e7720584f7fabcf5c5 Mon Sep 17 00:00:00 2001 From: BO ZHANG Date: Sat, 28 Oct 2023 12:06:28 +0800 Subject: [PATCH] coverage 93% --- Makefile | 8 ++++++++ csst_proto/__init__.py | 2 ++ csst_proto/flip_image.py | 2 +- tests/test_config.py | 37 +++++++++++++++++++++++++++++++++++++ tests/test_demo_function.py | 12 ++++++------ tests/test_flip_image.py | 24 ++++++++++-------------- 6 files changed, 64 insertions(+), 21 deletions(-) create mode 100644 tests/test_config.py diff --git a/Makefile b/Makefile index 82ebbdf..2779662 100644 --- a/Makefile +++ b/Makefile @@ -4,3 +4,11 @@ install: uninstall: pip uninstall csst_proto -y + +test: + coverage run -m pytest . --import-mode=importlib --cov-report=html --cov-report=term-missing + coverage report -m + rm -rf .coverage .pytest_cache + + + diff --git a/csst_proto/__init__.py b/csst_proto/__init__.py index fc76991..e01c00b 100644 --- a/csst_proto/__init__.py +++ b/csst_proto/__init__.py @@ -3,8 +3,10 @@ from .flip_image import flip_image, read_default_image from .flip_image_in_parallel import flip_multiple_images_jl, flip_multiple_images_mp from .config import read_config +# 版本号,可以通过csst_proto.__version__获取 __version__ = "0.0.1" +# 定义__all__变量,其中所有的对象会被检查类型标注和docstring __all__ = [ "demo_function", "DemoClass", diff --git a/csst_proto/flip_image.py b/csst_proto/flip_image.py index fa6d399..ef5014e 100644 --- a/csst_proto/flip_image.py +++ b/csst_proto/flip_image.py @@ -75,6 +75,6 @@ def read_default_image(): -------- >>> test_image = read_default_image() """ - fp_img = os.path.join(HERE, "/data/test_image.txt") + fp_img = os.path.join(HERE, "data/test_image.txt") print("reading file {} ...".format(fp_img)) return np.loadtxt(fp_img, dtype=int) diff --git a/tests/test_config.py b/tests/test_config.py new file mode 100644 index 0000000..c2f798b --- /dev/null +++ b/tests/test_config.py @@ -0,0 +1,37 @@ +""" +Identifier: csst_proto/test_config.py +Name: test_config.py +Description: Test reading toml format config. +Author: Bo Zhang +Created: 2023-10-26 +Modified-History: + 2023-10-26, Bo Zhang, created + 2023-10-26, Bo Zhang, add module header +""" + +import unittest +from csst_proto import read_config + + +class TestDemoFunction(unittest.TestCase): + def test_read_config(self): + """ + Aim + --- + Test demo function. + + Criteria + -------- + Pass if the demo function returns `1`. + + Details + ------- + The demo function returns the length of the input argument list. + This case aims to test whether the demo function returns `1` if input is `None`. + """ + # demo function test + config = read_config() + self.assertTrue( + set(config.keys()) == {"A", "B"} and config["A"] == 1 and config["B"] == 2, + "Single-argument case failed.", + ) diff --git a/tests/test_demo_function.py b/tests/test_demo_function.py index c717b3a..815629f 100644 --- a/tests/test_demo_function.py +++ b/tests/test_demo_function.py @@ -10,7 +10,7 @@ Modified-History: """ import unittest -from csst_proto.api import a_demo_function +from csst_proto import demo_function class TestDemoFunction(unittest.TestCase): @@ -31,7 +31,7 @@ class TestDemoFunction(unittest.TestCase): """ # demo function test self.assertTrue( - a_demo_function(None) == 1, + demo_function(None) == 1, "Single-argument case failed.", ) @@ -52,7 +52,7 @@ class TestDemoFunction(unittest.TestCase): """ # demo function test self.assertEqual( - a_demo_function(None, None), + demo_function(None, None), 2, "Double-argument case failed.", ) @@ -61,7 +61,7 @@ class TestDemoFunction(unittest.TestCase): """ Aim --- - Test demo function. + Test demo function in bad case. Criteria -------- @@ -74,8 +74,8 @@ class TestDemoFunction(unittest.TestCase): if input is `None, None, None`. """ # demo function test - self.assertNotEquals( - a_demo_function(None, None, None), + self.assertNotEqual( + demo_function(None, None, None), 2, "Triple-argument case failed.", ) diff --git a/tests/test_flip_image.py b/tests/test_flip_image.py index b2497bf..acd953b 100644 --- a/tests/test_flip_image.py +++ b/tests/test_flip_image.py @@ -2,15 +2,15 @@ import unittest import numpy as np -from csst_proto.api import flip_image, read_test_image -from csst_proto.flip_image import flip_multiple_images_jl, flip_multiple_images_mp +from csst_proto import flip_image, read_default_image +from csst_proto import flip_multiple_images_jl, flip_multiple_images_mp class FlipImageTestCase(unittest.TestCase): def test_flip_image(self): - """ test flip image """ + """test flip image""" self.assertTrue( - np.all(flip_image(read_test_image()) == np.array([[4, 3], [2, 1]])) + np.all(flip_image(read_default_image()) == np.array([[4, 3], [2, 1]])) ) # the code fails for 1D array @@ -18,21 +18,17 @@ class FlipImageTestCase(unittest.TestCase): flip_image(np.array([1, 2, 3, 4])) def test_flip_multiple_images_mp(self): - """ test flip multiple images with multiprocessing """ + """test flip multiple images with multiprocessing""" n_jobs = 10 - imgs = [read_test_image() for i_job in range(n_jobs)] + imgs = [read_default_image() for i_job in range(n_jobs)] flipped_imgs = flip_multiple_images_mp(imgs, n_jobs) for i_job in range(n_jobs): - self.assertTrue( - np.all(flipped_imgs[i_job] == np.array([[4, 3], [2, 1]])) - ) + self.assertTrue(np.all(flipped_imgs[i_job] == np.array([[4, 3], [2, 1]]))) def test_flip_multiple_images_jl(self): - """ test flip multiple images with joblib """ + """test flip multiple images with joblib""" n_jobs = 10 - imgs = [read_test_image() for i_job in range(n_jobs)] + imgs = [read_default_image() for i_job in range(n_jobs)] flipped_imgs = flip_multiple_images_jl(imgs, n_jobs) for i_job in range(n_jobs): - self.assertTrue( - np.all(flipped_imgs[i_job] == np.array([[4, 3], [2, 1]])) - ) + self.assertTrue(np.all(flipped_imgs[i_job] == np.array([[4, 3], [2, 1]]))) -- GitLab