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

        subfolder = box.create_folder_by_id(
            folder_name='id_subfolder', parent_folder_id=self.temp_folder_id)

        # Create a couple of files in the temp folder
        table = Table([['phone_number', 'last_name', 'first_name'],
                       ['4435705355', 'Warren', 'Elizabeth'],
                       ['5126993336', 'Obama', 'Barack']])

        box.upload_table_to_folder_id(table, 'temp1', folder_id=subfolder)
        box.upload_table_to_folder_id(table, 'temp2', folder_id=subfolder)
        box.create_folder_by_id(folder_name='temp_folder1',
                                parent_folder_id=subfolder)
        box.create_folder_by_id(folder_name='temp_folder2',
                                parent_folder_id=subfolder)

        file_list = box.list_files_by_id(folder_id=subfolder)
        self.assertEqual(['temp1', 'temp2'], file_list['name'])

        # Check that if we delete a file, it's no longer there
        for box_file in file_list:
            if box_file['name'] == 'temp1':
                box.delete_file_by_id(box_file['id'])
                break
        file_list = box.list_files_by_id(folder_id=subfolder)
        self.assertEqual(['temp2'], file_list['name'])

        folder_list = box.list_folders_by_id(folder_id=subfolder)['name']
        self.assertEqual(['temp_folder1', 'temp_folder2'], folder_list)
Exemplo n.º 2
0
    def test_get_item_id(self) -> None:
        # Count on environment variables being set
        box = Box()

        # Create a subfolder in which we'll do this test
        sub_sub_folder_name = 'item_subfolder'
        sub_sub_folder_id = box.create_folder_by_id(
            folder_name=sub_sub_folder_name,
            parent_folder_id=self.temp_folder_id)

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

        box_file = box.upload_table_to_folder_id(table,
                                                 'phone_numbers',
                                                 folder_id=sub_sub_folder_id)

        # Now try getting various ids
        file_path = f'{self.temp_folder_name}/item_subfolder/phone_numbers'
        self.assertEqual(box_file.id, box.get_item_id(path=file_path))

        file_path = f'{self.temp_folder_name}/item_subfolder'
        self.assertEqual(sub_sub_folder_id, box.get_item_id(path=file_path))

        file_path = self.temp_folder_name
        self.assertEqual(self.temp_folder_id, box.get_item_id(path=file_path))

        # Trailing "/"
        with self.assertRaises(ValueError):
            file_path = f'{self.temp_folder_name}/item_subfolder/phone_numbers/'
            box.get_item_id(path=file_path)

        # Nonexistent file
        with self.assertRaises(ValueError):
            file_path = f'{self.temp_folder_name}/item_subfolder/nonexistent/phone_numbers'
            box.get_item_id(path=file_path)

        # File (rather than folder) in middle of path
        with self.assertRaises(ValueError):
            file_path = f'{self.temp_folder_name}/file_in_subfolder/phone_numbers'
            box.get_item_id(path=file_path)
Exemplo n.º 3
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()
Exemplo n.º 4
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')