def test_create_Link_to_entity_with_the_same_parent(): parent = "syn123" file = File("new file", parent=parent, id="syn456") file_bundle = { 'accessControlList': '/repo/v1/entity/syn456/acl', 'entityType': 'org.sagebionetworks.repo.model.FileEntity', 'annotations': '/repo/v1/entity/syn456/annotations', 'uri': '/repo/v1/entity/syn456', 'createdOn': '2018-08-27T20:48:43.562Z', 'parentId': 'syn123', 'versionNumber': 1, 'dataFileHandleId': '3594', 'modifiedOn': '2018-08-27T20:48:44.938Z', 'versionLabel': '1', 'createdBy': '1', 'versions': '/repo/v1/entity/syn456/version', 'name': 'new file', 'concreteType': 'org.sagebionetworks.repo.model.FileEntity', 'etag': '62fd1a76-ed9c-425a-b4a8-1c4e6aad7fc6', 'modifiedBy': '1', 'id': 'syn456', 'versionUrl': '/repo/v1/entity/syn456/version/1' } link = Link(targetId=file, parent=parent) syn = Synapse(skip_checks=True) with patch.object(syn, "_getEntity", return_value=file_bundle): pytest.raises(ValueError, syn.store, link)
def _copyLink(syn, entity, destinationId, updateExisting=False): """ Copies Link entities :param entity: A synapse ID of a Link entity :param destinationId: Synapse ID of a folder/project that the file wants to be copied to :param updateExisting: Can choose to update files that have the same name Default to False """ ent = syn.get(entity) # CHECK: If Link is in the same parent directory (throw an error) if not updateExisting: existingEntity = syn.findEntityId(ent.name, parent=destinationId) if existingEntity is not None: raise ValueError( 'An entity named "%s" already exists in this location. Link could not be copied' % ent.name) newLink = Link(ent.linksTo['targetId'], parent=destinationId, targetVersion=ent.linksTo.get('targetVersionNumber')) try: newLink = syn.store(newLink) return newLink.id except SynapseHTTPError as e: if e.response.status_code == 404: print("WARNING: The target of this link %s no longer exists" % ent.id) return None else: raise e
def test_syncFromSynapse_Links(): """This function tests recursive download of links as defined in syncFromSynapse most of the functionality of this function are already tested in the tests/integration/test_command_line_client::test_command_get_recursive_and_query which means that the only test if for path=None """ # Create a Project project_entity = syn.store(synapseclient.Project(name=str(uuid.uuid4()))) schedule_for_cleanup(project_entity.id) # Create a Folder in Project folder_entity = syn.store( Folder(name=str(uuid.uuid4()), parent=project_entity)) # Create a Folder hierarchy in folder_entity inner_folder_entity = syn.store( Folder(name=str(uuid.uuid4()), parent=folder_entity)) second_folder_entity = syn.store( Folder(name=str(uuid.uuid4()), parent=project_entity)) # Create and upload two files in Folder uploaded_paths = [] for i in range(2): f = utils.make_bogus_data_file() uploaded_paths.append(f) schedule_for_cleanup(f) file_entity = syn.store(File(f, parent=project_entity)) # Create links to inner folder syn.store(Link(file_entity.id, parent=folder_entity)) # Add a file in the project level as well f = utils.make_bogus_data_file() uploaded_paths.append(f) schedule_for_cleanup(f) file_entity = syn.store(File(f, parent=second_folder_entity)) # Create link to inner folder syn.store(Link(file_entity.id, parent=inner_folder_entity)) # Test recursive get output = synapseutils.syncFromSynapse(syn, folder_entity, followLink=True) assert_equals(len(output), len(uploaded_paths)) for f in output: assert_in(f.path, uploaded_paths)
def test_copy(): """Tests the copy function""" # Create a Project project_entity = syn.store(Project(name=str(uuid.uuid4()))) schedule_for_cleanup(project_entity.id) # Create two Folders in Project folder_entity = syn.store( Folder(name=str(uuid.uuid4()), parent=project_entity)) second_folder = syn.store( Folder(name=str(uuid.uuid4()), parent=project_entity)) third_folder = syn.store( Folder(name=str(uuid.uuid4()), parent=project_entity)) schedule_for_cleanup(folder_entity.id) schedule_for_cleanup(second_folder.id) schedule_for_cleanup(third_folder.id) # Annotations and provenance repo_url = 'https://github.com/Sage-Bionetworks/synapsePythonClient' annos = {'test': ['hello_world']} prov = Activity(name="test", used=repo_url) # Create, upload, and set annotations/provenance on a file in Folder filename = utils.make_bogus_data_file() schedule_for_cleanup(filename) file_entity = syn.store(File(filename, parent=folder_entity)) externalURL_entity = syn.store( File(repo_url, name='rand', parent=folder_entity, synapseStore=False)) syn.setAnnotations(file_entity, annos) syn.setAnnotations(externalURL_entity, annos) syn.setProvenance(externalURL_entity.id, prov) schedule_for_cleanup(file_entity.id) schedule_for_cleanup(externalURL_entity.id) # ------------------------------------ # TEST COPY FILE # ------------------------------------ output = synapseutils.copy(syn, file_entity.id, destinationId=project_entity.id) output_URL = synapseutils.copy(syn, externalURL_entity.id, destinationId=project_entity.id, skipCopyAnnotations=True) # Verify that our copied files are identical copied_ent = syn.get(output[file_entity.id]) copied_URL_ent = syn.get(output_URL[externalURL_entity.id], downloadFile=False) copied_ent_annot = syn.getAnnotations(copied_ent) copied_url_annot = syn.getAnnotations(copied_URL_ent) copied_prov = syn.getProvenance(copied_ent) copied_url_prov = syn.getProvenance(copied_URL_ent) schedule_for_cleanup(copied_ent.id) schedule_for_cleanup(copied_URL_ent.id) # TEST: set_Provenance = Traceback assert_equals(copied_prov['used'][0]['reference']['targetId'], file_entity.id) assert_equals(copied_url_prov['used'][0]['reference']['targetId'], externalURL_entity.id) # TEST: Make sure copied files are the same assert_equals(copied_ent_annot, annos) assert_equals(copied_ent.dataFileHandleId, file_entity.dataFileHandleId) # TEST: Make sure copied URLs are the same assert_equals(copied_url_annot, {}) assert_equals(copied_URL_ent.externalURL, repo_url) assert_equals(copied_URL_ent.name, 'rand') assert_equals(copied_URL_ent.dataFileHandleId, externalURL_entity.dataFileHandleId) # TEST: Throw error if file is copied to a folder/project that has a file with the same filename assert_raises(ValueError, synapseutils.copy, syn, project_entity.id, destinationId=project_entity.id) assert_raises(ValueError, synapseutils.copy, syn, file_entity.id, destinationId=project_entity.id) assert_raises(ValueError, synapseutils.copy, syn, file_entity.id, destinationId=third_folder.id, setProvenance="gib") assert_raises(ValueError, synapseutils.copy, syn, file_entity.id, destinationId=file_entity.id) # Test: setProvenance = None output = synapseutils.copy(syn, file_entity.id, destinationId=second_folder.id, setProvenance=None) assert_raises(SynapseHTTPError, syn.getProvenance, output[file_entity.id]) schedule_for_cleanup(output[file_entity.id]) # Test: setProvenance = Existing output_URL = synapseutils.copy(syn, externalURL_entity.id, destinationId=second_folder.id, setProvenance="existing") output_prov = syn.getProvenance(output_URL[externalURL_entity.id]) schedule_for_cleanup(output_URL[externalURL_entity.id]) assert_equals(output_prov['name'], prov['name']) assert_equals(output_prov['used'], prov['used']) # ------------------------------------ # TEST COPY LINKS # ------------------------------------ second_file = utils.make_bogus_data_file() # schedule_for_cleanup(filename) second_file_entity = syn.store(File(second_file, parent=project_entity)) link_entity = Link(second_file_entity.id, parent=folder_entity.id) link_entity = syn.store(link_entity) copied_link = synapseutils.copy(syn, link_entity.id, destinationId=second_folder.id) old = syn.get(link_entity.id, followLink=False) new = syn.get(copied_link[link_entity.id], followLink=False) assert_equals(old.linksTo['targetId'], new.linksTo['targetId']) schedule_for_cleanup(second_file_entity.id) schedule_for_cleanup(link_entity.id) schedule_for_cleanup(copied_link[link_entity.id]) time.sleep(3) assert_raises(ValueError, synapseutils.copy, syn, link_entity.id, destinationId=second_folder.id) # ------------------------------------ # TEST COPY TABLE # ------------------------------------ second_project = syn.store(Project(name=str(uuid.uuid4()))) schedule_for_cleanup(second_project.id) cols = [ Column(name='n', columnType='DOUBLE', maximumSize=50), Column(name='c', columnType='STRING', maximumSize=50), Column(name='i', columnType='INTEGER') ] data = [[2.1, 'foo', 10], [2.2, 'bar', 20], [2.3, 'baz', 30]] schema = syn.store( Schema(name='Testing', columns=cols, parent=project_entity.id)) syn.store(RowSet(schema=schema, rows=[Row(r) for r in data])) table_map = synapseutils.copy(syn, schema.id, destinationId=second_project.id) copied_table = syn.tableQuery('select * from %s' % table_map[schema.id]) rows = copied_table.asRowSet()['rows'] # TEST: Check if all values are the same for i, row in enumerate(rows): assert_equals(row['values'], data[i]) assert_raises(ValueError, synapseutils.copy, syn, schema.id, destinationId=second_project.id) schedule_for_cleanup(schema.id) schedule_for_cleanup(table_map[schema.id]) # ------------------------------------ # TEST COPY FOLDER # ------------------------------------ mapping = synapseutils.copy(syn, folder_entity.id, destinationId=second_project.id) for i in mapping: old = syn.get(i, downloadFile=False) new = syn.get(mapping[i], downloadFile=False) assert_equals(old.name, new.name) assert_equals(old.annotations, new.annotations) assert_equals(old.concreteType, new.concreteType) assert_raises(ValueError, synapseutils.copy, syn, folder_entity.id, destinationId=second_project.id) # TEST: Throw error if excludeTypes isn't in file, link and table or isn't a list assert_raises(ValueError, synapseutils.copy, syn, second_folder.id, destinationId=second_project.id, excludeTypes=["foo"]) assert_raises(ValueError, synapseutils.copy, syn, second_folder.id, destinationId=second_project.id, excludeTypes="file") # TEST: excludeType = ["file"], only the folder is created second = synapseutils.copy(syn, second_folder.id, destinationId=second_project.id, excludeTypes=["file", "table", "link"]) copied_folder = syn.get(second[second_folder.id]) assert_equals(copied_folder.name, second_folder.name) assert_equals(len(second), 1) # TEST: Make sure error is thrown if foldername already exists assert_raises(ValueError, synapseutils.copy, syn, second_folder.id, destinationId=second_project.id) # ------------------------------------ # TEST COPY PROJECT # ------------------------------------ third_project = syn.store(Project(name=str(uuid.uuid4()))) schedule_for_cleanup(third_project.id) mapping = synapseutils.copy(syn, project_entity.id, destinationId=third_project.id) for i in mapping: old = syn.get(i, downloadFile=False) new = syn.get(mapping[i], downloadFile=False) if not isinstance(old, Project): assert_equals(old.name, new.name) assert_equals(old.annotations, new.annotations) assert_equals(old.concreteType, new.concreteType) # TEST: Can't copy project to a folder assert_raises(ValueError, synapseutils.copy, syn, project_entity.id, destinationId=second_folder.id)
def test_Entity(): # Update the project project_name = str(uuid.uuid4()) project = Project(name=project_name) project = syn.store(project) schedule_for_cleanup(project) project = syn.getEntity(project) assert project.name == project_name # Create and get a Folder folder = Folder('Test Folder', parent=project, description='A place to put my junk', foo=1000) folder = syn.createEntity(folder) folder = syn.getEntity(folder) assert folder.name == 'Test Folder' assert folder.parentId == project.id assert folder.description == 'A place to put my junk' assert folder.foo[0] == 1000 # Update and get the Folder folder.pi = 3.14159265359 folder.description = 'The rejects from the other folder' folder = syn.store(folder) folder = syn.get(folder) assert folder.name == 'Test Folder' assert folder.parentId == project.id assert folder.description == 'The rejects from the other folder' assert folder.pi[0] == 3.14159265359 # Test CRUD on Files, check unicode path = utils.make_bogus_data_file() schedule_for_cleanup(path) a_file = File(path, parent=folder, description= u'Description with funny characters: Déjà vu, ประเทศไทย, 中国', contentType='text/flapdoodle', foo='An arbitrary value', bar=[33, 44, 55], bday=Datetime(2013, 3, 15), band=u"Motörhead", lunch=u"すし") a_file = syn.store(a_file) assert a_file.path == path a_file = syn.getEntity(a_file) assert a_file.description == u'Description with funny characters: Déjà vu, ประเทศไทย, 中国', u'description= %s' % a_file.description assert a_file['foo'][ 0] == 'An arbitrary value', u'foo= %s' % a_file['foo'][0] assert a_file['bar'] == [33, 44, 55] assert a_file['bday'][0] == Datetime(2013, 3, 15) assert a_file.contentType == 'text/flapdoodle', u'contentType= %s' % a_file.contentType assert a_file['band'][0] == u"Motörhead", u'band= %s' % a_file['band'][0] assert a_file['lunch'][0] == u"すし", u'lunch= %s' % a_file['lunch'][0] a_file = syn.downloadEntity(a_file) assert filecmp.cmp(path, a_file.path) # Update the File a_file.path = path a_file['foo'] = 'Another arbitrary chunk of text data' a_file['new_key'] = 'A newly created value' a_file = syn.updateEntity(a_file) assert a_file['foo'][0] == 'Another arbitrary chunk of text data' assert a_file['bar'] == [33, 44, 55] assert a_file['bday'][0] == Datetime(2013, 3, 15) assert a_file.new_key[0] == 'A newly created value' assert a_file.path == path assert a_file.versionNumber == 1, "unexpected version number: " + str( a_file.versionNumber) #Test create, store, get Links link = Link(a_file['id'], targetVersion=a_file.versionNumber, parent=project) link = syn.store(link) assert link['linksTo']['targetId'] == a_file['id'] assert link['linksTo']['targetVersionNumber'] == a_file.versionNumber assert link['linksToClassName'] == a_file['concreteType'] testLink = syn.get(link, followLink=False) assert testLink == link link = syn.getEntity(link) assert link['foo'][0] == 'Another arbitrary chunk of text data' assert link['bar'] == [33, 44, 55] assert link['bday'][0] == Datetime(2013, 3, 15) assert link.new_key[0] == 'A newly created value' assert utils.equal_paths(link.path, path) assert link.versionNumber == 1, "unexpected version number: " + str( a_file.versionNumber) # Upload a new File and verify new_path = utils.make_bogus_data_file() schedule_for_cleanup(new_path) a_file = syn.uploadFile(a_file, new_path) a_file = syn.downloadEntity(a_file) assert filecmp.cmp(new_path, a_file.path) assert a_file.versionNumber == 2 # Make sure we can still get the older version of file old_random_data = syn.get(a_file.id, version=1) assert filecmp.cmp(old_random_data.path, path) tmpdir = tempfile.mkdtemp() schedule_for_cleanup(tmpdir) ## test file name override a_file.fileNameOverride = "peaches_en_regalia.zoinks" syn.store(a_file) ## TODO We haven't defined how filename override interacts with ## TODO previously cached files so, side-step that for now by ## TODO making sure the file is not in the cache! syn.cache.remove(a_file.dataFileHandleId, delete=True) a_file_retreived = syn.get(a_file, downloadLocation=tmpdir) assert os.path.basename( a_file_retreived.path) == a_file.fileNameOverride, os.path.basename( a_file_retreived.path) ## test getting the file from the cache with downloadLocation parameter (SYNPY-330) a_file_cached = syn.get(a_file.id, downloadLocation=tmpdir) assert a_file_cached.path is not None assert os.path.basename( a_file_cached.path) == a_file.fileNameOverride, a_file_cached.path print("\n\nList of files in project:\n") syn._list(project, recursive=True)
def test_Entity(syn, project, schedule_for_cleanup): # Update the project project_name = str(uuid.uuid4()) project = Project(name=project_name) project = syn.store(project) schedule_for_cleanup(project) project = syn.get(project) assert project.name == project_name # Create and get a Folder folder = Folder('Test Folder', parent=project, description='A place to put my junk', foo=1000) folder = syn.store(folder) folder = syn.get(folder) assert folder.name == 'Test Folder' assert folder.parentId == project.id assert folder.description == 'A place to put my junk' assert folder.foo[0] == 1000 # Update and get the Folder folder.pi = 3.14159265359 folder.description = 'The rejects from the other folder' folder = syn.store(folder) folder = syn.get(folder) assert folder.name == 'Test Folder' assert folder.parentId == project.id assert folder.description == 'The rejects from the other folder' assert folder.pi[0] == 3.14159265359 # Test CRUD on Files, check unicode path = utils.make_bogus_data_file() schedule_for_cleanup(path) a_file = File(path, parent=folder, description=u'Description with funny characters: Déjà vu, ประเทศไทย, 中国', contentType='text/flapdoodle', foo='An arbitrary value', bar=[33, 44, 55], bday=Datetime(2013, 3, 15), band=u"Motörhead", lunch=u"すし") a_file = syn.store(a_file) assert a_file.path == path a_file = syn.get(a_file) assert a_file.description ==\ u'Description with funny characters: Déjà vu, ประเทศไทย, 中国', \ u'description= %s' % a_file.description assert a_file['foo'][0] == 'An arbitrary value', u'foo= %s' % a_file['foo'][0] assert a_file['bar'] == [33, 44, 55] assert a_file['bday'][0] == Datetime(2013, 3, 15) assert a_file.contentType == 'text/flapdoodle', u'contentType= %s' % a_file.contentType assert a_file['band'][0] == u"Motörhead", u'band= %s' % a_file['band'][0] assert a_file['lunch'][0] == u"すし", u'lunch= %s' % a_file['lunch'][0] a_file = syn.get(a_file) assert filecmp.cmp(path, a_file.path) b_file = File(name="blah", parent=folder, dataFileHandleId=a_file.dataFileHandleId) b_file = syn.store(b_file) assert b_file.dataFileHandleId == a_file.dataFileHandleId # Update the File a_file.path = path a_file['foo'] = 'Another arbitrary chunk of text data' a_file['new_key'] = 'A newly created value' a_file = syn.store(a_file, forceVersion=False) assert a_file['foo'][0] == 'Another arbitrary chunk of text data' assert a_file['bar'] == [33, 44, 55] assert a_file['bday'][0] == Datetime(2013, 3, 15) assert a_file.new_key[0] == 'A newly created value' assert a_file.path == path assert a_file.versionNumber == 1, "unexpected version number: " + str(a_file.versionNumber) # Test create, store, get Links # If version isn't specified, targetVersionNumber should not be set link = Link(a_file['id'], parent=project) link = syn.store(link) assert link['linksTo']['targetId'] == a_file['id'] assert link['linksTo'].get('targetVersionNumber') is None assert link['linksToClassName'] == a_file['concreteType'] link = Link(a_file['id'], targetVersion=a_file.versionNumber, parent=project) link = syn.store(link) assert link['linksTo']['targetId'] == a_file['id'] assert link['linksTo']['targetVersionNumber'] == a_file.versionNumber assert link['linksToClassName'] == a_file['concreteType'] testLink = syn.get(link) assert testLink == link link = syn.get(link, followLink=True) assert link['foo'][0] == 'Another arbitrary chunk of text data' assert link['bar'] == [33, 44, 55] assert link['bday'][0] == Datetime(2013, 3, 15) assert link.new_key[0] == 'A newly created value' assert utils.equal_paths(link.path, path) assert link.versionNumber == 1, "unexpected version number: " + str(a_file.versionNumber) newfolder = Folder('Testing Folder', parent=project) newfolder = syn.store(newfolder) link = Link(newfolder, parent=folder.id) link = syn.store(link) assert link['linksTo']['targetId'] == newfolder.id assert link['linksToClassName'] == newfolder['concreteType'] assert link['linksTo'].get('targetVersionNumber') is None # Upload a new File and verify new_path = utils.make_bogus_data_file() schedule_for_cleanup(new_path) a_file.path = new_path a_file = syn.store(a_file) a_file = syn.get(a_file) assert filecmp.cmp(new_path, a_file.path) assert a_file.versionNumber == 2 # Make sure we can still get the older version of file old_random_data = syn.get(a_file.id, version=1) assert filecmp.cmp(old_random_data.path, path) tmpdir = tempfile.mkdtemp() schedule_for_cleanup(tmpdir) # test getting the file from the cache with downloadLocation parameter (SYNPY-330) a_file_cached = syn.get(a_file.id, downloadLocation=tmpdir) assert a_file_cached.path is not None assert os.path.basename(a_file_cached.path) == os.path.basename(a_file.path)
def test_copy(): """Tests the copy function""" # Create a Project project_entity = syn.store(Project(name=str(uuid.uuid4()))) schedule_for_cleanup(project_entity.id) acl = syn.setPermissions( project_entity, other_user['principalId'], accessType=['READ', 'CREATE', 'UPDATE', 'DOWNLOAD']) # Create two Folders in Project folder_entity = syn.store( Folder(name=str(uuid.uuid4()), parent=project_entity)) second_folder = syn.store( Folder(name=str(uuid.uuid4()), parent=project_entity)) third_folder = syn.store( Folder(name=str(uuid.uuid4()), parent=project_entity)) schedule_for_cleanup(folder_entity.id) schedule_for_cleanup(second_folder.id) schedule_for_cleanup(third_folder.id) # Annotations and provenance repo_url = 'https://github.com/Sage-Bionetworks/synapsePythonClient' annots = {'test': ['hello_world']} prov = Activity(name="test", used=repo_url) # Create, upload, and set annotations/provenance on a file in Folder filename = utils.make_bogus_data_file() schedule_for_cleanup(filename) file_entity = syn.store(File(filename, parent=folder_entity)) externalURL_entity = syn.store( File(repo_url, name='rand', parent=folder_entity, synapseStore=False)) syn.setAnnotations(file_entity, annots) syn.setAnnotations(externalURL_entity, annots) syn.setProvenance(externalURL_entity.id, prov) schedule_for_cleanup(file_entity.id) schedule_for_cleanup(externalURL_entity.id) # ------------------------------------ # TEST COPY FILE # ------------------------------------ output = synapseutils.copy(syn, file_entity.id, destinationId=project_entity.id) output_URL = synapseutils.copy(syn, externalURL_entity.id, destinationId=project_entity.id, skipCopyAnnotations=True) #Verify that our copied files are identical copied_ent = syn.get(output[file_entity.id]) copied_URL_ent = syn.get(output_URL[externalURL_entity.id], downloadFile=False) copied_ent_annot = syn.getAnnotations(copied_ent) copied_url_annot = syn.getAnnotations(copied_URL_ent) copied_prov = syn.getProvenance(copied_ent) copied_url_prov = syn.getProvenance(copied_URL_ent) schedule_for_cleanup(copied_ent.id) schedule_for_cleanup(copied_URL_ent.id) # TEST: set_Provenance = Traceback print("Test: setProvenance = Traceback") assert copied_prov['used'][0]['reference']['targetId'] == file_entity.id assert copied_url_prov['used'][0]['reference'][ 'targetId'] == externalURL_entity.id # TEST: Make sure copied files are the same assert copied_ent_annot == annots assert copied_ent.dataFileHandleId == file_entity.dataFileHandleId # TEST: Make sure copied URLs are the same assert copied_url_annot == {} assert copied_URL_ent.externalURL == repo_url assert copied_URL_ent.name == 'rand' assert copied_URL_ent.dataFileHandleId == externalURL_entity.dataFileHandleId # TEST: Throw error if file is copied to a folder/project that has a file with the same filename assert_raises(ValueError, synapseutils.copy, syn, project_entity.id, destinationId=project_entity.id) assert_raises(ValueError, synapseutils.copy, syn, file_entity.id, destinationId=project_entity.id) assert_raises(ValueError, synapseutils.copy, syn, file_entity.id, destinationId=third_folder.id, setProvenance="gib") assert_raises(ValueError, synapseutils.copy, syn, file_entity.id, destinationId=file_entity.id) print("Test: setProvenance = None") output = synapseutils.copy(syn, file_entity.id, destinationId=second_folder.id, setProvenance=None) assert_raises(SynapseHTTPError, syn.getProvenance, output[file_entity.id]) schedule_for_cleanup(output[file_entity.id]) print("Test: setProvenance = Existing") output_URL = synapseutils.copy(syn, externalURL_entity.id, destinationId=second_folder.id, setProvenance="existing") output_prov = syn.getProvenance(output_URL[externalURL_entity.id]) schedule_for_cleanup(output_URL[externalURL_entity.id]) assert output_prov['name'] == prov['name'] assert output_prov['used'] == prov['used'] if 'username' not in other_user or 'password' not in other_user: sys.stderr.write( '\nWarning: no test-authentication configured. skipping testing copy function when trying to copy file made by another user.\n' ) return try: print( "Test: Other user copy should result in different data file handle" ) syn_other = synapseclient.Synapse(skip_checks=True) syn_other.login(other_user['username'], other_user['password']) output = synapseutils.copy(syn_other, file_entity.id, destinationId=third_folder.id) new_copied_ent = syn.get(output[file_entity.id]) new_copied_ent_annot = syn.getAnnotations(new_copied_ent) schedule_for_cleanup(new_copied_ent.id) copied_URL_ent.externalURL = "https://www.google.com" copied_URL_ent = syn.store(copied_URL_ent) output = synapseutils.copy(syn_other, copied_URL_ent.id, destinationId=third_folder.id, version=1) new_copied_URL = syn.get(output[copied_URL_ent.id], downloadFile=False) schedule_for_cleanup(new_copied_URL.id) assert new_copied_ent_annot == annots assert new_copied_ent.dataFileHandleId != copied_ent.dataFileHandleId #Test if copying different versions gets you the correct file assert new_copied_URL.versionNumber == 1 assert new_copied_URL.externalURL == repo_url assert new_copied_URL.dataFileHandleId != copied_URL_ent.dataFileHandleId finally: syn_other.logout() # ------------------------------------ # TEST COPY LINKS # ------------------------------------ print("Test: Copy Links") second_file = utils.make_bogus_data_file() #schedule_for_cleanup(filename) second_file_entity = syn.store(File(second_file, parent=project_entity)) link_entity = Link(second_file_entity.id, parent=folder_entity.id) link_entity = syn.store(link_entity) #function under test uses queries which are eventually consistent but not immediately after creating the entities start_time = time.time() while syn.query("select id from entity where id=='%s'" % link_entity.id).get('totalNumberOfResults') <= 0: assert_less(time.time() - start_time, QUERY_TIMEOUT_SEC) time.sleep(2) copied_link = synapseutils.copy(syn, link_entity.id, destinationId=second_folder.id) old = syn.get(link_entity.id, followLink=False) new = syn.get(copied_link[link_entity.id], followLink=False) assert old.linksTo['targetId'] == new.linksTo['targetId'] assert old.linksTo['targetVersionNumber'] == new.linksTo[ 'targetVersionNumber'] schedule_for_cleanup(second_file_entity.id) schedule_for_cleanup(link_entity.id) schedule_for_cleanup(copied_link[link_entity.id]) time.sleep(3) assert_raises(ValueError, synapseutils.copy, syn, link_entity.id, destinationId=second_folder.id) # ------------------------------------ # TEST COPY TABLE # ------------------------------------ second_project = syn.store(Project(name=str(uuid.uuid4()))) schedule_for_cleanup(second_project.id) print("Test: Copy Tables") cols = [ Column(name='n', columnType='DOUBLE', maximumSize=50), Column(name='c', columnType='STRING', maximumSize=50), Column(name='i', columnType='INTEGER') ] data = [[2.1, 'foo', 10], [2.2, 'bar', 20], [2.3, 'baz', 30]] schema = syn.store( Schema(name='Testing', columns=cols, parent=project_entity.id)) row_reference_set = syn.store( RowSet(columns=cols, schema=schema, rows=[Row(r) for r in data])) table_map = synapseutils.copy(syn, schema.id, destinationId=second_project.id) copied_table = syn.tableQuery('select * from %s' % table_map[schema.id]) rows = copied_table.asRowSet()['rows'] # TEST: Check if all values are the same for i, row in enumerate(rows): assert row['values'] == data[i] assert_raises(ValueError, synapseutils.copy, syn, schema.id, destinationId=second_project.id) schedule_for_cleanup(schema.id) schedule_for_cleanup(table_map[schema.id]) # ------------------------------------ # TEST COPY FOLDER # ------------------------------------ print("Test: Copy Folder") mapping = synapseutils.copy(syn, folder_entity.id, destinationId=second_project.id) for i in mapping: old = syn.get(i, downloadFile=False) new = syn.get(mapping[i], downloadFile=False) assert old.name == new.name assert old.annotations == new.annotations assert old.concreteType == new.concreteType assert_raises(ValueError, synapseutils.copy, syn, folder_entity.id, destinationId=second_project.id) # TEST: Throw error if excludeTypes isn't in file, link and table or isn't a list assert_raises(ValueError, synapseutils.copy, syn, second_folder.id, destinationId=second_project.id, excludeTypes=["foo"]) assert_raises(ValueError, synapseutils.copy, syn, second_folder.id, destinationId=second_project.id, excludeTypes="file") # TEST: excludeType = ["file"], only the folder is created second = synapseutils.copy(syn, second_folder.id, destinationId=second_project.id, excludeTypes=["file", "table", "link"]) copied_folder = syn.get(second[second_folder.id]) assert copied_folder.name == second_folder.name assert len(second) == 1 # TEST: Make sure error is thrown if foldername already exists start_time = time.time() while syn.query("select id from entity where id=='%s'" % copied_folder.id).get('totalNumberOfResults') <= 0: assert_less(time.time() - start_time, QUERY_TIMEOUT_SEC) time.sleep(2) assert_raises(ValueError, synapseutils.copy, syn, second_folder.id, destinationId=second_project.id) # ------------------------------------ # TEST COPY PROJECT # ------------------------------------ print("Test: Copy Project") third_project = syn.store(Project(name=str(uuid.uuid4()))) schedule_for_cleanup(third_project.id) mapping = synapseutils.copy(syn, project_entity.id, destinationId=third_project.id) for i in mapping: old = syn.get(i, downloadFile=False) new = syn.get(mapping[i], downloadFile=False) if not isinstance(old, Project): assert old.name == new.name assert old.annotations == new.annotations assert old.concreteType == new.concreteType # TEST: Can't copy project to a folder assert_raises(ValueError, synapseutils.copy, syn, project_entity.id, destinationId=second_folder.id)