Skip to content

Commit

Permalink
fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
marianoeramirez committed Jul 8, 2024
1 parent b4497f7 commit 4c21966
Showing 1 changed file with 14 additions and 16 deletions.
30 changes: 14 additions & 16 deletions src/cities_light/tests/test_downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ def test_download_calls_source_matches_destination(self, m_check):
m_check.return_value = True
downloader = Downloader()
source = 'file:///a.txt'
destination = '/a.txt'
destination = '/tmp/a.txt'
# The downloader.download will return false
# as source and destination are same
# The downloader.source_matches_destination will return
Expand All @@ -167,7 +167,7 @@ def test_download_calls_needs_downloading(self, m_check, m_need):
m_need.return_value = False
downloader = Downloader()
source = 'file:///a.txt'
destination = '/a.txt'
destination = '/tmp/a.txt'
# Here dowaloder.needs_downloading() will return false
# as the time of modifiaction of dest>= time of source
# and the size od source and destination are same
Expand All @@ -190,33 +190,31 @@ def test_download(self, m_check, m_need):
m_need.return_value = True
downloader = Downloader()
source = 'file:///b.txt'
destination = '/a.txt'
destination = '/tmp/a.txt'

tmpfile = tempfile.SpooledTemporaryFile(max_size=1024000, mode='wb')
tmpfile.write(b'source content')
tmpfile.seek(0)

mock_open = mock.mock_open()
with mock.patch('cities_light.downloader.urlopen',
return_value=tmpfile):
module_name = '{}.b.open'.format('cities_light.downloader.urlopen')
mock_open = mock.mock_open()
# The downoader.needs_downloading will return true and last three
# lines of downloader.download will copy the source to sestination
with mock.patch(module_name, mock_open):
self.assertTrue(downloader.download(
source,
destination,
False))
handle = mock_open()
handle.write.assert_called_once_with(b'source content')
return_value=tmpfile), mock.patch('cities_light.downloader.open', mock_open):
# The downloader.needs_downloading will return true and last three
# lines of downloader.download will copy the source to destination
self.assertTrue(downloader.download(
source,
destination,
False))
handle = mock_open()
handle.write.assert_called_once_with(b'source content')

def test_not_download(self):
"""Tests actual not download."""
with mock.patch.object(Downloader, 'source_matches_destination') as m:
m.return_value = True
downloader = Downloader()
source = 'file:///b.txt'
destination = '/a.txt'
destination = '/tmp/a.txt'
with mock.patch('cities_light.downloader.urlopen') as uo_mock:
downloader.download(source, destination)
uo_mock.assert_not_called()
Expand Down

0 comments on commit 4c21966

Please sign in to comment.