Skip to content

Commit 607f5b8

Browse files
authored
improve testing
1 parent b3417be commit 607f5b8

File tree

2 files changed

+51
-14
lines changed

2 files changed

+51
-14
lines changed

tests/initial_setup.sh

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,12 @@ then
1818
exit 1
1919
fi
2020

21-
# downloading metabase jar
22-
wget -q https://downloads.metabase.com/v$MB_VERSION/metabase.jar
21+
# cleaup (in case the script is not running for the first time)
22+
rm -f metabase.db.mv.db
23+
rm -f metabase.db.trace.db
24+
25+
# downloading metabase jar file
26+
wget -O metabase.jar -q https://downloads.metabase.com/v$MB_VERSION/metabase.jar
2327

2428
# starting metabase jar locally
2529
java -jar metabase.jar > logs 2>&1 &
@@ -81,7 +85,17 @@ json='{
8185
"visualization_settings": {},
8286
"collection_id": 2
8387
}'
84-
echo "$json" | curl -X POST http://localhost:3000/api/card -H "Content-Type: application/json" -H "X-Metabase-Session:$session_id" -d @-
88+
echo "$json" | curl -X POST http://localhost:3000/api/card -H "Content-Type: application/json" -H "X-Metabase-Session:$session_id" -d @- > output
89+
90+
# the order of the IDs assigned to columns is not based on the db column order
91+
grep -q ',73.*,72' output
92+
if [[ $? -eq 0 ]]; then
93+
col1_id=73;
94+
col2_id=72;
95+
else
96+
col1_id=72;
97+
col2_id=73;
98+
fi
8599

86100
json='{
87101
"name":"test_card_2",
@@ -94,7 +108,7 @@ json='{
94108
"name":"test_filter",
95109
"display-name":"Test filter",
96110
"type":"dimension",
97-
"dimension":["field",72,null],
111+
"dimension":["field",COL1_ID,null],
98112
"widget-type":"string/=",
99113
"default":null,
100114
"id":"810912da-ead5-c87e-de32-6dc5723b9067"
@@ -115,6 +129,9 @@ json='{
115129
}],
116130
"collection_id":2
117131
}'
132+
# add the value of $col1_id (because of presense of single and double quotes in the json string, I decided to add the variable value in this way)
133+
json=$(echo "$json" | sed "s/COL1_ID/$col1_id/g")
134+
118135
echo "$json" | curl -X POST http://localhost:3000/api/card -H "Content-Type: application/json" -H "X-Metabase-Session:$session_id" -d @-
119136

120137
json='{
@@ -123,13 +140,16 @@ json='{
123140
"type":"query",
124141
"query":{
125142
"source-table":9,
126-
"filter":["=",["field",72,null],"row1 cell1","row3 cell1"]},
143+
"filter":["=",["field",COL1_ID,null],"row1 cell1","row3 cell1"]},
127144
"database":2
128145
},
129146
"display":"table",
130147
"visualization_settings":{},
131148
"collection_id":2
132149
}'
150+
# add the value of $col1_id
151+
json=$(echo "$json" | sed "s/COL1_ID/$col1_id/g")
152+
133153
echo "$json" | curl -X POST http://localhost:3000/api/card -H "Content-Type: application/json" -H "X-Metabase-Session:$session_id" -d @-
134154

135155
json='{
@@ -139,8 +159,8 @@ json='{
139159
"database":2,
140160
"query":{
141161
"source-table":9,
142-
"aggregation":[["avg",["field",73,null]]],
143-
"breakout":[["field",72,null]],
162+
"aggregation":[["avg",["field",COL2_ID,null]]],
163+
"breakout":[["field",COL1_ID,null]],
144164
"order-by":[["desc",["aggregation",0,null]]]
145165
},
146166
"type":"query"
@@ -149,6 +169,9 @@ json='{
149169
"visualization_settings":{"table.pivot":false,"graph.dimensions":["col1"],"graph.metrics":["avg"]},
150170
"collection_id":2
151171
}'
172+
# add the value of $col1_id and $col2_id
173+
json=$(echo "$json" | sed "s/COL1_ID/$col1_id/g" | sed "s/COL2_ID/$col2_id/g")
174+
152175
echo "$json" | curl -X POST http://localhost:3000/api/card -H "Content-Type: application/json" -H "X-Metabase-Session:$session_id" -d @-
153176

154177
# create a test dashboard

tests/test_metabase_api.py

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -125,11 +125,11 @@ def test_get_table_metadata(self):
125125

126126

127127
def test_get_columns_name_id(self):
128-
name_id_mapping = mb.get_columns_name_id(table_id=9)
129-
self.assertEqual(name_id_mapping['col1'], 72)
128+
name_id_mapping = mb.get_columns_name_id(table_id=1) # table with id 1 is the products table from sample dataset
129+
self.assertEqual(name_id_mapping['CATEGORY'], 1)
130130

131-
id_name_mapping = mb.get_columns_name_id(table_id=9, column_id_name=True)
132-
self.assertEqual(id_name_mapping[72], 'col1')
131+
id_name_mapping = mb.get_columns_name_id(table_id=1, column_id_name=True)
132+
self.assertEqual(id_name_mapping[1], 'CATEGORY')
133133

134134

135135

@@ -278,15 +278,29 @@ def test_clone_card(self):
278278
# simple/custom question
279279
res2 = mb.clone_card(3, 9, 10, new_card_name='test_clone_simple1', new_card_collection_id=1, return_card=True)
280280
res3 = mb.clone_card(4, 9, 10, new_card_name='test_clone_simple2', new_card_collection_id=1, return_card=True)
281-
expected_res3_query = { 'database': 2,
281+
282+
# rewriting a value because it's not reliable
283+
res3['dataset_query']['query']['order-by'] = ''
284+
285+
expected_res3_query_version1 = { 'database': 2,
282286
'query': {'source-table': 10,
283287
'aggregation': [['avg', ['field', 75, None]]],
284288
'breakout': [['field', 74, None]],
285-
'order-by': [['desc', ['aggregation', 0, None]]]
289+
'order-by': ''
286290
},
287291
'type': 'query'
288292
}
289-
self.assertEqual(res3['dataset_query'], expected_res3_query)
293+
# we have two versions because the assigned field id is not necessarily in the order of columns as they appear in db
294+
expected_res3_query_version2 = { 'database': 2,
295+
'query': {'source-table': 10,
296+
'aggregation': [['avg', ['field', 74, None]]],
297+
'breakout': [['field', 75, None]],
298+
'order-by': ''
299+
},
300+
'type': 'query'
301+
}
302+
303+
self.assertTrue(res3['dataset_query'] == expected_res3_query_version1 or res3['dataset_query'] == expected_res3_query_version2, res3['dataset_query'])
290304

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

0 commit comments

Comments
 (0)