From 9ef7a961f6366ce8f426a6b268c5baaa1fc35bcd Mon Sep 17 00:00:00 2001 From: Roman Mogilatov Date: Wed, 9 Dec 2015 19:28:52 +0200 Subject: [PATCH] Update representation of injections --- dependency_injector/injections.py | 36 ++++------ tests/test_injections.py | 113 +++++++++++++----------------- 2 files changed, 61 insertions(+), 88 deletions(-) diff --git a/dependency_injector/injections.py b/dependency_injector/injections.py index 6cb63137..1b3d2ed6 100644 --- a/dependency_injector/injections.py +++ b/dependency_injector/injections.py @@ -66,22 +66,16 @@ class Injection(object): return self.injectable() return self.injectable - def __str__(self, raw=False): + def __str__(self): """Return string representation of provider. - :param raw: Flag for returning of raw representation string - :type raw: bool - :rtype: str """ - if self.injectable_is_provider: - injectable_representation = self.injectable.__repr__(True) - else: - injectable_representation = repr(self.injectable) - representation = '{injection}({injectable})'.format( - injection=self.__class__.__name__, - injectable=injectable_representation) - return '<{0}>'.format(representation) if not raw else representation + return '<{injection}({injectable}) at {address}>'.format( + injection='.'.join((self.__class__.__module__, + self.__class__.__name__)), + injectable=repr(self.injectable), + address=hex(id(self))) __repr__ = __str__ @@ -103,23 +97,17 @@ class _NamedInjection(Injection): self.name = name super(_NamedInjection, self).__init__(injectable) - def __str__(self, raw=False): + def __str__(self): """Return string representation of provider. - :param raw: Flag for returning of raw representation string - :type raw: bool - :rtype: str """ - if self.injectable_is_provider: - injectable_representation = self.injectable.__repr__(True) - else: - injectable_representation = repr(self.injectable) - representation = '{injection}({name}, {injectable})'.format( - injection=self.__class__.__name__, + return '<{injection}({name}, {injectable}) at {address}>'.format( name=repr(self.name), - injectable=injectable_representation) - return '<{0}>'.format(representation) if not raw else representation + injection='.'.join((self.__class__.__module__, + self.__class__.__name__)), + injectable=repr(self.injectable), + address=hex(id(self))) __repr__ = __str__ diff --git a/tests/test_injections.py b/tests/test_injections.py index e3969d94..ac68f541 100644 --- a/tests/test_injections.py +++ b/tests/test_injections.py @@ -1,6 +1,5 @@ """Dependency injector injections unittests.""" -import six import unittest2 as unittest from dependency_injector import injections @@ -38,17 +37,15 @@ class InjectionTests(unittest.TestCase): self.assertIsInstance(injection.value, TestCatalog.Bundle) - def test_repr_with_scalar_value(self): - """Test Injection representation with scalar value.""" - injection = injections.Injection(123) - self.assertEqual(repr(injection), '') - - def test_repr_with_provider(self): - """Test Injection representation with provider.""" - injection = injections.Injection(providers.Factory(object)) - self.assertEqual(repr(injection), - ''.format( - six.moves.builtins.__name__)) + def test_repr(self): + """Test Injection representation.""" + provider = providers.Factory(object) + injection = injections.Injection(provider) + self.assertEqual( + repr(injection), + ''.format( + repr(provider), + hex(id(injection)))) class ArgTests(unittest.TestCase): @@ -59,17 +56,15 @@ class ArgTests(unittest.TestCase): injection = injections.Arg('some_value') self.assertEqual(injection.injectable, 'some_value') - def test_repr_with_scalar_value(self): - """Test Arg representation with scalar value.""" - injection = injections.Arg(123) - self.assertEqual(repr(injection), '') - - def test_repr_with_provider(self): - """Test Arg representation with provider.""" - injection = injections.Arg(providers.Factory(object)) - self.assertEqual(repr(injection), - ''.format( - six.moves.builtins.__name__)) + def test_repr(self): + """Test Arg representation.""" + provider = providers.Factory(object) + injection = injections.Arg(provider) + self.assertEqual( + repr(injection), + ''.format( + repr(provider), + hex(id(injection)))) class KwArgTests(unittest.TestCase): @@ -81,19 +76,16 @@ class KwArgTests(unittest.TestCase): self.assertEqual(injection.name, 'some_arg_name') self.assertEqual(injection.injectable, 'some_value') - def test_repr_with_scalar_value(self): - """Test KwArg representation with scalar value.""" - injection = injections.KwArg('some_arg_name', 123) - self.assertEqual(repr(injection), '') - - def test_repr_with_provider(self): - """Test KwArg representation with provider.""" - injection = injections.KwArg('some_arg_name', - providers.Factory(object)) - self.assertEqual(repr(injection), - ''.format( - six.moves.builtins.__name__)) + def test_repr(self): + """Test KwArg representation.""" + provider = providers.Factory(object) + injection = injections.KwArg('name', provider) + self.assertEqual( + repr(injection), + ''.format( + repr('name'), + repr(provider), + hex(id(injection)))) class AttributeTests(unittest.TestCase): @@ -105,20 +97,17 @@ class AttributeTests(unittest.TestCase): self.assertEqual(injection.name, 'some_arg_name') self.assertEqual(injection.injectable, 'some_value') - def test_repr_with_scalar_value(self): - """Test Attribute representation with scalar value.""" - injection = injections.Attribute('some_arg_name', 123) - self.assertEqual(repr(injection), - '') - - def test_repr_with_provider(self): - """Test Attribute representation with provider.""" - injection = injections.Attribute('some_arg_name', - providers.Factory(object)) - self.assertEqual(repr(injection), - ''.format( - six.moves.builtins.__name__)) + def test_repr(self): + """Test Attribute representation.""" + provider = providers.Factory(object) + injection = injections.Attribute('name', provider) + self.assertEqual( + repr(injection), + ''.format( + repr('name'), + repr(provider), + hex(id(injection)))) class MethodTests(unittest.TestCase): @@ -130,20 +119,16 @@ class MethodTests(unittest.TestCase): self.assertEqual(injection.name, 'some_arg_name') self.assertEqual(injection.injectable, 'some_value') - def test_repr_with_scalar_value(self): - """Test Method representation with scalar value.""" - injection = injections.Method('some_arg_name', 123) - self.assertEqual(repr(injection), - '') - - def test_repr_with_provider(self): - """Test Method representation with provider.""" - injection = injections.Method('some_arg_name', - providers.Factory(object)) - self.assertEqual(repr(injection), - ''.format( - six.moves.builtins.__name__)) + def test_repr(self): + """Test Method representation.""" + provider = providers.Factory(object) + injection = injections.Method('name', provider) + self.assertEqual( + repr(injection), + ''.format( + repr('name'), + repr(provider), + hex(id(injection)))) class InjectTests(unittest.TestCase):