def setUp(self):
     self.mock_key = mock.MagicMock('mock Key')
     self.mock_key.get_contents_to_filename = mock.MagicMock(
         return_value='contents')
     self.mock_key.set_contents_from_filename = mock.MagicMock(
         return_value='contents set')
     self.mock_key.delete = mock.MagicMock(return_value='deleted')
     self.root_dir = RandomTempDir()
     self.temp_file_path = self.root_dir.temp_file_path()
Ejemplo n.º 2
0
 def setUp(self):
     self.mock_key = mock.MagicMock('mock Key')
     self.mock_key.get_contents_to_filename = mock.MagicMock(return_value='contents')
     self.mock_key.set_contents_from_filename = mock.MagicMock(return_value='contents set')
     self.mock_key.delete = mock.MagicMock(return_value='deleted')
     self.root_dir = RandomTempDir()
     self.temp_file_path = self.root_dir.temp_file_path()
Ejemplo n.º 3
0
class S3CacheTest(unittest.TestCase):
   
    def setUp(self):
        self.mock_key = mock.MagicMock('mock Key')
        self.mock_key.get_contents_to_filename = mock.MagicMock(return_value='contents')
        self.mock_key.set_contents_from_filename = mock.MagicMock(return_value='contents set')
        self.mock_key.delete = mock.MagicMock(return_value='deleted')
        self.root_dir = RandomTempDir()
        self.temp_file_path = self.root_dir.temp_file_path()

    def tearDown(self):
        shutil.rmtree(self.root_dir.full_path, True)


    def test_that_when_the_given_key_exists_then_get_calls_get_contents_to_filename(self):
        path = self.temp_file_path
        with mock.patch( 'postcode_api.caches.s3_cache.S3Cache._s3_key', return_value=self.mock_key ):
            self.mock_key.exists = mock.MagicMock(return_value=True)
            S3Cache().get('my_key', path)
            self.mock_key.get_contents_to_filename.assert_called_with_arguments(path)


    def test_that_when_the_given_key_does_not_exist_then_get_does_not_call_get_contents_to_filename(self):
        with mock.patch( 'postcode_api.caches.s3_cache.S3Cache._s3_key', return_value=self.mock_key ):
            self.mock_key.exists = mock.MagicMock(return_value=False)
            S3Cache().get('my_key', self.temp_file_path)
            self.assertEqual(False, self.mock_key.get_contents_to_filename.called)

    def test_that_when_the_given_key_exists_then_has_returns_true(self):
        with mock.patch( 'postcode_api.caches.s3_cache.S3Cache._s3_key', return_value=self.mock_key ):
            self.mock_key.exists = mock.MagicMock(return_value=True)
            self.assertEqual(True, S3Cache().has('some key'))

    def test_that_when_the_given_key_does_not_exist_then_has_returns_false(self):
        with mock.patch( 'postcode_api.caches.s3_cache.S3Cache._s3_key', return_value=self.mock_key ):
            self.mock_key.exists = mock.MagicMock(return_value=False)
            self.assertEqual(False, S3Cache().has('some key'))

    def test_that_put_sets_the_given_cache_key_with_the_contents_of_the_given_filename(self):
        with mock.patch( 'postcode_api.caches.s3_cache.S3Cache._s3_key', return_value=self.mock_key ):
            S3Cache().put('my_key', self.temp_file_path)
            self.assertEqual(True, self.mock_key.set_contents_from_filename.called)

    def test_that_when_the_given_key_exists_then_delete_calls_delete_on_the_key(self):
        with mock.patch( 'postcode_api.caches.s3_cache.S3Cache._s3_key', return_value=self.mock_key ):
            self.mock_key.exists = mock.MagicMock(return_value=True)
            S3Cache().delete('my_key')
            self.mock_key.delete.assert_called()

    def test_that_when_the_given_key_does_not_exist_then_delete_does_not_call_delete_on_the_key(self):
        with mock.patch( 'postcode_api.caches.s3_cache.S3Cache._s3_key', return_value=self.mock_key ):
            self.mock_key.exists = mock.MagicMock(return_value=False)
            S3Cache().delete('my_key')
            self.assertEqual(False, self.mock_key.delete.called)
