Skip to content

Commit 1188a93

Browse files
Remove unneeded API calls in folder_file_ops
Added construct_path and construct_empty_path to base- provider for individual providers to make paths for folder-file-ops without needing to make API calls
1 parent cc68aca commit 1188a93

File tree

26 files changed

+377
-74
lines changed

26 files changed

+377
-74
lines changed

tests/core/test_provider.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,15 @@ async def test_revalidate_path_is_child(self, provider1):
104104
assert str(path) == str(new_path.parent)
105105
assert new_path.name == 'text_file.txt'
106106

107+
@pytest.mark.asyncio
108+
async def test_construct_empty_path(self, provider1):
109+
path = await provider1.validate_path('/folder/')
110+
data = utils.MockFileMetadata()
111+
empty_path = await provider1.construct_empty_path(path, data)
112+
113+
# Make sure its actually not a real path
114+
assert empty_path.identifier is None
115+
107116

108117
class TestHandleNameConflict:
109118

tests/providers/bitbucket/test_provider.py

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ async def test_validate_v1_path_root(self, provider):
6161

6262
assert wb_path_v1 == wb_path_v0
6363
assert wb_path_v1.branch_name == default_branch_body['name']
64-
assert wb_path_v1.commit_sha == None
64+
assert wb_path_v1.commit_sha is None
6565

6666
@pytest.mark.asyncio
6767
@pytest.mark.aiohttpretty
@@ -77,8 +77,8 @@ async def test_validate_v1_path(self, provider, path, kind):
7777
default_branch_url = provider._build_v1_repo_url('main-branch')
7878
aiohttpretty.register_json_uri('GET', default_branch_url, body=default_branch_body)
7979

80-
dir_listing_body = test_fixtures['root_dir_listing']
81-
dir_listing_url = provider._build_v1_repo_url('src', default_branch) + '/'
80+
dir_listing_body = test_fixtures['root_dir_listing']
81+
dir_listing_url = provider._build_v1_repo_url('src', default_branch) + '/'
8282
aiohttpretty.register_json_uri('GET', dir_listing_url, body=dir_listing_body)
8383

8484
try:
@@ -107,9 +107,9 @@ async def test_validate_v1_path(self, provider, path, kind):
107107
async def test_validate_v1_path_commit_sha(self, provider, arg_name, arg_val, attr_name):
108108
test_fixtures = fixtures.validate_path
109109

110-
dir_listing_body = test_fixtures['root_dir_listing']
110+
dir_listing_body = test_fixtures['root_dir_listing']
111111
base_commit = dir_listing_body['node']
112-
dir_listing_url = provider._build_v1_repo_url('src', arg_val) + '/'
112+
dir_listing_url = provider._build_v1_repo_url('src', arg_val) + '/'
113113
aiohttpretty.register_json_uri('GET', dir_listing_url, body=dir_listing_body)
114114

