Skip to content

Commit

Permalink
fix: vision product search tests to call setup and teardown and use u…
Browse files Browse the repository at this point in the history
…uid (#2830)
  • Loading branch information
nnegrey authored Feb 12, 2020
1 parent 4aa16fd commit 47af18d
Show file tree
Hide file tree
Showing 8 changed files with 145 additions and 119 deletions.
45 changes: 45 additions & 0 deletions vision/cloud-client/product_search/create_product_set_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Copyright 2020 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import os
import uuid

import pytest

from product_set_management import (
create_product_set, delete_product_set, list_product_sets)


PROJECT_ID = os.getenv('GCLOUD_PROJECT')
LOCATION = 'us-west1'

PRODUCT_SET_DISPLAY_NAME = 'fake_product_set_display_name_for_testing'
PRODUCT_SET_ID = 'test_{}'.format(uuid.uuid4())


@pytest.fixture(scope="function", autouse=True)
def teardown():
yield

# tear down
delete_product_set(PROJECT_ID, LOCATION, PRODUCT_SET_ID)


def test_create_product_set(capsys):
create_product_set(
PROJECT_ID, LOCATION, PRODUCT_SET_ID,
PRODUCT_SET_DISPLAY_NAME)
list_product_sets(PROJECT_ID, LOCATION)
out, _ = capsys.readouterr()
assert PRODUCT_SET_ID in out
45 changes: 45 additions & 0 deletions vision/cloud-client/product_search/create_product_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Copyright 2020 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import os
import uuid

import pytest

from product_management import create_product, delete_product, list_products


PROJECT_ID = os.getenv('GCLOUD_PROJECT')
LOCATION = 'us-west1'

PRODUCT_DISPLAY_NAME = 'fake_product_display_name_for_testing'
PRODUCT_CATEGORY = 'homegoods'
PRODUCT_ID = 'test_{}'.format(uuid.uuid4())


@pytest.fixture(scope="function", autouse=True)
def teardown():
yield

# tear down
delete_product(PROJECT_ID, LOCATION, PRODUCT_ID)


def test_create_product(capsys):
create_product(
PROJECT_ID, LOCATION, PRODUCT_ID,
PRODUCT_DISPLAY_NAME, PRODUCT_CATEGORY)
list_products(PROJECT_ID, LOCATION)
out, _ = capsys.readouterr()
assert PRODUCT_ID in out
65 changes: 26 additions & 39 deletions vision/cloud-client/product_search/import_product_sets_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@
# limitations under the License.

import os
import uuid

import pytest

from google.cloud import storage

from import_product_sets import import_product_sets
from product_in_product_set_management import list_products_in_product_set
from product_management import delete_product, list_products
Expand All @@ -26,48 +29,38 @@
PROJECT_ID = os.getenv('GCLOUD_PROJECT')
LOCATION = 'us-west1'

GCS_URI = 'gs://cloud-samples-data/vision/product_search/product_sets.csv'
FILENAME = uuid.uuid4()
GCS_URI = 'gs://{}/vision/{}.csv'.format(PROJECT_ID, FILENAME)
PRODUCT_SET_DISPLAY_NAME = 'fake_product_set_display_name_for_testing'
PRODUCT_SET_ID = 'fake_product_set_id_for_testing'
PRODUCT_ID_1 = 'fake_product_id_for_testing_1'
PRODUCT_ID_2 = 'fake_product_id_for_testing_2'
PRODUCT_SET_ID = 'test_{}'.format(uuid.uuid4())
PRODUCT_ID_1 = 'test_{}'.format(uuid.uuid4())
IMAGE_URI_1 = 'shoes_1.jpg'
IMAGE_URI_2 = 'shoes_2.jpg'


@pytest.fixture
def teardown():
# no set up, tear down only
yield None
@pytest.fixture(scope="function", autouse=True)
def setup_teardown():
# Create the product set csv file locally and upload it to GCS
# This is so that there is a unique product set ID for all python version
# tests.
client = storage.Client(project=PROJECT_ID)
bucket = client.get_bucket(PROJECT_ID)
blob = storage.Blob("vision/{}.csv".format(FILENAME), bucket)
blob.upload_from_string(
'"gs://cloud-samples-data/vision/product_search/shoes_1.jpg",' +
'"{}",'.format(IMAGE_URI_1) +
'"{}",'.format(PRODUCT_SET_ID) +
'"{}",'.format(PRODUCT_ID_1) +
'"apparel",,"style=womens","0.1,0.1,0.9,0.1,0.9,0.9,0.1,0.9"')

yield

delete_product(PROJECT_ID, LOCATION, PRODUCT_ID_1)
delete_product(PROJECT_ID, LOCATION, PRODUCT_ID_2)
delete_product_set(PROJECT_ID, LOCATION, PRODUCT_SET_ID)
# Delete the created file
blob.delete(client)


def test_import_product_sets(capsys, teardown):
list_product_sets(PROJECT_ID, LOCATION)
out, _ = capsys.readouterr()
assert PRODUCT_SET_ID not in out

list_products(PROJECT_ID, LOCATION)
out, _ = capsys.readouterr()
assert PRODUCT_ID_1 not in out
assert PRODUCT_ID_2 not in out

list_products_in_product_set(PROJECT_ID, LOCATION, PRODUCT_SET_ID)
out, _ = capsys.readouterr()
assert PRODUCT_ID_1 not in out
assert PRODUCT_ID_2 not in out

list_reference_images(PROJECT_ID, LOCATION, PRODUCT_ID_1)
out, _ = capsys.readouterr()
assert IMAGE_URI_1 not in out

list_reference_images(PROJECT_ID, LOCATION, PRODUCT_ID_2)
out, _ = capsys.readouterr()
assert IMAGE_URI_2 not in out

def test_import_product_sets(capsys):
import_product_sets(PROJECT_ID, LOCATION, GCS_URI)

list_product_sets(PROJECT_ID, LOCATION)
Expand All @@ -77,17 +70,11 @@ def test_import_product_sets(capsys, teardown):
list_products(PROJECT_ID, LOCATION)
out, _ = capsys.readouterr()
assert PRODUCT_ID_1 in out
assert PRODUCT_ID_2 in out

list_products_in_product_set(PROJECT_ID, LOCATION, PRODUCT_SET_ID)
out, _ = capsys.readouterr()
assert PRODUCT_ID_1 in out
assert PRODUCT_ID_2 in out

list_reference_images(PROJECT_ID, LOCATION, PRODUCT_ID_1)
out, _ = capsys.readouterr()
assert IMAGE_URI_1 in out

list_reference_images(PROJECT_ID, LOCATION, PRODUCT_ID_2)
out, _ = capsys.readouterr()
assert IMAGE_URI_2 in out
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.

import os
import uuid

import pytest

Expand All @@ -28,42 +29,38 @@
LOCATION = 'us-west1'

PRODUCT_SET_DISPLAY_NAME = 'fake_product_set_display_name_for_testing'
PRODUCT_SET_ID = 'fake_product_set_id_for_testing'
PRODUCT_SET_ID = 'test_set_{}'.format(uuid.uuid4())

PRODUCT_DISPLAY_NAME = 'fake_product_display_name_for_testing'
PRODUCT_CATEGORY = 'homegoods'
PRODUCT_ID = 'fake_product_id_for_testing'
PRODUCT_ID = 'test_product_{}'.format(uuid.uuid4())


@pytest.fixture
def product_and_product_set():
@pytest.fixture(scope="function", autouse=True)
def setup_teardown():
# set up
create_product_set(
PROJECT_ID, LOCATION, PRODUCT_SET_ID, PRODUCT_SET_DISPLAY_NAME)
create_product(
PROJECT_ID, LOCATION, PRODUCT_ID,
PRODUCT_DISPLAY_NAME, PRODUCT_CATEGORY)

yield None
yield

# tear down
delete_product(PROJECT_ID, LOCATION, PRODUCT_ID)
delete_product_set(PROJECT_ID, LOCATION, PRODUCT_SET_ID)


def test_add_product_to_product_set(capsys, product_and_product_set):
list_products_in_product_set(PROJECT_ID, LOCATION, PRODUCT_SET_ID)
out, _ = capsys.readouterr()
assert 'Product id: {}'.format(PRODUCT_ID) not in out

def test_add_product_to_product_set(capsys):
add_product_to_product_set(
PROJECT_ID, LOCATION, PRODUCT_ID, PRODUCT_SET_ID)
list_products_in_product_set(PROJECT_ID, LOCATION, PRODUCT_SET_ID)
out, _ = capsys.readouterr()
assert 'Product id: {}'.format(PRODUCT_ID) in out


def test_remove_product_from_product_set(capsys, product_and_product_set):
def test_remove_product_from_product_set(capsys):
add_product_to_product_set(
PROJECT_ID, LOCATION, PRODUCT_ID, PRODUCT_SET_ID)
list_products_in_product_set(PROJECT_ID, LOCATION, PRODUCT_SET_ID)
Expand All @@ -77,7 +74,7 @@ def test_remove_product_from_product_set(capsys, product_and_product_set):
assert 'Product id: {}'.format(PRODUCT_ID) not in out


def test_purge_products_in_product_set(capsys, product_and_product_set):
def test_purge_products_in_product_set(capsys):
add_product_to_product_set(
PROJECT_ID, LOCATION, PRODUCT_ID, PRODUCT_SET_ID)
list_products(PROJECT_ID, LOCATION)
Expand All @@ -90,5 +87,3 @@ def test_purge_products_in_product_set(capsys, product_and_product_set):
list_products(PROJECT_ID, LOCATION)
out, _ = capsys.readouterr()
assert 'Product id: {}'.format(PRODUCT_ID) not in out

print(out)
37 changes: 8 additions & 29 deletions vision/cloud-client/product_search/product_management_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@
# limitations under the License.

import os
import uuid

import pytest

from product_management import (
create_product, delete_product, get_product, list_products,
create_product, delete_product, list_products,
purge_orphan_products, update_product_labels)


Expand All @@ -26,13 +27,13 @@

PRODUCT_DISPLAY_NAME = 'fake_product_display_name_for_testing'
PRODUCT_CATEGORY = 'homegoods'
PRODUCT_ID = 'fake_product_id_for_testing'
PRODUCT_ID = 'test_{}'.format(uuid.uuid4())
KEY = 'fake_key_for_testing'
VALUE = 'fake_value_for_testing'


@pytest.fixture
def product():
@pytest.fixture(scope="function", autouse=True)
def setup_teardown():
# set up
create_product(
PROJECT_ID, LOCATION, PRODUCT_ID,
Expand All @@ -44,22 +45,7 @@ def product():
delete_product(PROJECT_ID, LOCATION, PRODUCT_ID)


def test_create_product(capsys):
list_products(PROJECT_ID, LOCATION)
out, _ = capsys.readouterr()
assert PRODUCT_ID not in out

create_product(
PROJECT_ID, LOCATION, PRODUCT_ID,
PRODUCT_DISPLAY_NAME, PRODUCT_CATEGORY)
list_products(PROJECT_ID, LOCATION)
out, _ = capsys.readouterr()
assert PRODUCT_ID in out

delete_product(PROJECT_ID, LOCATION, PRODUCT_ID)


def test_delete_product(capsys, product):
def test_delete_product(capsys):
list_products(PROJECT_ID, LOCATION)
out, _ = capsys.readouterr()
assert PRODUCT_ID in out
Expand All @@ -71,21 +57,14 @@ def test_delete_product(capsys, product):
assert PRODUCT_ID not in out


def test_update_product_labels(capsys, product):
get_product(PROJECT_ID, LOCATION, PRODUCT_ID)
out, _ = capsys.readouterr()
assert KEY not in out
assert VALUE not in out

def test_update_product_labels(capsys):
update_product_labels(PROJECT_ID, LOCATION, PRODUCT_ID, KEY, VALUE)
out, _ = capsys.readouterr()
assert KEY in out
assert VALUE in out

delete_product(PROJECT_ID, LOCATION, PRODUCT_ID)


def test_purge_orphan_products(capsys, product):
def test_purge_orphan_products(capsys):
list_products(PROJECT_ID, LOCATION)
out, _ = capsys.readouterr()
assert PRODUCT_ID in out
Expand Down
29 changes: 5 additions & 24 deletions vision/cloud-client/product_search/product_set_management_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.

import os
import uuid

import pytest

Expand All @@ -24,37 +25,17 @@
LOCATION = 'us-west1'

PRODUCT_SET_DISPLAY_NAME = 'fake_product_set_display_name_for_testing'
PRODUCT_SET_ID = 'fake_product_set_id_for_testing'
PRODUCT_SET_ID = 'test_{}'.format(uuid.uuid4())


@pytest.fixture
def product_set():
@pytest.fixture(scope="function", autouse=True)
def setup():
# set up
create_product_set(
PROJECT_ID, LOCATION, PRODUCT_SET_ID, PRODUCT_SET_DISPLAY_NAME)

yield None

# tear down
delete_product_set(PROJECT_ID, LOCATION, PRODUCT_SET_ID)


def test_create_product_set(capsys):
list_product_sets(PROJECT_ID, LOCATION)
out, _ = capsys.readouterr()
assert PRODUCT_SET_ID not in out

create_product_set(
PROJECT_ID, LOCATION, PRODUCT_SET_ID,
PRODUCT_SET_DISPLAY_NAME)
list_product_sets(PROJECT_ID, LOCATION)
out, _ = capsys.readouterr()
assert PRODUCT_SET_ID in out

delete_product_set(PROJECT_ID, LOCATION, PRODUCT_SET_ID)


def test_delete_product_set(capsys, product_set):
def test_delete_product_set(capsys):
list_product_sets(PROJECT_ID, LOCATION)
out, _ = capsys.readouterr()
assert PRODUCT_SET_ID in out
Expand Down
Loading

0 comments on commit 47af18d

Please sign in to comment.