def test_fetch_hash_url(self, murlopen, mexists, mmakedirs, mremove): res = backend.URLResource( 'name', { 'url': 'http://example.com/path/fn', 'hash': 'http://example.com/path/fn.hash', 'hash_type': 'hash_type', }, 'od') mexists.return_value = True murlopen.return_value.read.return_value = b'myhash' mopen = mock.mock_open() with mock.patch.object(backend, 'open', mopen, create=True): res.fetch() assert not mmakedirs.called mremove.assert_called_with('od/name/fn') murlopen.assert_any_call('http://example.com/path/fn') mopen.assert_any_call('od/name/fn', 'wb') murlopen.assert_any_call('http://example.com/path/fn.hash') mopen.assert_any_call('od/name/fn.hash', 'w+') self.assertEqual(res.hash, 'myhash') res = backend.URLResource( 'name', { 'url': 'http://example.com/path/fn', 'hash': 'http://example.com/path/fn.hash', 'hash_type': 'hash_type', }, 'od') murlopen.reset_mock() with mock.patch.object(backend, 'open', mopen, create=True): res.fetch('http://mirror.com/cache/') murlopen.assert_any_call('http://mirror.com/cache/name/fn') mopen.assert_any_call('od/name/fn', 'wb') murlopen.assert_any_call('http://mirror.com/cache/name/fn.hash') mopen.assert_any_call('od/name/fn.hash', 'w+')
def test_init_explicit_filename(self): res = backend.URLResource('name', { 'url': 'http://example.com/fn', 'filename': 'myfn' }, 'od') self.assertEqual(res.filename, 'myfn') self.assertEqual(res.destination, 'od/name/myfn')
def test_init_empty(self): res = backend.URLResource('name', {}, 'od') self.assertEqual(res.url, '') self.assertEqual(res.filename, '') self.assertEqual(res.destination, 'od/name/') self.assertEqual(res.hash, '') self.assertEqual(res.hash_type, '') self.assertEqual(res.output_dir, 'od')
def test_init(self): res = backend.URLResource( 'name', { 'url': 'http://example.com/fn', 'hash': 'hash', 'hash_type': 'hash_type', }, 'od') self.assertEqual(res.url, 'http://example.com/fn') self.assertEqual(res.filename, 'fn') self.assertEqual(res.destination, 'od/name/fn') self.assertEqual(res.hash, 'hash') self.assertEqual(res.hash_type, 'hash_type')
def test_fetch(self, murlopen, mopen, mexists, mmakedirs, mremove): res = backend.URLResource( 'name', { 'url': 'http://example.com/path/fn', 'hash': 'hash', 'hash_type': 'hash_type', }, 'od') mexists.return_value = True res.fetch() assert not mmakedirs.called mremove.assert_called_with('od/name/fn') murlopen.assert_called_with('http://example.com/path/fn') mopen.assert_called_with('od/name/fn', 'wb') mexists.return_value = False res.fetch('http://mirror.com/cache/') mmakedirs.assert_called_with('od/name') murlopen.assert_called_with('http://mirror.com/cache/name/fn') mopen.assert_called_with('od/name/fn', 'wb')
def test_init_explicit_destination(self): res = backend.URLResource('name', { 'url': 'http://example.com/fn', 'destination': 'dst' }, 'od') self.assertEqual(res.destination, 'dst')