115115
path = '/foo-file.txt'
@@ -145,9 +145,9 @@ async def test_validate_v1_path_commit_sha(self, provider, arg_name, arg_val, at
145145
async def test_validate_v1_path_subfolder(self, provider):
146146
test_fixtures = fixtures.validate_path
147147

148-
dir_listing_body = test_fixtures['subfolder_dir_listing']
148+
dir_listing_body = test_fixtures['subfolder_dir_listing']
149149
base_commit = dir_listing_body['node']
150-
dir_listing_url = provider._build_v1_repo_url('src', 'main-branch', 'subfolder') + '/'
150+
dir_listing_url = provider._build_v1_repo_url('src', 'main-branch', 'subfolder') + '/'
151151
aiohttpretty.register_json_uri('GET', dir_listing_url, body=dir_listing_body)
152152

153153
path = '/subfolder/.gitkeep'
@@ -186,8 +186,8 @@ async def test_get_metadata_for_file(self, provider):
186186
path = BitbucketPath('/foo-file.txt', _ids=[(base_ref, 'develop'), (base_ref, 'develop')])
187187

188188
test_fixtures = fixtures.validate_path
189-
dir_listing_body = test_fixtures['root_dir_listing']
190-
dir_listing_url = provider._build_v1_repo_url('src', base_ref) + '/'
189+
dir_listing_body = test_fixtures['root_dir_listing']
190+
dir_listing_url = provider._build_v1_repo_url('src', base_ref) + '/'
191191
aiohttpretty.register_json_uri('GET', dir_listing_url, body=dir_listing_body)
192192

193193
result = await provider.metadata(path)
@@ -211,8 +211,8 @@ async def test_get_metadata_for_folder(self, provider):
211211
path = BitbucketPath('/', _ids=[(None, 'develop')], folder=True)
212212

213213
test_fixtures = fixtures.validate_path
214-
dir_listing_body = test_fixtures['root_dir_listing']
215-
dir_listing_url = provider._build_v1_repo_url('src', 'develop') + '/'
214+
dir_listing_body = test_fixtures['root_dir_listing']
215+
dir_listing_url = provider._build_v1_repo_url('src', 'develop') + '/'
216216
aiohttpretty.register_json_uri('GET', dir_listing_url, body=dir_listing_body)
217217

218218
result = await provider.metadata(path)
@@ -229,8 +229,8 @@ async def test_get_metadata_for_file(self, provider):
229229
path = BitbucketPath('/foo-file.txt', _ids=[(base_ref, 'develop'), (base_ref, 'develop')])
230230

231231
test_fixtures = fixtures.validate_path
232-
dir_listing_body = test_fixtures['root_dir_listing']
233-
dir_listing_url = provider._build_v1_repo_url('src', base_ref) + '/'
232+
dir_listing_body = test_fixtures['root_dir_listing']
233+
dir_listing_url = provider._build_v1_repo_url('src', base_ref) + '/'
234234
aiohttpretty.register_json_uri('GET', dir_listing_url, body=dir_listing_body)
235235

236236
download_url = provider._build_v1_repo_url('raw', path.commit_sha, *path.path_tuple())
@@ -268,17 +268,38 @@ async def test_copy_to(self, provider):
268268
assert e.value.code == 501
269269

270270
def test_can_intra_move(self, provider):
271-
assert provider.can_intra_move(provider) == False
271+
assert provider.can_intra_move(provider) is False
272272

273273
def test_can_intra_copy(self, provider):
274-
assert provider.can_intra_copy(provider) == False
274+
assert provider.can_intra_copy(provider) is False
275275

276276

277277
# leftover bits
278278
class TestMisc:
279279

280+
@pytest.mark.asyncio
281+
async def test_construct_path(self, provider):
282+
name = 'aaa-01-2.txt'
283+
subdir = 'plaster'
284+
full_path = '/{}/{}'.format(subdir, name)
285+
branch = 'master'
286+
commit_sha = '123abc456def'
287+
288+
path = BitbucketPath(full_path, _ids=[
289+
(commit_sha, branch), (commit_sha, branch), (commit_sha, branch)
290+
])
291+
292+
metadata = BitbucketFileMetadata(fixtures.file_metadata, path, owner=fixtures.owner, repo=fixtures.repo)
293+
child_path = await provider.construct_path(path.parent, metadata)
294+
rev_metadata = await provider.revalidate_path(path.parent,
295+
metadata.name, folder=metadata.is_folder)
296+
297+
assert child_path.full_path == path.full_path
298+
assert child_path == path
299+
assert child_path == rev_metadata
300+
280301
def test_can_duplicate_name(self, provider):
281-
assert provider.can_duplicate_names() == False
302+
assert provider.can_duplicate_names() is False
282303

283304
def test_path_from_metadata(self, provider):
284305
name = 'aaa-01-2.txt'
@@ -292,7 +313,7 @@ def test_path_from_metadata(self, provider):
292313
])
293314