class FilesystemCacheTest(unittest.TestCase):
   
    def setUp(self):
        self.root_dir = RandomTempDir()
        self.temp_file_path = self.root_dir.temp_file_path()
        self.cache = FilesystemCache(self.root_dir.full_path)

    def tearDown(self):
        shutil.rmtree(self.root_dir.full_path, True)
        

    def test_that_when_the_given_key_exists_then_get_copies_the_file_to_filename(self):
        self.cache.has = mock.MagicMock(return_value=False)
        with mock.patch( 'shutil.copy2', return_value='copied' ) as mock_copy:
            FilesystemCache().get('my_key', self.root_dir.full_path)
            mock_copy.assert_called_with_arguments(self.root_dir.full_path)


    def test_that_when_the_given_key_does_not_exist_then_get_does_not_copy_the_file(self):
        self.cache.has = mock.MagicMock(return_value=False)
        with mock.patch( 'shutil.copy2' ) as mock_copy:
            FilesystemCache().get('my_key', self.root_dir.full_path)
            self.assertEqual(False, mock_copy.called)

    def test_that_when_the_given_key_exists_then_has_returns_true(self):
        with mock.patch( 'os.path.isfile', return_value=True ):
            self.assertEqual(True, FilesystemCache().has('some key'))

    def test_that_when_the_given_key_does_not_exist_then_has_returns_false(self):
        with mock.patch( 'os.path.isfile', return_value=False ):
            self.assertEqual(False, FilesystemCache().has('some key'))

    def test_that_put_sets_the_given_cache_key_with_the_contents_of_the_given_filename(self):
        path = self.root_dir.full_path
        self.cache.has = mock.MagicMock(return_value=False)
        with mock.patch( 'shutil.copy2', return_value='copied' ) as mock_copy:
            FilesystemCache().put('my_key', path)
            mock_copy.assert_called_with_arguments(path)

    def test_that_when_the_given_key_exists_then_delete_removes_the_file(self):
        with mock.patch( 'os.remove' ) as mock_delete:
            self.cache.has = mock.MagicMock(return_value=True)
            FilesystemCache().delete('my_key')
            mock_delete.assert_called()

    def test_that_when_the_given_key_does_not_exist_then_delete_does_not_call_delete_on_the_key(self):
        with mock.patch( 'os.remove' ) as mock_delete:
            self.cache.has = mock.MagicMock(return_value=False)
            FilesystemCache().delete('my_key')
            self.assertEqual(False, mock_delete.called)
 def setUp(self):
     self.root_dir = RandomTempDir()
     self.temp_file_path = self.root_dir.temp_file_path()
     self.cache = FilesystemCache(self.root_dir.full_path)
