def test_activity_used_execute_methods(): """test activity creation and used and execute methods""" a = Activity(name='Fuzz', description='hipster beard dataset') a.used({ 'id': 'syn101', 'versionNumber': 42, 'concreteType': 'org.sagebionetworks.repo.model.FileEntity' }) a.executed('syn102', targetVersion=1) usedEntities = a['used'] len(usedEntities) == 2 assert a['name'] == 'Fuzz' assert a['description'] == 'hipster beard dataset' ## ??? are activities supposed to come back in order? Let's not count on it used_syn101 = _find_used( a, lambda res: res['reference']['targetId'] == 'syn101') assert used_syn101['reference']['targetVersionNumber'] == 42 assert used_syn101['wasExecuted'] == False used_syn102 = _find_used( a, lambda res: res['reference']['targetId'] == 'syn102') assert used_syn102['reference']['targetVersionNumber'] == 1 assert used_syn102['wasExecuted'] == True
def test_activity_used_execute_methods(): """test activity creation and used and execute methods""" a = Activity(name='Fuzz', description='hipster beard dataset') a.used({'id': 'syn101', 'versionNumber': 42, 'concreteType': 'org.sagebionetworks.repo.model.FileEntity'}) a.executed('syn102', targetVersion=1) usedEntities = a['used'] len(usedEntities), 2 assert_equals(a['name'], 'Fuzz') assert_equals(a['description'], 'hipster beard dataset') used_syn101 = _find_used(a, lambda res: res['reference']['targetId'] == 'syn101') assert_equals(used_syn101['reference']['targetVersionNumber'], 42) assert_false(used_syn101['wasExecuted']) used_syn102 = _find_used(a, lambda res: res['reference']['targetId'] == 'syn102') assert_equals(used_syn102['reference']['targetVersionNumber'], 1) assert_true(used_syn102['wasExecuted'])
def test_activity_used_execute_methods(): """test activity creation and used and execute methods""" a = Activity(name="Fuzz", description="hipster beard dataset") a.used({"id": "syn101", "versionNumber": 42, "concreteType": "org.sagebionetworks.repo.model.FileEntity"}) a.executed("syn102", targetVersion=1) usedEntities = a["used"] len(usedEntities) == 2 assert a["name"] == "Fuzz" assert a["description"] == "hipster beard dataset" ## ??? are activities supposed to come back in order? Let's not count on it used_syn101 = _find_used(a, lambda res: res["reference"]["targetId"] == "syn101") assert used_syn101["reference"]["targetVersionNumber"] == 42 assert used_syn101["wasExecuted"] == False used_syn102 = _find_used(a, lambda res: res["reference"]["targetId"] == "syn102") assert used_syn102["reference"]["targetVersionNumber"] == 1 assert used_syn102["wasExecuted"] == True
def test_activity_used_execute_methods(): """test activity creation and used and execute methods""" a = Activity(name='Fuzz', description='hipster beard dataset') a.used({'id':'syn101', 'versionNumber':42, 'concreteType': 'org.sagebionetworks.repo.model.Data'}) a.executed('syn102', targetVersion=1) usedEntities = a['used'] len(usedEntities) == 2 assert a['name'] == 'Fuzz' assert a['description'] == 'hipster beard dataset' ## ??? are activities supposed to come back in order? Let's not count on it used_syn101 = _find_used(a, lambda res: res['reference']['targetId'] == 'syn101') assert used_syn101['reference']['targetVersionNumber'] == 42 assert used_syn101['wasExecuted'] == False used_syn102 = _find_used(a, lambda res: res['reference']['targetId'] == 'syn102') assert used_syn102['reference']['targetVersionNumber'] == 1 assert used_syn102['wasExecuted'] == True
def test_activity_creation_by_constructor(): """test activity creation adding used entities by the constructor""" ue1 = {'reference': {'targetId': 'syn101', 'targetVersionNumber': 42}, 'wasExecuted': False} ue2 = {'id': 'syn102', 'versionNumber': 2, 'concreteType': 'org.sagebionetworks.repo.model.FileEntity'} ue3 = 'syn103' a = Activity(name='Fuzz', description='hipster beard dataset', used=[ue1, ue3], executed=[ue2]) used_syn101 = _find_used(a, lambda res: res['reference']['targetId'] == 'syn101') assert_is_not_none(used_syn101) assert_equals(used_syn101['reference']['targetVersionNumber'], 42) assert_false(used_syn101['wasExecuted']) used_syn102 = _find_used(a, lambda res: res['reference']['targetId'] == 'syn102') assert_is_not_none(used_syn102) assert_equals(used_syn102['reference']['targetVersionNumber'], 2) assert_true(used_syn102['wasExecuted']) used_syn103 = _find_used(a, lambda res: res['reference']['targetId'] == 'syn103') assert_is_not_none(used_syn103)
def test_activity_used_url(): """test activity creation with UsedURLs""" u1 = 'http://xkcd.com' u2 = {'name': 'The Onion', 'url': 'http://theonion.com'} u3 = { 'name': 'Seriously advanced code', 'url': 'https://github.com/cbare/Pydoku/blob/ef88069f70823808f3462410e941326ae7ffbbe0/solver.py', 'wasExecuted': True } u4 = { 'name': 'Heavy duty algorithm', 'url': 'https://github.com/cbare/Pydoku/blob/master/solver.py' } a = Activity(name='Foobarbat', description='Apply foo to a bar and a bat', used=[u1, u2, u3], executed=[u3, u4]) a.executed(url='http://cran.r-project.org/web/packages/glmnet/index.html', name='glm.net') a.used(url='http://earthquake.usgs.gov/earthquakes/feed/geojson/2.5/day', name='earthquakes') u = _find_used(a, lambda res: 'url' in res and res['url'] == u1) assert u is not None assert u['url'] == u1 assert u['wasExecuted'] == False u = _find_used(a, lambda res: 'name' in res and res['name'] == 'The Onion') assert u is not None assert u['url'] == 'http://theonion.com' assert u['wasExecuted'] == False u = _find_used( a, lambda res: 'name' in res and res['name'] == 'Seriously advanced code') assert u is not None assert u['url'] == u3['url'] assert u['wasExecuted'] == u3['wasExecuted'] u = _find_used( a, lambda res: 'name' in res and res['name'] == 'Heavy duty algorithm') assert u is not None assert u['url'] == u4['url'] assert u['wasExecuted'] == True u = _find_used(a, lambda res: 'name' in res and res['name'] == 'glm.net') assert u is not None assert u[ 'url'] == 'http://cran.r-project.org/web/packages/glmnet/index.html' assert u['wasExecuted'] == True u = _find_used(a, lambda res: 'name' in res and res['name'] == 'earthquakes') assert u is not None assert u[ 'url'] == 'http://earthquake.usgs.gov/earthquakes/feed/geojson/2.5/day' assert u['wasExecuted'] == False
def test_activity_creation_by_constructor(): """test activity creation adding used entities by the constructor""" ue1 = { 'reference': { 'targetId': 'syn101', 'targetVersionNumber': 42 }, 'wasExecuted': False } ue2 = { 'id': 'syn102', 'versionNumber': 2, 'concreteType': 'org.sagebionetworks.repo.model.FileEntity' } ue3 = 'syn103' a = Activity(name='Fuzz', description='hipster beard dataset', used=[ue1, ue3], executed=[ue2]) # print(a['used']) used_syn101 = _find_used( a, lambda res: res['reference']['targetId'] == 'syn101') assert used_syn101 is not None assert used_syn101['reference']['targetVersionNumber'] == 42 assert used_syn101['wasExecuted'] == False used_syn102 = _find_used( a, lambda res: res['reference']['targetId'] == 'syn102') assert used_syn102 is not None assert used_syn102['reference']['targetVersionNumber'] == 2 assert used_syn102['wasExecuted'] == True used_syn103 = _find_used( a, lambda res: res['reference']['targetId'] == 'syn103') assert used_syn103 is not None
def test_activity_creation_by_constructor(): """test activity creation adding used entities by the constructor""" ue1 = {"reference": {"targetId": "syn101", "targetVersionNumber": 42}, "wasExecuted": False} ue2 = {"id": "syn102", "versionNumber": 2, "concreteType": "org.sagebionetworks.repo.model.FileEntity"} ue3 = "syn103" a = Activity(name="Fuzz", description="hipster beard dataset", used=[ue1, ue3], executed=[ue2]) # print(a['used']) used_syn101 = _find_used(a, lambda res: res["reference"]["targetId"] == "syn101") assert used_syn101 is not None assert used_syn101["reference"]["targetVersionNumber"] == 42 assert used_syn101["wasExecuted"] == False used_syn102 = _find_used(a, lambda res: res["reference"]["targetId"] == "syn102") assert used_syn102 is not None assert used_syn102["reference"]["targetVersionNumber"] == 2 assert used_syn102["wasExecuted"] == True used_syn103 = _find_used(a, lambda res: res["reference"]["targetId"] == "syn103") assert used_syn103 is not None
def test_activity_creation_by_constructor(): """test activity creation adding used entities by the constructor""" ue1 = {'reference':{'targetId':'syn101', 'targetVersionNumber':42}, 'wasExecuted':False} ue2 = {'id':'syn102', 'versionNumber':2, 'concreteType': 'org.sagebionetworks.repo.model.Code'} ue3 = 'syn103' a = Activity(name='Fuzz', description='hipster beard dataset', used=[ue1, ue3], executed=[ue2]) # print a['used'] used_syn101 = _find_used(a, lambda res: res['reference']['targetId'] == 'syn101') assert used_syn101 is not None assert used_syn101['reference']['targetVersionNumber'] == 42 assert used_syn101['wasExecuted'] == False used_syn102 = _find_used(a, lambda res: res['reference']['targetId'] == 'syn102') assert used_syn102 is not None assert used_syn102['reference']['targetVersionNumber'] == 2 assert used_syn102['wasExecuted'] == True used_syn103 = _find_used(a, lambda res: res['reference']['targetId'] == 'syn103') assert used_syn103 is not None
def test_activity_used_execute_methods(): """test activity creation and used and execute methods""" a = Activity(name='Fuzz', description='hipster beard dataset') a.used({ 'id': 'syn101', 'versionNumber': 42, 'concreteType': 'org.sagebionetworks.repo.model.FileEntity' }) a.executed('syn102', targetVersion=1) usedEntities = a['used'] len(usedEntities), 2 assert_equals(a['name'], 'Fuzz') assert_equals(a['description'], 'hipster beard dataset') used_syn101 = _find_used( a, lambda res: res['reference']['targetId'] == 'syn101') assert_equals(used_syn101['reference']['targetVersionNumber'], 42) assert_false(used_syn101['wasExecuted']) used_syn102 = _find_used( a, lambda res: res['reference']['targetId'] == 'syn102') assert_equals(used_syn102['reference']['targetVersionNumber'], 1) assert_true(used_syn102['wasExecuted'])
def test_activity_used_url(): """test activity creation with UsedURLs""" u1 = "http://xkcd.com" u2 = {"name": "The Onion", "url": "http://theonion.com"} u3 = { "name": "Seriously advanced code", "url": "https://github.com/cbare/Pydoku/blob/ef88069f70823808f3462410e941326ae7ffbbe0/solver.py", "wasExecuted": True, } u4 = {"name": "Heavy duty algorithm", "url": "https://github.com/cbare/Pydoku/blob/master/solver.py"} a = Activity(name="Foobarbat", description="Apply foo to a bar and a bat", used=[u1, u2, u3], executed=[u3, u4]) a.executed(url="http://cran.r-project.org/web/packages/glmnet/index.html", name="glm.net") a.used(url="http://earthquake.usgs.gov/earthquakes/feed/geojson/2.5/day", name="earthquakes") u = _find_used(a, lambda res: "url" in res and res["url"] == u1) assert u is not None assert u["url"] == u1 assert u["wasExecuted"] == False u = _find_used(a, lambda res: "name" in res and res["name"] == "The Onion") assert u is not None assert u["url"] == "http://theonion.com" assert u["wasExecuted"] == False u = _find_used(a, lambda res: "name" in res and res["name"] == "Seriously advanced code") assert u is not None assert u["url"] == u3["url"] assert u["wasExecuted"] == u3["wasExecuted"] u = _find_used(a, lambda res: "name" in res and res["name"] == "Heavy duty algorithm") assert u is not None assert u["url"] == u4["url"] assert u["wasExecuted"] == True u = _find_used(a, lambda res: "name" in res and res["name"] == "glm.net") assert u is not None assert u["url"] == "http://cran.r-project.org/web/packages/glmnet/index.html" assert u["wasExecuted"] == True u = _find_used(a, lambda res: "name" in res and res["name"] == "earthquakes") assert u is not None assert u["url"] == "http://earthquake.usgs.gov/earthquakes/feed/geojson/2.5/day" assert u["wasExecuted"] == False
def test_activity_used_url(): """test activity creation with UsedURLs""" u1 = 'http://xkcd.com' u2 = {'name': 'The Onion', 'url': 'http://theonion.com'} u3 = {'name': 'Seriously advanced code', 'url': 'https://github.com/cbare/Pydoku/blob/ef88069f70823808f3462410e941326ae7ffbbe0/solver.py', 'wasExecuted': True} u4 = {'name': 'Heavy duty algorithm', 'url': 'https://github.com/cbare/Pydoku/blob/master/solver.py'} a = Activity(name='Foobarbat', description='Apply foo to a bar and a bat', used=[u1, u2, u3], executed=[u3, u4]) a.executed(url='http://cran.r-project.org/web/packages/glmnet/index.html', name='glm.net') a.used(url='http://earthquake.usgs.gov/earthquakes/feed/geojson/2.5/day', name='earthquakes') u = _find_used(a, lambda res: 'url' in res and res['url'] == u1) assert_is_not_none(u) assert_equals(u['url'], u1) assert_false(u['wasExecuted']) u = _find_used(a, lambda res: 'name' in res and res['name'] == 'The Onion') assert_is_not_none(u) assert_equals(u['url'], 'http://theonion.com') assert_false(u['wasExecuted']) u = _find_used(a, lambda res: 'name' in res and res['name'] == 'Seriously advanced code') assert_is_not_none(u) assert_equals(u['url'], u3['url']) assert_equals(u['wasExecuted'], u3['wasExecuted']) u = _find_used(a, lambda res: 'name' in res and res['name'] == 'Heavy duty algorithm') assert_is_not_none(u) assert_equals(u['url'], u4['url']) assert_true(u['wasExecuted']) u = _find_used(a, lambda res: 'name' in res and res['name'] == 'glm.net') assert_is_not_none(u) assert_equals(u['url'], 'http://cran.r-project.org/web/packages/glmnet/index.html') assert_true(u['wasExecuted']) u = _find_used(a, lambda res: 'name' in res and res['name'] == 'earthquakes') assert_is_not_none(u) assert_equals(u['url'], 'http://earthquake.usgs.gov/earthquakes/feed/geojson/2.5/day') assert_false(u['wasExecuted'])
def test_command_line_client(): # Create a Project output = run('synapse', '--skip-checks', 'create', '-name', str(uuid.uuid4()), '-description', 'test of command line client', 'Project') project_id = parse(r'Created entity:\s+(syn\d+)\s+', output) schedule_for_cleanup(project_id) # Create a File filename = utils.make_bogus_data_file() schedule_for_cleanup(filename) output = run('synapse', '--skip-checks', 'add', '-name', 'BogusFileEntity', '-description', 'Bogus data to test file upload', '-parentid', project_id, filename) file_entity_id = parse(r'Created/Updated entity:\s+(syn\d+)\s+', output) # Verify that we stored the file in Synapse f1 = syn.get(file_entity_id) fh = syn._getFileHandle(f1.dataFileHandleId) assert_equals(fh['concreteType'], 'org.sagebionetworks.repo.model.file.S3FileHandle') # Get File from the command line output = run('synapse', '--skip-checks', 'get', file_entity_id) downloaded_filename = parse(r'Downloaded file:\s+(.*)', output) schedule_for_cleanup(downloaded_filename) assert_true(os.path.exists(downloaded_filename)) assert_true(filecmp.cmp(filename, downloaded_filename)) # Update the File filename = utils.make_bogus_data_file() schedule_for_cleanup(filename) output = run('synapse', '--skip-checks', 'store', '--id', file_entity_id, filename) # Get the File again output = run('synapse', '--skip-checks', 'get', file_entity_id) downloaded_filename = parse(r'Downloaded file:\s+(.*)', output) schedule_for_cleanup(downloaded_filename) assert_true(os.path.exists(downloaded_filename)) assert_true(filecmp.cmp(filename, downloaded_filename)) # Move the file to new folder folder = syn.store(synapseclient.Folder(parentId=project_id)) output = run('synapse', 'mv', '--id', file_entity_id, '--parentid', folder.id) movedFile = syn.get(file_entity_id, downloadFile=False) assert_equals(movedFile.parentId, folder.id) # Test Provenance repo_url = 'https://github.com/Sage-Bionetworks/synapsePythonClient' output = run('synapse', '--skip-checks', 'set-provenance', '-id', file_entity_id, '-name', 'TestActivity', '-description', 'A very excellent provenance', '-used', file_entity_id, '-executed', repo_url) output = run('synapse', '--skip-checks', 'get-provenance', '--id', file_entity_id) activity = json.loads(output) assert_equals(activity['name'], 'TestActivity') assert_equals(activity['description'], 'A very excellent provenance') used = utils._find_used(activity, lambda used: 'reference' in used) assert_equals(used['reference']['targetId'], file_entity_id) used = utils._find_used(activity, lambda used: 'url' in used) assert_equals(used['url'], repo_url) assert_true(used['wasExecuted']) # Note: 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' # Test external file handle output = run('synapse', '--skip-checks', 'add', '-name', 'Singapore', '-description', 'A nice picture of Singapore', '-parentid', project_id, singapore_url) exteral_entity_id = parse(r'Created/Updated entity:\s+(syn\d+)\s+', output) # Verify that we created an external file handle f2 = syn.get(exteral_entity_id) fh = syn._getFileHandle(f2.dataFileHandleId) assert_equals(fh['concreteType'], 'org.sagebionetworks.repo.model.file.ExternalFileHandle') output = run('synapse', '--skip-checks', 'get', exteral_entity_id) downloaded_filename = parse(r'Downloaded file:\s+(.*)', output) schedule_for_cleanup(downloaded_filename) assert_true(os.path.exists(downloaded_filename)) # Delete the Project run('synapse', '--skip-checks', 'delete', project_id)
def test_command_line_client(): # Create a Project output = run('synapse', '--skip-checks', 'create', '-name', str(uuid.uuid4()), '-description', 'test of command line client', 'Project') project_id = parse(r'Created entity:\s+(syn\d+)\s+', output) schedule_for_cleanup(project_id) # Create a File filename = utils.make_bogus_data_file() schedule_for_cleanup(filename) output = run('synapse', '--skip-checks', 'add', '-name', 'BogusFileEntity', '-description', 'Bogus data to test file upload', '-parentid', project_id, filename) file_entity_id = parse(r'Created/Updated entity:\s+(syn\d+)\s+', output) # Verify that we stored the file in Synapse f1 = syn.get(file_entity_id) fh = syn._getFileHandle(f1.dataFileHandleId) assert fh['concreteType'] == 'org.sagebionetworks.repo.model.file.S3FileHandle' # Get File from the command line output = run('synapse', '--skip-checks', 'get', file_entity_id) downloaded_filename = parse(r'Creating\s+(.*)', output) schedule_for_cleanup(downloaded_filename) assert os.path.exists(downloaded_filename) assert filecmp.cmp(filename, downloaded_filename) # Update the File filename = utils.make_bogus_data_file() schedule_for_cleanup(filename) output = run('synapse', '--skip-checks', 'update', '-id', file_entity_id, filename) updated_entity_id = parse(r'Updated entity:\s+(syn\d+)', output) # Get the File again output = run('synapse', '--skip-checks', 'get', file_entity_id) downloaded_filename = parse(r'Creating\s+(.*)', output) schedule_for_cleanup(downloaded_filename) assert os.path.exists(downloaded_filename) assert filecmp.cmp(filename, downloaded_filename) # Create a deprecated Data object filename = utils.make_bogus_data_file() schedule_for_cleanup(filename) output = run('synapse', '--skip-checks', 'add', '-name', 'BogusData', '-description', 'Bogus data to test file upload', '-type', 'Data', '-parentid', project_id, filename) data_entity_id = parse(r'Created/Updated entity:\s+(syn\d+)\s+', output) # Get the Data object back output = run('synapse', '--skip-checks', 'get', data_entity_id) downloaded_filename = parse(r'Creating\s+(.*)', output) schedule_for_cleanup(downloaded_filename) assert os.path.exists(downloaded_filename) assert filecmp.cmp(filename, downloaded_filename) # Update the Data object filename = utils.make_bogus_data_file() schedule_for_cleanup(filename) output = run('synapse', '--skip-checks', 'update', '-id', data_entity_id, filename) updated_entity_id = parse(r'Updated entity:\s+(syn\d+)', output) # Get the Data object back again output = run('synapse', '--skip-checks', 'get', updated_entity_id) downloaded_filename = parse(r'Creating\s+(.*)', output) schedule_for_cleanup(downloaded_filename) assert os.path.exists(downloaded_filename) assert filecmp.cmp(filename, downloaded_filename) # Test query output = run('synapse', '--skip-checks', 'query', 'select id, name from entity where parentId=="%s"' % project_id) assert 'BogusData' in output assert data_entity_id in output assert 'BogusFileEntity' in output assert file_entity_id in output # Test Provenance repo_url = 'https://github.com/Sage-Bionetworks/synapsePythonClient' output = run('synapse', '--skip-checks', 'set-provenance', '-id', file_entity_id, '-name', 'TestActivity', '-description', 'A very excellent provenance', '-used', data_entity_id, '-executed', repo_url) activity_id = parse(r'Set provenance record (\d+) on entity syn\d+', output) output = run('synapse', '--skip-checks', 'get-provenance', '-id', file_entity_id) activity = json.loads(output) assert activity['name'] == 'TestActivity' assert activity['description'] == 'A very excellent provenance' used = utils._find_used(activity, lambda used: 'reference' in used) assert used['reference']['targetId'] == data_entity_id used = utils._find_used(activity, lambda used: 'url' in used) assert used['url'] == repo_url assert used['wasExecuted'] == True # Note: 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' # Test external file handle output = run('synapse', '--skip-checks', 'add', '-name', 'Singapore', '-description', 'A nice picture of Singapore', '-type', 'File', '-parentid', project_id, singapore_url) exteral_entity_id = parse(r'Created/Updated entity:\s+(syn\d+)\s+', output) # Verify that we created an external file handle f2 = syn.get(exteral_entity_id) fh = syn._getFileHandle(f2.dataFileHandleId) assert fh['concreteType'] == 'org.sagebionetworks.repo.model.file.ExternalFileHandle' output = run('synapse', '--skip-checks', 'get', exteral_entity_id) downloaded_filename = parse(r'Creating\s+(.*)', output) schedule_for_cleanup(downloaded_filename) assert os.path.exists(downloaded_filename) # Delete the Project output = run('synapse', '--skip-checks', 'delete', project_id)
def test_command_line_client(): """Test command line client""" ## Note: ## In Windows, quoting with single-quotes, as in -name 'My entity name' ## seems to cause problems in the call to subprocess.check_output. Make ## sure to use double-quotes. ## create project output = run('synapse --skip-checks create -name "%s" -description "test of command line client" Project' % str(str(uuid.uuid4()))) project_id = parse(r'Created entity:\s+(syn\d+)\s+', output) schedule_for_cleanup(project_id) ## create file filename = utils.make_bogus_data_file() schedule_for_cleanup(filename) output = run('synapse --skip-checks add -name "BogusFileEntity" -description "Bogus data to test file upload" -parentid %s %s' % (project_id, filename,)) file_entity_id = parse(r'Created entity:\s+(syn\d+)\s+', output) ## verify that we stored the file in synapse f1 = syn.get(file_entity_id) fh = syn._getFileHandle(f1.dataFileHandleId) assert fh['concreteType'] == 'org.sagebionetworks.repo.model.file.S3FileHandle' ## get file output = run('synapse --skip-checks get %s' % file_entity_id) downloaded_filename = parse(r'creating\s+(.*)', output) schedule_for_cleanup(downloaded_filename) assert os.path.exists(downloaded_filename) assert filecmp.cmp(filename, downloaded_filename) ## update file filename = utils.make_bogus_data_file() schedule_for_cleanup(filename) output = run('synapse --skip-checks update -id %s %s' % (file_entity_id, filename,)) updated_entity_id = parse(r'Updated entity:\s+(syn\d+)', output) ## get file output = run('synapse --skip-checks get %s' % file_entity_id) downloaded_filename = parse(r'creating\s+(.*)', output) schedule_for_cleanup(downloaded_filename) assert os.path.exists(downloaded_filename) assert filecmp.cmp(filename, downloaded_filename) ## create data filename = utils.make_bogus_data_file() schedule_for_cleanup(filename) output = run('synapse --skip-checks add -name "BogusData" -description "Bogus data to test file upload" -type Data -parentid %s %s' % (project_id, filename,)) data_entity_id = parse(r'Created entity:\s+(syn\d+)\s+', output) ## get data output = run('synapse --skip-checks get %s' % data_entity_id) downloaded_filename = parse(r'creating\s+(.*)', output) schedule_for_cleanup(downloaded_filename) assert os.path.exists(downloaded_filename) assert filecmp.cmp(filename, downloaded_filename) ## update data filename = utils.make_bogus_data_file() schedule_for_cleanup(filename) output = run('synapse --skip-checks update -id %s %s' % (data_entity_id, filename,)) updated_entity_id = parse(r'Updated entity:\s+(syn\d+)', output) ## get data output = run('synapse --skip-checks get %s' % data_entity_id) downloaded_filename = parse(r'creating\s+(.*)', output) schedule_for_cleanup(downloaded_filename) assert os.path.exists(downloaded_filename) assert filecmp.cmp(filename, downloaded_filename) # query output = run('synapse --skip-checks query "select id, name from entity where parentId==\\"%s\\""' % project_id) assert 'BogusData' in output assert data_entity_id in output assert 'BogusFileEntity' in output assert file_entity_id in output # provenance repo_url = 'https://github.com/Sage-Bionetworks/synapsePythonClient' output = run('synapse --skip-checks set-provenance -id %s -name TestActivity -description "A very excellent provenance" -used %s -executed "%s"' % (file_entity_id, data_entity_id, repo_url,)) activity_id = parse(r'Set provenance record (\d+) on entity syn\d+', output) output = run('synapse --skip-checks get-provenance -id %s' % (file_entity_id,)) activity = json.loads(output) assert activity['name'] == 'TestActivity' assert activity['description'] == 'A very excellent provenance' used = utils._find_used(activity, lambda used: 'reference' in used) assert used['reference']['targetId'] == data_entity_id used = utils._find_used(activity, lambda used: 'url' in used) assert used['url'] == repo_url assert used['wasExecuted'] == True ## 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' ## test external file handle output = run('synapse --skip-checks add -name "Singapore" -description "A nice picture of Singapore" -type File -parentid %s %s' % (project_id, singapore_url,)) exteral_entity_id = parse(r'Created entity:\s+(syn\d+)\s+', output) ## verify that we created an external file handle f2 = syn.get(exteral_entity_id) fh = syn._getFileHandle(f2.dataFileHandleId) assert fh['concreteType'] == 'org.sagebionetworks.repo.model.file.ExternalFileHandle' output = run('synapse --skip-checks get %s' % exteral_entity_id) downloaded_filename = parse(r'creating\s+(.*)', output) schedule_for_cleanup(downloaded_filename) assert os.path.exists(downloaded_filename) ## delete project output = run('synapse --skip-checks delete %s' % project_id)
def test_command_line_client(): print("TESTING CMD LINE CLIENT") # Create a Project output = run('synapse', '--skip-checks', 'create', '-name', str(uuid.uuid4()), '-description', 'test of command line client', 'Project') project_id = parse(r'Created entity:\s+(syn\d+)\s+', output) schedule_for_cleanup(project_id) # Create a File filename = utils.make_bogus_data_file() schedule_for_cleanup(filename) output = run('synapse', '--skip-checks', 'add', '-name', 'BogusFileEntity', '-description', 'Bogus data to test file upload', '-parentid', project_id, filename) file_entity_id = parse(r'Created/Updated entity:\s+(syn\d+)\s+', output) # Verify that we stored the file in Synapse f1 = syn.get(file_entity_id) fh = syn._getFileHandle(f1.dataFileHandleId) assert_equals(fh['concreteType'], 'org.sagebionetworks.repo.model.file.S3FileHandle') # Get File from the command line output = run('synapse', '--skip-checks', 'get', file_entity_id) downloaded_filename = parse(r'Downloaded file:\s+(.*)', output) schedule_for_cleanup(downloaded_filename) assert_true(os.path.exists(downloaded_filename)) assert_true(filecmp.cmp(filename, downloaded_filename)) # Update the File filename = utils.make_bogus_data_file() schedule_for_cleanup(filename) output = run('synapse', '--skip-checks', 'store', '--id', file_entity_id, filename) # Get the File again output = run('synapse', '--skip-checks', 'get', file_entity_id) downloaded_filename = parse(r'Downloaded file:\s+(.*)', output) schedule_for_cleanup(downloaded_filename) assert_true(os.path.exists(downloaded_filename)) assert_true(filecmp.cmp(filename, downloaded_filename)) # Store the same file and don't force a new version # Get the existing file to determine it's current version current_file = syn.get(file_entity_id, downloadFile=False) current_version = current_file.versionNumber # Store it without forcing version output = run('synapse', '--skip-checks', 'store', '--noForceVersion', '--id', file_entity_id, filename) # Get the File again and check that the version did not change new_file = syn.get(file_entity_id, downloadFile=False) new_version = new_file.versionNumber assert_equals(current_version, new_version) # Move the file to new folder folder = syn.store(synapseclient.Folder(parentId=project_id)) output = run('synapse', 'mv', '--id', file_entity_id, '--parentid', folder.id) movedFile = syn.get(file_entity_id, downloadFile=False) assert_equals(movedFile.parentId, folder.id) # Test Provenance repo_url = 'https://github.com/Sage-Bionetworks/synapsePythonClient' output = run('synapse', '--skip-checks', 'set-provenance', '-id', file_entity_id, '-name', 'TestActivity', '-description', 'A very excellent provenance', '-used', file_entity_id, '-executed', repo_url) output = run('synapse', '--skip-checks', 'get-provenance', '--id', file_entity_id) activity = json.loads(output) assert_equals(activity['name'], 'TestActivity') assert_equals(activity['description'], 'A very excellent provenance') used = utils._find_used(activity, lambda used: 'reference' in used) assert_equals(used['reference']['targetId'], file_entity_id) used = utils._find_used(activity, lambda used: 'url' in used) assert_equals(used['url'], repo_url) assert_true(used['wasExecuted']) # Note: 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' # Test external file handle output = run('synapse', '--skip-checks', 'add', '-name', 'Singapore', '-description', 'A nice picture of Singapore', '-parentid', project_id, singapore_url) exteral_entity_id = parse(r'Created/Updated entity:\s+(syn\d+)\s+', output) # Verify that we created an external file handle f2 = syn.get(exteral_entity_id) fh = syn._getFileHandle(f2.dataFileHandleId) assert_equals(fh['concreteType'], 'org.sagebionetworks.repo.model.file.ExternalFileHandle') output = run('synapse', '--skip-checks', 'get', exteral_entity_id) downloaded_filename = parse(r'Downloaded file:\s+(.*)', output) schedule_for_cleanup(downloaded_filename) assert_true(os.path.exists(downloaded_filename)) # Delete the Project run('synapse', '--skip-checks', 'delete', project_id)
def test_command_line_client(): # Create a Project output = run('synapse', '--skip-checks', 'create', '-name', str(uuid.uuid4()), '-description', 'test of command line client', 'Project') project_id = parse(r'Created entity:\s+(syn\d+)\s+', output) schedule_for_cleanup(project_id) # Create a File filename = utils.make_bogus_data_file() schedule_for_cleanup(filename) output = run('synapse', '--skip-checks', 'add', '-name', 'BogusFileEntity', '-description', 'Bogus data to test file upload', '-parentid', project_id, filename) file_entity_id = parse(r'Created/Updated entity:\s+(syn\d+)\s+', output) # Verify that we stored the file in Synapse f1 = syn.get(file_entity_id) fh = syn._getFileHandle(f1.dataFileHandleId) assert fh[ 'concreteType'] == 'org.sagebionetworks.repo.model.file.S3FileHandle' # Get File from the command line output = run('synapse', '--skip-checks', 'get', file_entity_id) downloaded_filename = parse(r'Downloaded file:\s+(.*)', output) schedule_for_cleanup(downloaded_filename) assert os.path.exists(downloaded_filename) assert filecmp.cmp(filename, downloaded_filename) # Update the File filename = utils.make_bogus_data_file() schedule_for_cleanup(filename) output = run('synapse', '--skip-checks', 'store', '--id', file_entity_id, filename) updated_entity_id = parse(r'Created/Updated entity:\s+(syn\d+)', output) # Get the File again output = run('synapse', '--skip-checks', 'get', file_entity_id) downloaded_filename = parse(r'Downloaded file:\s+(.*)', output) schedule_for_cleanup(downloaded_filename) assert os.path.exists(downloaded_filename) assert filecmp.cmp(filename, downloaded_filename) # Test query output = run( 'synapse', '--skip-checks', 'query', 'select id, name from entity where parentId=="%s"' % project_id) assert 'BogusFileEntity' in output assert file_entity_id in output # Move the file to new folder folder = syn.store(synapseclient.Folder(parentId=project_id)) output = run('synapse', 'mv', '--id', file_entity_id, '--parentid', folder.id) downloaded_filename = parse(r'Moved\s+(.*)', output) movedFile = syn.get(file_entity_id, downloadFile=False) assert movedFile.parentId == folder.id # Test Provenance repo_url = 'https://github.com/Sage-Bionetworks/synapsePythonClient' output = run('synapse', '--skip-checks', 'set-provenance', '-id', file_entity_id, '-name', 'TestActivity', '-description', 'A very excellent provenance', '-used', file_entity_id, '-executed', repo_url) activity_id = parse(r'Set provenance record (\d+) on entity syn\d+', output) output = run('synapse', '--skip-checks', 'get-provenance', '--id', file_entity_id) activity = json.loads(output) assert activity['name'] == 'TestActivity' assert activity['description'] == 'A very excellent provenance' used = utils._find_used(activity, lambda used: 'reference' in used) assert used['reference']['targetId'] == file_entity_id used = utils._find_used(activity, lambda used: 'url' in used) assert used['url'] == repo_url assert used['wasExecuted'] == True # Note: 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' # Test external file handle output = run('synapse', '--skip-checks', 'add', '-name', 'Singapore', '-description', 'A nice picture of Singapore', '-parentid', project_id, singapore_url) exteral_entity_id = parse(r'Created/Updated entity:\s+(syn\d+)\s+', output) # Verify that we created an external file handle f2 = syn.get(exteral_entity_id) fh = syn._getFileHandle(f2.dataFileHandleId) assert fh[ 'concreteType'] == 'org.sagebionetworks.repo.model.file.ExternalFileHandle' output = run('synapse', '--skip-checks', 'get', exteral_entity_id) downloaded_filename = parse(r'Downloaded file:\s+(.*)', output) schedule_for_cleanup(downloaded_filename) assert os.path.exists(downloaded_filename) # Delete the Project output = run('synapse', '--skip-checks', 'delete', project_id)
def test_command_line_client(): """Test command line client""" ## Note: ## In Windows, quoting with single-quotes, as in -name 'My entity name' ## seems to cause problems in the call to subprocess.check_output. Make ## sure to use double-quotes. ## create project output = run( 'synapse create -name "%s" -description "test of command line client" Project' % str(str(uuid.uuid4()))) project_id = parse(r'Created entity:\s+(syn\d+)\s+', output) schedule_for_cleanup(project_id) ## create file filename = utils.make_bogus_data_file() schedule_for_cleanup(filename) output = run( 'synapse add -name "BogusFileEntity" -description "Bogus data to test file upload" -parentid %s %s' % ( project_id, filename, )) file_entity_id = parse(r'Created entity:\s+(syn\d+)\s+', output) ## verify that we stored the file in synapse f1 = syn.get(file_entity_id) fh = syn._getFileHandle(f1.dataFileHandleId) assert fh[ 'concreteType'] == 'org.sagebionetworks.repo.model.file.S3FileHandle' ## get file output = run('synapse get %s' % file_entity_id) downloaded_filename = parse(r'creating\s+(.*)', output) schedule_for_cleanup(downloaded_filename) assert os.path.exists(downloaded_filename) assert filecmp.cmp(filename, downloaded_filename) ## update file filename = utils.make_bogus_data_file() schedule_for_cleanup(filename) output = run('synapse update -id %s %s' % ( file_entity_id, filename, )) updated_entity_id = parse(r'Updated entity:\s+(syn\d+)', output) ## get file output = run('synapse get %s' % file_entity_id) downloaded_filename = parse(r'creating\s+(.*)', output) schedule_for_cleanup(downloaded_filename) assert os.path.exists(downloaded_filename) assert filecmp.cmp(filename, downloaded_filename) ## create data filename = utils.make_bogus_data_file() schedule_for_cleanup(filename) output = run( 'synapse add -name "BogusData" -description "Bogus data to test file upload" -type Data -parentid %s %s' % ( project_id, filename, )) data_entity_id = parse(r'Created entity:\s+(syn\d+)\s+', output) ## get data output = run('synapse get %s' % data_entity_id) downloaded_filename = parse(r'creating\s+(.*)', output) schedule_for_cleanup(downloaded_filename) assert os.path.exists(downloaded_filename) assert filecmp.cmp(filename, downloaded_filename) ## update data filename = utils.make_bogus_data_file() schedule_for_cleanup(filename) output = run('synapse update -id %s %s' % ( data_entity_id, filename, )) updated_entity_id = parse(r'Updated entity:\s+(syn\d+)', output) ## get data output = run('synapse get %s' % data_entity_id) downloaded_filename = parse(r'creating\s+(.*)', output) schedule_for_cleanup(downloaded_filename) assert os.path.exists(downloaded_filename) assert filecmp.cmp(filename, downloaded_filename) # query output = run( 'synapse query "select id, name from entity where parentId==\\"%s\\""' % project_id) assert 'BogusData' in output assert data_entity_id in output assert 'BogusFileEntity' in output assert file_entity_id in output # provenance repo_url = 'https://github.com/Sage-Bionetworks/synapsePythonClient' output = run( 'synapse set-provenance -id %s -name TestActivity -description "A very excellent provenance" -used %s -executed "%s"' % ( file_entity_id, data_entity_id, repo_url, )) activity_id = parse(r'Set provenance record (\d+) on entity syn\d+', output) output = run('synapse get-provenance -id %s' % (file_entity_id, )) activity = json.loads(output) assert activity['name'] == 'TestActivity' assert activity['description'] == 'A very excellent provenance' used = utils._find_used(activity, lambda used: 'reference' in used) assert used['reference']['targetId'] == data_entity_id used = utils._find_used(activity, lambda used: 'url' in used) assert used['url'] == repo_url assert used['wasExecuted'] == True ## 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' ## test external file handle output = run( 'synapse add -name "Singapore" -description "A nice picture of Singapore" -type File -parentid %s %s' % ( project_id, singapore_url, )) exteral_entity_id = parse(r'Created entity:\s+(syn\d+)\s+', output) ## verify that we created an external file handle f2 = syn.get(exteral_entity_id) fh = syn._getFileHandle(f2.dataFileHandleId) assert fh[ 'concreteType'] == 'org.sagebionetworks.repo.model.file.ExternalFileHandle' output = run('synapse get %s' % exteral_entity_id) downloaded_filename = parse(r'creating\s+(.*)', output) schedule_for_cleanup(downloaded_filename) assert os.path.exists(downloaded_filename) ## delete project output = run('synapse delete %s' % project_id)