294315
metadata = BitbucketFileMetadata(fixtures.file_metadata, path, owner=fixtures.owner, repo=fixtures.repo)
295-
child_path = provider.path_from_metadata(path.parent, metadata)
316+
child_path = provider.path_from_metadata(path.parent, metadata)
296317

297318
assert child_path.full_path == path.full_path
298319
assert child_path == path

tests/providers/box/test_provider.py

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -834,23 +834,31 @@ class TestOperations:
834834

835835
@pytest.mark.asyncio
836836
@pytest.mark.aiohttpretty
837-
async def test_can_duplicate_names(self, provider):
837+
async def test_construct_path(self, provider, root_provider_fixtures):
838+
path = WaterButlerPath('/50 shades of nope/', _ids=(provider.folder, '0'))
839+
item = root_provider_fixtures['revalidate_metadata']['entries'][0]
840+
data = BoxFileMetadata(item, path)
841+
842+
url = provider.build_url('folders', path.identifier, 'items',
843+
fields='id,name,type', limit=1000)
844+
aiohttpretty.register_json_uri('GET', url, body=root_provider_fixtures['revalidate_metadata'])
845+
846+
con_metadata = await provider.construct_path(path, data)
847+
848+
path_metadata = await provider.revalidate_path(path, data.name)
849+
assert con_metadata == path_metadata
850+
851+
def test_can_duplicate_names(self, provider):
838852
assert provider.can_duplicate_names() is False
839853

840-
@pytest.mark.asyncio
841-
@pytest.mark.aiohttpretty
842-
async def test_shares_storage_root(self, provider, other_provider):
854+
def test_shares_storage_root(self, provider, other_provider):
843855
assert provider.shares_storage_root(other_provider) is False
844856
assert provider.shares_storage_root(provider) is True
845857

846-
@pytest.mark.asyncio
847-
@pytest.mark.aiohttpretty
848-
async def test_can_intra_move(self, provider, other_provider):
858+
def test_can_intra_move(self, provider, other_provider):
849859
assert provider.can_intra_move(other_provider) is False
850860
assert provider.can_intra_move(provider) is True
851861

852-
@pytest.mark.asyncio
853-
@pytest.mark.aiohttpretty
854-
async def test_can_intra_copy(self, provider, other_provider):
862+
def test_can_intra_copy(self, provider, other_provider):
855863
assert provider.can_intra_copy(other_provider) is False
856864
assert provider.can_intra_copy(provider) is True

tests/providers/cloudfiles/test_provider.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import json
44
import time
55
import hashlib
6-
import functools
76
from unittest import mock
87

98
import furl
@@ -17,6 +16,7 @@
1716
from waterbutler.core.path import WaterButlerPath
1817
from waterbutler.providers.cloudfiles import CloudFilesProvider
1918
from waterbutler.providers.cloudfiles import settings as cloud_settings
19+
from waterbutler.providers.cloudfiles.metadata import CloudFilesFileMetadata
2020

2121

2222
@pytest.fixture
@@ -180,7 +180,8 @@ def file_metadata():
180180
('ETAG', 'edfa12d00b779b4b37b81fe5b61b2b3f'),
181181
('CONTENT-TYPE', 'text/html; charset=UTF-8'),
182182
('X-TRANS-ID', 'txf876a4b088e3451d94442-00549b7c6aiad3'),
183-
('DATE', 'Thu, 25 Dec 2014 02:54:34 GMT')
183+
('DATE', 'Thu, 25 Dec 2014 02:54:34 GMT'),
184+
('NAME', 'file.txt')
184185
])
185186

186187

@@ -664,6 +665,17 @@ async def test_v1_validate_path(self, connected_provider):
664665

665666
class TestOperations:
666667

