XZise has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/239683

Change subject: [FEAT] Support assertAPIError
......................................................................

[FEAT] Support assertAPIError

To assert a specific APIError the base class supports `assertAPIError` which
wraps around `assertRaises` and additionally checks if the error is of a
specific code and optionally a specific info. Like `assertRaises` it can be
used as a context manager when additional checks should be done afterwards.

Change-Id: Ib211974ec3acd17fb45c2280e084723257a88650
---
M tests/aspects.py
M tests/upload_tests.py
2 files changed, 50 insertions(+), 5 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/pywikibot/core 
refs/changes/83/239683/1

diff --git a/tests/aspects.py b/tests/aspects.py
index fa0e486..111aacb 100644
--- a/tests/aspects.py
+++ b/tests/aspects.py
@@ -45,7 +45,7 @@
 
 from pywikibot import Site
 from pywikibot.comms import http
-from pywikibot.data.api import Request as _original_Request
+from pywikibot.data.api import Request as _original_Request, APIError
 from pywikibot.exceptions import ServerError, NoUsername
 from pywikibot.family import WikimediaFamily
 from pywikibot.site import BaseSite
@@ -248,6 +248,54 @@
 
     assertPagelistTitles = assertPageTitlesEqual
 
+    def assertAPIError(self, code, info=None, callable_obj=None, *args,
+                       **kwargs):
+        """
+        Assert that a specific APIError wrapped around L{assertRaises}.
+
+        If no callable object is defined and it returns a context manager, that
+        context manager will return the underlying context manager used by
+        L{assertRaises}. So it's possible to access the APIError by using it's
+        C{exception} attribute.
+
+        @param code: The code of the error which must have happened.
+        @type code: str
+        @param info: The info string of the error or None if no it shouldn't be
+            checked.
+        @type info: str or None
+        @param callable_obj: The object that will be tested. If None it returns
+            a context manager like L{assertRaises}.
+        @type callable_obj: callable
+        @param args: The positional arguments forwarded to the callable object.
+        @param kwargs: The keyword arguments forwared to the callable object.
+        @return: The context manager if callable_obj is None and None 
otherwise.
+        @rtype: None or context manager
+        """
+        class AssertContext(object):
+            def __enter__(ctx):
+                ctx.cm = self.assertRaises(APIError, msg=kwargs.pop('msg', 
None))
+                ctx.cm.__enter__()
+                return ctx.cm
+
+            def __exit__(ctx, exc_type, exc_value, tb):
+                print('EXIT')
+                result = ctx.cm.__exit__(exc_type, exc_value, tb)
+                if isinstance(exc_value, APIError):
+                    assert result is True
+                    ctx.exception = exc_value
+                    self.assertEqual(exc_value.code, code)
+                    if info:
+                        self.assertEqual(exc_value, info)
+                return result
+
+            def handle(ctx):
+                if callable_obj is None:
+                    return ctx
+                with ctx:
+                    callable_obj(*args, **kwargs)
+
+        return AssertContext().handle()
+
 
 class TestTimerMixin(TestCaseBase):
 
diff --git a/tests/upload_tests.py b/tests/upload_tests.py
index 610a9d8..1e4b787 100644
--- a/tests/upload_tests.py
+++ b/tests/upload_tests.py
@@ -17,8 +17,6 @@
 
 import pywikibot
 
-from pywikibot.data.api import APIError
-
 from tests import _images_dir
 from tests.aspects import unittest, TestCase
 
@@ -107,9 +105,8 @@
         self._finish_upload(chunk_size, self.sounds_png)
 
         # Check if it's still cached
-        with self.assertRaises(APIError) as cm:
+        with self.assertAPIError('siiinvalidsessiondata') as cm:
             self.site.stash_info(self._file_key)
-        self.assertEqual(cm.exception.code, 'siiinvalidsessiondata')
         self.assertTrue(cm.exception.info.startswith('File not found'),
                         'info ({0}) did not start with '
                         '"File not found"'.format(cm.exception.info))

-- 
To view, visit https://gerrit.wikimedia.org/r/239683
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ib211974ec3acd17fb45c2280e084723257a88650
Gerrit-PatchSet: 1
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: XZise <commodorefabia...@gmx.de>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to