def testConvertApprovals(self): """Test we can convert ApprovalValues.""" cnxn = None config = tracker_bizobj.MakeDefaultProjectIssueConfig(789) config.field_defs = [ tracker_bizobj.MakeFieldDef( 1, 789, 'DesignReview', tracker_pb2.FieldTypes.APPROVAL_TYPE, None, None, False, False, False, None, None, None, False, None, None, None, None, 'doc', False), tracker_bizobj.MakeFieldDef( 2, 789, 'PrivacyReview', tracker_pb2.FieldTypes.APPROVAL_TYPE, None, None, False, False, False, 0, 99, None, False, None, None, None, None, 'doc', False), tracker_bizobj.MakeFieldDef( 5, 789, 'UXReview', tracker_pb2.FieldTypes.APPROVAL_TYPE, None, None, False, False, False, None, None, None, False, None, None, None, None, 'doc', False), tracker_bizobj.MakeFieldDef( 6, 789, 'Homepage', tracker_pb2.FieldTypes.URL_TYPE, None, None, False, False, False, None, None, None, False, None, None, None, None, 'doc', False), ] phases = [ tracker_pb2.Phase(phase_id=1), tracker_pb2.Phase(phase_id=2, name="JustAPhase", rank=3), ] ts = 1536260059 expected = [ api_pb2_v1.Approval( approvalName="DesignReview", approvers=[self.person_1], setter=self.person_1, status=api_pb2_v1.ApprovalStatus.needsReview, setOn=datetime.datetime.fromtimestamp(ts), ), api_pb2_v1.Approval( approvalName="UXReview", approvers=[self.person_1], status=api_pb2_v1.ApprovalStatus.notSet, phaseName="JustAPhase", ), ] avs = [ tracker_pb2.ApprovalValue( approval_id=1, approver_ids=[111], setter_id=111, status=tracker_pb2.ApprovalStatus.NEEDS_REVIEW, set_on=ts), tracker_pb2.ApprovalValue( approval_id=5, approver_ids=[111], phase_id=2) ] actual = api_pb2_v1_helpers.convert_approvals( cnxn, avs, self.services, config, phases) self.assertEqual(actual, expected)
def testConvertApprovals_errors(self): """we dont crash on bad requests.""" cnxn = None config = tracker_bizobj.MakeDefaultProjectIssueConfig(789) config.field_defs = [ tracker_bizobj.MakeFieldDef(1, 789, 'DesignReview', tracker_pb2.FieldTypes.APPROVAL_TYPE, None, None, False, False, False, None, None, None, False, None, None, None, None, 'doc', False), tracker_bizobj.MakeFieldDef(5, 789, 'UXReview', tracker_pb2.FieldTypes.APPROVAL_TYPE, None, None, False, False, False, None, None, None, False, None, None, None, None, 'doc', False), tracker_bizobj.MakeFieldDef(3, 789, 'DesignDoc', tracker_pb2.FieldTypes.URL_TYPE, None, None, False, False, False, 0, 99, None, False, None, None, None, None, 'doc', False), ] phases = [] avs = [ tracker_pb2.ApprovalValue(approval_id=1, approver_ids=[111]), # phase does not exist tracker_pb2.ApprovalValue(approval_id=2, phase_id=2), tracker_pb2.ApprovalValue( approval_id=3), # field 3 is not an approval tracker_pb2.ApprovalValue(approval_id=4), # field 4 does not exist ] expected = [ api_pb2_v1.Approval(approvalName="DesignReview", approvers=[self.person_1], status=api_pb2_v1.ApprovalStatus.notSet) ] actual = api_pb2_v1_helpers.convert_approvals(cnxn, avs, self.services, config, phases) self.assertEqual(actual, expected)
def convert_approvals(cnxn, approval_values, services, config, phases): """Convert an Issue's Monorail ApprovalValue PBs to API Approval""" fds_by_id = {fd.field_id: fd for fd in config.field_defs} phases_by_id = {phase.phase_id: phase for phase in phases} approvals = [] for av in approval_values: approval_fd = fds_by_id.get(av.approval_id) if approval_fd is None: logging.warning('Approval (%d) does not exist' % av.approval_id) continue if approval_fd.field_type is not tracker_pb2.FieldTypes.APPROVAL_TYPE: logging.warning( 'field %s has unexpected field_type: %s' % (approval_fd.field_name, approval_fd.field_type.name)) continue approval = api_pb2_v1.Approval() approval.approvalName = approval_fd.field_name approvers = [ convert_person(approver_id, cnxn, services) for approver_id in av.approver_ids ] approval.approvers = [approver for approver in approvers if approver] approval.status = api_pb2_v1.ApprovalStatus(av.status.number) if av.setter_id: approval.setter = convert_person(av.setter_id, cnxn, services) if av.set_on: approval.setOn = datetime.datetime.fromtimestamp(av.set_on) if av.phase_id: try: approval.phaseName = phases_by_id[av.phase_id].name except KeyError: logging.warning('phase %d not found in given phases list' % av.phase_id) approvals.append(approval) return approvals
def testConvertIssue(self): """Convert an internal Issue PB to an IssueWrapper API PB.""" self.services.project.TestAddProject( 'test-project', owner_ids=[2], project_id=12345) self.services.user.TestAddUser('*****@*****.**', 111) mar = mock.Mock() mar.cnxn = None mar.project_name = 'test-project' mar.project_id = 12345 mar.auth.effective_ids = {111} mar.perms = permissions.READ_ONLY_PERMISSIONSET mar.profiler = profiler.Profiler() mar.config = tracker_bizobj.MakeDefaultProjectIssueConfig(12345) mar.config.field_defs = [ tracker_bizobj.MakeFieldDef( 1, 12345, 'EstDays', tracker_pb2.FieldTypes.INT_TYPE, None, None, False, False, False, None, None, None, False, None, None, None, None, 'doc', False, approval_id=2), tracker_bizobj.MakeFieldDef( 2, 12345, 'DesignReview', tracker_pb2.FieldTypes.APPROVAL_TYPE, None, None, False, False, False, None, None, None, False, None, None, None, None, 'doc', False), tracker_bizobj.MakeFieldDef( 3, 12345, 'StringField', tracker_pb2.FieldTypes.STR_TYPE, None, None, False, False, False, None, None, None, False, None, None, None, None, 'doc', False), tracker_bizobj.MakeFieldDef( 4, 12345, 'DressReview', tracker_pb2.FieldTypes.APPROVAL_TYPE, None, None, False, False, False, None, None, None, False, None, None, None, None, 'doc', False), ] self.services.config.StoreConfig(mar.cnxn, mar.config) now = 1472067725 now_dt = datetime.datetime.fromtimestamp(now) fvs = [ tracker_bizobj.MakeFieldValue( 1, 4, None, None, None, None, False, phase_id=4), tracker_bizobj.MakeFieldValue( 3, None, 'string', None, None, None, False, phase_id=4), # missing phase tracker_bizobj.MakeFieldValue( 3, None, u'\xe2\x9d\xa4\xef\xb8\x8f', None, None, None, False, phase_id=2), ] phases = [ tracker_pb2.Phase(phase_id=3, name="JustAPhase", rank=4), tracker_pb2.Phase(phase_id=4, name="NotAPhase", rank=9) ] approval_values = [ tracker_pb2.ApprovalValue( approval_id=2, phase_id=3, approver_ids=[111]), tracker_pb2.ApprovalValue(approval_id=4, approver_ids=[111]) ] issue = fake.MakeTestIssue( 12345, 1, 'one', 'New', 111, field_values=fvs, approval_values=approval_values, phases=phases) issue.opened_timestamp = now issue.owner_modified_timestamp = now issue.status_modified_timestamp = now issue.component_modified_timestamp = now # TODO(jrobbins): set up a lot more fields. for cls in [api_pb2_v1.IssueWrapper, api_pb2_v1.IssuesGetInsertResponse]: result = api_pb2_v1_helpers.convert_issue(cls, issue, mar, self.services) self.assertEquals(1, result.id) self.assertEquals('one', result.title) self.assertEquals('one', result.summary) self.assertEquals(now_dt, result.published) self.assertEquals(now_dt, result.owner_modified) self.assertEquals(now_dt, result.status_modified) self.assertEquals(now_dt, result.component_modified) self.assertEquals( result.fieldValues, [api_pb2_v1.FieldValue( fieldName='EstDays', fieldValue='4', approvalName='DesignReview', derived=False), api_pb2_v1.FieldValue(fieldName='StringField', fieldValue='string', phaseName="NotAPhase", derived=False), api_pb2_v1.FieldValue(fieldName='StringField', fieldValue=u'\xe2\x9d\xa4\xef\xb8\x8f', derived=False), ] ) self.assertEqual( result.approvalValues, [api_pb2_v1.Approval( approvalName="DesignReview", approvers=[self.person_1], status=api_pb2_v1.ApprovalStatus.notSet, phaseName="JustAPhase", ), api_pb2_v1.Approval( approvalName="DressReview", approvers=[self.person_1], status=api_pb2_v1.ApprovalStatus.notSet, )] ) self.assertEqual( result.phases, [api_pb2_v1.Phase(phaseName="JustAPhase", rank=4), api_pb2_v1.Phase(phaseName="NotAPhase", rank=9) ])