# -*- coding: utf-8 -*-
# Copyright 2016-2017 Luc Saffre
# License: BSD (see file COPYING for details)

"""Runs some tests about the disable-delete handler and cascading deletes.

You can run only these tests by issuing::

  $ go team
  $ python manage.py test tests.test_ddh

Or::

  $ go book
  $ python setup.py test -s tests.ProjectsTests.test_ddh

"""

from __future__ import unicode_literals
from __future__ import print_function

from django.core.exceptions import ValidationError
from lino.utils.djangotest import RemoteAuthTestCase
from lino.api import rt

from lino.utils.instantiator import create_row as create
    

class DDHTests(RemoteAuthTestCase):
    maxDiff = None

    def test01(self):
        from lino.modlib.users.choicelists import UserTypes
        Ticket = rt.models.tickets.Ticket
        Project = rt.models.tickets.Project
        User = rt.models.users.User
        Star = rt.models.stars.Star
        # ContentType = rt.models.contenttypes.ContentType
        # ct_Ticket = ContentType.objects.get_for_model(Ticket)

        create(Project, name='project')
        robin = create(User, username='robin',
                       first_name="Robin",
                       user_type=UserTypes.admin,
                       language="en")

        def createit():
            return create(Ticket, summary="Test", user=robin)

        #
        # If there are no vetos, user can ask to delete it
        #
        obj = createit()
        obj.delete()

        obj = createit()

        if False:
            try:
                robin.delete()
                self.fail("Expected veto")
            except Warning as e:
                self.assertEqual(
                    str(e), "Cannot delete User Robin "
                    "because 1 Tickets refer to it.")

        
        star = create(Star, owner=obj, user=robin)
        
        try:
            robin.delete()
            self.fail("Expected veto")
        except Warning as e:
            self.assertEqual(
                str(e), "Cannot delete User Robin "
                "because 1 Stars refer to it.")

        self.assertEqual(Star.objects.count(), 1)
        self.assertEqual(Ticket.objects.count(), 1)
        star.delete()
        obj.delete()
        self.assertEqual(Star.objects.count(), 0)
        self.assertEqual(Ticket.objects.count(), 0)

@@ -891,19 +891,27 @@
        S
-tar
+ubscription
  = rt.mo
@@ -919,18 +919,66 @@
 els.
-stars.Star
+tickets.Subscription%0A        Site = rt.models.tickets.Site
 %0A   
@@ -1110,22 +1110,26 @@
     
-create(Project
+site = create(Site
 , na
@@ -1319,16 +1319,68 @@
 ge=%22en%22)
+%0A        create(Subscription, site=site, user=robin)
 %0A%0A      
@@ -1449,32 +1449,43 @@
 est%22, user=robin
+, site=site
 )%0A%0A        #%0A   
@@ -1550,35 +1550,38 @@
       #%0A        
-obj
+ticket
  = createit()%0A  
@@ -1578,35 +1578,38 @@
 ateit()%0A        
-obj
+ticket
 .delete()%0A%0A     
@@ -1611,19 +1611,22 @@
         
-obj
+ticket
  = creat
@@ -1644,22 +1644,68 @@
     
-if False:%0A    
+# we cannot delete the user because a ticket refers to it:%0A%0A
     
@@ -1717,36 +1717,32 @@
 ry:%0A            
-   
- 
 robin.delete()%0A 
@@ -1732,36 +1732,32 @@
  robin.delete()%0A
-    
             self
@@ -1783,28 +1783,24 @@
 o%22)%0A        
-    
 except Warni
@@ -1812,36 +1812,32 @@
  e:%0A            
- 
-   
 self.assertEqual
@@ -1830,36 +1830,32 @@
 lf.assertEqual(%0A
-    
                 
@@ -1898,36 +1898,32 @@
                 
-  
-  
 %22because 1 Ticke
@@ -1963,107 +1963,118 @@
    s
-tar = create(Star, owner=obj, user=robin)%0A        %0A        try:%0A            robin.delete(
+elf.assertEqual(Subscription.objects.count(), 1)%0A        self.assertEqual(Ticket.objects.count(), 1
 )%0A
+%0A
         
     
@@ -2073,149 +2073,124 @@
     
-    self.fail(%22Expected veto%22)%0A        except Warning as e:%0A            self.assertEqual(%0A                str(e), %22Cannot delete User Robin %22
+# when we have deleted the ticket, deleting the user works%0A        # because the subscription is deleted in cascade:
 %0A   
@@ -2190,32 +2190,33 @@
 de:%0A        
+%0A
         
 %22because 1 S
@@ -2207,89 +2207,45 @@
     
-%22because 1 Stars refer to it.%22)%0A%0A        self.assertEqual(Star.objects.count(), 1
+ticket.delete()%0A        robin.delete(
 )%0A  
@@ -2259,38 +2259,44 @@
 elf.assertEqual(
-Ticket
+Subscription
 .objects.count()
@@ -2301,52 +2301,9 @@
 (), 
-1)%0A        star.delete()%0A        obj.delete(
+0
 )%0A  
@@ -2329,12 +2329,14 @@
 ual(
-Star
+Ticket
 .obj
@@ -2369,38 +2369,36 @@
 elf.assertEqual(
-Ticket
+User
 .objects.count()

fix a failure on travis