def test_switches_backend(self): """ Tests that calling an operation will switch backend if the current backend doesn't support it """ def say_hello(backend): return "Hello world!" self.FakeBackend.operations = { 'say_hello': say_hello, } def say_goodbye(backend): return "Goodbye!" self.AnotherFakeBackend.operations = { 'say_goodbye': say_goodbye, } image = Image(self.FakeBackend(), 'jpeg') self.assertIsInstance(image.backend, self.FakeBackend) image.say_goodbye() self.assertIsInstance(image.backend, self.AnotherFakeBackend) image.say_hello() self.assertIsInstance(image.backend, self.FakeBackend)
def test_save_as_foo(self): image = Image() image.save_as_jpeg = mock.MagicMock() with self.assertRaises(ValueError): image.save("foo", "outfile") self.assertFalse(image.save_as_jpeg.mock_calls)
def test_getattr_operation(self): """ Tests that __getattr__ looks up operations correctly """ def myop(backend): pass self.FakeBackend.operations = { 'test': myop, } image = Image(self.FakeBackend(), 'jpeg') self.assertIsInstance(image.test, types.FunctionType)
def test_return_value_gets_passed_back(self): """ Tests that the return value of the underlying function gets passed back to the caller """ def myop(backend): return "Hello world!" self.FakeBackend.operations = { 'test': myop, } image = Image(self.FakeBackend(), 'jpeg') self.assertEqual(image.test(), "Hello world!")
def test_getattr_operation_unknown(self): """ Tests that __getattr__ raises an AttributeError when the requested attribute is not an operation """ def myop(backend): pass self.FakeBackend.operations = { 'test': myop, } image = Image(self.FakeBackend(), 'jpeg') self.assertRaises(AttributeError, getattr, image, 'test2')
def test_calls_function(self): """ Tests that calling the operation calls the underlying function """ def myop(backend): backend.func_called = True self.FakeBackend.operations = { 'test': myop, } image = Image(self.FakeBackend(), 'jpeg') image.backend.func_called = False image.test() self.assertTrue(image.backend.func_called)
def test_args_get_passed_through(self): """ Tests that args get passed through to the underlying function """ def myop(backend, *args, **kwargs): backend.passed_args = args backend.passed_kwargs = kwargs self.FakeBackend.operations = { 'test': myop, } image = Image(self.FakeBackend(), 'jpeg') image.backend.passed_args = None image.backend.passed_kwargs = None image.test('Hello', 'World', name="Karl") self.assertEqual(image.backend.passed_args, ('Hello', 'World')) self.assertEqual(image.backend.passed_kwargs, {'name': "Karl"})
def test_save_as_jpeg(self): image = Image() image.save_as_jpeg = mock.MagicMock() image.save("jpeg", "outfile") image.save_as_jpeg.assert_called_with("outfile")