From ce2405ec3dd97e8bdf8f63312e3c6ce59ef562d4 Mon Sep 17 00:00:00 2001 From: Andrei Zeliankou Date: Mon, 12 Jun 2023 14:16:59 +0100 Subject: Tests: prerequisites checking reworked. Prerequisites check moved to the module level to simplify class structure. Discovery and prerequisites checks functions moved to the separate files. Introduced "require" fixture to provide per-test requirements check. --- test/test_static_variables.py | 2 -- 1 file changed, 2 deletions(-) (limited to 'test/test_static_variables.py') diff --git a/test/test_static_variables.py b/test/test_static_variables.py index 370c3e6f..f69a936e 100644 --- a/test/test_static_variables.py +++ b/test/test_static_variables.py @@ -6,8 +6,6 @@ from unit.applications.proto import TestApplicationProto class TestStaticVariables(TestApplicationProto): - prerequisites = {} - @pytest.fixture(autouse=True) def setup_method_fixture(self, temp_dir): os.makedirs(f'{temp_dir}/assets/dir') -- cgit From c183bd8749a19477390f8cb77efe5f6d223f0905 Mon Sep 17 00:00:00 2001 From: Andrei Zeliankou Date: Wed, 14 Jun 2023 18:20:09 +0100 Subject: Tests: get rid of classes in test files. Class usage came from the unittest framework and it was always redundant after migration to the pytest. This commit removes classes from files containing tests to make them more readable and understandable. --- test/test_static_variables.py | 152 ++++++++++++++++++++++-------------------- 1 file changed, 79 insertions(+), 73 deletions(-) (limited to 'test/test_static_variables.py') diff --git a/test/test_static_variables.py b/test/test_static_variables.py index f69a936e..bc39e90e 100644 --- a/test/test_static_variables.py +++ b/test/test_static_variables.py @@ -2,76 +2,82 @@ import os from pathlib import Path import pytest -from unit.applications.proto import TestApplicationProto - - -class TestStaticVariables(TestApplicationProto): - @pytest.fixture(autouse=True) - def setup_method_fixture(self, temp_dir): - os.makedirs(f'{temp_dir}/assets/dir') - os.makedirs(f'{temp_dir}/assets/d$r') - Path(f'{temp_dir}/assets/index.html').write_text('0123456789') - Path(f'{temp_dir}/assets/dir/file').write_text('file') - Path(f'{temp_dir}/assets/d$r/file').write_text('d$r') - - self._load_conf( - { - "listeners": {"*:7080": {"pass": "routes"}}, - "routes": [{"action": {"share": f'{temp_dir}/assets$uri'}}], - } - ) - - def update_share(self, share): - if isinstance(share, list): - return self.conf(share, 'routes/0/action/share') - - return self.conf(f'"{share}"', 'routes/0/action/share') - - def test_static_variables(self, temp_dir): - assert self.get(url='/index.html')['status'] == 200 - assert self.get(url='/d$r/file')['status'] == 200 - - assert 'success' in self.update_share('$uri') - assert self.get(url=f'{temp_dir}/assets/index.html')['status'] == 200 - - assert 'success' in self.update_share(f'{temp_dir}/assets${{uri}}') - assert self.get(url='/index.html')['status'] == 200 - - def test_static_variables_array(self, temp_dir): - assert 'success' in self.update_share( - [f'{temp_dir}/assets$uri', '$uri'] - ) - - assert self.get(url='/dir/file')['status'] == 200 - assert self.get(url=f'{temp_dir}/assets/index.html')['status'] == 200 - assert self.get(url='/blah')['status'] == 404 - - assert 'success' in self.conf( - { - "share": [f'{temp_dir}/assets$uri', '$uri'], - "fallback": {"return": 201}, - }, - 'routes/0/action', - ) - - assert self.get(url='/dir/file')['status'] == 200 - assert self.get(url=f'{temp_dir}/assets/index.html')['status'] == 200 - assert self.get(url='/dir/blah')['status'] == 201 - - def test_static_variables_buildin_start(self, temp_dir): - assert 'success' in self.update_share('$uri/assets/index.html') - assert self.get(url=temp_dir)['status'] == 200 - - def test_static_variables_buildin_mid(self, temp_dir): - assert 'success' in self.update_share(f'{temp_dir}$uri/index.html') - assert self.get(url='/assets')['status'] == 200 - - def test_static_variables_buildin_end(self): - assert self.get(url='/index.html')['status'] == 200 - - def test_static_variables_invalid(self, temp_dir): - assert 'error' in self.update_share(f'{temp_dir}/assets/d$r$uri') - assert 'error' in self.update_share(f'{temp_dir}/assets/$$uri') - assert 'error' in self.update_share( - [f'{temp_dir}/assets$uri', f'{temp_dir}/assets/dir', '$$uri'] - ) +from unit.applications.proto import ApplicationProto + +client = ApplicationProto() + + +@pytest.fixture(autouse=True) +def setup_method_fixture(temp_dir): + os.makedirs(f'{temp_dir}/assets/dir') + os.makedirs(f'{temp_dir}/assets/d$r') + Path(f'{temp_dir}/assets/index.html').write_text('0123456789') + Path(f'{temp_dir}/assets/dir/file').write_text('file') + Path(f'{temp_dir}/assets/d$r/file').write_text('d$r') + + assert 'success' in client.conf( + { + "listeners": {"*:7080": {"pass": "routes"}}, + "routes": [{"action": {"share": f'{temp_dir}/assets$uri'}}], + } + ) + + +def update_share(share): + if isinstance(share, list): + return client.conf(share, 'routes/0/action/share') + + return client.conf(f'"{share}"', 'routes/0/action/share') + + +def test_static_variables(temp_dir): + assert client.get(url='/index.html')['status'] == 200 + assert client.get(url='/d$r/file')['status'] == 200 + + assert 'success' in update_share('$uri') + assert client.get(url=f'{temp_dir}/assets/index.html')['status'] == 200 + + assert 'success' in update_share(f'{temp_dir}/assets${{uri}}') + assert client.get(url='/index.html')['status'] == 200 + + +def test_static_variables_array(temp_dir): + assert 'success' in update_share([f'{temp_dir}/assets$uri', '$uri']) + + assert client.get(url='/dir/file')['status'] == 200 + assert client.get(url=f'{temp_dir}/assets/index.html')['status'] == 200 + assert client.get(url='/blah')['status'] == 404 + + assert 'success' in client.conf( + { + "share": [f'{temp_dir}/assets$uri', '$uri'], + "fallback": {"return": 201}, + }, + 'routes/0/action', + ) + + assert client.get(url='/dir/file')['status'] == 200 + assert client.get(url=f'{temp_dir}/assets/index.html')['status'] == 200 + assert client.get(url='/dir/blah')['status'] == 201 + + +def test_static_variables_buildin_start(temp_dir): + assert 'success' in update_share('$uri/assets/index.html') + assert client.get(url=temp_dir)['status'] == 200 + + +def test_static_variables_buildin_mid(temp_dir): + assert 'success' in update_share(f'{temp_dir}$uri/index.html') + assert client.get(url='/assets')['status'] == 200 + + +def test_static_variables_buildin_end(): + assert client.get(url='/index.html')['status'] == 200 + + +def test_static_variables_invalid(temp_dir): + assert 'error' in update_share(f'{temp_dir}/assets/d$r$uri') + assert 'error' in update_share(f'{temp_dir}/assets/$$uri') + assert 'error' in update_share( + [f'{temp_dir}/assets$uri', f'{temp_dir}/assets/dir', '$$uri'] + ) -- cgit