Skip to content

Commit

Permalink
add test for "compare_modules" function
Browse files Browse the repository at this point in the history
  • Loading branch information
Mateus Latrova committed Sep 28, 2023
1 parent 1c40bdd commit 487b5a3
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
Empty file added tests/_data/empty.txt
Empty file.
3 changes: 3 additions & 0 deletions tests/_data/imports.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pandas==2.0.0
numpy>=1.2.3
torch<4.0.0
21 changes: 21 additions & 0 deletions tests/test_pipreqs.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,12 @@ def setUp(self):
'after_method_is_valid_even_if_not_pep8'
]
self.modules2 = ['beautifulsoup4']
self.parsed_packages = [{'name': 'pandas', 'version': '2.0.0'}, {'name': 'numpy', 'version': '1.2.3'}, {'name': 'torch', 'version': '4.0.0'}]

self.local = ["docopt", "requests", "nose", 'pyflakes']
self.project = os.path.join(os.path.dirname(__file__), "_data")
self.empty_filepath = os.path.join(self.project, "empty.txt")
self.imports_filepath = os.path.join(self.project, "imports.txt")
self.project_clean = os.path.join(
os.path.dirname(__file__),
"_data_clean"
Expand Down Expand Up @@ -322,6 +326,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
Expand Down

0 comments on commit 487b5a3

Please sign in to comment.