From 98ad783346143d6afe279b4ed48b1d01e1055725 Mon Sep 17 00:00:00 2001 From: kylenstone Date: Sun, 17 May 2020 13:10:03 -0400 Subject: [PATCH 1/6] Add .download folder and .vscode settings to .gitignore --- .gitignore | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 708925b5..694d0323 100644 --- a/.gitignore +++ b/.gitignore @@ -11,7 +11,6 @@ __pycache__/ build/ develop-eggs/ dist/ -downloads/ eggs/ .eggs/ lib/ @@ -47,6 +46,9 @@ coverage.xml .hypothesis/ .pytest_cache/ +# Local data used for tests +downloads/ + # Translations *.mo *.pot @@ -89,6 +91,7 @@ venv/ ENV/ env.bak/ venv.bak/ +.vscode # Spyder project settings .spyderproject @@ -107,4 +110,4 @@ venv.bak/ # Pipenv Pipfile -Pipfile.lock \ No newline at end of file +Pipfile.lock From 48f83b6dc39200d2374015c62c58feb1a09dd9eb Mon Sep 17 00:00:00 2001 From: kylenstone Date: Sun, 17 May 2020 13:12:19 -0400 Subject: [PATCH 2/6] Add conftest.py file --- tests/conftest.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index 6ff505e3..61aec338 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,6 +1,12 @@ import pytest +import os + from frameioclient import FrameioClient -@pytest.fixture -def frameioclient(token): - return FrameioClient("aaaabbbbccccddddeeee") +token = os.getenv('FRAME_IO_TOKEN') + +@pytest.fixture() +def setup_client(): + client = FrameioClient(token) + return client + \ No newline at end of file From 8e531a564f64a7020f537e7fd995449fd452bc40 Mon Sep 17 00:00:00 2001 From: kylenstone Date: Sun, 17 May 2020 13:26:57 -0400 Subject: [PATCH 3/6] Use setup_client() fixture in test_frameioclient --- tests/test_frameioclient.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/test_frameioclient.py b/tests/test_frameioclient.py index e60490a8..6c4588a0 100644 --- a/tests/test_frameioclient.py +++ b/tests/test_frameioclient.py @@ -1,5 +1,5 @@ -import pytest from frameioclient import FrameioClient -def test_FrameioClient(frameioclient): - assert type(frameioclient) == FrameioClient +def test_FrameioClient(setup_client): + client = setup_client + assert type(client) == FrameioClient \ No newline at end of file From 12053793644991f890e1c30070c662118e9f2143 Mon Sep 17 00:00:00 2001 From: kylenstone Date: Sun, 17 May 2020 14:00:31 -0400 Subject: [PATCH 4/6] Mv integration.py to test_integration.py. Use setup fixture, tests are passing --- tests/test_integration.py | 158 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 158 insertions(+) create mode 100644 tests/test_integration.py diff --git a/tests/test_integration.py b/tests/test_integration.py new file mode 100644 index 00000000..05ff99d4 --- /dev/null +++ b/tests/test_integration.py @@ -0,0 +1,158 @@ +import os +import sys +import json +import mimetypes +import platform +import time +import pytest + +from frameioclient import FrameioClient + +# .env variables used for test assertions +project_id = os.getenv('PROJECT_ID') +root_asset_id = os.getenv('ROOT_ASSET_ID') +download_asset_id = os.getenv('DOWNLOAD_FOLDER_ID') +upload_folder_id = os.getenv('UPLOAD_FOLDER_ID') + + +# Test download functionality +def test_download(setup_client): + client = setup_client + + print("Testing download function...") + with pytest.raises(FileExistsError): + os.mkdir('downloads') + asset_list = client.get_asset_children( + download_asset_id, + page=1, + page_size=40, + include="children" + ) + + print("Downloading {} files.".format(len(asset_list))) + for asset in asset_list: + client.download(asset, 'downloads') + + print("Done downloading files") + + return True + +# Test upload functionality +def test_upload(setup_client): + client = setup_client + print("Beginning upload test") + # Create new parent asset + + print("Creating new folder to upload to") + new_folder = client.create_asset( + parent_asset_id=root_asset_id, + name="Python {} Upload Test".format(platform.python_version()), + type="folder", + ) + + new_parent_id = new_folder['id'] + + print("Folder created, id: {}".format(new_parent_id)) + + # Upload all the files we downloaded earlier + dled_files = os.listdir('downloads') + + for count, fn in enumerate(dled_files, start=1): + print("Uploading {}".format(fn)) + abs_path = os.path.join(os.curdir, 'downloads', fn) + filesize = os.path.getsize(abs_path) + filename = os.path.basename(abs_path) + filemime = mimetypes.guess_type(abs_path)[0] + + asset = client.create_asset( + parent_asset_id=new_parent_id, + name=filename, + type="file", + filetype=filemime, + filesize=filesize + ) + + with open(abs_path, "rb") as ul_file: + client.upload(asset, ul_file) + + print("Done uploading file {} of {}".format((count), len(dled_files))) + + print("Sleeping for 5 seconds to allow uploads to finish...") + time.sleep(5) + + print("Continuing...") + + return new_parent_id + +# Flatten asset children and pull out important info for comparison +def flatten_asset_children(asset_children): + flat_dict = dict() + + for asset in asset_children: + flat_dict[asset['name']] = asset['filesize'] + + return flat_dict + + +def check_upload_completion(setup_client, download_folder_id, upload_folder_id): + client = setup_client + # Do a comparison against filenames and filesizes here to make sure they match + + print("Beginning upload comparison check") + + # Get asset children for download folder + dl_asset_children = client.get_asset_children( + download_folder_id, + page=1, + page_size=40, + include="children" + ) + + print("Got asset children for original download folder") + + # Get asset children for upload folder + ul_asset_children = client.get_asset_children( + upload_folder_id, + page=1, + page_size=40, + include="children" + ) + + print("Got asset children for uploaded folder") + + dl_items = flatten_asset_children(dl_asset_children) + ul_items = flatten_asset_children(ul_asset_children) + + print("Running comparison...") + + if sys.version_info.major >= 3: + import operator + comparison = operator.eq(dl_items, ul_items) + + if comparison == False: + print("File mismatch between upload and download") + sys.exit(1) + + else: + # Use different comparsion function in < Py3 + comparison = cmp(dl_items, ul_items) + if comparison != 0: + print("File mismatch between upload and download") + sys.exit(1) + + print("Integration test passed!!!") + + return True + + +def clean_up(setup_client, asset_to_delete): + client = setup_client + print("Removing files from test...") + + try: + client._api_call('delete', '/assets/{}'.format(asset_to_delete)) + print("Managed to cleanup!") + except Exception as e: + print(e) + + return True \ No newline at end of file From ab89bf0b999f12a4d733df15b04b10ffe11622e9 Mon Sep 17 00:00:00 2001 From: kylenstone Date: Sun, 17 May 2020 14:01:44 -0400 Subject: [PATCH 5/6] remove integration.py. Add test for uploading single .txt file --- tests/integration.py | 177 -------------------------------------- tests/test_integration.py | 29 ++++++- 2 files changed, 28 insertions(+), 178 deletions(-) delete mode 100644 tests/integration.py diff --git a/tests/integration.py b/tests/integration.py deleted file mode 100644 index 8d0aaa9a..00000000 --- a/tests/integration.py +++ /dev/null @@ -1,177 +0,0 @@ -import os -import sys -import json -import mimetypes -import platform -import time - -from frameioclient import FrameioClient - -token = os.getenv("FRAMEIO_TOKEN") -project_id = os.getenv("PROJECT_ID") -download_asset_id = os.getenv("DOWNLOAD_FOLDER_ID") - - -# Initialize the client -def init_client(): - if len(token) < 5: - print("Bad token, exiting test.") - sys.exit(1) - - client = FrameioClient(token) - print("Client connection initialized.") - - return client - -# Test download functionality -def test_download(client): - print("Testing download function...") - os.mkdir('downloads') - - asset_list = client.get_asset_children( - download_asset_id, - page=1, - page_size=40, - include="children" - ) - - print("Downloading {} files.".format(len(asset_list))) - for asset in asset_list: - client.download(asset, 'downloads') - - print("Done downloading files") - - return True - -# Test upload functionality -def test_upload(client): - print("Beginning upload test") - # Create new parent asset - project_info = client.get_project(project_id) - root_asset_id = project_info['root_asset_id'] - - print("Creating new folder to upload to") - new_folder = client.create_asset( - parent_asset_id=root_asset_id, - name="Python {} Upload Test".format(platform.python_version()), - type="folder", - ) - - new_parent_id = new_folder['id'] - - print("Folder created, id: {}".format(new_parent_id)) - - # Upload all the files we downloaded earlier - dled_files = os.listdir('downloads') - - for count, fn in enumerate(dled_files, start=1): - print("Uploading {}".format(fn)) - abs_path = os.path.join(os.curdir, 'downloads', fn) - filesize = os.path.getsize(abs_path) - filename = os.path.basename(abs_path) - filemime = mimetypes.guess_type(abs_path)[0] - - asset = client.create_asset( - parent_asset_id=new_parent_id, - name=filename, - type="file", - filetype=filemime, - filesize=filesize - ) - - with open(abs_path, "rb") as ul_file: - client.upload(asset, ul_file) - - print("Done uploading file {} of {}".format((count), len(dled_files))) - - print("Sleeping for 5 seconds to allow uploads to finish...") - time.sleep(5) - - print("Continuing...") - - return new_parent_id - -# Flatten asset children and pull out important info for comparison -def flatten_asset_children(asset_children): - flat_dict = dict() - - for asset in asset_children: - flat_dict[asset['name']] = asset['filesize'] - - return flat_dict - - -def check_upload_completion(client, download_folder_id, upload_folder_id): - # Do a comparison against filenames and filesizes here to make sure they match - - print("Beginning upload comparison check") - - # Get asset children for download folder - dl_asset_children = client.get_asset_children( - download_folder_id, - page=1, - page_size=40, - include="children" - ) - - print("Got asset children for original download folder") - - # Get asset children for upload folder - ul_asset_children = client.get_asset_children( - upload_folder_id, - page=1, - page_size=40, - include="children" - ) - - print("Got asset children for uploaded folder") - - dl_items = flatten_asset_children(dl_asset_children) - ul_items = flatten_asset_children(ul_asset_children) - - print("Running comparison...") - - if sys.version_info.major >= 3: - import operator - comparison = operator.eq(dl_items, ul_items) - - if comparison == False: - print("File mismatch between upload and download") - sys.exit(1) - - else: - # Use different comparsion function in < Py3 - comparison = cmp(dl_items, ul_items) - if comparison != 0: - print("File mismatch between upload and download") - sys.exit(1) - - print("Integration test passed!!!") - - return True - - -def clean_up(client, asset_to_delete): - print("Removing files from test...") - - try: - client._api_call('delete', '/assets/{}'.format(asset_to_delete)) - print("Managed to cleanup!") - except Exception as e: - print(e) - - return True - -def run_test(): - print("Beginning Integration test...") - - client = init_client() - test_download(client) - upload_folder_id = test_upload(client) - check_upload_completion(client, download_asset_id, upload_folder_id) - clean_up(client, upload_folder_id) - - print("Test complete, exiting...") - -if __name__ == "__main__": - run_test() diff --git a/tests/test_integration.py b/tests/test_integration.py index 05ff99d4..95736345 100644 --- a/tests/test_integration.py +++ b/tests/test_integration.py @@ -18,7 +18,7 @@ # Test download functionality def test_download(setup_client): client = setup_client - + print("Testing download function...") with pytest.raises(FileExistsError): os.mkdir('downloads') @@ -37,6 +37,33 @@ def test_download(setup_client): return True +# Test uploading a single .txt file +def test_upload_single_file(setup_client, tmpdir): + client = setup_client + testdir = tmpdir.mkdir('single_file_upload') + file = testdir.join('file.txt') + file.write('file body') + print(file) + + # python-frameio-client only supports uploads in asset form (see library for details) + upload_asset = client.create_asset( + parent_asset_id=upload_folder_id, + name="file.txt", + type="file", + filetype="text/plain", + filesize="10" + ) + + # Expect FileNotFound exception becaucse it's not a real file + with pytest.raises(FileNotFoundError): + with open('file.txt', 'rb') as upload_file: + client.upload(upload_asset, upload_file) + + assert file.read() == 'file body' + assert upload_asset['name'] == 'file.txt' + assert upload_asset['project_id'] == project_id + assert upload_asset['parent_id'] == upload_folder_id + # Test upload functionality def test_upload(setup_client): client = setup_client From 5f0a4d148b1c67512e06413970b62d6643c56118 Mon Sep 17 00:00:00 2001 From: kylenstone Date: Sun, 17 May 2020 23:49:01 -0400 Subject: [PATCH 6/6] try-catch on make directory --- tests/test_integration.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/test_integration.py b/tests/test_integration.py index 95736345..3f1f054e 100644 --- a/tests/test_integration.py +++ b/tests/test_integration.py @@ -20,8 +20,10 @@ def test_download(setup_client): client = setup_client print("Testing download function...") - with pytest.raises(FileExistsError): + try: os.mkdir('downloads') + except FileExistsError: + print("Directory already exists, moving on...") asset_list = client.get_asset_children( download_asset_id, page=1,