Skip to content

Commit

Permalink
fix issue #33
Browse files Browse the repository at this point in the history
  • Loading branch information
vvaezian committed May 16, 2022
1 parent e75a62c commit 0aa0d63
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 17 deletions.
29 changes: 20 additions & 9 deletions metabase_api/metabase_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -1080,16 +1080,27 @@ def clone_card(self, card_id,

# simple/custom questions
elif card_info['dataset_query']['type'] == 'query':
filters_data = card_info['dataset_query']['query']

query_data = card_info['dataset_query']['query']

# change the underlying table for the card
filters_data['source-table'] = target_table_id
# change filters source
for index, item in enumerate(filters_data['filter']):
if type(item) == list:
column_id = item[1][1]
column_name = source_table_col_id_name_mapping[column_id]
target_col_id = target_table_col_name_id_mapping[column_name]
card_info['dataset_query']['query']['filter'][index][1][1] = target_col_id
query_data['source-table'] = target_table_id

# transform to string so it is easier to replace the column IDs
query_data_str = str(query_data)

# find column IDs
import re
res = re.findall("\['field', .*?\]", query_data_str)
source_column_IDs = [ eval(i)[1] for i in res ]

# replace column IDs from old table with the column IDs from new table
for source_col_id in source_column_IDs:
source_col_name = source_table_col_id_name_mapping[source_col_id]
target_col_id = target_table_col_name_id_mapping[source_col_name]
query_data_str = query_data_str.replace("['field', {}, ".format(source_col_id), "['field', {}, ".format(target_col_id))

card_info['dataset_query']['query'] = eval(query_data_str)

new_card_json = {}
for key in ['dataset_query', 'display', 'visualization_settings']:
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setuptools.setup(
name="metabase-api",
version="0.2.14.2",
version="0.2.15",
author="Vahid Vaezian",
author_email="[email protected]",
description="A Python Wrapper for Metabase API",
Expand Down
32 changes: 25 additions & 7 deletions tests/test_metabase_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ def test_get_table_metadata(self):
self.assertEqual(table_info['fields'][0]['name'], 'col1')



def test_get_columns_name_id(self):
name_id_mapping = mb.get_columns_name_id(table_id=101)
self.assertEqual(name_id_mapping['col1'], 490)
Expand All @@ -154,6 +155,7 @@ def test_get_columns_name_id(self):
self.assertEqual(id_name_mapping[490], 'col1')



def test_friendly_names_is_disabled(self):
res = mb.friendly_names_is_disabled()
self.assertEqual(res, True)
Expand Down Expand Up @@ -215,6 +217,7 @@ def test_create_card(self):
Metabase_API_Test.cleanup_objects['card'].extend([ res1['id'], res3['id'], res3['id'] ])



def test_create_collection(self):
t = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
res = mb.create_collection(f'test_create_collection {t}', parent_collection_id=28, return_results=True)
Expand All @@ -227,6 +230,7 @@ def test_create_collection(self):
Metabase_API_Test.cleanup_objects['collection'].append(res['id'])



def test_create_segment(self):
t = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
res = mb.create_segment(segment_name='test_create_segment_{}'.format(t), table_name='test_table', column_name='col2', column_values=[1, 2], return_segment=True)
Expand All @@ -235,6 +239,7 @@ def test_create_segment(self):
Metabase_API_Test.cleanup_objects['segment'].append(res['id'])



def test_copy_card(self):
t = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
newCard_id = mb.copy_card(source_card_id=166, destination_collection_id=29, destination_card_name='test_copy_card_{}'.format(t))
Expand Down Expand Up @@ -284,11 +289,13 @@ def test_search(self):
def test_get_card_data(self):
# json
res = mb.get_card_data(card_id=166)
json_data = [{'col1': 'row1 cell1', 'col2': 1},
{'col1': None, 'col2': 2},
{'col1': 'row3 cell1', 'col2': None},
{'col1': None, 'col2': None},
{'col1': 'row5 cell1', 'col2': 5}]
json_data = [
{'col1': 'row1 cell1', 'col2': 1},
{'col1': None, 'col2': 2},
{'col1': 'row3 cell1', 'col2': None},
{'col1': None, 'col2': None},
{'col1': 'row5 cell1', 'col2': 5}
]
self.assertEqual(res, json_data)

# csv
Expand All @@ -302,14 +309,25 @@ def test_get_card_data(self):
self.assertEqual(res, filtered_data)



def test_clone_card(self):
# native question
res = mb.clone_card(273, 101, 103, new_card_name='test_clone_native', new_card_collection_id=29, return_card=True)
# simple/custom question
res2 = mb.clone_card(274, 101, 103, new_card_name='test_clone_simple', new_card_collection_id=29, return_card=True)
res2 = mb.clone_card(274, 101, 103, new_card_name='test_clone_simple1', new_card_collection_id=29, return_card=True)
res3 = mb.clone_card(491, 101, 103, new_card_name='test_clone_simple2', new_card_collection_id=29, return_card=True)
expected_res3_query = { 'database': 5,
'query': {'source-table': 103,
'aggregation': [['avg', ['field', 493, None]]],
'breakout': [['field', 494, None]],
'order-by': [['desc', ['aggregation', 0, None]]]
},
'type': 'query'
}
self.assertEqual(res3['dataset_query'], expected_res3_query)

# add to cleanup list
Metabase_API_Test.cleanup_objects['card'].extend([res['id'], res2['id']])
Metabase_API_Test.cleanup_objects['card'].extend([res['id'], res2['id'], res3['id']])



Expand Down

0 comments on commit 0aa0d63

Please sign in to comment.