Esempio n. 1
0
    def test_errors(self) -> None:
        # Count on environment variables being set
        box = Box()

        nonexistent_id = '9999999'
        table = Table([['phone_number', 'last_name', 'first_name'],
                       ['4435705355', 'Warren', 'Elizabeth'],
                       ['5126993336', 'Obama', 'Barack']])

        # Upload a bad format
        with self.assertRaises(ValueError):
            box.upload_table_to_folder_id(table, 'temp1', format='bad_format')

        # Download a bad format
        with self.assertRaises(ValueError):
            box.get_table_by_file_id(file_id=nonexistent_id,
                                     format='bad_format')

        # Upload to non-existent folder
        with self.assertLogs(level=logging.WARNING):
            with self.assertRaises(BoxAPIException):
                box.upload_table_to_folder_id(table,
                                              'temp1',
                                              folder_id=nonexistent_id)

        # Download a non-existent file
        with self.assertLogs(level=logging.WARNING):
            with self.assertRaises(BoxAPIException):
                box.get_table_by_file_id(nonexistent_id, format='json')

        # Create folder in non-existent parent
        with self.assertRaises(ValueError):
            box.create_folder('nonexistent_path/path')

        # Create folder in non-existent parent
        with self.assertLogs(level=logging.WARNING):
            with self.assertRaises(BoxAPIException):
                box.create_folder_by_id(folder_name='subfolder',
                                        parent_folder_id=nonexistent_id)

        # Try using bad credentials
        box = Box(access_token='5345345345')
        with self.assertLogs(level=logging.WARNING):
            with self.assertRaises(BoxOAuthException):
                box.list_files_by_id()
Esempio n. 2
0
    def test_upload_file(self) -> None:
        # Count on environment variables being set
        box = Box()

        table = Table([['phone_number', 'last_name', 'first_name'],
                       ['4435705355', 'Warren', 'Elizabeth'],
                       ['5126993336', 'Obama', 'Barack']])
        box_file = box.upload_table_to_folder_id(table,
                                                 'phone_numbers',
                                                 folder_id=self.temp_folder_id)

        new_table = box.get_table_by_file_id(box_file.id)

        # Check that what we saved is equal to what we got back
        self.assertEqual(str(table), str(new_table))

        # Check that things also work in JSON
        box_file = box.upload_table_to_folder_id(table,
                                                 'phone_numbers_json',
                                                 folder_id=self.temp_folder_id,
                                                 format='json')

        new_table = box.get_table_by_file_id(box_file.id, format='json')

        # Check that what we saved is equal to what we got back
        self.assertEqual(str(table), str(new_table))

        # Now check the same thing with paths instead of file_id
        path_filename = 'path_phone_numbers'
        box_file = box.upload_table(
            table, f'{self.temp_folder_name}/{path_filename}')
        new_table = box.get_table(
            path=f'{self.temp_folder_name}/{path_filename}')

        # Check that we throw an exception with bad formats
        with self.assertRaises(ValueError):
            box.upload_table_to_folder_id(table,
                                          'phone_numbers',
                                          format='illegal_format')
        with self.assertRaises(ValueError):
            box.get_table_by_file_id(box_file.id, format='illegal_format')