Skip to content
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

Remove resource #890

Merged
merged 2 commits into from
Apr 5, 2017
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
5 changes: 3 additions & 2 deletions appengine/standard/bigquery/main_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import main

RESOURCES = os.path.join(os.path.dirname(__file__), 'resources')
PROJECT = os.environ['GCLOUD_PROJECT']


Expand Down Expand Up @@ -49,11 +50,11 @@ def test_loggedin(app, login):
assert re.search(r'.*oauth2.*', response.headers['Location'])


def test_oauthed(resource, app, login):
def test_oauthed(app, login):
login()

mock_http = HttpMock(
resource('datasets-list.json'),
os.path.join(RESOURCES, 'datasets-list.json'),
{'status': '200'})

with mock.patch.object(main.decorator, 'http', return_value=mock_http):
Expand Down
13 changes: 7 additions & 6 deletions bigquery/api/load_data_by_post_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,16 @@

from load_data_by_post import load_data

RESOURCES = os.path.join(os.path.dirname(__file__), 'resources')
PROJECT = os.environ['GCLOUD_PROJECT']
DATASET_ID = 'ephemeral_test_dataset'
TABLE_ID = 'load_data_by_post'


@flaky
def test_load_csv_data(resource, capsys):
schema_path = resource('schema.json')
data_path = resource('data.csv')
def test_load_csv_data(capsys):
schema_path = os.path.join(RESOURCES, 'schema.json')
data_path = os.path.join(RESOURCES, 'data.csv')

