From 98fb46965b1f4a286fa4c5d3ffe802a7094c7990 Mon Sep 17 00:00:00 2001 From: Mateus Latrova Date: Thu, 28 Sep 2023 20:28:32 -0300 Subject: [PATCH] add test for "compare_modules" function --- tests/_data/empty.txt | 0 tests/_data/imports.txt | 3 +++ tests/test_pipreqs.py | 20 ++++++++++++++++++++ 3 files changed, 23 insertions(+) create mode 100644 tests/_data/empty.txt create mode 100644 tests/_data/imports.txt diff --git a/tests/_data/empty.txt b/tests/_data/empty.txt new file mode 100644 index 0000000..e69de29 diff --git a/tests/_data/imports.txt b/tests/_data/imports.txt new file mode 100644 index 0000000..32eea8a --- /dev/null +++ b/tests/_data/imports.txt @@ -0,0 +1,3 @@ +pandas==2.0.0 +numpy>=1.2.3 +torch<4.0.0 \ No newline at end of file diff --git a/tests/test_pipreqs.py b/tests/test_pipreqs.py index 039d4fd..f1c6751 100644 --- a/tests/test_pipreqs.py +++ b/tests/test_pipreqs.py @@ -39,6 +39,9 @@ def setUp(self): self.project = os.path.join(os.path.dirname(__file__), "_data") self.project_clean = os.path.join(os.path.dirname(__file__), "_data_clean") self.project_invalid = os.path.join(os.path.dirname(__file__), "_invalid_data") + self.parsed_packages = [{'name': 'pandas', 'version': '2.0.0'}, {'name': 'numpy', 'version': '1.2.3'}, {'name': 'torch', 'version': '4.0.0'}] + self.empty_filepath = os.path.join(self.project, "empty.txt") + self.imports_filepath = os.path.join(self.project, "imports.txt") self.project_with_ignore_directory = os.path.join( os.path.dirname(__file__), "_data_ignore" ) @@ -427,6 +430,23 @@ def test_clean_with_imports_to_clean(self): data = f.read().lower() self.assertTrue(cleaned_module not in data) + def test_compare_modules(self): + test_cases = [ + ( self.empty_filepath, [], set() ), # both empty + ( self.empty_filepath, self.parsed_packages, set() ), # only file empty + ( self.imports_filepath, [], set(package["name"] for package in self.parsed_packages) ), # only imports empty + ( self.imports_filepath, self.parsed_packages, set() ), # no difference + ( self.imports_filepath, self.parsed_packages[1:], set([self.parsed_packages[0]["name"]]) ) # common case + ] + + for test_case in test_cases: + with self.subTest(test_case): + filename, imports, expected_modules_not_imported = test_case + + modules_not_imported = pipreqs.compare_modules(filename, imports) + + self.assertSetEqual(modules_not_imported, expected_modules_not_imported) + def tearDown(self): """ Remove requiremnts.txt files that were written