diff --git a/Makefile b/Makefile index 82ebbdf53ac191fffc8c119c8819d64ac6f82630..277966218e0814edfe352944f9e42a700059ddc4 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 fc76991c768b5e466a6983b4d76fcbf47a575808..e01c00bdac271f57f537d4904be9a1e3742b020b 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 fa6d3995d7cebcfeb110778eaee58c5edbdca2cb..ef5014e7705c4beaa43ef725e4d0769a4bb3e2aa 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 0000000000000000000000000000000000000000..c2f798b32d2ccfa8abaf011b7efe7c530d013059 --- /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 c717b3a60f739854671ce989f4dad642818da692..815629f408104561c16549fab832a5897dc72d5a 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 b2497bf3b5228ce4927d8636dc7403511ab41726..acd953b68d785368a84c223e85c8993a35919244 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]])))