Skip to content

Commit

Permalink
Removed dependency on @mock.patch('test_core...')
Browse files Browse the repository at this point in the history
The construct was working fine if starting in the tests directory but
the module would not be found in the context of python setup.py tests
when the current directory is one level up.
  • Loading branch information
elmotec committed Jan 16, 2021
1 parent 24d5f35 commit da384bf
Showing 1 changed file with 25 additions and 21 deletions.
46 changes: 25 additions & 21 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,8 @@ def other():
)

class FakeProject(scm.Project):
"""Fake project with pre-determined values for the download return values."""

def download(self, data: pd.DataFrame) -> scm.DownloadResult:
pass

Expand Down Expand Up @@ -434,8 +436,9 @@ def setUp(self):
def get_complexity(self):
"""Factor retrieval of complexity"""
project = self.FakeProject()
with mock.patch(
"test_core.GetComplexityTestCase.FakeProject.download",
with mock.patch.object(
self.FakeProject,
"download",
autospec=True,
side_effect=[
scm.DownloadResult("r1", "f.py", self.file_content_1),
Expand All @@ -460,23 +463,26 @@ def test_lizard_analyze(self, _):
self.assertEqual(4, actual.nloc)
self.assertEqual(2.0, actual.average_cyclomatic_complexity)

@mock.patch(
"test_core.GetComplexityTestCase.FakeProject.download",
autospec=True,
return_value=scm.DownloadResult(1, "f.py", ""),
)
def test_handles_no_function(self, _):
def test_handles_no_function(self):
"""Handles files with no function well."""
actual = (
cm.get_complexity(self.log, self.FakeProject())
.reset_index()
.pipe(pd.Series.astype, "string")
)
columns = (
"function".split()
+ cm.core._lizard_fields
+ "file_tokens file_nloc".split()
)
project = self.FakeProject()
with mock.patch.object(
self.FakeProject,
"download",
autospec=True,
return_value=scm.DownloadResult(1, "f.py", ""),
) as download:
actual = (
cm.get_complexity(self.log, project)
.reset_index()
.pipe(pd.Series.astype, "string")
)
download.assert_called_with(project, self.log)
expected = pd.DataFrame(data={k: [] for k in columns}, dtype="string")
self.assertEqual(expected, actual)

Expand All @@ -487,16 +493,14 @@ def test_analysis_empty_input_return_empty_output(self, _):
actual = cm.get_complexity(self.log, self.FakeProject())
self.assertTrue(actual.empty)

@mock.patch(
"test_core.GetComplexityTestCase.FakeProject.download",
autospec=True,
return_value=scm.DownloadResult(1, "/", ""),
)
def test_use_default_download(self, download):
def test_use_default_download(self):
"""When the context.downlad_funcc is defined, use it."""
project = self.FakeProject()
_ = cm.get_complexity(self.log, project)
download.assert_called_with(project, self.log)
with mock.patch.object(
self.FakeProject, "download", return_value=scm.DownloadResult(1, "/", "")
) as download:
_ = cm.get_complexity(self.log, project)
download.assert_called_with(self.log)

def test_analysis_with_groupby_svn_download(self):
"""Check interface with svn."""
Expand Down

0 comments on commit da384bf

Please sign in to comment.