def test_teams(syn, project, schedule_for_cleanup): name = "My Uniquely Named Team " + str(uuid.uuid4()) team = syn.store(Team(name=name, description="A fake team for testing...")) schedule_for_cleanup(team) # not logged in, teams are public anonymous_syn = Synapse() found_team = anonymous_syn.getTeam(team.id) assert team == found_team p = syn.getUserProfile() found = None for m in anonymous_syn.getTeamMembers(team): if m.member.ownerId == p.ownerId: found = m break assert found is not None, "Couldn't find user {} in team".format( p.userName) # needs to be retried 'cause appending to the search index is asynchronous tries = 8 sleep_time = 1 found_team = None while tries > 0: try: found_team = anonymous_syn.getTeam(name) break except ValueError: tries -= 1 if tries > 0: time.sleep(sleep_time) sleep_time *= 2 assert team == found_team
def test_teams(syn): unique_name = "Team Gnarly Rad " + str(uuid.uuid4()) team = Team(name=unique_name, description="A gnarly rad team", canPublicJoin=True) team = syn.store(team) team2 = syn.getTeam(team.id) assert team == team2 # Asynchronously populates index, so wait 'til it's there retry = 0 backoff = 0.2 while retry < 10: retry += 1 time.sleep(backoff) backoff *= 2 found_teams = list(syn._findTeam(team.name)) if len(found_teams) > 0: break else: print("Failed to create team. May not be a real error.") syn.delete(team) assert team == found_teams[0]
def find_objects(uuid): """Based on the given UUID (as a string), find demo artifacts""" found_objects = {} results = list( syn.chunkedQuery('select id from project where project.name == "%s"' % (CHALLENGE_PROJECT_NAME + " " + uuid))) if results: found_objects['challenge_project'] = syn.get(results[0]['project.id']) results = list( syn.chunkedQuery('select id from project where project.name == "%s"' % (PARTICIPANT_PROJECT_NAME + " " + uuid))) if results: found_objects['participant_project'] = syn.get( results[0]['project.id']) response = syn.restGET("/teams?fragment=" + urllib.quote(CHALLENGE_PROJECT_NAME + " " + uuid + " Participants")) if len(response['results']) > 0: found_objects['participants_team'] = Team(**response['results'][0]) else: warnings.warn("Couldn't find team: %s" % (CHALLENGE_PROJECT_NAME + " " + uuid + " Participants")) response = syn.restGET("/teams?fragment=" + urllib.quote(CHALLENGE_PROJECT_NAME + " " + uuid + " Administrators")) if len(response['results']) > 0: found_objects['admin_team'] = Team(**response['results'][0]) else: warnings.warn( "Couldn't find team: %s" % (CHALLENGE_PROJECT_NAME + " " + uuid + " Administrators")) response = syn.restGET("/teams?fragment=" + urllib.quote("My team " + uuid)) if len(response['results']) > 0: found_objects['my_team'] = Team(**response['results'][0]) else: warnings.warn("Couldn't find team: %s" % ("My team " + uuid)) return found_objects
def create_team(self, **kwargs): """Creates a new Team and adds it to the trash queue.""" if 'name' not in kwargs: kwargs['name'] = self.uniq_name(prefix=kwargs.get('prefix', '')) kwargs.pop('prefix', None) team = SynapseProxy.client().store(Team(**kwargs)) self.dispose_of(team) return team
def setup(self): self.eval_id = 111 self.team_id = 123 self.team = Team(name="test", id=self.team_id) self.hash = 3 self.member_eligible = { 'isEligible': True, 'isRegistered': True, 'isQuotaFilled': False, 'principalId': 222, 'hasConflictingSubmission': False } self.member_not_registered = { 'isEligible': False, 'isRegistered': False, 'isQuotaFilled': False, 'principalId': 223, 'hasConflictingSubmission': False } self.member_quota_filled = { 'isEligible': False, 'isRegistered': True, 'isQuotaFilled': True, 'principalId': 224, 'hasConflictingSubmission': False } self.member_has_conflict = { 'isEligible': True, 'isRegistered': True, 'isQuotaFilled': False, 'principalId': 225, 'hasConflictingSubmission': True } self.eligibility = { 'teamId': self.team_id, 'evaluationId': self.eval_id, 'teamEligibility': { 'isEligible': True, 'isRegistered': True, 'isQuotaFilled': False }, 'membersEligibility': [ self.member_eligible, self.member_not_registered, self.member_quota_filled, self.member_has_conflict ], 'eligibilityStateHash': self.hash } self.patch_restGET = patch.object(syn, "restGET", return_value=self.eligibility) self.mock_restGET = self.patch_restGET.start()
def test_dispose(syn_client, syn_test_helper, temp_file): project = syn_client.store(Project(name=syn_test_helper.uniq_name())) folder = syn_client.store( Folder(name=syn_test_helper.uniq_name(prefix='Folder '), parent=project)) file = syn_client.store( File(name=syn_test_helper.uniq_name(prefix='File '), path=temp_file, parent=folder)) team = syn_client.store( Team(name=syn_test_helper.uniq_name(prefix='Team '))) wiki = syn_client.store( Wiki(title=syn_test_helper.uniq_name(prefix='Wiki '), owner=project)) wikiChild = syn_client.store( Wiki(title=syn_test_helper.uniq_name(prefix='Wiki Child '), owner=project, parentWikiId=wiki.id)) syn_objects = [project, folder, file, team, wiki, wikiChild] for syn_obj in syn_objects: syn_test_helper.dispose_of(syn_obj) assert syn_obj in syn_test_helper._trash syn_test_helper.dispose() assert len(syn_test_helper._trash) == 0 for syn_obj in syn_objects: with pytest.raises( synapseclient.core.exceptions.SynapseHTTPError) as ex: if isinstance(syn_obj, Wiki): syn_client.getWiki(syn_obj) elif isinstance(syn_obj, Team): syn_client.getTeam(syn_obj.id) else: syn_client.get(syn_obj, downloadFile=False) err_str = str(ex.value) assert "Not Found" in err_str or "cannot be found" in err_str or "is in trash can" in err_str or "does not exist" in err_str try: os.remove(temp_file) except: pass
def get_or_create_team(self, **kwargs) -> Team: """Gets an existing team by name or creates a new one. Args: Same arguments as synapseclient.Team Returns: A synapseclient.Team """ team = Team(**kwargs) team = self._find_by_obj_or_create(team) self.logger.info('{} Team {} ({})'.format(self._update_str, team.name, team.id)) return team
def create_team(self, **kwargs): """Creates a new Team and adds it to the trash queue.""" if 'name' not in kwargs: kwargs['name'] = self.uniq_name(prefix=kwargs.get('prefix', '')) kwargs.pop('prefix', None) team = SynapseProxy.client().store(Team(**kwargs)) # Wait for the team to be available in Synapse. tries = 0 while True: tries += 1 try: SynapseProxy.client().getTeam(team.name) break except ValueError: if tries >= 10: raise Exception('Timed out waiting for Team to be available in Synapse.') else: time.sleep(3) self.dispose_of(team) return team
def test(): ids = {} # teams are really just permissions groups, and we need to create a group # that has permissions to submit to the challenge try: participants_team = syn.getTeam("Participants Group") print "Loaded team %s %s" % (participants_team.id, participants_team.name) except: participants_team = syn.store( Team( name='Participants Group', description='A team for people who have joined the challenge')) print syn.setPermissions(project, participants_team.id, ['READ', 'DOWNLOAD']) for evaluation in find_or_create_evaluations(): print syn.setPermissions(evaluation, participants_team.id, [ 'CREATE', 'READ', 'UPDATE', 'PARTICIPATE', 'SUBMIT', 'READ_PRIVATE_SUBMISSION' ]) print "Created team %s %s" % (participants_team.id, participants_team.name) ids['participants_team'] = participants_team.id ## the challenge object associates the challenge project with the ## participants team try: challenge_object = syn.restGET( "/entity/{ID}/challenge".format(ID=urllib.quote(project.id))) print "Loaded challenge object: %s" % challenge_object['id'] except: raise challenge_json = { 'participantTeamId': participants_team.id, 'projectId': project.id } challenge_object = syn.restPOST("/challenge", body=json.dumps(challenge_json)) print "Created challenge object: %s" % challenge_object['id'] ids['challenge_object'] = challenge_object['id'] # get a submission team test_submissions_teams = syn.restGET( '/challenge/{challengeId}/challengeTeam'.format( challengeId=challenge_object['id'])) if test_submissions_teams['totalNumberOfResults'] == 0: # create a team that will make submissions test_submissions_team = syn.store( Team(name="Test Submission Team", description='A team to test the submissions machinery')) # register team with challenge request_body = { 'teamId': test_submissions_team['teamId'], 'challengeId': challenge_object['id'] } test_submissions_team = syn.restPOST( '/challenge/{challengeId}/challengeTeam'.format( challengeId=challenge_object['id']), json.dumps(request_body)) elif test_submissions_teams['totalNumberOfResults'] >= 1: test_submissions_team = test_submissions_teams['results'][0] ids['test_submissions_team'] = test_submissions_team['id'] print "Restored a team to test submissions: %s" % ( test_submissions_team['id'], ) else: print test_submissions_teams assert False, "We only expect a single test submission user." # Create the participant project try: participant_project = syn.get("syn6170604") except: raise participant_project = syn.store( Project(name="Test Submission Team Project")) print "Created project %s %s" % (participant_project.id, participant_project.name) for evaluation in find_or_create_evaluations(): participant_file = syn.store( File(synapseclient.utils.make_bogus_data_file(), parent=participant_project)) syn.submit( evaluation=evaluation, entity=participant_file, team="Participants Group", ) for evaluation in find_or_create_evaluations(): if evaluation.name == 'Between Cell Type Regression': score_submission = score_regression_submission else: score_submission = score_classification_submission validate(evaluation) score(evaluation, score_submission) return
def set_up(): try: uuid_suffix = " " + str(uuid.uuid4()) # Create the Challenge Project challenge_project = syn.store( Project(name=CHALLENGE_PROJECT_NAME + uuid_suffix)) print "Created project %s %s" % (challenge_project.id, challenge_project.name) evaluation = syn.store( Evaluation( name=challenge_project.name, contentSource=challenge_project.id, status="OPEN", submissionInstructionsMessage= "To submit to the XYZ Challenge, send a tab-delimited file as described here: https://...", submissionReceiptMessage= "Your submission has been received. For further information, consult the leader board at https://..." ), quota=dict( numberOfRounds=1, roundDurationMillis=1000 * 60 * 60 * 48, ## 48 hours submissionLimit=20, firstRoundStart=datetime.now().strftime( synapseclient.utils.ISO_FORMAT))) print "Created Evaluation %s %s" % (evaluation.id, evaluation.name) # Create teams for participants and administrators participants_team = syn.store( Team( name=CHALLENGE_PROJECT_NAME + uuid_suffix + ' Participants', description='A team for people who have joined the challenge')) print "Created team %s %s" % (participants_team.id, participants_team.name) admin_team = syn.store( Team(name=CHALLENGE_PROJECT_NAME + uuid_suffix + ' Administrators', description='A team for challenge administrators')) print "Created team %s %s" % (admin_team.id, admin_team.name) # give the teams permissions on challenge artifacts # see: http://rest.synapse.org/org/sagebionetworks/repo/model/ACCESS_TYPE.html # see: http://rest.synapse.org/org/sagebionetworks/evaluation/model/UserEvaluationPermissions.html syn.setPermissions(challenge_project, admin_team.id, [ 'CREATE', 'READ', 'UPDATE', 'DELETE', 'CHANGE_PERMISSIONS', 'DOWNLOAD', 'UPLOAD' ]) syn.setPermissions(challenge_project, participants_team.id, ['READ', 'DOWNLOAD']) syn.setPermissions(evaluation, admin_team.id, [ 'CREATE', 'READ', 'UPDATE', 'DELETE', 'CHANGE_PERMISSIONS', 'DOWNLOAD', 'PARTICIPATE', 'SUBMIT', 'DELETE_SUBMISSION', 'UPDATE_SUBMISSION', 'READ_PRIVATE_SUBMISSION' ]) syn.setPermissions(evaluation, participants_team.id, [ 'CREATE', 'READ', 'UPDATE', 'PARTICIPATE', 'SUBMIT', 'READ_PRIVATE_SUBMISSION' ]) ## the challenge object associates the challenge project with the ## participants team challenge_object = create_challenge_object(challenge_project, participants_team) # create a team that will make submissions my_team = syn.store( Team(name="My team" + uuid_suffix, description='A team to make submissions')) # register team with challenge request_body = { 'teamId': my_team.id, 'challengeId': challenge_object.id } syn.restPOST( '/challenge/{challengeId}/challengeTeam'.format( challengeId=challenge_object.id), json.dumps(request_body)) # Create the participant project participant_project = syn.store( Project(name=PARTICIPANT_PROJECT_NAME + uuid_suffix)) print "Created project %s %s" % (participant_project.id, participant_project.name) participant_file = syn.store( File(synapseclient.utils.make_bogus_data_file(), parent=participant_project)) # Write challenge config file, which is just an ordinary python # script that can be manually edited later. current_user = syn.getUserProfile() write_config(challenge_syn_id=challenge_project.id, challenge_name=CHALLENGE_PROJECT_NAME, admin_user_ids=[current_user.ownerId], evaluation_queues=[evaluation]) return dict(challenge_project=challenge_project, challenge_object=challenge_object, evaluation=evaluation, participant_project=participant_project, participant_file=participant_file, participants_team=participants_team, admin_team=admin_team, my_team=my_team, uuid_suffix=uuid_suffix) except Exception as ex: tear_down(locals()) raise
def create_team(name, description): return syn.store( Team(name=name, description=description, canPublicJoin=True))