668+
@pytest.mark.asyncio
669+
async def test_construct_path(self, provider, file_metadata):
670+
# Construct replaces revalidate_path in some instances, so it should always
671+
# be tested against it, even if calls revalidate_path
672+
path = WaterButlerPath('/file.txt')
673+
674+
data = CloudFilesFileMetadata(file_metadata)
675+
rev_path = await provider.revalidate_path(path.parent, data.name)
676+
con_path = await provider.construct_path(path.parent, data)
677+
assert rev_path == con_path
678+
667679
@pytest.mark.asyncio
668680
@pytest.mark.aiohttpretty
669681
async def test_ensure_connection(self, provider, auth_json, mock_temp_key):

tests/providers/dataverse/test_provider.py

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,6 @@ async def test_revalidate_path(self, provider, native_dataset_metadata):
110110
status=200,
111111
body=native_dataset_metadata)
112112

113-
114113
base = await provider.validate_v1_path('/')
115114

116115
wb_path = await provider.revalidate_path(base, '/thefile.txt')
@@ -301,7 +300,7 @@ async def test_upload_checksum_mismatch(self, provider, file_stream,
301300
])
302301

303302
path = await provider.validate_path(path)
304-
with pytest.raises(exceptions.UploadChecksumMismatchError) as exc:
303+
with pytest.raises(exceptions.UploadChecksumMismatchError):
305304
await provider.upload(file_stream, path)
306305

307306
assert aiohttpretty.has_call(method='POST', uri=url)
@@ -511,12 +510,39 @@ async def test_metadata_never_published_raises_errors(self, provider):
511510

512511
path = await provider.validate_path('/')
513512
with pytest.raises(exceptions.MetadataError) as e:
514-
result = await provider.metadata(path)
513+
await provider.metadata(path)
515514

516515
assert e.value.code == 400
517516

518517

519518
class TestUtils:
520519

520+
@pytest.mark.asyncio
521+
@pytest.mark.aiohttpretty
522+
async def test_construct_path(self, provider, native_dataset_metadata):
523+
entry = native_dataset_metadata['data']['files'][0]['datafile']
524+
metadata = DataverseFileMetadata(entry, 'latest')
525+
526+
draft_url = provider.build_url(dvs.JSON_BASE_URL.format(provider._id, 'latest'),
527+
key=provider.token)
528+
published_url = provider.build_url(dvs.JSON_BASE_URL.format(provider._id,
529+
'latest-published'),
530+
key=provider.token)
531+
532+
aiohttpretty.register_json_uri('GET',
533+
draft_url,
534+
status=200,
535+
body=native_dataset_metadata)
536+
aiohttpretty.register_json_uri('GET',
537+
published_url,
538+
status=200,
539+
body=native_dataset_metadata)
540+
541+
base = await provider.validate_v1_path('/')
542+
543+
rev_path = await provider.revalidate_path(base, metadata.name)
544+
con_path = await provider.construct_path(base, metadata)
545+
assert rev_path == con_path
546+
521547
def test_utils(self, provider):
522548
assert not provider.can_duplicate_names()

tests/providers/dropbox/test_provider.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -722,6 +722,18 @@ async def test_intra_move_casing_change(self, provider):
722722

723723
class TestOperations:
724724

725+
@pytest.mark.asyncio
726+
async def test_construct_path(self, provider, root_provider_fixtures):
727+
# Construct replaces revalidate_path in some instances, so it should always
728+
# be tested against it, even if calls revalidate_path
729+
path = WaterButlerPath('/file.txt')
730+
731+
data = DropboxFileMetadata(root_provider_fixtures['file_metadata'], folder=False)
732+
733+
rev_path = await provider.revalidate_path(path.parent, data.name)
734+
con_path = await provider.construct_path(path.parent, data)
735+
assert rev_path == con_path
736+
725737
def test_can_intra_copy(self, provider):
726738
assert provider.can_intra_copy(provider)
727739

@@ -732,7 +744,7 @@ def test_can_intra_move(self, provider):
732744
assert provider.can_intra_move(provider)
733745

