Преглед на файлове

Handle multiple gist dirs by merging them into one and update test_create UT

During creating gists, if we hit MultipleObjectsReturned exception, this patch will merge all duplicate gist directories into one and return it.

Updated the test_create UT by checking the counts of gist dirs before and after creating a new gist
Harshg999 преди 4 години
родител
ревизия
bfa31c2cf1
променени са 2 файла, в които са добавени 39 реда и са изтрити 7 реда
  1. 23 3
      desktop/core/src/desktop/api2_tests.py
  2. 16 4
      desktop/core/src/desktop/models.py

+ 23 - 3
desktop/core/src/desktop/api2_tests.py

@@ -699,6 +699,29 @@ class TestDocumentGist(object):
         json.loads(Document2.objects.get(type='gist', uuid=gist2['uuid']).data)['statement_raw']
         json.loads(Document2.objects.get(type='gist', uuid=gist2['uuid']).data)['statement_raw']
     )
     )
 
 
+    # Create multiple gist directories and then make a new gist
+    home_dir = Directory.objects.get_home_directory(self.user)
+    gist_dir2 = Directory.objects.create(name='Gist', owner=self.user, parent_directory=home_dir)
+
+    assert_equal(2, Directory.objects.filter(name='Gist', type='directory').count())
+
+    response3 = self._create_gist(
+        statement='SELECT 3',
+        doc_type='hive-query',
+        name='test_gist_create3',
+    )
+    gist3 = json.loads(response3.content)
+
+    # Gist directories merged into one
+    assert_equal(1, Directory.objects.filter(name='Gist', type='directory').count())
+
+    assert_true(Document2.objects.filter(type='gist', name='test_gist_create3'))
+    assert_true(Document2.objects.filter(type='gist', uuid=gist3['uuid']))
+    assert_equal(
+        'SELECT 3',
+        json.loads(Document2.objects.get(type='gist', uuid=gist3['uuid']).data)['statement_raw']
+    )
+
 
 
   def test_get(self):
   def test_get(self):
     response = self._create_gist(
     response = self._create_gist(
@@ -726,9 +749,6 @@ class TestDocumentGist(object):
 
 
     assert_true(home_dir.children.filter(name='Gist').exists())
     assert_true(home_dir.children.filter(name='Gist').exists())
 
 
-    gist_dir2 = Directory.objects.create(name='Gist', owner=self.user, parent_directory=home_dir)
-    assert_raises(Directory.MultipleObjectsReturned, Document2.objects.get_gist_directory, self.user)
-
 
 
   def test_get_unfurl(self):
   def test_get_unfurl(self):
     # Unfurling on
     # Unfurling on

+ 16 - 4
desktop/core/src/desktop/models.py

@@ -1068,10 +1068,22 @@ class Document2Manager(models.Manager, Document2QueryMixin):
 
 
   def get_gist_directory(self, user):
   def get_gist_directory(self, user):
     home_dir = self.get_home_directory(user)
     home_dir = self.get_home_directory(user)
-    gist_dir, created = Directory.objects.get_or_create(name=Document2.GIST_DIR, owner=user, parent_directory=home_dir)
-    if created:
-      LOG.info('Successfully created gist directory for user: %s' % user.username)
-    return gist_dir
+    try:
+
+      gist_dir, created = Directory.objects.get_or_create(name=Document2.GIST_DIR, owner=user, parent_directory=home_dir)
+      if created:
+        LOG.info('Successfully created gist directory for user: %s' % user.username)
+      return gist_dir
+    except Directory.MultipleObjectsReturned:
+      LOG.error('Multiple Gist directories detected. Merging all into one.')
+
+      gist_dirs = list(self.filter(owner=user, parent_directory=home_dir, name=Document2.GIST_DIR, type='directory').order_by('-last_modified'))
+      parent_home_dir = gist_dirs.pop()
+      for dir in gist_dirs:
+        dir.children.exclude(name='.Trash').update(parent_directory=parent_home_dir)
+        dir.delete()
+
+      return parent_home_dir
 
 
   def get_by_path(self, user, path):
   def get_by_path(self, user, path):
     """
     """