Эх сурвалжийг харах

[beeswax] Convert a series of test to the new json and refresh API

Romain Rigaux 12 жил өмнө
parent
commit
75a9a98

+ 1 - 1
apps/beeswax/src/beeswax/api.py

@@ -401,7 +401,7 @@ def save_results(request, query_history_id):
           response['id'] = query_history.id
           response['query'] = query_history.query
           response['path'] = target_dir
-          response['success_url'] = reverse('view', kwargs={'path': target_dir})
+          response['success_url'] = '/filebrowser/view%s' % target_dir
           response['watch_url'] = reverse(get_app_name(request) + ':api_watch_query_refresh_json', kwargs={'id': query_history.id})
         elif form.cleaned_data['save_target'] == form.SAVE_TYPE_TBL:
           query_history = db.create_table_as_a_select(request, query_history, form.cleaned_data['target_table'], result_meta)

+ 3 - 2
apps/beeswax/src/beeswax/create_table.py

@@ -22,6 +22,7 @@ import json
 import logging
 
 from django.core.urlresolvers import reverse
+from django.http import QueryDict
 from django.utils.translation import ugettext as _
 
 from desktop.context_processors import get_app_name
@@ -259,14 +260,14 @@ def _submit_create_and_load(request, create_hql, table_name, path, do_load, data
   """
   Submit the table creation, and setup the load to happen (if ``do_load``).
   """
-  on_success_params = {}
+  on_success_params = QueryDict('', mutable=True)
   app_name = get_app_name(request)
 
   if do_load:
     on_success_params['table'] = table_name
     on_success_params['path'] = path
     on_success_params['removeHeader'] = request.POST.get('removeHeader')
-    on_success_url = reverse(app_name + ':load_after_create', kwargs={'database': database})
+    on_success_url = reverse(app_name + ':load_after_create', kwargs={'database': database}) + '?' + on_success_params.urlencode()
   else:
     on_success_url = reverse('metastore:describe_table', kwargs={'database': database, 'table': table_name})
 

+ 1 - 1
apps/beeswax/src/beeswax/server/hive_server2_lib.py

@@ -59,7 +59,7 @@ class HiveServerTable(Table):
 
   @property
   def is_view(self):
-    return HiveServerTRow(self.table, self.table_schema).col('TABLE_TYPE') == 'VIRTUAL_VIEW'
+    return HiveServerTRow(self.table, self.table_schema).col('TABLE_TYPE') == 'VIEW' # Used to be VIRTUAL_VIEW
 
   @property
   def partition_keys(self):

+ 2 - 2
apps/beeswax/src/beeswax/templates/execute.mako

@@ -1927,8 +1927,8 @@ function watchEvents() {
 
 // Knockout
 viewModel = new BeeswaxViewModel("${app_name}");
-% if query:
-  viewModel.design.history.id(${query.id});
+% if query_history:
+  viewModel.design.history.id(${query_history.id});
   viewModel.fetchQueryHistory();
   $(document).on('fetched.query', function(e) {
     viewModel.watchQueryLoop();

+ 74 - 15
apps/beeswax/src/beeswax/tests.py

@@ -37,7 +37,6 @@ from django.contrib.auth.models import User
 from django.core.urlresolvers import reverse
 
 from desktop.lib.django_test_util import make_logged_in_client, assert_equal_mod_whitespace
-from desktop.lib.django_test_util import assert_similar_pages
 from desktop.lib.test_utils import grant_access, add_to_group
 from desktop.lib.security_util import get_localhost_name
 
@@ -769,9 +768,13 @@ for x in sys.stdin:
         'path': target_dir
       }
       resp = self.client.post('/beeswax/api/query/%s/results/save' % qid, save_data, follow=True)
-      context = json.loads(resp.content)
-      resp = self.client.get(context['watch_url'], follow=True)
-      resp = wait_for_query_to_finish(self.client, resp, max=60)
+      content = json.loads(resp.content)
+
+      if content['status'] == 0:
+        success_url = content['success_url']
+        resp = self.client.get(content['watch_url'], follow=True)
+        resp = wait_for_query_to_finish(self.client, resp, max=60)
+        resp.success_url = success_url # Hack until better API
 
       # Check that data is right
       if verify:
@@ -785,6 +788,7 @@ for x in sys.stdin:
 
         assert_equal(256, len(data_buf.strip().split('\n')))
         assert_true('255' in data_buf)
+
       return resp
 
     TARGET_DIR_ROOT = '/tmp/beeswax.test_save_results'
@@ -802,6 +806,7 @@ for x in sys.stdin:
     hql = "SELECT * FROM test"
     resp = _make_query(self.client, hql, wait=True, local=False, max=180.0)
     resp = save_and_verify(resp, TARGET_DIR_ROOT + '/1', verify=False)
+    resp = self.client.get(resp.success_url)
     # Success and went to FB
     assert_true('File Browser' in resp.content, resp.content)
 
@@ -809,12 +814,14 @@ for x in sys.stdin:
     hql = "SELECT foo, bar FROM test"
     resp = _make_query(self.client, hql, wait=True, local=False, max=180.0)
     resp = save_and_verify(resp, TARGET_DIR_ROOT + '/2')
+    resp = self.client.get(resp.success_url)
     assert_true('File Browser' in resp.content, resp.content)
 
     # Partition tables
     hql = "SELECT * FROM test_partitions"
     resp = _make_query(self.client, hql, wait=True, local=False, max=180.0)
     resp = save_and_verify(resp, TARGET_DIR_ROOT + '/3', verify=False)
+    resp = self.client.get(resp.success_url)
     assert_true('File Browser' in resp.content, resp.content)
 
 
@@ -829,15 +836,15 @@ for x in sys.stdin:
         'path': target_tbl
       }
       resp = self.client.post('/beeswax/api/query/%s/results/save' % qid, save_data, follow=True)
-      context = json.loads(resp.content)
-      resp = self.client.get(context['watch_url'], follow=True)
+      content = json.loads(resp.content)
+      resp = self.client.get(content['watch_url'], follow=True)
       wait_for_query_to_finish(self.client, resp, max=120)
 
       # Check that data is right. The SELECT may not give us the whole table.
       resp = _make_query(self.client, 'SELECT * FROM %s' % target_tbl, wait=True, local=False)
-      resp = fetch_query_result_data(self.client, resp)
+      content = fetch_query_result_data(self.client, resp)
       for i in xrange(90):
-        assert_equal([i, '0x%x' % (i,)], context['results'][i])
+        assert_equal([i, '0x%x' % (i,)], content['results'][i])
 
     TARGET_TBL_ROOT = 'test_copy'
 
@@ -1133,18 +1140,47 @@ for x in sys.stdin:
       'cols-2-column_type': 'string',
       'cols-next_form_id': '3',
     }, follow=True)
