|
1 | 1 | #!/usr/bin/env python3
|
2 | 2 | """Tests for the tests package."""
|
3 | 3 | #
|
4 |
| -# (C) Pywikibot team, 2014-2023 |
| 4 | +# (C) Pywikibot team, 2014-2025 |
5 | 5 | #
|
6 | 6 | # Distributed under the terms of the MIT license.
|
7 | 7 | from __future__ import annotations
|
|
10 | 10 | from contextlib import suppress
|
11 | 11 |
|
12 | 12 | from tests import utils
|
13 |
| -from tests.aspects import TestCase |
| 13 | +from tests.aspects import DefaultSiteTestCase, TestCase, require_version |
14 | 14 |
|
15 | 15 |
|
16 | 16 | class HttpServerProblemTestCase(TestCase):
|
@@ -78,6 +78,88 @@ def test_assert_length_fail(self):
|
78 | 78 | self.assertLength(None, self.seq)
|
79 | 79 |
|
80 | 80 |
|
| 81 | +class TestRequireVersionDry(DefaultSiteTestCase): |
| 82 | + |
| 83 | + """Test require_version decorator.""" |
| 84 | + |
| 85 | + dry = True |
| 86 | + |
| 87 | + @require_version('') |
| 88 | + def method(self): |
| 89 | + """Test method for decorator.""" |
| 90 | + |
| 91 | + def test_require_version(self): |
| 92 | + """Test require_version for DrySite.""" |
| 93 | + with self.assertRaisesRegex( |
| 94 | + TypeError, |
| 95 | + f'{type(self).__name__}.site must be a BaseSite not DrySite'): |
| 96 | + self.method() |
| 97 | + |
| 98 | + |
| 99 | +class TestRequireVersion(DefaultSiteTestCase): |
| 100 | + |
| 101 | + """Test require_version decorator.""" |
| 102 | + |
| 103 | + @require_version('') |
| 104 | + def method_with_params(self, key): |
| 105 | + """Test method for decorated methods with unsupported arguments.""" |
| 106 | + |
| 107 | + def method_failing(self): |
| 108 | + """Test method for decorator with invalid parameter.""" |
| 109 | + self.assertTrue(False, 'should never happen') |
| 110 | + |
| 111 | + @require_version('>=1.31') |
| 112 | + def method_succeed(self): |
| 113 | + """Test that decorator passes.""" |
| 114 | + self.assertTrue(False, 'intentional fail for method_succeed test') |
| 115 | + |
| 116 | + @require_version('<1.31') |
| 117 | + def method_fail(self): |
| 118 | + """Test that decorator skips.""" |
| 119 | + self.assertTrue(False, 'intentional fail for test') |
| 120 | + |
| 121 | + def test_unsupported_methods(self): |
| 122 | + """Test require_version with unsupported methods.""" |
| 123 | + with self.assertRaisesRegex( |
| 124 | + TypeError, "Test method 'method_with_params' has parameters"): |
| 125 | + self.method_with_params('42') |
| 126 | + with self.assertRaisesRegex( |
| 127 | + TypeError, "Test method 'method_with_params' has parameters"): |
| 128 | + self.method_with_params(key='42') |
| 129 | + with self.assertRaisesRegex(ValueError, |
| 130 | + 'There is no valid operator given '): |
| 131 | + self.method_with_params() |
| 132 | + |
| 133 | + def test_version_needed(self): |
| 134 | + """Test for invalid decorator parameters.""" |
| 135 | + with self.assertRaisesRegex(ValueError, |
| 136 | + 'There is no valid operator given '): |
| 137 | + require_version('foo')(self.method_failing)(self) |
| 138 | + with self.assertRaisesRegex(ValueError, |
| 139 | + 'first operand foo should not be set'): |
| 140 | + require_version('foo>bar')(self.method_failing)(self) |
| 141 | + with self.assertRaisesRegex(ValueError, 'Invalid version number'): |
| 142 | + require_version('>bar')(self.method_failing)(self) |
| 143 | + with self.assertRaisesRegex(unittest.SkipTest, |
| 144 | + r'MediaWiki < v1\.31 required'): |
| 145 | + require_version('<1.31')(self.method_failing)(self) |
| 146 | + with self.assertRaisesRegex( |
| 147 | + unittest.SkipTest, |
| 148 | + r'MediaWiki < v1\.31 required to run this test'): |
| 149 | + require_version('<1.31', |
| 150 | + 'run this test')(self.method_failing)(self) |
| 151 | + |
| 152 | + def test_decorator(self): |
| 153 | + """Test that decorator passes or skips.""" |
| 154 | + with self.assertRaisesRegex( |
| 155 | + AssertionError, |
| 156 | + 'intentional fail for method_succeed test'): |
| 157 | + self.method_succeed() |
| 158 | + with self.assertRaisesRegex(unittest.SkipTest, |
| 159 | + r'MediaWiki < v1\.31 required'): |
| 160 | + self.method_fail() |
| 161 | + |
| 162 | + |
81 | 163 | class UtilsTests(TestCase):
|
82 | 164 |
|
83 | 165 | """Tests for tests.utils."""
|
|
0 commit comments