1From e5aa66b1af2d49f159c4daefc598f96744ee988d Mon Sep 17 00:00:00 2001 2From: Zhixiong Chi <zhixiong.chi@windriver.com> 3Date: Thu, 29 Feb 2024 12:31:58 -0800 4Subject: [PATCH] [PATCH] Drop ptests fixtures and recorde_modes 5 6The usage of fixture in test_fixtures has been deprecated. 7See https://docs.pytest.org/en/stable/explanation/fixtures.html and 8https://docs.pytest.org/en/stable/deprecations.html#calling-fixtures-directly 9for more information about fixtures. 10Meanwhile the test_record_modes relies on httpbin.org which has been sold and 11re-sold several times, and it adds X-Amzn-Trace-Id header that can possibly 12diff for each request. 13It leads to ptest failure, so drop it now until we find the solution. 14 15Upstream-Status: Inappropriate [OE-Specific] 16 17Signed-off-by: Zhixiong Chi <zhixiong.chi@windriver.com> 18Signed-off-by: Khem Raj <raj.khem@gmail.com> 19--- 20 tests/integration/test_fixtures.py | 60 ----------- 21 tests/integration/test_record_modes.py | 141 ------------------------- 22 tests/unit/test_fixtures.py | 94 ----------------- 23 3 files changed, 295 deletions(-) 24 delete mode 100644 tests/integration/test_fixtures.py 25 delete mode 100644 tests/integration/test_record_modes.py 26 delete mode 100644 tests/unit/test_fixtures.py 27 28diff --git a/tests/integration/test_fixtures.py b/tests/integration/test_fixtures.py 29deleted file mode 100644 30index fc3d1e7..0000000 31--- a/tests/integration/test_fixtures.py 32+++ /dev/null 33@@ -1,60 +0,0 @@ 34-import os.path 35- 36-import pytest 37- 38- 39-@pytest.mark.usefixtures('betamax_session') 40-class TestPyTestFixtures: 41- @pytest.fixture(autouse=True) 42- def setup(self, request): 43- """After test hook to assert everything.""" 44- def finalizer(): 45- test_dir = os.path.abspath('.') 46- cassette_name = ('tests.integration.test_fixtures.' # Module name 47- 'TestPyTestFixtures.' # Class name 48- 'test_pytest_fixture' # Test function name 49- '.json') 50- file_name = os.path.join(test_dir, 'tests', 'cassettes', 51- cassette_name) 52- assert os.path.exists(file_name) is True 53- 54- request.addfinalizer(finalizer) 55- 56- def test_pytest_fixture(self, betamax_session): 57- """Exercise the fixture itself.""" 58- resp = betamax_session.get('https://httpbin.org/get') 59- assert resp.ok 60- 61- 62-@pytest.mark.usefixtures('betamax_parametrized_session') 63-class TestPyTestParametrizedFixtures: 64- @pytest.fixture(autouse=True) 65- def setup(self, request): 66- """After test hook to assert everything.""" 67- def finalizer(): 68- test_dir = os.path.abspath('.') 69- cassette_name = ('tests.integration.test_fixtures.' # Module name 70- 'TestPyTestParametrizedFixtures.' # Class name 71- 'test_pytest_fixture' # Test function name 72- '[https---httpbin.org-get]' # Parameter 73- '.json') 74- file_name = os.path.join(test_dir, 'tests', 'cassettes', 75- cassette_name) 76- assert os.path.exists(file_name) is True 77- 78- request.addfinalizer(finalizer) 79- 80- @pytest.mark.parametrize('url', ('https://httpbin.org/get',)) 81- def test_pytest_fixture(self, betamax_parametrized_session, url): 82- """Exercise the fixture itself.""" 83- resp = betamax_parametrized_session.get(url) 84- assert resp.ok 85- 86- 87-@pytest.mark.parametrize('problematic_arg', [r'aaa\bbb', 'ccc:ddd', 'eee*fff']) 88-def test_pytest_parametrize_with_filesystem_problematic_chars( 89- betamax_parametrized_session, problematic_arg): 90- """ 91- Exercice parametrized args containing characters which might cause 92- problems when getting translated into file names. """ 93- assert True 94diff --git a/tests/integration/test_record_modes.py b/tests/integration/test_record_modes.py 95deleted file mode 100644 96index 988b851..0000000 97--- a/tests/integration/test_record_modes.py 98+++ /dev/null 99@@ -1,141 +0,0 @@ 100-import re 101- 102-from betamax import Betamax, BetamaxError 103- 104-from tests.integration.helper import IntegrationHelper 105- 106- 107-class TestRecordOnce(IntegrationHelper): 108- def test_records_new_interaction(self): 109- s = self.session 110- with Betamax(s).use_cassette('test_record_once') as betamax: 111- self.cassette_path = betamax.current_cassette.cassette_path 112- assert betamax.current_cassette.is_empty() is True 113- r = s.get('http://httpbin.org/get') 114- assert r.status_code == 200 115- assert betamax.current_cassette.is_empty() is True 116- assert betamax.current_cassette.interactions != [] 117- 118- def test_replays_response_from_cassette(self): 119- s = self.session 120- with Betamax(s).use_cassette('test_replays_response') as betamax: 121- self.cassette_path = betamax.current_cassette.cassette_path 122- assert betamax.current_cassette.is_empty() is True 123- r0 = s.get('http://httpbin.org/get') 124- assert r0.status_code == 200 125- assert betamax.current_cassette.interactions != [] 126- assert len(betamax.current_cassette.interactions) == 1 127- r1 = s.get('http://httpbin.org/get') 128- assert len(betamax.current_cassette.interactions) == 2 129- assert r1.status_code == 200 130- r0_headers = r0.headers.copy() 131- r0_headers.pop('Date') 132- r0_headers.pop('Age', None) 133- r0_headers.pop('X-Processed-Time', None) 134- r1_headers = r1.headers.copy() 135- r1_headers.pop('Date') 136- r1_headers.pop('Age', None) 137- r1_headers.pop('X-Processed-Time', None) 138- # NOTE(sigmavirus24): This fails if the second request is 139- # technically a second later. Ignoring the Date headers allows 140- # this test to succeed. 141- # NOTE(hroncok): httpbin.org added X-Processed-Time header that 142- # can possibly differ (and often does) 143- r0_content = r0.content.decode(encoding='utf-8', errors='strict') 144- r1_content = r1.content.decode(encoding='utf-8', errors='strict') 145- r0_content = re.sub('"X-Amzn-Trace-Id": "[^"]+"', '"X-Amzn-Trace-Id": ""', r0_content) 146- r1_content = re.sub('"X-Amzn-Trace-Id": "[^"]+"', '"X-Amzn-Trace-Id": ""', r1_content) 147- # NOTE(jhatler): httpbin.org added "X-Amzn-Trace-Id" to their 148- # response, which is a unique ID that will differ between requests. 149- # We remove it from the response body before comparing. 150- assert r0_headers == r1_headers 151- assert r0_content == r1_content 152- 153- 154-class TestRecordNone(IntegrationHelper): 155- def test_raises_exception_when_no_interactions_present(self): 156- s = self.session 157- with Betamax(s) as betamax: 158- betamax.use_cassette('test', record='none') 159- self.cassette_created = False 160- assert betamax.current_cassette is not None 161- self.assertRaises(BetamaxError, s.get, 'http://httpbin.org/get') 162- 163- def test_record_none_does_not_create_cassettes(self): 164- s = self.session 165- with Betamax(s) as betamax: 166- self.assertRaises(ValueError, betamax.use_cassette, 167- 'test_record_none', record='none') 168- self.cassette_created = False 169- 170- 171-class TestRecordNewEpisodes(IntegrationHelper): 172- def setUp(self): 173- super(TestRecordNewEpisodes, self).setUp() 174- with Betamax(self.session).use_cassette('test_record_new'): 175- self.session.get('http://httpbin.org/get') 176- self.session.get('http://httpbin.org/redirect/2') 177- 178- def test_records_new_events_with_existing_cassette(self): 179- s = self.session 180- opts = {'record': 'new_episodes'} 181- with Betamax(s).use_cassette('test_record_new', **opts) as betamax: 182- cassette = betamax.current_cassette 183- self.cassette_path = cassette.cassette_path 184- assert cassette.interactions != [] 185- assert len(cassette.interactions) == 4 186- assert cassette.is_empty() is False 187- s.get('https://httpbin.org/get') 188- assert len(cassette.interactions) == 5 189- 190- with Betamax(s).use_cassette('test_record_new') as betamax: 191- cassette = betamax.current_cassette 192- assert len(cassette.interactions) == 5 193- r = s.get('https://httpbin.org/get') 194- assert r.status_code == 200 195- 196- 197-class TestRecordNewEpisodesCreatesCassettes(IntegrationHelper): 198- def test_creates_new_cassettes(self): 199- recorder = Betamax(self.session) 200- opts = {'record': 'new_episodes'} 201- cassette_name = 'test_record_new_makes_new_cassettes' 202- with recorder.use_cassette(cassette_name, **opts) as betamax: 203- self.cassette_path = betamax.current_cassette.cassette_path 204- self.session.get('https://httpbin.org/get') 205- 206- 207-class TestRecordAll(IntegrationHelper): 208- def setUp(self): 209- super(TestRecordAll, self).setUp() 210- with Betamax(self.session).use_cassette('test_record_all'): 211- self.session.get('http://httpbin.org/get') 212- self.session.get('http://httpbin.org/redirect/2') 213- self.session.get('http://httpbin.org/get') 214- 215- def test_records_new_interactions(self): 216- s = self.session 217- opts = {'record': 'all'} 218- with Betamax(s).use_cassette('test_record_all', **opts) as betamax: 219- cassette = betamax.current_cassette 220- self.cassette_path = cassette.cassette_path 221- assert cassette.interactions != [] 222- assert len(cassette.interactions) == 5 223- assert cassette.is_empty() is False 224- s.post('http://httpbin.org/post', data={'foo': 'bar'}) 225- assert len(cassette.interactions) == 6 226- 227- with Betamax(s).use_cassette('test_record_all') as betamax: 228- assert len(betamax.current_cassette.interactions) == 6 229- 230- def test_replaces_old_interactions(self): 231- s = self.session 232- opts = {'record': 'all'} 233- with Betamax(s).use_cassette('test_record_all', **opts) as betamax: 234- cassette = betamax.current_cassette 235- self.cassette_path = cassette.cassette_path 236- assert cassette.interactions != [] 237- assert len(cassette.interactions) == 5 238- assert cassette.is_empty() is False 239- s.get('http://httpbin.org/get') 240- assert len(cassette.interactions) == 5 241diff --git a/tests/unit/test_fixtures.py b/tests/unit/test_fixtures.py 242deleted file mode 100644 243index 41f33eb..0000000 244--- a/tests/unit/test_fixtures.py 245+++ /dev/null 246@@ -1,94 +0,0 @@ 247-try: 248- import unittest.mock as mock 249-except ImportError: 250- import mock 251- 252-import pytest 253-import unittest 254- 255-import requests 256- 257-import betamax 258-from betamax.fixtures import pytest as pytest_fixture 259-from betamax.fixtures import unittest as unittest_fixture 260- 261- 262-class TestPyTestFixture(unittest.TestCase): 263- def setUp(self): 264- self.mocked_betamax = mock.MagicMock() 265- self.patched_betamax = mock.patch.object( 266- betamax.recorder, 'Betamax', return_value=self.mocked_betamax) 267- self.patched_betamax.start() 268- 269- def tearDown(self): 270- self.patched_betamax.stop() 271- 272- def test_adds_stop_as_a_finalizer(self): 273- # Mock a pytest request object 274- request = mock.MagicMock() 275- request.cls = request.module = None 276- request.node.name = request.function.__name__ = 'test' 277- 278- pytest_fixture._betamax_recorder(request) 279- assert request.addfinalizer.called is True 280- request.addfinalizer.assert_called_once_with(self.mocked_betamax.stop) 281- 282- def test_auto_starts_the_recorder(self): 283- # Mock a pytest request object 284- request = mock.MagicMock() 285- request.cls = request.module = None 286- request.node.name = request.function.__name__ = 'test' 287- 288- pytest_fixture._betamax_recorder(request) 289- self.mocked_betamax.start.assert_called_once_with() 290- 291- 292-class FakeBetamaxTestCase(unittest_fixture.BetamaxTestCase): 293- def test_fake(self): 294- pass 295- 296- 297-class TestUnittestFixture(unittest.TestCase): 298- def setUp(self): 299- self.mocked_betamax = mock.MagicMock() 300- self.patched_betamax = mock.patch.object( 301- betamax.recorder, 'Betamax', return_value=self.mocked_betamax) 302- self.betamax = self.patched_betamax.start() 303- self.fixture = FakeBetamaxTestCase(methodName='test_fake') 304- 305- def tearDown(self): 306- self.patched_betamax.stop() 307- 308- def test_setUp(self): 309- self.fixture.setUp() 310- 311- self.mocked_betamax.use_cassette.assert_called_once_with( 312- 'FakeBetamaxTestCase.test_fake' 313- ) 314- self.mocked_betamax.start.assert_called_once_with() 315- 316- def test_setUp_rejects_arbitrary_session_classes(self): 317- self.fixture.SESSION_CLASS = object 318- 319- with pytest.raises(AssertionError): 320- self.fixture.setUp() 321- 322- def test_setUp_accepts_session_subclasses(self): 323- class TestSession(requests.Session): 324- pass 325- 326- self.fixture.SESSION_CLASS = TestSession 327- 328- self.fixture.setUp() 329- 330- assert self.betamax.called is True 331- call_kwargs = self.betamax.call_args[-1] 332- assert isinstance(call_kwargs['session'], TestSession) 333- 334- def test_tearDown_calls_stop(self): 335- recorder = mock.Mock() 336- self.fixture.recorder = recorder 337- 338- self.fixture.tearDown() 339- 340- recorder.stop.assert_called_once_with() 341-- 3422.44.0 343 344