-    resp = self.client.get(reverse("beeswax:api_watch_query_refresh_json", kwargs={'id': resp.context['query'].id}), follow=True)
-    resp = wait_for_query_to_finish(self.client, resp, max=180.0)
+
+    #
+    # Little nightmare here:
+    # We have a POST (create table) with a redirect (load data) of redirect (show table)
+    #
+    assert_equal(resp.context['action'], 'watch-redirect')
+    on_success_url_load_data = resp.context['on_success_url']
+    assert_true('auto_load' in on_success_url_load_data, on_success_url_load_data)
+    query_history = resp.context['query_history']
+
+    resp = self.client.get(reverse('beeswax:api_fetch_query_history', kwargs={'query_history_id': query_history.id}), follow=True)
+    content = json.loads(resp.content)
+    watch_url = content['query_history']['watch_url']
+
+    class MockResponse():
+      def __init__(self, content):
+        self.content = json.dumps(content)
+
+    # Wait for CREATE TABLE to finis
+    resp = wait_for_query_to_finish(self.client, MockResponse({'status': 'ok', 'watch_url': watch_url}), max=180.0)
+
+    # Get URL that will load the data into the table. Also get the URL that will show the table in metastore app.
+    resp = self.client.get(on_success_url_load_data, follow=True)
+    assert_equal(resp.context['action'], 'watch-redirect')
+    on_success_url_show_table = resp.context['on_success_url']
+    assert_true('/metastore/table/' in on_success_url_show_table, on_success_url_show_table)
+    query_history = resp.context['query_history']
+
+    # Wait for load data to finish
+    resp = wait_for_query_to_finish(self.client, MockResponse({'status': 'ok', 'watch_url': watch_url}), max=180.0)
 
     # Check data is in the table (by describing it)
