def test_shouldReturnFirstConfiguredValueThatIsNotNone(fixture): mocked = mock.mock() mocked.expectCalls(mock.call("foo", ())) mocked.expectCalls(mock.call("foo", (), ret="")) mocked.expectCalls(mock.call("foo", (), ret=5)) assert mocked.foo() == "" mocked.checkExpectedCalls()
def test_shouldCloseAllSubLoggersWhenItIsClosed(): subLogger1 = mock.mock() subLogger2 = mock.mock() subLogger1.expectCalls(mock.call("close", ())) subLogger2.expectCalls(mock.call("close", ())) with TeeLogger(subLogger1, subLogger2): pass subLogger1.checkExpectedCalls() subLogger2.checkExpectedCalls()
def test_shouldBeAbleToAllowAnyNumberOfCallsForSomeExpectations(fixture): mocked = mock.mock() mocked.expectCalls(mock.call("baz", (1, )), mock.call("baz", (), anyNumber=True), mock.call("foo", (), anyNumber=True), inAnyOrder=False) mocked.foo() mocked.baz() mocked.baz(1) mocked.baz() mocked.checkExpectedCalls()
def test_shouldReturnTheErrorsOfTheFirstValidatorGroupThatReturnsSome(): rules = [object(), object()] errors1 = ["first error", "foo"] errors2 = ["second error", "bar"] errors3 = ["third error", "quux"] sub0, sub1, sub2, sub3 = mock.mock(), mock.mock(), mock.mock(), mock.mock() sub0.expectCalls(mock.call("validate", (rules, ), ret=[])) sub1.expectCalls(mock.call("validate", (rules, ), ret=errors1)) sub2.expectCalls(mock.call("validate", (rules, ), ret=errors2)) sub3.expectCalls(mock.call("validate", (rules, ), ret=errors3)) validator = ValidatorCollectionValidator([[sub0], [sub2, sub1], [sub3]]) assert set(validator.validate(rules)) == set(errors2 + errors1)
def test_shouldImmediatelyWriteAnyChunkToAllOfItsSubLoggers(): chunk = b"abcdef" subLogger1 = mock.mock() subLogger2 = mock.mock() subLogger1.expectCalls(mock.call("write", (chunk, ))) subLogger2.expectCalls(mock.call("write", (chunk, ))) logger = TeeLogger(subLogger1, subLogger2) logger.write(chunk) subLogger1.checkExpectedCalls() subLogger2.checkExpectedCalls()
def test_shouldForwardGetOutputAndExecuteCallsToWrappedRunner(fixture): returnValue = ["the bag"] wrapped, runner = fixture.construct() wrapped.expectCallsInOrder( mock.call("getOutput", ("file", "1", "2"), {"delimiter": "\t"}, ret=returnValue)) assert runner.getOutput("file", "1", "2", delimiter="\t") == returnValue wrapped.checkExpectedCalls() wrapped.expectCallsInOrder( mock.call("execute", ("executable", "foo"), ret=None)) runner.execute("executable", "foo") wrapped.checkExpectedCalls()
def test_shouldBeAbleToSimultaneouslyCheckMultipleGroupsOfExpectations( fixture): mocked = mock.mock() mocked.expectCalls(mock.call("foo", ()), mock.call("bar", ())) mocked.expectCalls(mock.call("bar", ()), mock.call("foo", ()), mock.call("quux", ()), inAnyOrder=True) mocked.foo() mocked.bar() with pytest.raises(AssertionError): mocked.checkExpectedCalls() mocked.quux() mocked.checkExpectedCalls()
def checkMethod(methodName): wrapped.expectCallsInOrder( mock.call(methodName, (firstRunnerPath, executable, "one", "two"), ret="")) if methodName == "execute": runner.execute(executable, "one", "two") else: runner.getOutput(executable, "one", "two") wrapped.checkExpectedCalls()
def test_shouldCallTheWrappedSchedsExecuteMethodIfItHasOne(): wrappedSched = mock.mock() execEnv, scheduling, returnedObject = object(), object(), object() wrappedSched.expectCalls( mock.call("execute", (execEnv, scheduling), ret=returnedObject)) assert DefaultImplScheduler(wrappedSched).execute(execEnv, scheduling) is \ returnedObject
def test_shouldCallRunSynchronizerWhenExecutedAsADefault(): returnedObject = object() execEnv = mock.mock() execEnv.expectCalls(mock.call("runSynchronizer", (), ret=returnedObject)) schedWithoutExecute = object() scheduler = DefaultImplScheduler(schedWithoutExecute) assert scheduler.execute(execEnv, anyScheduling()) is returnedObject
def test_shouldMakeExpectationsOverrideNormalMethods(fixture): mocked = mock.mock() mocked.foo = lambda arg: None mocked.foo(5) mocked.expectCalls(mock.call("foo", (2, ))) with pytest.raises(AssertionError): mocked.foo(5)
def test_shouldNotScheduleAnythingAndFailIfValidationFails(fixture): sched1, sched2 = mockScheds(2) rule1, rule2 = fixture.ruleWithSched(sched1), fixture.ruleWithSched(sched2) ruleSet = fixture.makeRuleSet(rule1, rule2) validator = mock.mock() validator.expectCalls(mock.call("validate", (ruleSet, ), ret=["raven"])) with pytest.raises(ValidationException) as ex: ruleSet.schedule(validator) assert ex.value.errors == ["raven"]
def test_shouldBeAbleToTestKeywordArguments(fixture): mocked = mock.mock() mocked.expectCallsInAnyOrder( mock.call("foo", (4, ), {"arg": 2}, anyNumber=True), mock.callMatching("bar", lambda *args, **kwargs: args == (4, ) and kwargs == {"arg": 2}, anyNumber=True)) mocked.foo(4, arg=2) with pytest.raises(AssertionError): mocked.foo(4, arg=3) with pytest.raises(AssertionError): mocked.foo(4) mocked.bar(4, arg=2) with pytest.raises(AssertionError): mocked.bar(4, arg=0)
def expectTestCalls(mockedObject, anyOrder): mockedObject.expectCalls(mock.call("foo", (40, )), mock.call("bar", ()), inAnyOrder=anyOrder)
def loadScheduler(self, module, name, initArgs=[]): self.moduleLoader.expectCalls( mock.call("loadFromFile", (self.path, name), ret=module)) ret = self.loader.loadFromFile(self.path, name, initArgs) self.moduleLoader.checkExpectedCalls() return ret