def test_PlayerSkippingAroundWhenEntersCommBreakDoesntSkipCommercial(self): # Setup when(self.player).isPlaying().thenReturn(True) when(self.program).getCommercials().thenReturn( [CommercialBreak(500, 600)]) # close to beginning = within 2 secs from start of commercial when(self.player).getTime().thenReturn(501).thenReturn(502).thenReturn( 504).thenReturn(505) # mock a valid tracker history trackerHistory = [] for x in range(499, 500): trackerHistory.append(TrackerSample(time.time() + (x - 499), x)) when(self.tracker).getHistory(any()).thenReturn(trackerHistory) # Test skipper = TrackingCommercialSkipper(self.player, self.program, self.translator) skipper.onPlayBackStarted() time.sleep(1) when(self.player).isPlaying().thenReturn(False) skipper.onPlayBackStopped() # Verify verify(self.player, times(0)).seekTime(any())
def test_ensure_after_action_teardown_suppression_works_when_action_fails(self): task = mock(name="task", dependencies=[]) action_regular = mock(name="action_regular", execute_before=[], execute_after=["task"], teardown=False) when(action_regular).execute({}).thenRaise(ValueError("simulated action error")) action_teardown = mock(name="action_teardown", execute_before=[], execute_after=["task"], teardown=True) action_after_teardown = mock(name="action_after_teardown", execute_before=[], execute_after=["task"], teardown=False) self.execution_manager.register_action(action_regular) self.execution_manager.register_action(action_teardown) self.execution_manager.register_action(action_after_teardown) self.execution_manager.register_task(task) self.execution_manager.resolve_dependencies() try: self.execution_manager.execute_task(task) self.assertTrue(False, "should not have reached here") except Exception as e: self.assertEquals(type(e), ValueError) self.assertEquals(str(e), "simulated action error") verify(task).execute(any(), {}) verify(action_regular).execute({}) verify(action_teardown).execute({}) verify(action_after_teardown, times(0)).execute({})
def test_ensure_after_action_teardown_suppression_works_when_action_fails( self): task = mock(name="task", dependencies=[]) action_regular = mock(name="action_regular", execute_before=[], execute_after=["task"], teardown=False) when(action_regular).execute({}).thenRaise( ValueError("simulated action error")) action_teardown = mock(name="action_teardown", execute_before=[], execute_after=["task"], teardown=True) action_after_teardown = mock(name="action_after_teardown", execute_before=[], execute_after=["task"], teardown=False) self.execution_manager.register_action(action_regular) self.execution_manager.register_action(action_teardown) self.execution_manager.register_action(action_after_teardown) self.execution_manager.register_task(task) self.execution_manager.resolve_dependencies() try: self.execution_manager.execute_task(task) self.assertTrue(False, "should not have reached here") except Exception as e: self.assertEquals(type(e), ValueError) self.assertEquals(str(e), "simulated action error") verify(task).execute(any(), {}) verify(action_regular).execute({}) verify(action_teardown).execute({}) verify(action_after_teardown, times(0)).execute({})
def testStubsAndInvokesTwiceAndVerifies(self): theMock = mock() when(theMock).foo().thenReturn("foo") self.assertEquals("foo", theMock.foo()) self.assertEquals("foo", theMock.foo()) verify(theMock, times(2)).foo()
def testStubsVerifiesAndStubsAgain(self): theMock = mock() when(theMock).foo().thenReturn("foo") self.assertEquals("foo", theMock.foo()) verify(theMock).foo() when(theMock).foo().thenReturn("next foo") self.assertEquals("next foo", theMock.foo()) verify(theMock, times(2)).foo()
def test_ensure_action_registered_for_two_tasks_is_executed_only_once_if_single_attribute_is_present(self): spam = mock(name="spam", dependencies=[]) eggs = mock(name="eggs", dependencies=[]) self.execution_manager.register_task(spam, eggs) action = mock(name="action", execute_before=[], execute_after=["spam", "eggs"], only_once=True) self.execution_manager.register_action(action) self.execution_manager.resolve_dependencies() self.execution_manager.execute_execution_plan([spam, eggs]) verify(action, times(1)).execute(any())
def test_PlayerNeverEntersAnyCommBreaks(self): # Setup when(self.player).isPlaying().thenReturn(True) when(self.program).getCommercials().thenReturn([CommercialBreak(100, 200), CommercialBreak(600, 700)]) when(self.player).getTime().thenReturn(500) # Test skipper = TrackingCommercialSkipper(self.player, self.program, self.translator, self.settings) skipper.onPlayBackStarted() time.sleep(2) when(self.player).isPlaying().thenReturn(False) skipper.onPlayBackStopped() # Verify verify(self.player, times(0)).seekTime(any())
def test_RecordingWithNoCommBreaksDoesNothing(self): # Setup when(self.player).isPlaying().thenReturn(True) when(self.program).getCommercials().thenReturn([]) when(self.player).getTime().thenReturn(500) # Test skipper = TrackingCommercialSkipper(self.player, self.program, self.translator, self.settings) skipper.onPlayBackStarted() time.sleep(1) when(self.player).isPlaying().thenReturn(False) skipper.onPlayBackStopped() # Verify verify(self.player, times(0)).seekTime(any())
def test_RecordingWithNoCommBreaksDoesNothing(self): # Setup when(self.player).isPlaying().thenReturn(True) when(self.program).getCommercials().thenReturn([]) when(self.player).getTime().thenReturn(500) # Test skipper = TrackingCommercialSkipper(self.player, self.program, self.translator) skipper.onPlayBackStarted() time.sleep(1) when(self.player).isPlaying().thenReturn(False) skipper.onPlayBackStopped() # Verify verify(self.player, times(0)).seekTime(any())
def test_should_collect_multiple_tasks(self): def task(): pass setattr(task, TASK_ATTRIBUTE, True) def task2(): pass setattr(task2, TASK_ATTRIBUTE, True) module = mock() module.task = task module.task2 = task2 self.reactor.collect_tasks_and_actions_and_initializers(module) verify(self.execution_manager, times(2)).register_task(any(Task))
def test_PlayerNeverEntersAnyCommBreaks(self): # Setup when(self.player).isPlaying().thenReturn(True) when(self.program).getCommercials().thenReturn( [CommercialBreak(100, 200), CommercialBreak(600, 700)]) when(self.player).getTime().thenReturn(500) # Test skipper = TrackingCommercialSkipper(self.player, self.program, self.translator) skipper.onPlayBackStarted() time.sleep(2) when(self.player).isPlaying().thenReturn(False) skipper.onPlayBackStopped() # Verify verify(self.player, times(0)).seekTime(any())
def test_PlayerEntersCommBreakCloseToBeginningSkipsCommercial(self): # Setup when(self.player).isPlaying().thenReturn(True) when(self.program).getCommercials().thenReturn([CommercialBreak(500, 2500)]) # close to beginning = within 2 secs from start of commercial when(self.player).getTime().thenReturn(501).thenReturn(2501) # mock a valid tracker history trackerHistory = [] for x in range(490, 501): trackerHistory.append(TrackerSample(time.time() + x, x)) when(self.tracker).getHistory(any()).thenReturn(trackerHistory) # Test skipper = TrackingCommercialSkipper(self.player, self.program, self.translator) skipper.onPlayBackStarted() time.sleep(1) when(self.player).isPlaying().thenReturn(False) skipper.onPlayBackStopped() # Verify verify(self.player, times(1)).seekTime(any())
def test_PlayerSkippingAroundWhenEntersCommBreakDoesntSkipCommercial(self): # Setup when(self.player).isPlaying().thenReturn(True) when(self.program).getCommercials().thenReturn([CommercialBreak(500, 600)]) # close to beginning = within 2 secs from start of commercial when(self.player).getTime().thenReturn(501).thenReturn(502).thenReturn(504).thenReturn(505) # mock a valid tracker history trackerHistory = [] for x in range(499, 500): trackerHistory.append(TrackerSample(time.time() + (x - 499), x)) when(self.tracker).getHistory(any()).thenReturn(trackerHistory) # Test skipper = TrackingCommercialSkipper(self.player, self.program, self.translator, self.settings) skipper.onPlayBackStarted() time.sleep(1) when(self.player).isPlaying().thenReturn(False) skipper.onPlayBackStopped() # Verify verify(self.player, times(0)).seekTime(any())
def test_skip_if_file_already_exists_in_the_same_size(self): self.create_file_in(self.TARGET_PATH) self.create_file_in(self.SOURCE_PATH) self.ms.sync() verify(self.file_operator_mock, times(0)).copyfile(any(str), any(str))
def test_does_not_print_to_stderr_when_successful(self): self.given_file_added_in_commit("module/" + MIGRATION_PATH + "0.rb") check_filenames(self.commit_details, self.repository_details) verify(sys.stderr, times(0)).write(any())
def testNotifyAtNotifyNew(self): self._sendNotify("new", set(["testsnd1"])); verify(self.mockOutBuffer, times(2)).add(any())
def testNoNotifyAtNotifyOld(self): self._sendNotify("old", set(["testsnd1"])); verify(self.mockOutBuffer, times(1)).add(any())