Browse Source

HUE-950 [core] Adding tests for tagging and documents

Romain Rigaux 12 years ago
parent
commit
22308b201f

+ 22 - 18
desktop/core/src/desktop/models.py

@@ -55,6 +55,10 @@ class Settings(models.Model):
 
 class DocumentTagManager(models.Manager):
 
+  def get_default_tag(self, user):
+    tag, created = DocumentTag.objects.get_or_create(owner=user, tag=DocumentTag.DEFAULT)
+    return tag
+
   def get_trash_tag(self, user):
     tag, created = DocumentTag.objects.get_or_create(owner=user, tag=DocumentTag.TRASH)
     return tag
@@ -63,45 +67,45 @@ class DocumentTagManager(models.Manager):
     tag = DocumentTag.objects.create(tag=tag_name, owner=owner)
     return tag
 
-  def add_or_create_tag(self, owner, doc_id, tag_name, tag_id=None):
+  def tag(self, owner, doc_id, tag_name='', tag_id=None):
     try:
       tag = DocumentTag.objects.get(id=tag_id, owner=owner)
-      if tag == DocumentTag.get_trash_tag(owner):
+      if tag == DocumentTag.objects.get_trash_tag(owner):
         raise Exception(_("Can't add trash tag. Please trash the document from instead."))
-    except:
+    except DocumentTag.DoesNotExist:
       tag = DocumentTag.objects.create(tag=tag_name, owner=owner)
 
     doc = Document.objects.get_doc(doc_id, owner)
     doc.add_tag(tag)
     return tag
 
-  def remove_tag(self, tag_id, owner, doc_id):
+  def untag(self, tag_id, owner, doc_id):
     tag = DocumentTag.objects.get(id=tag_id, owner=owner)
+
     if tag == DocumentTag.get_trash_tag(owner):
       raise Exception(_("Can't remove trash tag. Please restore the document from the trash instead."))
 
     doc = Document.objects.get_doc(doc_id, owner=owner)
     doc.remove(tag)
-    if tag.tag != DocumentTag.TRASH and not tag.document_set.exists():
-      tag.delete()
 
   def delete_tag(self, tag_id, owner):
     tag = DocumentTag.objects.get(id=tag_id, owner=owner)
-    if tag == DocumentTag.get_trash_tag(owner):
-      raise Exception(_("Can't remove trash tag. Please restore the document from the trash instead."))
+    default_tag = DocumentTag.objects.get_default_tag(user=owner)
     
-    for doc in Document.objects.get_docs(owner):
-      doc.remove_tag(tag)
-    if tag.tag != DocumentTag.TRASH and not tag.document_set.exists():
+    if tag in (default_tag, DocumentTag.objects.get_trash_tag(owner)):
+      raise Exception(_("Can't remove default or trash tag. Please restore the document from the trash instead."))
+    else:
       tag.delete()
 
-  @classmethod
+    for doc in Document.objects.get_docs(owner).filter(tags=None):
+      doc.add(default_tag)
+
   def update_tags(self, owner, doc_id, tag_ids):
     doc = Document.objects.get_doc(doc_id, owner)
     
     for tag in doc.tags.all():
-      if tag.tag != DocumentTag.TRASH:
-        doc.remove_tag(tag)
+      if tag.tag not in (DocumentTag.TRASH, DocumentTag.DEFAULT):
+        doc.untag(tag)
     
     for tag_id in tag_ids:
       tag = DocumentTag.objects.get(id=tag_id, owner=owner)
@@ -190,7 +194,7 @@ class DocumentManager(models.Manager):
               extra=extra
           )
 
-    tag, created = DocumentTag.objects.get_or_create(owner=owner, tag=DocumentTag.DEFAULT)
+    tag = DocumentTag.objects.get_default_tag(user=owner)
     doc.tags.add(tag)
 
     return doc
@@ -202,7 +206,7 @@ class DocumentManager(models.Manager):
       for job in list(chain(Workflow.objects.all(), Coordinator.objects.all(), Bundle.objects.all())):
         if not job.doc.exists():
           doc = Document.objects.link(job, owner=job.owner, name=job.name, description=job.description)
-          tag, created = DocumentTag.objects.get_or_create(owner=job.owner, tag='default')
+          tag = DocumentTag.objects.get_default_tag(owner=job.owner)
           doc.tags.add(tag)
           if job.is_trashed:
             doc.send_to_trash()
@@ -215,7 +219,7 @@ class DocumentManager(models.Manager):
       for job in SavedQuery.objects.all():
         if not job.doc.exists():
           doc = Document.objects.link(job, owner=job.owner, name=job.name, description=job.desc, extra=job.type)