class S3CacheTest(unittest.TestCase):
    def setUp(self):
        self.mock_key = mock.MagicMock('mock Key')
        self.mock_key.get_contents_to_filename = mock.MagicMock(
            return_value='contents')
        self.mock_key.set_contents_from_filename = mock.MagicMock(
            return_value='contents set')
        self.mock_key.delete = mock.MagicMock(return_value='deleted')
        self.root_dir = RandomTempDir()
        self.temp_file_path = self.root_dir.temp_file_path()

    def tearDown(self):
        shutil.rmtree(self.root_dir.full_path, True)

    def test_that_when_the_given_key_exists_then_get_calls_get_contents_to_filename(
            self):
        path = self.temp_file_path
        with mock.patch('postcode_api.caches.s3_cache.S3Cache._s3_key',
                        return_value=self.mock_key):
            self.mock_key.exists = mock.MagicMock(return_value=True)
            S3Cache().get('my_key', path)
            self.mock_key.get_contents_to_filename.assert_called_with_arguments(
                path)

    def test_that_when_the_given_key_does_not_exist_then_get_does_not_call_get_contents_to_filename(
            self):
        with mock.patch('postcode_api.caches.s3_cache.S3Cache._s3_key',
                        return_value=self.mock_key):
            self.mock_key.exists = mock.MagicMock(return_value=False)
            S3Cache().get('my_key', self.temp_file_path)
            self.assertEqual(False,
                             self.mock_key.get_contents_to_filename.called)

    def test_that_when_the_given_key_exists_then_has_returns_true(self):
        with mock.patch('postcode_api.caches.s3_cache.S3Cache._s3_key',
                        return_value=self.mock_key):
            self.mock_key.exists = mock.MagicMock(return_value=True)
            self.assertEqual(True, S3Cache().has('some key'))

    def test_that_when_the_given_key_does_not_exist_then_has_returns_false(
            self):
        with mock.patch('postcode_api.caches.s3_cache.S3Cache._s3_key',
                        return_value=self.mock_key):
            self.mock_key.exists = mock.MagicMock(return_value=False)
            self.assertEqual(False, S3Cache().has('some key'))

    def test_that_put_sets_the_given_cache_key_with_the_contents_of_the_given_filename(
            self):
        with mock.patch('postcode_api.caches.s3_cache.S3Cache._s3_key',
                        return_value=self.mock_key):
            S3Cache().put('my_key', self.temp_file_path)
            self.assertEqual(True,
                             self.mock_key.set_contents_from_filename.called)

    def test_that_when_the_given_key_exists_then_delete_calls_delete_on_the_key(
            self):
        with mock.patch('postcode_api.caches.s3_cache.S3Cache._s3_key',
                        return_value=self.mock_key):
            self.mock_key.exists = mock.MagicMock(return_value=True)
            S3Cache().delete('my_key')
            self.mock_key.delete.assert_called()

    def test_that_when_the_given_key_does_not_exist_then_delete_does_not_call_delete_on_the_key(
            self):
        with mock.patch('postcode_api.caches.s3_cache.S3Cache._s3_key',
                        return_value=self.mock_key):
            self.mock_key.exists = mock.MagicMock(return_value=False)
            S3Cache().delete('my_key')
            self.assertEqual(False, self.mock_key.delete.called)
 def setUp(self):
     self.root_dir = RandomTempDir()
     self.temp_file_path = self.root_dir.temp_file_path()
class AddressBaseBasicDownloaderTest(unittest.TestCase):

    def setUp(self):
        self.root_dir = RandomTempDir()
        self.temp_file_path = self.root_dir.temp_file_path()

    def tearDown(self):
        shutil.rmtree(self.root_dir.full_path, True)

    def mock_env(self, env=None):
        if env is None:
            ftpuser = '******'
            ftppass = '******'
            ftpdir = 'my/dir'
            self.env = {
                'OS_FTP_USERNAME': ftpuser,
                'OS_FTP_PASSWORD': ftppass,
                'OS_FTP_ORDER_DIR': ftpdir}
        else:
            self.env = env

        return mock.patch.dict('os.environ', self.env, clear=True)

    def _mock_find_dir(self):
        mock_method = mock.MagicMock(return_value='SOMEDIR')
        AddressBaseBasicDownloader.find_dir_with_latest_full_file = mock_method
        return mock_method

    def test_passes_ftp_credentials(self):
        with mock.patch('ftplib.FTP') as ftp_class, self.mock_env():
            ftp = ftp_class.return_value
            AddressBaseBasicDownloader().download(self.root_dir.full_path)
            ftp_class.assertCalledWith('osmmftp.os.uk')
            ftp.login.assertCalledWith(
                self.env['OS_FTP_USERNAME'], self.env['OS_FTP_PASSWORD'])
            ftp.cwd.assertCalledWith(self.env['OS_FTP_ORDER_DIR'])

    def test_complains_if_ftp_credentials_not_set(self):
        self._mock_find_dir()
        logger = 'postcode_api.downloaders.addressbase_basic.log'
        with mock.patch('ftplib.FTP'), \
                mock.patch(logger) as log, \
                self.mock_env({}):

            AddressBaseBasicDownloader().download(self.root_dir.full_path)

            log.error.assert_has_calls([
                mock.call('OS_FTP_USERNAME not set!'),
                mock.call('OS_FTP_PASSWORD not set!')])

    def test_downloads_files_matching_pattern(self):
        self._mock_find_dir()
        with mock.patch('ftplib.FTP') as ftp_class, self.mock_env():
            ftp = ftp_class.return_value
            AddressBaseBasicDownloader().download(self.root_dir.full_path)
            self.assertTrue(ftp.dir.called)
            self.assertEqual('*_csv.zip', ftp.dir.call_args[0][0])

    def test_finds_dir_with_latest_full_file(self):
        self._mock_find_dir()
        with mock.patch('ftplib.FTP'), self.mock_env():
            dl = AddressBaseBasicDownloader()
            dl.download(self.root_dir.full_path)
            self.assertTrue(dl.find_dir_with_latest_full_file.called)
 def setUp(self):
     self.root_dir = RandomTempDir()
     self.temp_file_path = self.root_dir.temp_file_path()