734746
def test_cannot_intra_move_other(self, provider, other_provider):
735-
assert provider.can_intra_move(other_provider) == False
747+
assert provider.can_intra_move(other_provider) is False
736748

737749
def test_conflict_error_handler_not_found(self, provider, error_fixtures):
738750
error_path = '/Photos/folder/file'

tests/providers/figshare/test_provider.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1387,6 +1387,36 @@ async def test_article_revalidate_path_file(self, article_provider, crud_fixture
13871387

13881388
class TestMisc:
13891389

1390+
@pytest.mark.asyncio
1391+
@pytest.mark.aiohttpretty
1392+
async def test_construct_path(self, project_provider, root_provider_fixtures):
1393+
file_article_id = str(root_provider_fixtures['list_project_articles'][0]['id'])
1394+
folder_article_id = str(root_provider_fixtures['list_project_articles'][1]['id'])
1395+
1396+
root_parts = project_provider.root_path_parts
1397+
list_articles_url = project_provider.build_url(False, *root_parts, 'articles')
1398+
file_article_url = project_provider.build_url(False, *root_parts, 'articles',
1399+
file_article_id)
1400+
folder_article_url = project_provider.build_url(False, *root_parts, 'articles',
1401+
folder_article_id)
1402+
1403+
aiohttpretty.register_json_uri('GET', list_articles_url,
1404+
body=root_provider_fixtures['list_project_articles'],
1405+
params={'page': '1', 'page_size': str(MAX_PAGE_SIZE)})
1406+
aiohttpretty.register_json_uri('GET', list_articles_url, body=[],
1407+
params={'page': '2', 'page_size': str(MAX_PAGE_SIZE)})
1408+
aiohttpretty.register_json_uri('GET', file_article_url,
1409+
body=root_provider_fixtures['file_article_metadata'])
1410+
aiohttpretty.register_json_uri('GET', folder_article_url,
1411+
body=root_provider_fixtures['folder_article_metadata'])
1412+
1413+
path = FigsharePath('/test/', _ids=('0', folder_article_id), folder=True, parent_is_folder=False)
1414+
base_meta = root_provider_fixtures['file_article_metadata']
1415+
data = metadata.FigshareFileMetadata(base_meta, base_meta['files'][0])
1416+
rev_path = await project_provider.revalidate_path(path, data.name, folder=False)
1417+
con_path = await project_provider.construct_path(path, data)
1418+
assert con_path == rev_path
1419+
13901420
def test_path_from_metadata(self, project_provider, root_provider_fixtures):
13911421
file_article_metadata = root_provider_fixtures['file_article_metadata']
13921422
fig_metadata = metadata.FigshareFileMetadata(file_article_metadata)

tests/providers/filesystem/test_provider.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,17 @@ def credentials():
2424
return {}
2525

2626

27+
@pytest.fixture
28+
def file_metadata():
29+
return {
30+
'path': '/code/website/osfstoragecache/77094244-aa24-48da-9437-d8ce6f7a94e9',
31+
'modified_utc': '2017-09-20T15:16:02.601916+00:00',
32+
'mime_type': None,
33+
'size': 35981,
34+
'modified': 'Wed, 20 Sep 2017 15:16:02 +0000'
35+
}
36+
37+
2738
@pytest.fixture
2839
def settings(tmpdir):
2940
return {'folder': str(tmpdir)}
@@ -271,6 +282,14 @@ async def test_intra_move_file(self, provider):
271282

272283
class TestOperations:
273284

285+
@pytest.mark.asyncio
286+
async def test_construct_path(self, provider, file_metadata):
287+
data = FileSystemFileMetadata(file_metadata, '/')
288+
path = await provider.validate_path('/folder/')
289+
con_path = await provider.construct_path(path, data)
290+
rev_path = await provider.revalidate_path(path, data.name)
291+
assert con_path == rev_path
292+
274293
def test_can_duplicate_names(self, provider):
275294
assert provider.can_duplicate_names() is False
276295

0 commit comments

Comments
 (0)