Commits

Pierre-Yves David committed 8950afb

Compat with mock 0.6

(debian squeeze as python-mock 0.6)

Comments (0)

Files changed (4)

 from difflib import unified_diff
 from functools import wraps
 from os.path import (dirname, join, basename, exists, splitext)
+from contextlib import contextmanager
 
-from mock import patch
+from mock import patch, Mock
+
+if getattr(patch, 'object', None) is None:
+    # mock 0.6 compat
+    @contextmanager
+    def _object(obj, attr):
+        val = getattr(obj, attr)
+        try:
+            m =  Mock()
+            setattr(obj, attr, m)
+            yield m
+        finally:
+            setattr(obj, attr, val)
+    patch.object = _object
+
 
 # Time Mock
 

tests/test_handlers.py

 from StringIO import StringIO
 from os.path import splitext
 
-from mock import patch
-
 from goatlog.records import ContextOpenRecord, ContextCloseRecord, Log
 from goatlog.handlers import BaseRichHandler, JsonHandler
 
+from goatlog.testlib import patch
+
 # Utils
 def Call(*args, **kwargs):
     return args, kwargs
 from goatlog.exceptions import (SafeUpdateFailed, MismatchCloseContext)
 from utils import LogCapture
 
-from mock import patch
+from goatlog.testlib import patch
+
 from utils import (TimeSideEffect, SimpleOpenRecord, SimpleCloseRecord,
     SimpleLogRecord, mock_open_record, mock_close_record)
 
     data.update(kwargs)
     return type("%sRichRecord" % record_type.capitalize(), (), data)
 
+def MockOf(spec):
+    # mock 0.6 compat
+    try:
+        return Mock(spec_set=spec)
+    except TypeError:
+        return Mock(spec=spec)
+
+
 class JsonReemitTestCase(unittest.TestCase):
 
     def setUp(self):
         self.record_type = "log.sample"
         self.context_type = 'context.sample'
 
-        self.sample_record_cls = Mock(spec_set=RichRecord)
+        self.sample_record_cls = MockOf(RichRecord)
         self.sample_record_cls.__record_type__ = self.record_type
 
-        self.open_context_class = Mock(spec_set=ContextOpenRecord)
+        self.open_context_class = MockOf(ContextOpenRecord)
         self.open_context_class.__record_type__ = self.context_type
         self.open_context_class.__context_action__ = 'open'
 
-        self.close_context_class = Mock(spec_set=ContextCloseRecord)
+        self.close_context_class = MockOf(ContextCloseRecord)
         self.close_context_class.__record_type__ = self.context_type
         self.close_context_class.__context_action__ = 'close'
 
         self.sample_record_cls.load.assert_called_once_with(sub_record)
 
         self.assertTrue(self.open_context_class.load.called)
-        self.open_context_class.load.assert_called_once_with(record)
+        self.open_context_class.load.assert_called_once_with(record)