Skip to content

Commit 47af18d

Browse files
authored
fix: vision product search tests to call setup and teardown and use uuid (#2830)
1 parent 4aa16fd commit 47af18d

8 files changed

+145
-119
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Copyright 2020 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import os
16+
import uuid
17+
18+
import pytest
19+
20+
from product_set_management import (
21+
create_product_set, delete_product_set, list_product_sets)
22+
23+
24+
PROJECT_ID = os.getenv('GCLOUD_PROJECT')
25+
LOCATION = 'us-west1'
26+
27+
PRODUCT_SET_DISPLAY_NAME = 'fake_product_set_display_name_for_testing'
28+
PRODUCT_SET_ID = 'test_{}'.format(uuid.uuid4())
29+
30+
31+
@pytest.fixture(scope="function", autouse=True)
32+
def teardown():
33+
yield
34+
35+
# tear down
36+
delete_product_set(PROJECT_ID, LOCATION, PRODUCT_SET_ID)
37+
38+
39+
def test_create_product_set(capsys):
40+
create_product_set(
41+
PROJECT_ID, LOCATION, PRODUCT_SET_ID,
42+
PRODUCT_SET_DISPLAY_NAME)
43+
list_product_sets(PROJECT_ID, LOCATION)
44+
out, _ = capsys.readouterr()
45+
assert PRODUCT_SET_ID in out
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Copyright 2020 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import os
16+
import uuid
17+
18+
import pytest
19+
20+
from product_management import create_product, delete_product, list_products
21+
22+
23+
PROJECT_ID = os.getenv('GCLOUD_PROJECT')
24+
LOCATION = 'us-west1'
25+
26+
PRODUCT_DISPLAY_NAME = 'fake_product_display_name_for_testing'
27+
PRODUCT_CATEGORY = 'homegoods'
28+
PRODUCT_ID = 'test_{}'.format(uuid.uuid4())
29+
30+
31+
@pytest.fixture(scope="function", autouse=True)
32+
def teardown():
33+
yield
34+
35+
# tear down
36+
delete_product(PROJECT_ID, LOCATION, PRODUCT_ID)
37+
38+
39+
def test_create_product(capsys):
40+
create_product(
41+
PROJECT_ID, LOCATION, PRODUCT_ID,
42+
PRODUCT_DISPLAY_NAME, PRODUCT_CATEGORY)
43+
list_products(PROJECT_ID, LOCATION)
44+
out, _ = capsys.readouterr()
45+
assert PRODUCT_ID in out

vision/cloud-client/product_search/import_product_sets_test.py

Lines changed: 26 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,12 @@
1313
# limitations under the License.
1414

1515
import os
16+
import uuid
1617

1718
import pytest
1819

20+
from google.cloud import storage
21+
1922
from import_product_sets import import_product_sets
2023
from product_in_product_set_management import list_products_in_product_set
2124
from product_management import delete_product, list_products
@@ -26,48 +29,38 @@
2629
PROJECT_ID = os.getenv('GCLOUD_PROJECT')
2730
LOCATION = 'us-west1'
2831

29-
GCS_URI = 'gs://cloud-samples-data/vision/product_search/product_sets.csv'
32+
FILENAME = uuid.uuid4()
33+
GCS_URI = 'gs://{}/vision/{}.csv'.format(PROJECT_ID, FILENAME)
3034
PRODUCT_SET_DISPLAY_NAME = 'fake_product_set_display_name_for_testing'
31-
PRODUCT_SET_ID = 'fake_product_set_id_for_testing'
32-
PRODUCT_ID_1 = 'fake_product_id_for_testing_1'
33-
PRODUCT_ID_2 = 'fake_product_id_for_testing_2'
35+
PRODUCT_SET_ID = 'test_{}'.format(uuid.uuid4())
36+
PRODUCT_ID_1 = 'test_{}'.format(uuid.uuid4())
3437
IMAGE_URI_1 = 'shoes_1.jpg'
35-
IMAGE_URI_2 = 'shoes_2.jpg'
3638

3739

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

4357
delete_product(PROJECT_ID, LOCATION, PRODUCT_ID_1)
44-
delete_product(PROJECT_ID, LOCATION, PRODUCT_ID_2)
4558
delete_product_set(PROJECT_ID, LOCATION, PRODUCT_SET_ID)
59+
# Delete the created file
60+
blob.delete(client)
4661

4762

48-
def test_import_product_sets(capsys, teardown):
49-
list_product_sets(PROJECT_ID, LOCATION)
50-
out, _ = capsys.readouterr()
51-
assert PRODUCT_SET_ID not in out
52-
53-
list_products(PROJECT_ID, LOCATION)
54-
out, _ = capsys.readouterr()
55-
assert PRODUCT_ID_1 not in out
56-
assert PRODUCT_ID_2 not in out
57-
58-
list_products_in_product_set(PROJECT_ID, LOCATION, PRODUCT_SET_ID)
59-
out, _ = capsys.readouterr()
60-
assert PRODUCT_ID_1 not in out
61-
assert PRODUCT_ID_2 not in out
62-
63-
list_reference_images(PROJECT_ID, LOCATION, PRODUCT_ID_1)
64-
out, _ = capsys.readouterr()
65-
assert IMAGE_URI_1 not in out
66-
67-
list_reference_images(PROJECT_ID, LOCATION, PRODUCT_ID_2)
68-
out, _ = capsys.readouterr()
69-
assert IMAGE_URI_2 not in out
70-
63+
def test_import_product_sets(capsys):
7164
import_product_sets(PROJECT_ID, LOCATION, GCS_URI)
7265

7366
list_product_sets(PROJECT_ID, LOCATION)
@@ -77,17 +70,11 @@ def test_import_product_sets(capsys, teardown):
7770
list_products(PROJECT_ID, LOCATION)
7871
out, _ = capsys.readouterr()
7972
assert PRODUCT_ID_1 in out
80-
assert PRODUCT_ID_2 in out
8173

8274
list_products_in_product_set(PROJECT_ID, LOCATION, PRODUCT_SET_ID)
8375
out, _ = capsys.readouterr()
8476
assert PRODUCT_ID_1 in out
85-
assert PRODUCT_ID_2 in out
8677

8778
list_reference_images(PROJECT_ID, LOCATION, PRODUCT_ID_1)
8879
out, _ = capsys.readouterr()
8980
assert IMAGE_URI_1 in out
90-
91-
list_reference_images(PROJECT_ID, LOCATION, PRODUCT_ID_2)
92-
out, _ = capsys.readouterr()
93-
assert IMAGE_URI_2 in out

vision/cloud-client/product_search/product_in_product_set_management_test.py

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# limitations under the License.
1414

1515
import os
16+
import uuid
1617

1718
import pytest
1819

@@ -28,42 +29,38 @@
2829
LOCATION = 'us-west1'
2930

3031
PRODUCT_SET_DISPLAY_NAME = 'fake_product_set_display_name_for_testing'
31-
PRODUCT_SET_ID = 'fake_product_set_id_for_testing'
32+
PRODUCT_SET_ID = 'test_set_{}'.format(uuid.uuid4())
3233

3334
PRODUCT_DISPLAY_NAME = 'fake_product_display_name_for_testing'
3435
PRODUCT_CATEGORY = 'homegoods'
35-
PRODUCT_ID = 'fake_product_id_for_testing'
36+
PRODUCT_ID = 'test_product_{}'.format(uuid.uuid4())
3637

3738

38-
@pytest.fixture
39-
def product_and_product_set():
39+
@pytest.fixture(scope="function", autouse=True)
40+
def setup_teardown():
4041
# set up
4142
create_product_set(
4243
PROJECT_ID, LOCATION, PRODUCT_SET_ID, PRODUCT_SET_DISPLAY_NAME)
4344
create_product(
4445
PROJECT_ID, LOCATION, PRODUCT_ID,
4546
PRODUCT_DISPLAY_NAME, PRODUCT_CATEGORY)
4647

47-
yield None
48+
yield
4849

4950
# tear down
5051
delete_product(PROJECT_ID, LOCATION, PRODUCT_ID)
5152
delete_product_set(PROJECT_ID, LOCATION, PRODUCT_SET_ID)
5253

5354

54-
def test_add_product_to_product_set(capsys, product_and_product_set):
55-
list_products_in_product_set(PROJECT_ID, LOCATION, PRODUCT_SET_ID)
56-
out, _ = capsys.readouterr()
57-
assert 'Product id: {}'.format(PRODUCT_ID) not in out
58-
55+
def test_add_product_to_product_set(capsys):
5956
add_product_to_product_set(
6057
PROJECT_ID, LOCATION, PRODUCT_ID, PRODUCT_SET_ID)
6158
list_products_in_product_set(PROJECT_ID, LOCATION, PRODUCT_SET_ID)
6259
out, _ = capsys.readouterr()
6360
assert 'Product id: {}'.format(PRODUCT_ID) in out
6461

6562

66-
def test_remove_product_from_product_set(capsys, product_and_product_set):
63+
def test_remove_product_from_product_set(capsys):
6764
add_product_to_product_set(
6865
PROJECT_ID, LOCATION, PRODUCT_ID, PRODUCT_SET_ID)
6966
list_products_in_product_set(PROJECT_ID, LOCATION, PRODUCT_SET_ID)
@@ -77,7 +74,7 @@ def test_remove_product_from_product_set(capsys, product_and_product_set):
7774
assert 'Product id: {}'.format(PRODUCT_ID) not in out
7875

7976

80-
def test_purge_products_in_product_set(capsys, product_and_product_set):
77+
def test_purge_products_in_product_set(capsys):
8178
add_product_to_product_set(
8279
PROJECT_ID, LOCATION, PRODUCT_ID, PRODUCT_SET_ID)
8380
list_products(PROJECT_ID, LOCATION)
@@ -90,5 +87,3 @@ def test_purge_products_in_product_set(capsys, product_and_product_set):
9087
list_products(PROJECT_ID, LOCATION)
9188
out, _ = capsys.readouterr()
9289
assert 'Product id: {}'.format(PRODUCT_ID) not in out
93-
94-
print(out)

vision/cloud-client/product_search/product_management_test.py

Lines changed: 8 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,12 @@
1313
# limitations under the License.
1414

1515
import os
16+
import uuid
1617

1718
import pytest
1819

1920
from product_management import (
20-
create_product, delete_product, get_product, list_products,
21+
create_product, delete_product, list_products,
2122
purge_orphan_products, update_product_labels)
2223

2324

@@ -26,13 +27,13 @@
2627

2728
PRODUCT_DISPLAY_NAME = 'fake_product_display_name_for_testing'
2829
PRODUCT_CATEGORY = 'homegoods'
29-
PRODUCT_ID = 'fake_product_id_for_testing'
30+
PRODUCT_ID = 'test_{}'.format(uuid.uuid4())
3031
KEY = 'fake_key_for_testing'
3132
VALUE = 'fake_value_for_testing'
3233

3334

34-
@pytest.fixture
35-
def product():
35+
@pytest.fixture(scope="function", autouse=True)
36+
def setup_teardown():
3637
# set up
3738
create_product(
3839
PROJECT_ID, LOCATION, PRODUCT_ID,
@@ -44,22 +45,7 @@ def product():
4445
delete_product(PROJECT_ID, LOCATION, PRODUCT_ID)
4546

4647

47-
def test_create_product(capsys):
48-
list_products(PROJECT_ID, LOCATION)
49-
out, _ = capsys.readouterr()
50-
assert PRODUCT_ID not in out
51-
52-
create_product(
53-
PROJECT_ID, LOCATION, PRODUCT_ID,
54-
PRODUCT_DISPLAY_NAME, PRODUCT_CATEGORY)
55-
list_products(PROJECT_ID, LOCATION)
56-
out, _ = capsys.readouterr()
57-
assert PRODUCT_ID in out
58-
59-
delete_product(PROJECT_ID, LOCATION, PRODUCT_ID)
60-
61-
62-
def test_delete_product(capsys, product):
48+
def test_delete_product(capsys):
6349
list_products(PROJECT_ID, LOCATION)
6450
out, _ = capsys.readouterr()
6551
assert PRODUCT_ID in out
@@ -71,21 +57,14 @@ def test_delete_product(capsys, product):
7157
assert PRODUCT_ID not in out
7258

7359

74-
def test_update_product_labels(capsys, product):
75-
get_product(PROJECT_ID, LOCATION, PRODUCT_ID)
76-
out, _ = capsys.readouterr()
77-
assert KEY not in out
78-
assert VALUE not in out
79-
60+
def test_update_product_labels(capsys):
8061
update_product_labels(PROJECT_ID, LOCATION, PRODUCT_ID, KEY, VALUE)
8162
out, _ = capsys.readouterr()
8263
assert KEY in out
8364
assert VALUE in out
8465

85-
delete_product(PROJECT_ID, LOCATION, PRODUCT_ID)
86-
8766

88-
def test_purge_orphan_products(capsys, product):
67+
def test_purge_orphan_products(capsys):
8968
list_products(PROJECT_ID, LOCATION)
9069
out, _ = capsys.readouterr()
9170
assert PRODUCT_ID in out

vision/cloud-client/product_search/product_set_management_test.py

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# limitations under the License.
1414

1515
import os
16+
import uuid
1617

1718
import pytest
1819

@@ -24,37 +25,17 @@
2425
LOCATION = 'us-west1'
2526

2627
PRODUCT_SET_DISPLAY_NAME = 'fake_product_set_display_name_for_testing'
27-
PRODUCT_SET_ID = 'fake_product_set_id_for_testing'
28+
PRODUCT_SET_ID = 'test_{}'.format(uuid.uuid4())
2829

2930

30-
@pytest.fixture
31-
def product_set():
31+
@pytest.fixture(scope="function", autouse=True)
32+
def setup():
3233
# set up
3334
create_product_set(
3435
PROJECT_ID, LOCATION, PRODUCT_SET_ID, PRODUCT_SET_DISPLAY_NAME)
3536

36-
yield None
3737

38-
# tear down
39-
delete_product_set(PROJECT_ID, LOCATION, PRODUCT_SET_ID)
40-
41-
42-
def test_create_product_set(capsys):
43-
list_product_sets(PROJECT_ID, LOCATION)
44-
out, _ = capsys.readouterr()
45-
assert PRODUCT_SET_ID not in out
46-
47-
create_product_set(
48-
PROJECT_ID, LOCATION, PRODUCT_SET_ID,
49-
PRODUCT_SET_DISPLAY_NAME)
50-
list_product_sets(PROJECT_ID, LOCATION)
51-
out, _ = capsys.readouterr()
52-
assert PRODUCT_SET_ID in out
53-
54-
delete_product_set(PROJECT_ID, LOCATION, PRODUCT_SET_ID)
55-
56-
57-
def test_delete_product_set(capsys, product_set):
38+
def test_delete_product_set(capsys):
5839
list_product_sets(PROJECT_ID, LOCATION)
5940
out, _ = capsys.readouterr()
6041
assert PRODUCT_SET_ID in out

0 commit comments

Comments
 (0)