Пример #1
0
    def setUp(self):
        """
        Function to create configuration file before every test case.
        """
        open(self.config_file_path, 'w+').close()
        with open(self.config_file_path, 'a') as f:
            f.write(os.path.join(PROJECT_DIR, 'README.md') + '\n')
            f.write(os.path.join(PROJECT_DIR, '.gitignore') + '\n')
            f.write(os.path.join(PROJECT_DIR, 'LICENSE') + '\n')

        self.drive = GoogleDrive(path_to_config=self.config_file_path)
Пример #2
0
class TestGoogleDrive(unittest.TestCase):
    current_dir = os.path.dirname(os.path.realpath(__file__))
    config_file_path = os.path.join(current_dir, 'config')

    def setUp(self):
        """
        Function to create configuration file before every test case.
        """
        open(self.config_file_path, 'w+').close()
        with open(self.config_file_path, 'a') as f:
            f.write(os.path.join(PROJECT_DIR, 'README.md') + '\n')
            f.write(os.path.join(PROJECT_DIR, '.gitignore') + '\n')
            f.write(os.path.join(PROJECT_DIR, 'LICENSE') + '\n')

        self.drive = GoogleDrive(path_to_config=self.config_file_path)

    def tearDown(self):
        """
        Function to delete configuration file after every test case.
        """
        os.remove(self.config_file_path)
        del self.drive

    def test_get_files_to_upload(self):
        """
        get_files_to_upload should result equal for
        drive.get_files_to_upload and expected list with values
        from file.
        """
        expected_list = [os.path.join(PROJECT_DIR, 'README.md'),
                         os.path.join(PROJECT_DIR, '.gitignore'),
                         os.path.join(PROJECT_DIR, 'LICENSE')]
        self.assertEqual(expected_list, self.drive.get_files_to_upload())

    def test_get_service_with_credentials(self):
        """
        get_service_with_credentials should check if
        drive.get_service() returns not None and instance of
        type googleapiclient.discovery.Resource.
        """
        service = self.drive.get_service()
        self.assertNotEqual(service, None)
        self.assertTrue(isinstance(service, Resource))

    def test_get_service_with_wrong_credentials(self):
        """
        get_service_with_wrong_credentials should check if
        drive.get_service() raises AttributeError if
        GoogleDrive object has wrong credentials.
        """
        self.drive.credentials = None
        self.assertRaises(AttributeError, self.drive.get_service())

    def test_upload_file_with_correct_file_path(self):
        """
        upload_file_with_correct_file_path should result not
        equal to None and response from drive.upload_file().
        """
        response = self.drive.upload_file(
            os.path.join(PROJECT_DIR, 'README.md'))
        self.assertNotEqual(response, None)

    def test_upload_file_with_wrong_file_path(self):
        """
        upload_file_with_wrong_file_path should check if
        drive.upload_file raises IOError with nonexistent_file_path
        argument.
        """
        nonexistent_file_path = os.path.join(self.current_dir, 'nonexistent')
        self.assertRaises(IOError, self.drive.upload_file,
                          nonexistent_file_path)