def test_download_file(self) -> None: box = Box() table = Table([['phone_number', 'last_name', 'first_name'], ['4435705355', 'Warren', 'Elizabeth'], ['5126993336', 'Obama', 'Barack']]) uploaded_file = table.to_csv() path_filename = f'{self.temp_folder_name}/my_path' box.upload_table(table, path_filename) downloaded_file = box.download_file(path_filename) with open(uploaded_file) as uploaded, open( downloaded_file) as downloaded: self.assertEqual(str(uploaded.read()), str(downloaded.read()))
def test_list_files_by_path(self) -> None: # Count on environment variables being set box = Box() # Make sure our test folder is in the right place found_default = False for item in box.list(): if item['name'] == self.temp_folder_name: found_default = True break self.assertTrue( found_default, f'Failed to find test folder f{self.temp_folder_name} ' f'in default Box folder') subfolder_name = 'path_subfolder' subfolder_path = f'{self.temp_folder_name}/{subfolder_name}' box.create_folder(path=subfolder_path) # 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(table, f'{subfolder_path}/temp1') box.upload_table(table, f'{subfolder_path}/temp2') box.create_folder(f'{subfolder_path}/temp_folder1') box.create_folder(f'{subfolder_path}/temp_folder2') file_list = box.list(path=subfolder_path, item_type='file') 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(path=f'{subfolder_path}/temp1') break file_list = box.list(path=subfolder_path, item_type='file') self.assertEqual(['temp2'], file_list['name']) folder_list = box.list(path=subfolder_path, item_type='folder') self.assertEqual(['temp_folder1', 'temp_folder2'], folder_list['name']) # Make sure we can delete by path box.delete_folder(f'{subfolder_path}/temp_folder1') folder_list = box.list(path=subfolder_path, item_type='folder') self.assertEqual(['temp_folder2'], folder_list['name'])
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')