load_data(
schema_path,
Expand All @@ -43,9 +44,9 @@ def test_load_csv_data(resource, capsys):


@flaky
def test_load_json_data(resource, capsys):
schema_path = resource('schema.json')
data_path = resource('data.json')
def test_load_json_data(capsys):
schema_path = os.path.join(RESOURCES, 'schema.json')
data_path = os.path.join(RESOURCES, 'data.json')

load_data(
schema_path,
Expand Down
5 changes: 3 additions & 2 deletions bigquery/api/load_data_from_csv_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,17 @@

from load_data_from_csv import main

RESOURCES = os.path.join(os.path.dirname(__file__), 'resources')
PROJECT = os.environ['GCLOUD_PROJECT']
BUCKET = os.environ['CLOUD_STORAGE_BUCKET']
DATASET_ID = 'test_dataset'
TABLE_ID = 'test_import_table'


@flaky
def test_load_table(resource):
def test_load_table():
cloud_storage_input_uri = 'gs://{}/data.csv'.format(BUCKET)
schema_file = resource('schema.json')
schema_file = os.path.join(RESOURCES, 'schema.json')

main(
PROJECT,
Expand Down
5 changes: 3 additions & 2 deletions bigquery/api/streaming_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@

import streaming

RESOURCES = os.path.join(os.path.dirname(__file__), 'resources')
PROJECT = os.environ['GCLOUD_PROJECT']
DATASET_ID = 'test_dataset'
TABLE_ID = 'test_table'


def test_stream_row_to_bigquery(resource, capsys):
with open(resource('streamrows.json'), 'r') as rows_file:
def test_stream_row_to_bigquery(capsys):
with open(os.path.join(RESOURCES, 'streamrows.json'), 'r') as rows_file:
rows = json.load(rows_file)

streaming.get_rows = lambda: rows
Expand Down
7 changes: 5 additions & 2 deletions bigquery/cloud-client/load_data_from_file_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,17 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import os

import load_data_from_file

RESOURCES = os.path.join(os.path.dirname(__file__), 'resources')
DATASET_ID = 'test_dataset'
TABLE_ID = 'test_import_table'


def test_load_table(resource, capsys):
data_path = resource('data.csv')
def test_load_table(capsys):
data_path = os.path.join(RESOURCES, 'data.csv')

load_data_from_file.load_data_from_file(
DATASET_ID,
Expand Down
41 changes: 0 additions & 41 deletions conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,49 +16,8 @@

import mock
import pytest
import requests

PROJECT = os.environ['GCLOUD_PROJECT']
BUCKET = os.environ['CLOUD_STORAGE_BUCKET']


def get_resource_path(resource, local_path):
local_resource_path = os.path.join(local_path, 'resources', *resource)

if os.path.exists(local_resource_path):
return local_resource_path
else:
raise EnvironmentError('Resource {} not found.'.format(
os.path.join(*resource)))


@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)


def fetch_gcs_resource(resource, tmpdir, _chunk_size=1024):
resp = requests.get(resource, stream=True)
dest_file = str(tmpdir.join(os.path.basename(resource)))
with open(dest_file, 'wb') as f:
for chunk in resp.iter_content(_chunk_size):
f.write(chunk)

return dest_file


@pytest.fixture(scope='module')
def remote_resource():
"""Provides a function that downloads the given resource from Cloud
Storage, returning the path to the downloaded resource."""
remote_uri = 'http://storage.googleapis.com/{}/'.format(
BUCKET)

return lambda path, tmpdir: fetch_gcs_resource(
remote_uri + path.strip('/'), tmpdir)


@pytest.fixture
Expand Down
12 changes: 9 additions & 3 deletions language/ocr_nl/main_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,14 @@
import re
import zipfile

import requests

import main

BUCKET = os.environ['CLOUD_STORAGE_BUCKET']
TEST_IMAGE_URI = 'gs://{}/language/image8.png'.format(BUCKET)
OCR_IMAGES_URI = 'http://storage.googleapis.com/{}/{}'.format(
BUCKET, 'language/ocr_nl-images-small.zip')


def test_batch_empty():
Expand Down Expand Up @@ -79,14 +83,16 @@ def test_entities_list():
assert wurl == 'http://en.wikipedia.org/wiki/Mr_Bennet'


def test_main(remote_resource, tmpdir, capsys):
def test_main(tmpdir, capsys):
images_path = str(tmpdir.mkdir('images'))

# First, pull down some test data
zip_path = remote_resource('language/ocr_nl-images-small.zip', tmpdir)
response = requests.get(OCR_IMAGES_URI)
images_file = tmpdir.join('images.zip')
images_file.write_binary(response.content)

# Extract it to the image directory
with zipfile.ZipFile(zip_path) as zfile:
with zipfile.ZipFile(str(images_file)) as zfile:
zfile.extractall(images_path)

main.main(images_path, str(tmpdir.join('ocr_nl.db')))
Expand Down
19 changes: 11 additions & 8 deletions language/sentiment/sentiment_analysis_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,37 +11,40 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import os
import re

from sentiment_analysis import analyze

RESOURCES = os.path.join(os.path.dirname(__file__), 'resources')

def test_pos(resource, capsys):
analyze(resource('pos.txt'))

def test_pos(capsys):
analyze(os.path.join(RESOURCES, 'pos.txt'))
out, err = capsys.readouterr()
score = float(re.search('score of (.+?) with', out).group(1))
magnitude = float(re.search('magnitude of (.+?)', out).group(1))
assert score * magnitude > 0


def test_neg(resource, capsys):
analyze(resource('neg.txt'))
def test_neg(capsys):
analyze(os.path.join(RESOURCES, 'neg.txt'))
out, err = capsys.readouterr()
score = float(re.search('score of (.+?) with', out).group(1))
magnitude = float(re.search('magnitude of (.+?)', out).group(1))
assert score * magnitude < 0


def test_mixed(resource, capsys):
analyze(resource('mixed.txt'))
def test_mixed(capsys):
analyze(os.path.join(RESOURCES, 'mixed.txt'))
out, err = capsys.readouterr()
score = float(re.search('score of (.+?) with', out).group(1))
assert score <= 0.3
assert score >= -0.3


def test_neutral(resource, capsys):
analyze(resource('neutral.txt'))
def test_neutral(capsys):
analyze(os.path.join(RESOURCES, 'neutral.txt'))
out, err = capsys.readouterr()
magnitude = float(re.search('magnitude of (.+?)', out).group(1))
assert magnitude <= 2.0
7 changes: 5 additions & 2 deletions language/syntax_triples/main_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,13 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import os
import re

import main

RESOURCES = os.path.join(os.path.dirname(__file__), 'resources')


def test_dependents():
text = "I am eating a delicious banana"
Expand All @@ -41,8 +44,8 @@ def test_find_triples():
assert (1, 2, 5) == triple


def test_obama_example(resource, capsys):
main.main(resource('obama_wikipedia.txt'))
def test_obama_example(capsys):
main.main(os.path.join(RESOURCES, 'obama_wikipedia.txt'))
stdout, _ = capsys.readouterr()
lines = stdout.split('\n')
assert re.match(
Expand Down
2 changes: 1 addition & 1 deletion monitoring/api/v3/cloud-client/snippets_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def test_list_resources(capsys):
assert 'pubsub_topic' in out


def test_get_resource(capsys):
def test_get_resources(capsys):
snippets.get_monitored_resource_descriptor('pubsub_topic')
out, _ = capsys.readouterr()
assert 'A topic in Google Cloud Pub/Sub' in out
Expand Down
7 changes: 5 additions & 2 deletions speech/api-client/transcribe_async_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import os
import re

from transcribe_async import main

RESOURCES = os.path.join(os.path.dirname(__file__), 'resources')

def test_main(resource, capsys):
main(resource('audio.raw'))

def test_main(capsys):
main(os.path.join(RESOURCES, 'audio.raw'))
out, err = capsys.readouterr()

assert re.search(r'how old is the Brooklyn Bridge', out, re.DOTALL | re.I)
7 changes: 5 additions & 2 deletions speech/api-client/transcribe_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import os
import re

from transcribe import main

RESOURCES = os.path.join(os.path.dirname(__file__), 'resources')

def test_main(resource, capsys):
main(resource('audio.raw'))

def test_main(capsys):
main(os.path.join(RESOURCES, 'audio.raw'))
out, err = capsys.readouterr()

assert re.search(r'how old is the Brooklyn Bridge', out, re.DOTALL | re.I)
10 changes: 7 additions & 3 deletions speech/cloud-client/transcribe_async_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,23 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import os
import re

import transcribe_async

RESOURCES = os.path.join(os.path.dirname(__file__), 'resources')

def test_transcribe(resource, capsys):
transcribe_async.transcribe_file(resource('audio.raw'))

def test_transcribe(capsys):
transcribe_async.transcribe_file(
os.path.join(RESOURCES, 'audio.raw'))
out, err = capsys.readouterr()

assert re.search(r'how old is the Brooklyn Bridge', out, re.DOTALL | re.I)


def test_transcribe_gcs(resource, capsys):
def test_transcribe_gcs(capsys):
transcribe_async.transcribe_gcs(
'gs://python-docs-samples-tests/speech/audio.flac')
out, err = capsys.readouterr()
Expand Down
8 changes: 6 additions & 2 deletions speech/cloud-client/transcribe_streaming_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,17 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import os
import re

import transcribe_streaming

RESOURCES = os.path.join(os.path.dirname(__file__), 'resources')

def test_transcribe_streaming(resource, capsys):
transcribe_streaming.transcribe_streaming(resource('audio.raw'))

def test_transcribe_streaming(capsys):
transcribe_streaming.transcribe_streaming(
os.path.join(RESOURCES, 'audio.raw'))
out, err = capsys.readouterr()

assert re.search(r'how old is the Brooklyn Bridge', out, re.DOTALL | re.I)
9 changes: 6 additions & 3 deletions speech/cloud-client/transcribe_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,22 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import os
import re

import transcribe

RESOURCES = os.path.join(os.path.dirname(__file__), 'resources')

def test_transcribe_file(resource, capsys):
transcribe.transcribe_file(resource('audio.raw'))

def test_transcribe_file(capsys):
transcribe.transcribe_file(os.path.join(RESOURCES, 'audio.raw'))
out, err = capsys.readouterr()

assert re.search(r'how old is the Brooklyn Bridge', out, re.DOTALL | re.I)


def test_transcribe_gcs(resource, capsys):
def test_transcribe_gcs(capsys):
transcribe.transcribe_gcs(
'gs://python-docs-samples-tests/speech/audio.flac')
out, err = capsys.readouterr()
Expand Down
Loading