class AddressBaseBasicDownloaderTest(unittest.TestCase):
    def setUp(self):
        self.root_dir = RandomTempDir()
        self.temp_file_path = self.root_dir.temp_file_path()

    def tearDown(self):
        shutil.rmtree(self.root_dir.full_path, True)

    def mock_env(self, env=None):
        if env is None:
            ftpuser = '******'
            ftppass = '******'
            ftpdir = 'my/dir'
            self.env = {
                'OS_FTP_USERNAME': ftpuser,
                'OS_FTP_PASSWORD': ftppass,
                'OS_FTP_ORDER_DIR': ftpdir
            }
        else:
            self.env = env

        return mock.patch.dict('os.environ', self.env, clear=True)

    def _mock_find_dir(self):
        mock_method = mock.MagicMock(return_value='SOMEDIR')
        AddressBaseBasicDownloader.find_dir_with_latest_full_file = mock_method
        return mock_method

    def test_passes_ftp_credentials(self):
        with mock.patch('ftplib.FTP') as ftp_class, self.mock_env():
            ftp = ftp_class.return_value
            AddressBaseBasicDownloader().download(self.root_dir.full_path)
            ftp_class.assertCalledWith('osmmftp.os.uk')
            ftp.login.assertCalledWith(self.env['OS_FTP_USERNAME'],
                                       self.env['OS_FTP_PASSWORD'])
            ftp.cwd.assertCalledWith(self.env['OS_FTP_ORDER_DIR'])

    def test_complains_if_ftp_credentials_not_set(self):
        self._mock_find_dir()
        logger = 'postcode_api.downloaders.addressbase_basic.log'
        with mock.patch('ftplib.FTP'), \
                mock.patch(logger) as log, \
                self.mock_env({}):

            AddressBaseBasicDownloader().download(self.root_dir.full_path)

            log.error.assert_has_calls([
                mock.call('OS_FTP_USERNAME not set!'),
                mock.call('OS_FTP_PASSWORD not set!')
            ])

    def test_downloads_files_matching_pattern(self):
        self._mock_find_dir()
        with mock.patch('ftplib.FTP') as ftp_class, self.mock_env():
            ftp = ftp_class.return_value
            AddressBaseBasicDownloader().download(self.root_dir.full_path)
            self.assertTrue(ftp.dir.called)
            self.assertEqual('*_csv.zip', ftp.dir.call_args[0][0])

    def test_finds_dir_with_latest_full_file(self):
        self._mock_find_dir()
        with mock.patch('ftplib.FTP'), self.mock_env():
            dl = AddressBaseBasicDownloader()
            dl.download(self.root_dir.full_path)
            self.assertTrue(dl.find_dir_with_latest_full_file.called)