def test_getNextJobStatus_wrong_type(self): """Only TranslationMergeJobs should result.""" #suppress job creation. with EventRecorder(): packaging = self.factory.makePackagingLink() TranslationSplitJob.forPackaging(packaging) self.assertIs(None, TranslationMergeJob.getNextJobStatus(packaging))
def test_init_notifies(self): """Creating a Packaging should generate an event.""" with EventRecorder() as recorder: packaging = Packaging() (event, ) = recorder.events self.assertIsInstance(event, ObjectCreatedEvent) self.assertIs(packaging, event.object)
def test_getNextJobStatus_order(self): """Status should order by id.""" with EventRecorder(): packaging = self.factory.makePackagingLink() job = TranslationMergeJob.forPackaging(packaging) job.start() TranslationMergeJob.forPackaging(packaging) self.assertEqual(JobStatus.RUNNING, TranslationMergeJob.getNextJobStatus(packaging))
def test_getNextJobStatus(self): """Should find next packaging job.""" #suppress job creation. with EventRecorder(): packaging = self.factory.makePackagingLink() self.assertIs(None, TranslationMergeJob.getNextJobStatus(packaging)) TranslationMergeJob.forPackaging(packaging) self.assertEqual(JobStatus.WAITING, TranslationMergeJob.getNextJobStatus(packaging))
def test_destroySelf_notifies(self): """destroySelf creates a notification.""" packaging = self.factory.makePackagingLink() user = self.factory.makePerson(karma=200) with person_logged_in(user): with EventRecorder() as recorder: removeSecurityProxy(packaging).destroySelf() (event, ) = recorder.events self.assertIsInstance(event, ObjectDeletedEvent) self.assertIs(removeSecurityProxy(packaging), event.object)
def test_attachments(self): # The attachment comment has no content and it does not notify. token = self.process_extra_data("""\ MIME-Version: 1.0 Content-type: multipart/mixed; boundary=boundary --boundary Content-disposition: attachment; filename='attachment1' Content-type: text/plain; charset=utf-8 This is an attachment. --boundary Content-disposition: attachment; filename='attachment2' Content-description: Attachment description. Content-type: text/plain; charset=ISO-8859-1 This is another attachment, with a description. --boundary-- """) view = self.create_initialized_view() view.publishTraverse(view.request, token) with EventRecorder() as recorder: view.submit_bug_action.success(self.get_form()) # Subscribers are only notified about the new bug event; # The extra comment for the attchments was silent. self.assertEqual(1, len(recorder.events)) self.assertEqual(view.added_bug, recorder.events[0].object) transaction.commit() bug = view.added_bug attachments = [at for at in bug.attachments_unpopulated] self.assertEqual(2, len(attachments)) attachment = attachments[0] self.assertEqual('attachment1', attachment.title) self.assertEqual('attachment1', attachment.libraryfile.filename) self.assertEqual('text/plain; charset=utf-8', attachment.libraryfile.mimetype) self.assertEqual('This is an attachment.\n\n', attachment.libraryfile.read()) self.assertEqual(2, bug.messages.count()) self.assertEqual(2, len(bug.messages[1].bugattachments)) notifications = [ no.message for no in view.request.response.notifications ] self.assertContentEqual([ '<p class="last">Thank you for your bug report.</p>', html_escape( 'The file "attachment1" was attached to the bug report.'), html_escape( 'The file "attachment2" was attached to the bug report.') ], notifications)
def test_deletePackaging_notifies(self): """Deleting a Packaging creates a notification.""" packaging_util = getUtility(IPackagingUtil) packaging = self.factory.makePackagingLink() user = self.factory.makePerson(karma=200) with person_logged_in(user): with EventRecorder() as recorder: packaging_util.deletePackaging(packaging.productseries, packaging.sourcepackagename, packaging.distroseries) (event, ) = recorder.events self.assertIsInstance(event, ObjectDeletedEvent) self.assertIs(removeSecurityProxy(packaging), event.object)
def test_getNextJobStatus_wrong_packaging(self): """Jobs on wrong packaging should be ignored.""" #suppress job creation. with EventRecorder(): packaging = self.factory.makePackagingLink() self.factory.makePackagingLink(productseries=packaging.productseries) self.assertIs(None, TranslationMergeJob.getNextJobStatus(packaging)) self.factory.makePackagingLink() self.factory.makePackagingLink(distroseries=packaging.distroseries) self.assertIs(None, TranslationMergeJob.getNextJobStatus(packaging)) TranslationMergeJob.create( sourcepackagename=packaging.sourcepackagename, distroseries=packaging.distroseries, productseries=self.factory.makeProductSeries()) self.assertIs(None, TranslationMergeJob.getNextJobStatus(packaging))
def test_description_and_comments(self): # The first extra text part is added to the desciption, all other # extra parts become additional bug messages. token = self.process_extra_data("""\ MIME-Version: 1.0 Content-type: multipart/mixed; boundary=boundary --boundary Content-disposition: inline Content-type: text/plain; charset=utf-8 Added to the description. --boundary Content-disposition: inline Content-type: text/plain; charset=utf-8 A bug comment. --boundary-- """) view = self.create_initialized_view() view.publishTraverse(view.request, token) self.assertEqual('Added to the description.', view.extra_data.extra_description) with EventRecorder() as recorder: view.submit_bug_action.success(self.get_form()) # Subscribers are only notified about the new bug event; # The extra comment for the attchments was silent. self.assertEqual(2, len(recorder.events)) bug_event, message_event = recorder.events transaction.commit() bug = view.added_bug self.assertEqual(bug, bug_event.object) self.assertEqual('test description\n\n' 'Added to the description.', bug.description) self.assertEqual(2, bug.messages.count()) self.assertEqual(bug.bug_messages[-1], message_event.object) notifications = [ no.message for no in view.request.response.notifications ] self.assertContentEqual([ '<p class="last">Thank you for your bug report.</p>', 'Additional information was added to the bug description.', 'A comment with additional information was added to the' ' bug report.' ], notifications)
def test_run_object_events(self): # While the job runs a single IObjectModifiedEvent is issued when the # preview diff has been calculated. bmp = self.createExampleBzrMerge()[0] job = UpdatePreviewDiffJob.create(bmp) self.factory.makeRevisionsForBranch(bmp.source_branch, count=1) bmp.source_branch.next_mirror_time = None with dbuser("merge-proposal-jobs"): with EventRecorder() as event_recorder: JobRunner([job]).runAll() bmp_object_events = [ event for event in event_recorder.events if (IObjectModifiedEvent.providedBy(event) and event.object == bmp) ] self.assertEqual(1, len(bmp_object_events), "Expected one event, got: %r" % bmp_object_events) self.assertEqual(["preview_diff"], bmp_object_events[0].edited_fields)
def test_getNextJobStatus_status(self): """Only RUNNING and WAITING jobs should influence status.""" #suppress job creation. with EventRecorder(): packaging = self.factory.makePackagingLink() job = TranslationMergeJob.forPackaging(packaging) job.start() self.assertEqual(JobStatus.RUNNING, TranslationMergeJob.getNextJobStatus(packaging)) job.fail() self.assertIs(None, TranslationMergeJob.getNextJobStatus(packaging)) job2 = TranslationMergeJob.forPackaging(packaging) job2.start() job2.complete() job3 = TranslationMergeJob.forPackaging(packaging) job3.suspend() self.assertIs(None, TranslationMergeJob.getNextJobStatus(packaging))
def test_generates_notification(self): obj = Thing(0) with EventRecorder() as recorder: with notify_modified(obj, ["attr"]): obj.attr = 1 self.assertThat( recorder.events, MatchesListwise([ MatchesAll( Provides(IObjectModifiedEvent), MatchesStructure( object=MatchesStructure(attr=Equals(1)), object_before_modification=MatchesAll( Provides(IThing), MatchesStructure(attr=Equals(0))), edited_fields=Equals(["attr"]), user=Provides(IUnauthenticatedPrincipal))), ]))
def test_different_user(self): obj = Thing(0) user = self.factory.makePerson() with EventRecorder() as recorder: with notify_modified(obj, ["attr"], user=user): obj.attr = 1 self.assertThat( recorder.events, MatchesListwise([ MatchesAll( Provides(IObjectModifiedEvent), MatchesStructure(object=MatchesStructure(attr=Equals(1)), object_before_modification=MatchesAll( Provides(IThing), MatchesStructure(attr=Equals(0))), edited_fields=Equals(["attr"]), user=Equals(user))), ]))
def test_mutate_edited_fields_within_block(self): obj = Thing(0) with EventRecorder() as recorder: edited_fields = set() with notify_modified(obj, edited_fields): obj.attr = 1 edited_fields.add("attr") self.assertThat( recorder.events, MatchesListwise([ MatchesAll( Provides(IObjectModifiedEvent), MatchesStructure( object=MatchesStructure(attr=Equals(1)), object_before_modification=MatchesAll( Provides(IThing), MatchesStructure(attr=Equals(0))), edited_fields=Equals(["attr"]), user=Provides(IUnauthenticatedPrincipal))), ]))
def test_setPackaging__change_existing_entry(self): """setPackaging() changes existing Packaging links.""" sourcepackage = self.factory.makeSourcePackage() productseries = self.factory.makeProductSeries() other_series = self.factory.makeProductSeries() user = self.factory.makePerson(karma=200) registrant = self.factory.makePerson() with EventRecorder() as recorder: with person_logged_in(user): sourcepackage.setPackaging(productseries, owner=registrant) sourcepackage.setPackaging(other_series, owner=registrant) packaging = sourcepackage.direct_packaging self.assertEqual(packaging.productseries, other_series) # The first call of setPackaging() created an ObjectCreatedEvent; # the second call created an ObjectDeletedEvent for the deletion # of the old packaging link, and another ObjectCreatedEvent # for the new Packaging. event1, event2, event3 = recorder.events self.assertIsInstance(event1, ObjectCreatedEvent) self.assertIsInstance(event2, ObjectDeletedEvent) self.assertIsInstance(event3, ObjectCreatedEvent)
def test_monitor(self): """\ `monitor` observes changes to a given merge proposal and issues `ObjectModifiedEvent` events if there are any. """ merge_proposal = self.factory.makeBranchMergeProposal() with EventRecorder() as event_recorder: # No event is issued when nothing is changed. with BranchMergeProposalDelta.monitor(merge_proposal): pass # Don't make changes. self.assertEqual(0, len(event_recorder.events)) # When one or more properties (of interest to # BranchMergeProposalDelta) are changed, a single event is issued. with BranchMergeProposalDelta.monitor(merge_proposal): merge_proposal.commit_message = "foo" merge_proposal.whiteboard = "bar" self.assertEqual(1, len(event_recorder.events)) [event] = event_recorder.events self.assertIsInstance(event, ObjectModifiedEvent) self.assertEqual(merge_proposal, event.object) self.assertContentEqual(["commit_message", "whiteboard"], event.edited_fields)
def _test_publication(self, publication, expected_transaction_log): # publication.finishReadOnlyRequest() issues an # IFinishReadOnlyRequestEvent and alters the transaction. fake_request = object() fake_object = object() fake_transaction = LoggingTransaction() with EventRecorder() as event_recorder: publication.finishReadOnlyRequest(fake_request, fake_object, fake_transaction) self.assertEqual(expected_transaction_log, fake_transaction.log) finish_events = [ event for event in event_recorder.events if IFinishReadOnlyRequestEvent.providedBy(event) ] self.assertEqual(1, len(finish_events), ("Expected only one IFinishReadOnlyRequestEvent, but " "got: %r" % finish_events)) [finish_event] = finish_events self.assertIs(fake_request, finish_event.request) self.assertIs(fake_object, finish_event.object)