-    resp = self.client.get('/metastore/table/default/test_create_import')
+    resp = self.client.get(on_success_url_show_table)
     cols = resp.context['table'].cols
     assert_equal(len(cols), 3)
     assert_equal([ col.name for col in cols ], [ 'col_a', 'col_b', 'col_c' ])
+    assert_equal([['ta\tb', 'nada', 'sp ace'], ['f\too', 'bar', 'fred'], ['a\ta', 'bb', 'cc']], resp.context['sample'])
     assert_true("nada" in resp.content, resp.content)
     assert_true("sp ace" in resp.content, resp.content)
 
-    # Test table creation and data loading
+    # Test table creation and data loading and removing header
     resp = self.client.post('/beeswax/create/import_wizard/default', {
       'submit_create': 'on',
       'path': '/tmp/comma.csv',
@@ -1165,11 +1201,34 @@ for x in sys.stdin:
       'cols-next_form_id': '3',
       'removeHeader': 'on'
     }, follow=True)
-    resp = self.client.get(reverse("beeswax:api_watch_query_refresh_json", kwargs={'id': resp.context['query'].id}), follow=True)
-    resp = wait_for_query_to_finish(self.client, resp, max=180.0)
+
+    # We have a POST (create table) with a redirect (load data) of redirect (show table)
+    assert_equal(resp.context['action'], 'watch-redirect')
+    on_success_url_load_data = resp.context['on_success_url']
+    assert_true('auto_load' in on_success_url_load_data, on_success_url_load_data)
+    query_history = resp.context['query_history']
+
+    resp = self.client.get(reverse('beeswax:api_fetch_query_history', kwargs={'query_history_id': query_history.id}), follow=True)
+    content = json.loads(resp.content)
+    watch_url = content['query_history']['watch_url']
+
+    # Wait for CREATE TABLE to finis
+    resp = wait_for_query_to_finish(self.client, MockResponse({'status': 'ok', 'watch_url': watch_url}), max=180.0)
+
+    # Get URL that will load the data into the table. Also get the URL that will show the table in metastore app.
+    resp = self.client.get(on_success_url_load_data, follow=True)
+    assert_equal(resp.context['action'], 'watch-redirect')
+    on_success_url_show_table = resp.context['on_success_url']
+    assert_true('/metastore/table/' in on_success_url_show_table, on_success_url_show_table)
+    query_history = resp.context['query_history']
+
+    # Wait for load data to finish
+    resp = wait_for_query_to_finish(self.client, MockResponse({'status': 'ok', 'watch_url': watch_url}), max=180.0)
+
+    # Check data is in the table (by describing it)
+    resp = self.client.get(on_success_url_show_table)
 
     # Check data is in the table (by describing it)
-    resp = self.client.get('/metastore/table/default/test_create_import_with_header')
     cols = resp.context['table'].cols
     assert_equal(len(cols), 3)
     assert_equal([ col.name for col in cols ], [ 'col_a', 'col_b', 'col_c' ])

+ 7 - 5
apps/beeswax/src/beeswax/views.py

@@ -343,8 +343,8 @@ def execute_query(request, design_id=None, query_history_id=None):
   action = 'query'
 
   if query_history_id:
-    query = authorized_get_query_history(request, query_history_id, must_exist=True)
-    design = query.design
+    query_history = authorized_get_query_history(request, query_history_id, must_exist=True)
+    design = query_history.design
 
     if 'on_success_url' in request.GET:
       if request.GET.get('on_success_url'):
@@ -360,14 +360,16 @@ def execute_query(request, design_id=None, query_history_id=None):
     app_name = get_app_name(request)
     query_type = SavedQuery.TYPES_MAPPING[app_name]
     design = safe_get_design(request, query_type, design_id)
-    query = None
+    query_history = None
 
   context = {
     'design': design,
-    'query': query,
+    'query': query_history, # Backward
+    'query_history': query_history,
     'autocomplete_base_url': reverse(get_app_name(request) + ':api_autocomplete_databases', kwargs={}),
     'can_edit_name': design and design.id and not design.is_auto,
-    'action': action
+    'action': action,
+    'on_success_url': request.GET.get('on_success_url')
   }
 
   return render('execute.mako', request, context)