-          tag, created = DocumentTag.objects.get_or_create(owner=job.owner, tag='default')
+          tag = DocumentTag.objects.get_default_tag(owner=job.owner)
           doc.tags.add(tag)
           if job.is_trashed:
             doc.send_to_trash()
@@ -228,7 +232,7 @@ class DocumentManager(models.Manager):
       for job in PigScript.objects.all():
         if not job.doc.exists():
           doc = Document.objects.link(job, owner=job.owner, name=job.dict['name'], description='')
-          tag, created = DocumentTag.objects.get_or_create(owner=job.owner, tag='default')
+          tag = DocumentTag.objects.get_default_tag(owner=job.owner)
           doc.tags.add(tag)
     except Exception, e:
       LOG.warn(force_unicode(e))

+ 58 - 21
desktop/core/src/desktop/tests.py

@@ -28,7 +28,7 @@ import desktop.views as views
 import proxy.conf
 
 from nose.plugins.attrib import attr
-from nose.tools import assert_true, assert_equal, assert_not_equal, assert_raises
+from nose.tools import assert_true, assert_false, assert_equal, assert_not_equal, assert_raises
 from django.conf.urls.defaults import patterns, url
 from django.contrib.auth.models import User
 from django.core.urlresolvers import reverse
@@ -43,6 +43,8 @@ from desktop.lib.django_util import TruncatingModel
 from desktop.lib.exceptions_renderable import PopupException
 from desktop.lib.test_utils import grant_access
 from desktop.views import check_config
+from desktop.models import DocumentTag , Document
+from pig.models import PigScript
 
 
 def setup_test_environment():
@@ -500,35 +502,70 @@ class TestDocModelTags():
     grant_access(self.user.username, self.user.username, "desktop")
     grant_access(self.user_not_me.username, self.user_not_me.username, "desktop")        
 
+  def add_tag(self, name):
+    response = self.client.post("/tag/add_tag", {'name': name})
+    assert_equal(0, json.loads(response.content)['status'], response.content)
+    return json.loads(response.content)['tag_id']
+
+  def add_doc(self, name):
+    script = PigScript.objects.create(owner=self.user)
+    doc = Document.objects.link(script, owner=script.owner, name=name)
+    return script, doc
+
   def test_add_tag(self):
-    response = self.client.get("/desktop/add_tag")    
-    assert_equal(-1, json.loads(response.content).status)
+    response = self.client.get("/tag/add_tag")    
+    assert_equal(-1, json.loads(response.content)['status'])
+
+    tag_id = self.add_tag('my_tag')
 
-    response = self.client.post("/desktop/add_tag", {'name': 'my_tag'})    
-    assert_equal(0, json.loads(response.content).status)
+    assert_true(DocumentTag.objects.filter(id=tag_id).exists())
   
   def test_remove_tags(self):
-    response = self.client.get("/desktop/remove_tags")    
-    assert_equal(-1, json.loads(response.content).status)
+    response = self.client.post("/tag/add_tag", {'name': 'my_tag'})
+    tag_id = json.loads(response.content)['tag_id']
+    
+    response = self.client.get("/tag/remove_tags")    
+    assert_equal(-1, json.loads(response.content)['status'])
 
-    response = self.client.post("/desktop/remove_tags", {'data': json.dumps({'tag_ids': []})})    
-    assert_equal(0, json.loads(response.content).status)  
+    response = self.client_not_me.post("/tag/remove_tags", {'data': json.dumps({'tag_ids': [tag_id]})})    
+    assert_equal(-1, json.loads(response.content)['status'], response.content)
+
+    response = self.client.post("/tag/remove_tags", {'data': json.dumps({'tag_ids': [tag_id]})})    
+    assert_equal(0, json.loads(response.content)['status'], response.content)
+    
+    assert_false(DocumentTag.objects.filter(id=tag_id).exists())
   
-  def test_list_tags(self):
-    response = self.client.get("/desktop/list_tags")        
-    assert_equal({}, json.loads(response.content))    
+  def test_list_tags(self):    
+    tag_id = self.add_tag('my_list_tags')
+
+    response = self.client.get("/tag/list_tags")        
+    assert_true([tag for tag in json.loads(response.content) if tag['id'] == tag_id], response.content)
   
   def test_list_docs(self):
-    response = self.client.get("/desktop/list_docs")        
-    assert_equal({}, json.loads(response.content))   
+    script, doc = self.add_doc('test-pig')
+
+    response = self.client.get("/doc/list_docs")        
+    assert_true([doc for doc in json.loads(response.content) if doc['id'] == script.id], response.content)
   
