Skip to content

Commit 4d5d8cb

Browse files
author
Jon Wayne Parrott
committed
Moving storage samples to py.test
1 parent d0e18c2 commit 4d5d8cb

File tree

4 files changed

+43
-26
lines changed

4 files changed

+43
-26
lines changed

conftest.py

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import os
2+
3+
import pytest
4+
from testing.cloud import Config, get_resource_path
5+
6+
7+
@pytest.fixture(scope='session')
8+
def cloud_config():
9+
"""Provides a configuration option as a proxy to environment variables."""
10+
return Config()
11+
12+
13+
@pytest.fixture(scope='module')
14+
def resource(request):
15+
"""Provides a function that returns the full path to a local or global
16+
testing resource"""
17+
local_path = os.path.dirname(request.module.__file__)
18+
return lambda *args: get_resource_path(args, local_path)

storage/api/compose_objects_test.py

+7-9
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,12 @@
1212
# limitations under the License.
1313

1414
from compose_objects import main
15-
from testing import CloudTest
1615

1716

18-
class TestComposeObjects(CloudTest):
19-
def test_main(self):
20-
main(
21-
self.config.CLOUD_STORAGE_BUCKET,
22-
'dest.txt',
23-
[self.resource_path('file1.txt'),
24-
self.resource_path('file2.txt')]
25-
)
17+
def test_main(cloud_config, resource):
18+
main(
19+
cloud_config.CLOUD_STORAGE_BUCKET,
20+
'dest.txt',
21+
[resource('file1.txt'),
22+
resource('file2.txt')]
23+
)

storage/api/list_objects_test.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@
1212
# limitations under the License.
1313

1414
from list_objects import main
15-
from testing import CloudTest
1615

1716

18-
class TestListObjects(CloudTest):
19-
def test_main(self):
20-
main(self.config.CLOUD_STORAGE_BUCKET)
17+
def test_main(cloud_config):
18+
main(cloud_config.CLOUD_STORAGE_BUCKET)

testing/cloud.py

+16-13
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,20 @@ def __getattr__(self, name):
4747
return os.environ[name]
4848

4949

50+
def get_resource_path(resource, local_path):
51+
global_resource_path = os.path.join(GLOBAL_RESOURCE_PATH, *resource)
52+
local_resource_path = os.path.join(local_path, 'resources', *resource)
53+
54+
if os.path.exists(local_resource_path):
55+
return local_resource_path
56+
57+
if os.path.exists(global_resource_path):
58+
return global_resource_path
59+
60+
raise EnvironmentError('Resource {} not found.'.format(
61+
os.path.join(*resource)))
62+
63+
5064
class CloudTest(unittest.TestCase):
5165
"""Common base class for cloud tests."""
5266
def __init__(self, *args, **kwargs):
@@ -58,16 +72,5 @@ def setUpClass(self):
5872
silence_requests()
5973

6074
def resource_path(self, *resource):
61-
global_resource_path = os.path.join(GLOBAL_RESOURCE_PATH, *resource)
62-
local_resource_path = os.path.join(
63-
os.path.dirname(inspect.getfile(self.__class__)),
64-
'resources', *resource)
65-
66-
if os.path.exists(local_resource_path):
67-
return local_resource_path
68-
69-
if os.path.exists(global_resource_path):
70-
return global_resource_path
71-
72-
raise EnvironmentError('Resource {} not found.'.format(
73-
os.path.join(*resource)))
75+
local_path = inspect.getfile(self.__class__)
76+
return get_resource_path(resource, local_path)

0 commit comments

Comments
 (0)