Skip to content

Commit

Permalink
Fixing unit test failures and add metadata function
Browse files Browse the repository at this point in the history
Fix unit test failures for python3.6 .
Add metadata function to get metadata of table.
  • Loading branch information
sangramql committed Jul 11, 2018
1 parent c01df0a commit b12a8be
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 13 deletions.
33 changes: 24 additions & 9 deletions bigtable/tableadmin/tableadmin.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,19 +63,15 @@ def run_table_operations(project_id, instance_id, table_id):
table.create()
print 'Created table {}.'.format(table_id)

# [START List existing tables in the instance.]
# [START bigtable_list_tables]
tables = instance.list_tables()
print 'Listing tables in current project...'
if tables != []:
for tbl in tables:
print tbl.table_id
print get_metadata(tbl), "\n"
else:
print 'No table exists in current project...'
# [END List existing tables in the instance.]

# Display name of the table.
print 'Printing table metadata...'
print table.table_id
# [END bigtable_list_tables]

# [START bigtable_create_family_gc_max_age]
print 'Creating column family cf1 with with MaxAge GC Rule...'
Expand Down Expand Up @@ -221,6 +217,23 @@ def run_table_operations(project_id, instance_id, table_id):
print 'execute command python tableadmin.py delete [project_id] \
[instance_id] --table [tableName] to delete the table.'

def get_metadata(table):
""" Get table metadata.
.. note::
This is temporary function as code for this is planned for\
development. Once complete, this function would be removed.
:type table: Table Class.
:param table: Table object.
Returns result dictionary of table metadata
"""
result = {("Table ID ", table.table_id): {}}
column_families = table.list_column_families()
for column_family, gc_rule in sorted(column_families.items()):
result[("Table ID ", table.table_id)][("Column Family ", column_family)] = gc_rule.to_pb()
return result

def exists(instance_obj, table_id):
""" Check whether table exists or not.
Expand All @@ -234,7 +247,7 @@ def exists(instance_obj, table_id):
:param instance_obj: Instance object.
:type table_id: str
:param table_id: Table id to create table.
:param table_id: Table id to identify table.
Returns bool
"""
for table in instance_obj.list_tables():
Expand All @@ -259,7 +272,8 @@ def delete_table(project_id, instance_id, table_id):
client = bigtable.Client(project=project_id, admin=True)
instance = client.instance(instance_id)
table = instance.table(table_id)

# [START bigtable_delete_table]
# Delete the entire table
print 'Checking if table {} exists...'.format(table_id)
if exists(instance, table_id):
print 'Table {} exists.'.format(table_id)
Expand All @@ -268,6 +282,7 @@ def delete_table(project_id, instance_id, table_id):
print 'Deleted {} table.'.format(table_id)
else:
print 'Table {} does not exists.'.format(table_id)
# [END bigtable_delete_table]


if __name__ == '__main__':
Expand Down
9 changes: 5 additions & 4 deletions bigtable/tableadmin/tableadmin_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ def test_run_table_operations(capsys):

run_table_operations(PROJECT, BIGTABLE_CLUSTER, table_name)
out, _ = capsys.readouterr()
assert 'Creating the {} table.'.format(table_name) in out

assert 'Creating the ' + table_name + ' table.' in out
assert 'Listing tables in current project.' in out
assert 'Creating column family cf1 with with MaxAge GC Rule' in out
assert 'Created MaxAge GC rule.' in out
Expand Down Expand Up @@ -61,6 +62,6 @@ def test_delete_table(capsys):
delete_table(PROJECT, BIGTABLE_CLUSTER, table_name)
out, _ = capsys.readouterr()

assert 'Table {} exists.'.format(table_name) in out
assert 'Deleting {} table.'.format(table_name) in out
assert 'Deleted {} table.'.format(table_name) in out
assert 'Table ' + table_name + ' exists.' in out
assert 'Deleting ' + table_name + ' table.' in out
assert 'Deleted ' + table_name + ' table.' in out

0 comments on commit b12a8be

Please sign in to comment.