def test_uploadFileEntity(): projectEntity = create_project() # Defaults to FileEntity fname = utils.make_bogus_data_file() entity = {'name' : 'foo', \ 'description' : 'A test file entity', \ 'parentId' : projectEntity['id']} # Create new FileEntity entity = syn.uploadFile(entity, fname) # Download and verify entity = syn.downloadEntity(entity) assert entity['files'][0] == os.path.basename(fname) assert filecmp.cmp(fname, entity['path']) # Check if we upload the wrong type of file handle fh = syn.restGET('/entity/%s/filehandles' % entity.id)['list'][0] assert fh['concreteType'] == 'org.sagebionetworks.repo.model.file.S3FileHandle' os.remove(fname) # Create a different temporary file fname = utils.make_bogus_data_file() # Update existing FileEntity entity = syn.uploadFile(entity, fname) # Download and verify that it is the same filename entity = syn.downloadEntity(entity) print entity['files'][0] print os.path.basename(fname) assert entity['files'][0] == os.path.basename(fname) assert filecmp.cmp(fname, entity['path']) os.remove(fname)
def test_entity_version(): # Test the ability to get specific versions of Synapse Entities #Create a new project project = create_project() entity = create_data_entity(project['id']) syn.setAnnotations(entity, {'fizzbuzz':111222}) #Get new entity and check that it is same entity = syn.getEntity(entity) assert entity.versionNumber == 1 entity.description = 'Changed something' entity.foo = 998877 entity = syn.updateEntity(entity, incrementVersion=True) assert entity.versionNumber == 2 annotations = syn.getAnnotations(entity, version=1) assert annotations['fizzbuzz'][0] == 111222 returnEntity = syn.getEntity(entity, version=1) assert returnEntity.versionNumber == 1 assert returnEntity['fizzbuzz'][0] == 111222 assert 'foo' not in returnEntity returnEntity = syn.getEntity(entity) assert returnEntity.versionNumber == 2 assert returnEntity['description'] == 'Changed something' assert returnEntity['foo'][0] == 998877 returnEntity = syn.get(entity, version=1) assert returnEntity.versionNumber == 1 assert returnEntity['fizzbuzz'][0] == 111222 assert 'foo' not in returnEntity
def test_annotations(): ## create a new project project = create_project() entity = create_data_entity(project['id']) a = syn.getAnnotations(entity) assert 'etag' in a print a a['bogosity'] = 'total' print a syn.setAnnotations(entity, a) a2 = syn.getAnnotations(entity) assert a2['bogosity'] == ['total'] a2['primes'] = [2,3,5,7,11,13,17,19,23,29] a2['phat_numbers'] = [1234.5678, 8888.3333, 1212.3434, 6677.8899] a2['goobers'] = ['chris', 'jen', 'jane'] a2['present_time'] = datetime.now() syn.setAnnotations(entity, a2) a3 = syn.getAnnotations(entity) assert a3['primes'] == [2,3,5,7,11,13,17,19,23,29] assert a3['phat_numbers'] == [1234.5678, 8888.3333, 1212.3434, 6677.8899] assert a3['goobers'] == ['chris', 'jen', 'jane'] ## only accurate to within a second 'cause synapse strips off the fractional part assert a3['present_time'][0].strftime('%Y-%m-%d %H:%M:%S') == a2['present_time'].strftime('%Y-%m-%d %H:%M:%S')
def test_annotations(): ## create a new project project = create_project() entity = create_data_entity(project['id']) a = syn.getAnnotations(entity) assert 'etag' in a print a a['bogosity'] = 'total' print a syn.setAnnotations(entity, a) a2 = syn.getAnnotations(entity) assert a2['bogosity'] == ['total'] a2['primes'] = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29] a2['phat_numbers'] = [1234.5678, 8888.3333, 1212.3434, 6677.8899] a2['goobers'] = ['chris', 'jen', 'jane'] a2['present_time'] = datetime.now() syn.setAnnotations(entity, a2) a3 = syn.getAnnotations(entity) assert a3['primes'] == [2, 3, 5, 7, 11, 13, 17, 19, 23, 29] assert a3['phat_numbers'] == [1234.5678, 8888.3333, 1212.3434, 6677.8899] assert a3['goobers'] == ['chris', 'jen', 'jane'] ## only accurate to within a second 'cause synapse strips off the fractional part assert a3['present_time'][0].strftime('%Y-%m-%d %H:%M:%S') == a2[ 'present_time'].strftime('%Y-%m-%d %H:%M:%S')
def test_store_activity(): """Test storing entities with Activities""" project = create_project() path = utils.make_bogus_data_file() schedule_for_cleanup(path) f = File(path, name='Hinkle horn honking holes', parent=project) honking = Activity(name='Hinkle horn honking', description='Nettlebed Cave is a limestone cave located on the South Island of New Zealand.') honking.used('http://www.flickr.com/photos/bevanbfree/3482259379/') honking.used('http://www.flickr.com/photos/bevanbfree/3482185673/') ## doesn't set the ID of the activity f = syn.store(f, activity=honking) honking = syn.getProvenance(f.id) ## now, we have an activity ID assert honking['name'] == 'Hinkle horn honking' assert len(honking['used']) == 2 assert honking['used'][0]['concreteType'] == 'org.sagebionetworks.repo.model.provenance.UsedURL' assert honking['used'][0]['wasExecuted'] == False assert honking['used'][0]['url'].startswith('http://www.flickr.com/photos/bevanbfree/3482') assert honking['used'][1]['concreteType'] == 'org.sagebionetworks.repo.model.provenance.UsedURL' assert honking['used'][1]['wasExecuted'] == False ## store another entity with the same activity f2 = File('http://en.wikipedia.org/wiki/File:Nettlebed_cave.jpg', name='Nettlebed Cave', parent=project) f2 = syn.store(f2, activity=honking) honking2 = syn.getProvenance(f2) assert honking['id'] == honking2['id']
def test_entity_version(): # Test the ability to get specific versions of Synapse Entities #Create a new project project = create_project() entity = create_data_entity(project['id']) syn.setAnnotations(entity, {'fizzbuzz': 111222}) #Get new entity and check that it is same entity = syn.getEntity(entity) assert entity.versionNumber == 1 entity.description = 'Changed something' entity.foo = 998877 entity = syn.updateEntity(entity, incrementVersion=True) assert entity.versionNumber == 2 annotations = syn.getAnnotations(entity, version=1) assert annotations['fizzbuzz'][0] == 111222 returnEntity = syn.getEntity(entity, version=1) assert returnEntity.versionNumber == 1 assert returnEntity['fizzbuzz'][0] == 111222 assert 'foo' not in returnEntity returnEntity = syn.getEntity(entity) assert returnEntity.versionNumber == 2 assert returnEntity['description'] == 'Changed something' assert returnEntity['foo'][0] == 998877 returnEntity = syn.get(entity, version=1) assert returnEntity.versionNumber == 1 assert returnEntity['fizzbuzz'][0] == 111222 assert 'foo' not in returnEntity
def test_get_and_store_by_name_and_parent_id(): project = create_project() path = utils.make_bogus_data_file() schedule_for_cleanup(path) f = File(path, name='Foobarbat', parent=project) f2 = syn.store(f) f = syn.get(f) assert f.id == f2.id assert f.name == f2.name assert f.parentId == f2.parentId ## new file path = utils.make_bogus_data_file() schedule_for_cleanup(path) ## should create a new version of the previous File entity f3 = File(path, name='Foobarbat', parent=project, description='banana', junk=1234) f3 = syn.store(f3) ## should be an update of the existing entity with the same name and parent assert f3.id == f.id assert f3.description == 'banana' assert f3.junk == [1234] assert filecmp.cmp(path, f3.path)
def test_ACL(): ## get the user's principalId, which is called ownerId and is ## returned as a string, while in the ACL, it's an integer current_user_id = int(syn.getUserProfile()['ownerId']) ## verify the validity of the other user try: profile = syn.getUserProfile(other_user['principalId']) except Exception as ex: if hasattr(ex, 'response') and ex.response.status_code == 404: raise Exception('Test invalid, test user doesn\'t exist.', ex) raise ## create a new project project = create_project() ## add permissions on the project for a new user acl = syn.setPermissions(project, other_user['principalId'], accessType=['READ', 'CREATE', 'UPDATE']) permissions = syn.getPermissions(project, current_user_id) assert 'DELETE' in permissions assert 'CHANGE_PERMISSIONS' in permissions assert 'READ' in permissions assert 'CREATE' in permissions assert 'UPDATE' in permissions permissions = syn.getPermissions(project, other_user['principalId']) assert 'READ' in permissions assert 'CREATE' in permissions assert 'UPDATE' in permissions
def test_store_with_create_or_update_flag(): project = create_project() filepath = utils.make_bogus_binary_file() bogus1 = File(filepath, name='Bogus Test File', parent=project) bogus1 = syn.store(bogus1, createOrUpdate=True) # Create a different file with the same name and parent new_filepath = utils.make_bogus_binary_file() bogus1.path = new_filepath # Expected behavior is that a new version of the first File will be created bogus2 = syn.store(bogus1, createOrUpdate=True) assert bogus2.id == bogus1.id assert bogus2.versionNumber == 2 assert not filecmp.cmp(bogus2.path, filepath) bogus2a = syn.get(bogus2.id) assert bogus2a.id == bogus1.id assert bogus2a.versionNumber == 2 assert filecmp.cmp(bogus2.path, bogus2a.path) # Create yet another file with the same name and parent newer_filepath = utils.make_bogus_binary_file() bogus3 = File(newer_filepath, name='Bogus Test File', parent=project) # Expected behavior is raising an exception with a 409 error assert_raises(requests.exceptions.HTTPError, syn.store, bogus3, createOrUpdate=False)
def test_store_redundantly_named_projects(): p1 = create_project() # If we store a project with the same name, and createOrUpdate==True, # it should become an update p2 = Project(p1.name) p2.updatedThing = 'Yep, sho\'nuf it\'s updated!' p2 = syn.store(p2, createOrUpdate=True) assert p1.id == p2.id assert p2.updatedThing == ['Yep, sho\'nuf it\'s updated!']
def test_query(): #Create a project then add entities and verify that I can find them with a query project = create_project() for i in range(2): try: entity = create_data_entity(project['id']) except Exception as ex: print ex print ex.response.text qry= syn.query("select id, name from entity where entity.parentId=='%s'" % project['id']) assert qry['totalNumberOfResults']==(i+1)
def test_keyword_annotations(): """ Test setting annotations using keyword arguments """ ## create a new project and data entity project = create_project() entity = create_data_entity(project['id']) annos = syn.setAnnotations(entity, wazoo='Frank', label='Barking Pumpkin', shark=16776960) assert annos['wazoo'] == ['Frank'] assert annos['label'] == ['Barking Pumpkin'] assert annos['shark'] == [16776960]
def test_updateEntity_version(): project = create_project() entity = create_data_entity(project['id']) entity['name'] = 'foobarbat' entity['description'] = 'This is a test entity...' entity = syn.updateEntity(entity, incrementVersion=True, versionLabel="Prada remix") returnEntity = syn.getEntity(entity) # syn.printEntity(returnEntity) assert returnEntity['name'] == 'foobarbat' assert returnEntity['description'] == 'This is a test entity...' assert returnEntity['versionNumber'] == 2 assert returnEntity['versionLabel'] == 'Prada remix'
def test_createEntity_with_provenance(): #Create a project entity = create_project() #Add a data entity to project data = DATA_JSON.copy() data['parentId']= entity['id'] #Create entity with provenance record entity = syn.createEntity(data, used='syn123') activity = syn.getProvenance(entity) assert activity['used'][0]['reference']['targetId'] == 'syn123'
def test_createEntity_with_provenance(): #Create a project entity = create_project() #Add a data entity to project data = DATA_JSON.copy() data['parentId'] = entity['id'] #Create entity with provenance record entity = syn.createEntity(data, used='syn123') activity = syn.getProvenance(entity) assert activity['used'][0]['reference']['targetId'] == 'syn123'
def test_uploadFile_given_dictionary(): project = create_project() data = DATA_JSON.copy() data['parentId']= project.id filename = utils.make_bogus_data_file() data = syn.uploadFile(data, filename) entity = syn.downloadEntity(data['id']) assert entity['files'][0]==os.path.basename(filename) assert filecmp.cmp(filename, os.path.join(entity['cacheDir'],entity['files'][0])) os.remove(filename)
def test_query(): #Create a project then add entities and verify that I can find them with a query project = create_project() for i in range(2): try: entity = create_data_entity(project['id']) except Exception as ex: print ex print ex.response.text qry = syn.query( "select id, name from entity where entity.parentId=='%s'" % project['id']) assert qry['totalNumberOfResults'] == (i + 1)
def test_uploadFile_given_dictionary(): project = create_project() data = DATA_JSON.copy() data['parentId'] = project.id filename = utils.make_bogus_data_file() data = syn.uploadFile(data, filename) entity = syn.downloadEntity(data['id']) assert entity['files'][0] == os.path.basename(filename) assert filecmp.cmp(filename, os.path.join(entity['cacheDir'], entity['files'][0])) os.remove(filename)
def test_uploadFile(): project = create_project() ## here, entity has been saved to Synapse and entity is an Entity object entity = create_data_entity(project['id']) #create a temporary file fname = utils.make_bogus_data_file() syn.uploadFile(entity, fname) #Download and verify that it is the same filename entity = syn.downloadEntity(entity) assert entity['files'][0]==os.path.basename(fname) assert filecmp.cmp(fname, os.path.join(entity['cacheDir'],entity['files'][0])) os.remove(fname)
def test_synapseStore_flag(): """Test storing entities while setting the synapseStore flag to False""" project = create_project() ## store a path to a local file (synapseStore=False) path = utils.make_bogus_data_file() schedule_for_cleanup(path) f1 = File(path, name='Totally bogus data', parent=project, synapseStore=False) f1 = syn.store(f1) f1a = syn.get(f1.id, downloadFile=False) assert f1a.name == 'Totally bogus data' assert f1a.path == path, 'path=' + str(f1a.path) + '; expected=' + path assert f1a.synapseStore == False ## make sure the test runs on Windows and other OS's if path[0].isalpha() and path[1] == ':': ## a windows file URL looks like this: file:///c:/foo/bar/bat.txt expected_url = 'file:///' + path else: expected_url = 'file://' + path assert f1a.externalURL == expected_url, 'unexpected externalURL: ' + f1a.externalURL ## a file path that doesn't exist should still work f2 = File('/path/to/local/file1.xyz', parentId=project.id, synapseStore=False) f2 = syn.store(f2) try: syn.get(f2) assert False except Exception as err: assert err.message.startswith("Could not download non-existent file") assert f1a.synapseStore == False ## Try a URL f3 = File('http://dev-versions.synapse.sagebase.org/synapsePythonClient', parent=project, synapseStore=False) f3 = syn.store(f3) f3a = syn.get(f3) assert f1a.synapseStore == False
def setup(module): print '\n' print '~' * 60 print os.path.basename(__file__) print '~' * 60 module.syn = integration.syn # Use the module-level syn object to communicate between main and child threads # - Read-only objects (for the children) module.syn.test_parent = create_project() module.syn.test_keepRunning = True # - Child writeable objects module.syn.test_errors = Queue() module.syn.test_runCountMutex = Lock() module.syn.test_threadsRunning = 0
def test_deprecated_entity_types(): """Test Data Entity object""" project = create_project() data = Data(parent=project) data = syn.createEntity(data) path = utils.make_bogus_data_file() schedule_for_cleanup(path) data = syn.uploadFile(data, path) ## make sure file comes back intact data = syn.downloadEntity(data) assert filecmp.cmp(path, os.path.join(data['cacheDir'], data['files'][0]))
def test_uploadFile(): project = create_project() ## here, entity has been saved to Synapse and entity is an Entity object entity = create_data_entity(project['id']) #create a temporary file fname = utils.make_bogus_data_file() syn.uploadFile(entity, fname) #Download and verify that it is the same filename entity = syn.downloadEntity(entity) assert entity['files'][0] == os.path.basename(fname) assert filecmp.cmp(fname, os.path.join(entity['cacheDir'], entity['files'][0])) os.remove(fname)
def test_get_with_downloadLocation_and_ifcollision(): project = create_project() # Make the file to get then delete it filepath = utils.make_bogus_binary_file() bogus = File(filepath, name='Bogus Test File', parent=project) bogus = syn.store(bogus) os.remove(filepath) # Compare stuff to this one normalBogus = syn.get(bogus) # Download to the temp folder, should be the same otherBogus = syn.get(bogus, downloadLocation=os.path.dirname(filepath)) assert otherBogus.id == normalBogus.id assert filecmp.cmp(otherBogus.path, normalBogus.path) # Invalidate the downloaded file's timestamps os.utime(otherBogus.path, (0, 0)) badtimestamps = os.path.getmtime(otherBogus.path) # Download again, should change the modification time overwriteBogus = syn.get(bogus, downloadLocation=os.path.dirname(filepath), ifcollision="overwrite.local") overwriteModTime = os.path.getmtime(overwriteBogus.path) assert badtimestamps != overwriteModTime # Download again, should not change the modification time otherBogus = syn.get(bogus, downloadLocation=os.path.dirname(filepath), ifcollision="keep.local") assert overwriteModTime == os.path.getmtime(otherBogus.path) # Invalidate the timestamps again os.utime(otherBogus.path, (0, 0)) badtimestamps = os.path.getmtime(otherBogus.path) # Download once more, but rename renamedBogus = syn.get(bogus, downloadLocation=os.path.dirname(filepath), ifcollision="keep.both") assert otherBogus.path != renamedBogus.path assert filecmp.cmp(otherBogus.path, renamedBogus.path) os.remove(otherBogus.path) os.remove(renamedBogus.path)
def test_fileEntity_round_trip(): ## create a new project project = create_project() ## file the setup.py file to upload original_path = os.path.join(os.path.dirname(client.__file__), '..', 'setup.py') entity = {'name':'Foobar', 'description':'A test file entity...', 'parentId':project['id']} entity = syn._createFileEntity(entity, original_path) entity_downloaded = syn.downloadEntity(entity['id']) path = os.path.join(entity_downloaded['cacheDir'], entity_downloaded['files'][0]) assert os.path.exists(path) assert filecmp.cmp(original_path, path) shutil.rmtree(entity_downloaded['cacheDir'])
def test_ExternalFileHandle(): project = create_project() ## Tests shouldn't have external dependencies, but this is a pretty picture of Singapore singapore_url = 'http://upload.wikimedia.org/wikipedia/commons/thumb/3/3e/1_singapore_city_skyline_dusk_panorama_2011.jpg/1280px-1_singapore_city_skyline_dusk_panorama_2011.jpg' singapore = File(singapore_url, parent=project) singapore = syn.store(singapore) fileHandle = syn._getFileHandle(singapore.dataFileHandleId) assert fileHandle['concreteType'] == 'org.sagebionetworks.repo.model.file.ExternalFileHandle' assert fileHandle['externalURL'] == singapore_url singapore = syn.get(singapore, downloadFile=True) assert singapore.path is not None assert singapore.externalURL == singapore_url assert os.path.exists(singapore.path)
def test_md5_query(): # Create a project then add the same entity several times and retrieve them via MD5 project = create_project() path = utils.make_bogus_data_file() schedule_for_cleanup(path) repeated = File(path, parent=project['id'], description='Same data over and over again') num = 5 for i in range(num): try: repeated.name = 'Repeated data %d.dat' % i syn.store(repeated) except Exception as ex: print ex print ex.response.text results = syn.md5Query(utils.md5_for_file(path).hexdigest()) print [res['id'] for res in results]
def test_store_activity(): """Test storing entities with Activities""" project = create_project() path = utils.make_bogus_data_file() schedule_for_cleanup(path) f = File(path, name='Hinkle horn honking holes', parent=project) honking = Activity( name='Hinkle horn honking', description= 'Nettlebed Cave is a limestone cave located on the South Island of New Zealand.' ) honking.used('http://www.flickr.com/photos/bevanbfree/3482259379/') honking.used('http://www.flickr.com/photos/bevanbfree/3482185673/') ## doesn't set the ID of the activity f = syn.store(f, activity=honking) honking = syn.getProvenance(f.id) ## now, we have an activity ID assert honking['name'] == 'Hinkle horn honking' assert len(honking['used']) == 2 assert honking['used'][0][ 'concreteType'] == 'org.sagebionetworks.repo.model.provenance.UsedURL' assert honking['used'][0]['wasExecuted'] == False assert honking['used'][0]['url'].startswith( 'http://www.flickr.com/photos/bevanbfree/3482') assert honking['used'][1][ 'concreteType'] == 'org.sagebionetworks.repo.model.provenance.UsedURL' assert honking['used'][1]['wasExecuted'] == False ## store another entity with the same activity f2 = File('http://en.wikipedia.org/wiki/File:Nettlebed_cave.jpg', name='Nettlebed Cave', parent=project) f2 = syn.store(f2, activity=honking) honking2 = syn.getProvenance(f2) assert honking['id'] == honking2['id']
def test_ExternalFileHandle(): project = create_project() ## Tests shouldn't have external dependencies, but this is a pretty picture of Singapore singapore_url = 'http://upload.wikimedia.org/wikipedia/commons/thumb/3/3e/1_singapore_city_skyline_dusk_panorama_2011.jpg/1280px-1_singapore_city_skyline_dusk_panorama_2011.jpg' singapore = File(singapore_url, parent=project) singapore = syn.store(singapore) fileHandle = syn._getFileHandle(singapore.dataFileHandleId) assert fileHandle[ 'concreteType'] == 'org.sagebionetworks.repo.model.file.ExternalFileHandle' assert fileHandle['externalURL'] == singapore_url singapore = syn.get(singapore, downloadFile=True) assert singapore.path is not None assert singapore.externalURL == singapore_url assert os.path.exists(singapore.path)
def test_synapseStore_flag(): """Test storing entities while setting the synapseStore flag to False""" project = create_project() ## store a path to a local file (synapseStore=False) path = utils.make_bogus_data_file() schedule_for_cleanup(path) f1 = File(path, name='Totally bogus data', parent=project, synapseStore=False) f1 = syn.store(f1) f1a = syn.get(f1.id, downloadFile=False) assert f1a.name == 'Totally bogus data' assert f1a.path == path, 'path='+str(f1a.path)+'; expected='+path assert f1a.synapseStore == False ## make sure the test runs on Windows and other OS's if path[0].isalpha() and path[1]==':': ## a windows file URL looks like this: file:///c:/foo/bar/bat.txt expected_url = 'file:///' + path else: expected_url = 'file://' + path assert f1a.externalURL==expected_url, 'unexpected externalURL: ' + f1a.externalURL ## a file path that doesn't exist should still work f2 = File('/path/to/local/file1.xyz', parentId=project.id, synapseStore=False) f2 = syn.store(f2) try: syn.get(f2) assert False except Exception as err: assert err.message.startswith("Could not download non-existent file") assert f1a.synapseStore == False ## Try a URL f3 = File('http://dev-versions.synapse.sagebase.org/synapsePythonClient', parent=project, synapseStore=False) f3 = syn.store(f3) f3a = syn.get(f3) assert f1a.synapseStore == False
def test_get_store_download_file_equals_false(): """ Test for SYNR-474: Python client crashes when storing syn.store(syn.get(..., downloadFile=False)) """ project = create_project() path = utils.make_bogus_data_file() schedule_for_cleanup(path) f = File(path, name='Foobarbat', parent=project) f = syn.store(f) f1 = syn.get(f.id, downloadFile=False) f1.description = 'Snorklewacker' f1.shoe_size = 11.5 f1 = syn.store(f1) f2 = syn.get(f.id, downloadFile=False) assert f2.description == f1.description assert f2.shoe_size == [11.5]
def test_chunked_query(): # Change the size of the query limit oldLimit = client.QUERY_LIMIT client.QUERY_LIMIT = 3 # Create a project to dump a bunch of Entities into project = create_project() for i in range(client.QUERY_LIMIT * 5): try: entity = create_data_entity(project['id']) except Exception as ex: print ex print ex.response.text iter = syn.chunkedQuery("select * from entity where entity.parentId=='%s'" % project['id']) count = 0 for res in iter: count += 1 assert count == (client.QUERY_LIMIT * 5) # Restore the size of the query limit client.QUERY_LIMIT = oldLimit
def test_fileEntity_round_trip(): ## create a new project project = create_project() ## file the setup.py file to upload original_path = os.path.join(os.path.dirname(client.__file__), '..', 'setup.py') entity = { 'name': 'Foobar', 'description': 'A test file entity...', 'parentId': project['id'] } entity = syn._createFileEntity(entity, original_path) entity_downloaded = syn.downloadEntity(entity['id']) path = os.path.join(entity_downloaded['cacheDir'], entity_downloaded['files'][0]) assert os.path.exists(path) assert filecmp.cmp(original_path, path) shutil.rmtree(entity_downloaded['cacheDir'])
def test_chunked_query(): # Change the size of the query limit oldLimit = client.QUERY_LIMIT client.QUERY_LIMIT = 3 # Create a project to dump a bunch of Entities into project = create_project() for i in range(client.QUERY_LIMIT * 5): try: entity = create_data_entity(project['id']) except Exception as ex: print ex print ex.response.text iter = syn.chunkedQuery( "select * from entity where entity.parentId=='%s'" % project['id']) count = 0 for res in iter: count += 1 assert count == (client.QUERY_LIMIT * 5) # Restore the size of the query limit client.QUERY_LIMIT = oldLimit
def test_store_with_force_version_flag(): project = create_project() filepath = utils.make_bogus_binary_file() bogus1 = File(filepath, name='Bogus Test File', parent=project) # Expect to get version 1 back bogus1 = syn.store(bogus1, forceVersion=False) assert bogus1.versionNumber == 1 # Re-store the same thing and don't up the version bogus2 = syn.store(bogus1, forceVersion=False) assert bogus1.versionNumber == 1 # Create a different file with the same name and parent new_filepath = utils.make_bogus_binary_file() bogus2.path = new_filepath # Expected behavior is that a new version of the first File will be created bogus2 = syn.store(bogus2, forceVersion=False) assert bogus2.id == bogus1.id assert bogus2.versionNumber == 2 assert not filecmp.cmp(bogus2.path, filepath)
def test_update_and_increment_version(): project = create_project() path = utils.make_bogus_data_file() schedule_for_cleanup(path) f = File(path, parent=project) f = syn.store(f) newversion = { 'id':f.id, 'name':f.name, 'description':'This is a totally new description', 'entityType':File._synapse_entity_type, 'dataFileHandleId':f.dataFileHandleId, 'etag':f.etag, 'parentId':f.parentId} updated_f = syn.store(newversion) assert updated_f.id == f.id assert updated_f.description == 'This is a totally new description' assert updated_f.versionNumber == 2
def test_round_trip(): fh = None filepath = utils.make_bogus_binary_file(6*MB + 777771, verbose=True) print 'Made bogus file: ', filepath try: fh = syn._chunkedUploadFile(filepath, verbose=False) print '=' * 60 print 'FileHandle:' syn.printEntity(fh) print 'creating project and file' project = create_project() junk = File(filepath, parent=project, dataFileHandleId=fh['id']) junk.properties.update(syn._createEntity(junk.properties)) print 'downloading file' junk.update(syn._downloadFileEntity(junk, filepath)) print 'comparing files' assert filecmp.cmp(filepath, junk.path) print 'ok!' finally: try: if 'junk' in locals(): syn.delete(junk) except Exception as ex: print ex try: os.remove(filepath) except Exception as ex: print ex if fh: print 'Deleting fileHandle', fh['id'] syn._deleteFileHandle(fh)
def test_update_and_increment_version(): project = create_project() path = utils.make_bogus_data_file() schedule_for_cleanup(path) f = File(path, parent=project) f = syn.store(f) newversion = { 'id': f.id, 'name': f.name, 'description': 'This is a totally new description', 'entityType': File._synapse_entity_type, 'dataFileHandleId': f.dataFileHandleId, 'etag': f.etag, 'parentId': f.parentId } updated_f = syn.store(newversion) assert updated_f.id == f.id assert updated_f.description == 'This is a totally new description' assert updated_f.versionNumber == 2
def test_uploadFileEntity(): projectEntity = create_project() # Defaults to FileEntity fname = utils.make_bogus_data_file() entity = {'name' : 'foo', \ 'description' : 'A test file entity', \ 'parentId' : projectEntity['id']} # Create new FileEntity entity = syn.uploadFile(entity, fname) # Download and verify entity = syn.downloadEntity(entity) assert entity['files'][0] == os.path.basename(fname) assert filecmp.cmp(fname, entity['path']) # Check if we upload the wrong type of file handle fh = syn.restGET('/entity/%s/filehandles' % entity.id)['list'][0] assert fh[ 'concreteType'] == 'org.sagebionetworks.repo.model.file.S3FileHandle' os.remove(fname) # Create a different temporary file fname = utils.make_bogus_data_file() # Update existing FileEntity entity = syn.uploadFile(entity, fname) # Download and verify that it is the same filename entity = syn.downloadEntity(entity) print entity['files'][0] print os.path.basename(fname) assert entity['files'][0] == os.path.basename(fname) assert filecmp.cmp(fname, entity['path']) os.remove(fname)
def test_md5_query(): # Create a project then add the same entity several times and retrieve them via MD5 project = create_project() path = utils.make_bogus_data_file() schedule_for_cleanup(path) repeated = File(path, parent=project['id'], description='Same data over and over again') num = 5 for i in range(num): try: repeated.name = 'Repeated data %d.dat' % i syn.store(repeated) except Exception as ex: print ex print ex.response.text results = syn.md5Query(utils.md5_for_file(path).hexdigest()) print[res['id'] for res in results] ## Not sure how to make this assertion more accurate ## Although we expect num results, it is possible for the MD5 to be non-unique assert len(results) == num
def test_evaluations(): ## create a new project project = create_project() name = 'Test Evaluation %s' % (str(uuid.uuid4()),) try: #Create evaluation ev = Evaluation(name=name, description='Evaluation for testing', contentSource=project['id'], status='CLOSED') ev = syn.store(ev) #Update evaluation ev['status']='OPEN' ev= syn.store(ev, createOrUpdate=True) assert ev.status == 'OPEN' ## add the current user as a participant user = syn.getUserProfile() syn.addEvaluationParticipant(ev, user['ownerId']) ## test getSubmissions with no submissions (SYNR-453) submissions = syn.getSubmissions(ev) assert len(list(submissions))==0 ## increase this to fully test paging by getEvaluationSubmissions num_of_submissions = 3 ## create a bunch of entities and submit them for evaluation sys.stdout.write('\ncreating evaluation submissions') for i in range(num_of_submissions): try: (fd, filename) = tempfile.mkstemp() with os.fdopen(fd, 'w') as f: f.write(str(random.gauss(0,1))) f.write('\n') f=File(filename, parentId=project.id, name='entry-%02d'%i, description ='An entry for testing evaluation') entity=syn.store(f) syn.submit(ev, entity) finally: os.remove(filename) sys.stdout.write('.') sys.stdout.flush() ## score the submissions submissions = syn.getSubmissions(ev) sys.stdout.write('\nscoring submissions') for submission in submissions: status=syn.getSubmissionStatus(submission) status.score = random.random() status.status = 'SCORED' status.report = 'a fabulous effort!' syn.store(status) sys.stdout.write('.') sys.stdout.flush() sys.stdout.write('\n') finally: syn.delete(ev) ## make sure it's deleted try: ev = syn.getEvaluation(ev) except Exception as e: print e assert e.response.status_code == 404
def test_provenance(): """ test provenance features """ ## create a new project project = create_project() ## create a data entity try: filename = utils.make_bogus_data_file() data_entity = create_data_entity(project['id']) data_entity = syn.uploadFile(data_entity, filename) finally: os.remove(filename) ## create a code entity, source of the data above code = """ ## Chris's fabulous random data generator ############################################################ import random random.seed(12345) data = [random.gauss(mu=0.0, sigma=1.0) for i in range(100)] """ try: try: f = tempfile.NamedTemporaryFile(suffix=".py", delete=False) f.write(code) f.write("\n") finally: f.close() CODE_JSON['parentId']= project['id'] code_entity = syn.createEntity(CODE_JSON) code_entity = syn.uploadFile(code_entity, f.name) finally: os.remove(f.name) ## create a new activity asserting that the code entity was used to ## make the data entity activity = Activity(name='random.gauss', description='Generate some random numbers') activity.used(code_entity, wasExecuted=True) activity.used({'name':'Superhack', 'url':'https://github.com/joe_coder/Superhack'}, wasExecuted=True) activity = syn.setProvenance(data_entity, activity) ## retrieve the saved provenance record retrieved_activity = syn.getProvenance(data_entity) ## does it match what we expect? assert retrieved_activity == activity ## test update new_description = 'Generate random numbers like a gangsta' retrieved_activity['description'] = new_description updated_activity = syn.updateActivity(retrieved_activity) ## did the update stick? assert updated_activity['name'] == retrieved_activity['name'] assert updated_activity['description'] == new_description ## test delete syn.deleteProvenance(data_entity) try: ## provenance should be gone now ## decided this should throw exception with a 404 deleted_activity = syn.getProvenance(data_entity['id']) except Exception as ex: assert ex.response.status_code == 404 else: assert False, 'Should throw 404 exception'
def test_provenance(): """ test provenance features """ ## create a new project project = create_project() ## create a data entity try: filename = utils.make_bogus_data_file() data_entity = create_data_entity(project['id']) data_entity = syn.uploadFile(data_entity, filename) finally: os.remove(filename) ## create a code entity, source of the data above code = """ ## Chris's fabulous random data generator ############################################################ import random random.seed(12345) data = [random.gauss(mu=0.0, sigma=1.0) for i in range(100)] """ try: try: f = tempfile.NamedTemporaryFile(suffix=".py", delete=False) f.write(code) f.write("\n") finally: f.close() CODE_JSON['parentId'] = project['id'] code_entity = syn.createEntity(CODE_JSON) code_entity = syn.uploadFile(code_entity, f.name) finally: os.remove(f.name) ## create a new activity asserting that the code entity was used to ## make the data entity activity = Activity(name='random.gauss', description='Generate some random numbers') activity.used(code_entity, wasExecuted=True) activity.used( { 'name': 'Superhack', 'url': 'https://github.com/joe_coder/Superhack' }, wasExecuted=True) activity = syn.setProvenance(data_entity, activity) ## retrieve the saved provenance record retrieved_activity = syn.getProvenance(data_entity) ## does it match what we expect? assert retrieved_activity == activity ## test update new_description = 'Generate random numbers like a gangsta' retrieved_activity['description'] = new_description updated_activity = syn.updateActivity(retrieved_activity) ## did the update stick? assert updated_activity['name'] == retrieved_activity['name'] assert updated_activity['description'] == new_description ## test delete syn.deleteProvenance(data_entity) try: ## provenance should be gone now ## decided this should throw exception with a 404 deleted_activity = syn.getProvenance(data_entity['id']) except Exception as ex: assert ex.response.status_code == 404 else: assert False, 'Should throw 404 exception'
def test_download_empty_entity(): project = create_project() entity = create_data_entity(project['id']) entity = syn.downloadEntity(entity)
def test_wikiAttachment(): md = """ This is a test wiki ======================= Blabber jabber blah blah boo. """ ## create a new project project = create_project() ## file the setup.py file to upload original_path = os.path.join(os.path.dirname(client.__file__), '..', 'setup.py') ## upload a file to the file handle service fileHandle = syn._uploadFileToFileHandleService(original_path) #Create and store the wiki wiki = Wiki(owner=project, title='A Test Wiki', markdown=md, attachmentFileHandleIds=[fileHandle['id']]) wiki=syn.store(wiki) #Create a wiki subpage subwiki = Wiki(owner=project, title='A sub-wiki', markdown='nothing', parentWikiId=wiki.id) subwiki=syn.store(subwiki) ## retrieve the root wiki from Synapse wiki2 = syn.getWiki(project) assert wiki==wiki2 ## retrieve the sub wiki from Synapse wiki2 = syn.getWiki(project, subpageId=subwiki.id) assert subwiki==wiki2 ## try making an update wiki['title'] = 'A New Title' wiki['markdown'] = wiki['markdown'] + "\nNew stuff here!!!\n" wiki = syn.store(wiki) assert wiki['title'] == 'A New Title' assert wiki['markdown'].endswith("\nNew stuff here!!!\n") headers = syn.getWikiHeaders(project) assert headers['totalNumberOfResults']==2 assert headers['results'][0]['title'] in (wiki['title'], subwiki['title']) ## retrieve the file we just uploaded #tmpdir = tempfile.mkdtemp() # file_props = syn._downloadWikiAttachment(project, wiki, # os.path.basename(original_path), dest_dir=tmpdir) # ## we get back a dictionary with path, files and cacheDir # path = file_props['path'] # ## check and delete it # assert os.path.exists(path) # assert filecmp.cmp(original_path, path) # shutil.rmtree(tmpdir) ## cleanup syn._deleteFileHandle(fileHandle) syn.delete(wiki) syn.delete(subwiki) ## test that delete worked try: deleted_wiki = syn.getWiki(project) except Exception as ex: assert ex.response.status_code == 404 else: assert False, 'Should raise 404 exception'
def test_evaluations(): ## create a new project project = create_project() name = 'Test Evaluation %s' % (str(uuid.uuid4()), ) try: #Create evaluation ev = Evaluation(name=name, description='Evaluation for testing', contentSource=project['id'], status='CLOSED') ev = syn.store(ev) #Update evaluation ev['status'] = 'OPEN' ev = syn.store(ev, createOrUpdate=True) assert ev.status == 'OPEN' ## add the current user as a participant user = syn.getUserProfile() syn.addEvaluationParticipant(ev, user['ownerId']) ## test getSubmissions with no submissions (SYNR-453) submissions = syn.getSubmissions(ev) assert len(list(submissions)) == 0 ## increase this to fully test paging by getEvaluationSubmissions num_of_submissions = 3 ## create a bunch of entities and submit them for evaluation sys.stdout.write('\ncreating evaluation submissions') for i in range(num_of_submissions): try: (fd, filename) = tempfile.mkstemp() with os.fdopen(fd, 'w') as f: f.write(str(random.gauss(0, 1))) f.write('\n') f = File(filename, parentId=project.id, name='entry-%02d' % i, description='An entry for testing evaluation') entity = syn.store(f) syn.submit(ev, entity) finally: os.remove(filename) sys.stdout.write('.') sys.stdout.flush() ## score the submissions submissions = syn.getSubmissions(ev) sys.stdout.write('\nscoring submissions') for submission in submissions: status = syn.getSubmissionStatus(submission) status.score = random.random() status.status = 'SCORED' status.report = 'a fabulous effort!' syn.store(status) sys.stdout.write('.') sys.stdout.flush() sys.stdout.write('\n') finally: syn.delete(ev) ## make sure it's deleted try: ev = syn.getEvaluation(ev) except Exception as e: print e assert e.response.status_code == 404
def test_wikiAttachment(): md = """ This is a test wiki ======================= Blabber jabber blah blah boo. """ ## create a new project project = create_project() ## file the setup.py file to upload original_path = os.path.join(os.path.dirname(client.__file__), '..', 'setup.py') ## upload a file to the file handle service fileHandle = syn._uploadFileToFileHandleService(original_path) #Create and store the wiki wiki = Wiki(owner=project, title='A Test Wiki', markdown=md, attachmentFileHandleIds=[fileHandle['id']]) wiki = syn.store(wiki) #Create a wiki subpage subwiki = Wiki(owner=project, title='A sub-wiki', markdown='nothing', parentWikiId=wiki.id) subwiki = syn.store(subwiki) ## retrieve the root wiki from Synapse wiki2 = syn.getWiki(project) assert wiki == wiki2 ## retrieve the sub wiki from Synapse wiki2 = syn.getWiki(project, subpageId=subwiki.id) assert subwiki == wiki2 ## try making an update wiki['title'] = 'A New Title' wiki['markdown'] = wiki['markdown'] + "\nNew stuff here!!!\n" wiki = syn.store(wiki) assert wiki['title'] == 'A New Title' assert wiki['markdown'].endswith("\nNew stuff here!!!\n") headers = syn.getWikiHeaders(project) assert headers['totalNumberOfResults'] == 2 assert headers['results'][0]['title'] in (wiki['title'], subwiki['title']) ## retrieve the file we just uploaded #tmpdir = tempfile.mkdtemp() # file_props = syn._downloadWikiAttachment(project, wiki, # os.path.basename(original_path), dest_dir=tmpdir) # ## we get back a dictionary with path, files and cacheDir # path = file_props['path'] # ## check and delete it # assert os.path.exists(path) # assert filecmp.cmp(original_path, path) # shutil.rmtree(tmpdir) ## cleanup syn._deleteFileHandle(fileHandle) syn.delete(wiki) syn.delete(subwiki) ## test that delete worked try: deleted_wiki = syn.getWiki(project) except Exception as ex: assert ex.response.status_code == 404 else: assert False, 'Should raise 404 exception'