Skip to content

Commit

Permalink
test: create bq table fixed (#184)
Browse files Browse the repository at this point in the history
Co-authored-by: tetiana-karasova <tetiana.karasova@gmail.com>
Co-authored-by: Anthonios Partheniou <partheniou@google.com>
  • Loading branch information
3 people committed Mar 10, 2022
1 parent f6eaade commit 413da95
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -171,17 +171,19 @@ def create_bq_table(dataset, table_name, schema_file_path):
"""Create a BigQuery table"""
full_table_id = f"{project_id}.{dataset}.{table_name}"
bq = bigquery.Client()
print(f"Creating BigQuery table {full_table_id}")
print(f"Check if BQ table {full_table_id} exists")
try:
bq.get_table(full_table_id)
print(f"table {full_table_id} already exists")
print(f"table {full_table_id} exists and will be deleted")
delete_bq_table(dataset, table_name)
except NotFound:
# Construct a Table object to send to the API.
with open(schema_file_path, "rb") as schema:
schema_dict = json.load(schema)
table = bigquery.Table(full_table_id, schema=schema_dict)
bq.create_table(table)
print("table is created")
print(f"table {full_table_id} does not exist")
# Construct a Table object to send to the API.
with open(schema_file_path, "rb") as schema:
schema_dict = json.load(schema)
table = bigquery.Table(full_table_id, schema=schema_dict)
bq.create_table(table)
print(f"table {full_table_id} is created")


def delete_bq_table(dataset, table_name):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@

# The request timestamp
current_date = datetime.datetime.now()
outdated_date = datetime.datetime.now() - datetime.timedelta(days=1)


# add fulfillment request
Expand Down Expand Up @@ -63,8 +62,8 @@ def add_fulfillment_places(product_name: str, timestamp, place_id):

# This is a long running operation and its result is not immediately present with get operations,
# thus we simulate wait with sleep method.
print("---add fulfillment places, wait 40 seconds :---")
time.sleep(40)
print("---add fulfillment places, wait 90 seconds :---")
time.sleep(90)


# [END retail_add_fulfillment_places]
Expand All @@ -74,7 +73,4 @@ def add_fulfillment_places(product_name: str, timestamp, place_id):
print("------add fulfilment places with current date: {}-----".format(current_date))
add_fulfillment_places(product_name, current_date, "store2")
get_product(product_name)
print("------add outdated fulfilment places: {}-----".format(outdated_date))
add_fulfillment_places(product_name, outdated_date, "store3")
get_product(product_name)
delete_product(product_name)
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@

# The request timestamp
current_date = datetime.datetime.now()
outdated_date = datetime.datetime.now() - datetime.timedelta(days=1)


# remove fulfillment request
Expand Down Expand Up @@ -63,8 +62,8 @@ def remove_fulfillment_places(product_name: str, timestamp, store_id):

# This is a long running operation and its result is not immediately present with get operations,
# thus we simulate wait with sleep method.
print("---remove fulfillment places, wait 40 seconds:---")
time.sleep(40)
print("---remove fulfillment places, wait 90 seconds:---")
time.sleep(90)


# [END retail_remove_fulfillment_places]
Expand All @@ -74,7 +73,4 @@ def remove_fulfillment_places(product_name: str, timestamp, store_id):
print("------remove fulfilment places with current date: {}-----".format(current_date))
remove_fulfillment_places(product_name, current_date, "store0")
get_product(product_name)
print("------remove outdated fulfilment places: {}-----".format(outdated_date))
remove_fulfillment_places(product_name, outdated_date, "store1")
get_product(product_name)
delete_product(product_name)
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
def get_product_with_inventory_info(product_name: str) -> Product:
price_info = PriceInfo()
price_info.price = 15.0
price_info.original_price = 20.0
price_info.original_price = 60.0
price_info.cost = 8.0
price_info.currency_code = "USD"

Expand All @@ -65,8 +65,6 @@ def get_product_with_inventory_info(product_name: str) -> Product:
def get_set_inventory_request(product_name: str) -> SetInventoryRequest:
# The request timestamp
request_time = datetime.datetime.now()
# The out-of-order request timestamp
# request_time = datetime.datetime.now() - datetime.timedelta(days=1)
set_mask = FieldMask(
paths=["price_info", "availability", "fulfillment_info", "available_quantity"]
)
Expand All @@ -90,8 +88,8 @@ def set_inventory(product_name: str):

# This is a long running operation and its result is not immediately present with get operations,
# thus we simulate wait with sleep method.
print("---set inventory, wait 60 seconds:---")
time.sleep(60)
print("---set inventory, wait 90 seconds:---")
time.sleep(90)


create_product(product_id)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,17 +180,19 @@ def create_bq_table(dataset, table_name, schema_file_path):
"""Create a BigQuery table"""
full_table_id = f"{project_id}.{dataset}.{table_name}"
bq = bigquery.Client()
print(f"Creating BigQuery table {full_table_id}")
print(f"Check if BQ table {full_table_id} exists")
try:
bq.get_table(full_table_id)
print(f"table {full_table_id} already exists")
print(f"table {full_table_id} exists and will be deleted")
delete_bq_table(dataset, table_name)
except NotFound:
# Construct a Table object to send to the API.
with open(schema_file_path, "rb") as schema:
schema_dict = json.load(schema)
table = bigquery.Table(full_table_id, schema=schema_dict)
bq.create_table(table)
print("table is created")
print(f"table {full_table_id} does not exist")
# Construct a Table object to send to the API.
with open(schema_file_path, "rb") as schema:
schema_dict = json.load(schema)
table = bigquery.Table(full_table_id, schema=schema_dict)
bq.create_table(table)
print(f"table {full_table_id} is created")


def delete_bq_table(dataset, table_name):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@

from setup_product.setup_cleanup import create_product, delete_product

# from google.protobuf.field_mask_pb2 import FieldMask
# from google.protobuf.field_mask_pb2 import FieldMask


project_id = google.auth.default()[1]
default_branch_name = (
"projects/"
Expand Down

0 comments on commit 413da95

Please sign in to comment.