-  def test_add_or_create_tag(self):
-    response = self.client.get("/desktop/add_or_create_tag$")        
-    assert_equal({}, json.loads(response.content))   
+  def test_tag(self):
+    script, doc = self.add_doc('tag_pig')
+    
+    response = self.client.post("/doc/tag", {'data': json.dumps({'doc_id': doc.id, 'tag': 'pig'})})        
+    assert_equal(0, json.loads(response.content)['status'], response.content)
+    
+    tag2_id = self.add_tag('pig2')
+    
+    response = self.client.post("/doc/tag", {'data': json.dumps({'doc_id': doc.id, 'tag_id': tag2_id})})
+    assert_equal(0, json.loads(response.content)['status'], response.content)
 
   def test_update_tags(self):
-    response = self.client.get("/desktop/update_tags")        
-    assert_equal({}, json.loads(response.content))  
+    script, doc = self.add_doc('update_tags')
+    
+    tag1_id = self.add_tag('update_tags_1')
+    tag2_id = self.add_tag('update_tags_2')
+    
+    response = self.client.post("/doc/update_tags", {'data': json.dumps({'doc_id': doc.id, 'tag_ids': [tag1_id, tag2_id]})})        
+    assert_equal(0, json.loads(response.content)['status'], response.content)  
 
 
 
@@ -544,9 +581,9 @@ class TestDocModelPermissions():
     grant_access(self.user_not_me.username, self.user_not_me.username, "desktop")        
 
   def test_add_or_update_permission(self):
-    response = self.client.get("/desktop/add_or_update_permission")    
+    response = self.client.get("/doc/add_or_update_permission")    
     assert_equal(-1, json.loads(response.content).status)
 
   def test_remove_permission(self):
-    response = self.client.get("/desktop/remove_permission")    
+    response = self.client.get("/doc/remove_permission")    
     assert_equal(-1, json.loads(response.content).status)

+ 3 - 2
desktop/core/src/desktop/urls.py

@@ -68,14 +68,15 @@ dynamic_patterns += patterns('desktop.views',
   (r'^debug/check_config_ajax$', 'check_config_ajax'),
   (r'^log_frontend_event$', 'log_frontend_event'),
 
-  # Doc model
+  # Tags
   (r'^tag/add_tag$', 'add_tag'),
   (r'^tag/remove_tags$', 'remove_tags'),
   (r'^tag/list_tags$', 'list_tags'),
   (r'^doc/list_docs$', 'list_docs'),
-  (r'^doc/add_or_create_tag$', 'add_or_create_tag'),
+  (r'^doc/tag$', 'tag'),
   (r'^doc/update_tags$', 'update_tags'),
 
+  # Permissions
   (r'^doc/add_or_update_permission$', 'add_or_update_permission'),
   (r'^doc/remove_permission$', 'remove_permission'),
 

+ 7 - 3
desktop/core/src/desktop/views.py

@@ -114,6 +114,7 @@ def add_tag(request):
     try:
       tag = DocumentTag.objects.create_tag(request.user, request.POST['name'])
       response['tag_id'] = tag.id
+      response['status'] = 0
     except Exception, e:
       response['message'] = force_unicode(e)
   else:
@@ -122,14 +123,15 @@ def add_tag(request):
   return HttpResponse(json.dumps(response), mimetype="application/json")
 
 
-def add_or_create_tag(request):
+def tag(request):
   response = {'status': -1, 'message': ''}
   
   if request.method == 'POST':
     request_json = json.loads(request.POST['data'])
     try:
-      tag = DocumentTag.objects.add_or_create_tag(request.user, request_json['doc_id'], request_json['tag'], request_json['tag_id'])
+      tag = DocumentTag.objects.tag(request.user, request_json['doc_id'], request_json.get('tag'), request_json.get('tag_id'))
       response['tag_id'] = tag.id
+      response['status'] = 0
     except Exception, e:
       response['message'] = force_unicode(e)
   else:
@@ -146,6 +148,7 @@ def update_tags(request):
     try:
       doc = DocumentTag.objects.update_tags(request.user, request_json['doc_id'], request_json['tag_ids'])
       response['doc'] = massage_doc_for_json(doc)
+      response['status'] = 0
     except Exception, e:
       response['message'] = force_unicode(e)
   else:
@@ -162,7 +165,8 @@ def remove_tags(request):
     try:
       for tag_id in request_json['tag_ids']:
         DocumentTag.objects.delete_tag(tag_id, request.user)
-      response['message'] = _('Tag removed !')
+      response['message'] = _('Tag(s) removed!')
+      response['status'] = 0
     except Exception, e:
       response['message'] = force_unicode(e)
   else: