Skip to content

Moving storage samples to py.test #190

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 18, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import os

import pytest
from testing.cloud import Config, get_resource_path


@pytest.fixture(scope='session')
def cloud_config():
"""Provides a configuration option as a proxy to environment variables."""
return Config()


@pytest.fixture(scope='module')
def resource(request):
"""Provides a function that returns the full path to a local or global
testing resource"""
local_path = os.path.dirname(request.module.__file__)
return lambda *args: get_resource_path(args, local_path)
16 changes: 7 additions & 9 deletions storage/api/compose_objects_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,12 @@
# limitations under the License.

from compose_objects import main
from testing import CloudTest


class TestComposeObjects(CloudTest):
def test_main(self):
main(
self.config.CLOUD_STORAGE_BUCKET,
'dest.txt',
[self.resource_path('file1.txt'),
self.resource_path('file2.txt')]
)
def test_main(cloud_config, resource):
main(
cloud_config.CLOUD_STORAGE_BUCKET,
'dest.txt',
[resource('file1.txt'),
resource('file2.txt')]
)
6 changes: 2 additions & 4 deletions storage/api/list_objects_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@
# limitations under the License.

from list_objects import main
from testing import CloudTest


class TestListObjects(CloudTest):
def test_main(self):
main(self.config.CLOUD_STORAGE_BUCKET)
def test_main(cloud_config):
main(cloud_config.CLOUD_STORAGE_BUCKET)
29 changes: 16 additions & 13 deletions testing/cloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,20 @@ def __getattr__(self, name):
return os.environ[name]


def get_resource_path(resource, local_path):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any particular reason you didn't stick with the same method signature as resource_path? eg with the variable-length *resource?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

local_path makes things awkward.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Howso?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm. I guess I could make it the first argument if you want - that would more or less fix it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I see what the difference is. I didn't realize that you weren't necessarily getting the resource from the local path. I see where the awkwardness lies now.

Okay - it's fine the way it is. Suggestion if you like it:

def get_resource_path(*resource, local_path=None):

and add a check for local_path before using it. That feels marginally more semantic for me, but whatever.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can do that with python 3, but not 2. It's invalid syntax to have vargs before kwargs.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll likely refactor this a bit anyway when I lift it into python-repo-tools.

global_resource_path = os.path.join(GLOBAL_RESOURCE_PATH, *resource)
local_resource_path = os.path.join(local_path, 'resources', *resource)

if os.path.exists(local_resource_path):
return local_resource_path

if os.path.exists(global_resource_path):
return global_resource_path

raise EnvironmentError('Resource {} not found.'.format(
os.path.join(*resource)))


class CloudTest(unittest.TestCase):
"""Common base class for cloud tests."""
def __init__(self, *args, **kwargs):
Expand All @@ -58,16 +72,5 @@ def setUpClass(self):
silence_requests()

def resource_path(self, *resource):
global_resource_path = os.path.join(GLOBAL_RESOURCE_PATH, *resource)
local_resource_path = os.path.join(
os.path.dirname(inspect.getfile(self.__class__)),
'resources', *resource)

if os.path.exists(local_resource_path):
return local_resource_path

if os.path.exists(global_resource_path):
return global_resource_path

raise EnvironmentError('Resource {} not found.'.format(
os.path.join(*resource)))
local_path = os.path.dirname(inspect.getfile(self.__class__))
return get_resource_path(resource, local_path)