from __future__ import absolute_import
from __future__ import unicode_literals
from django.test import TestCase
from corehq.apps.groups.models import Group
from corehq.apps.users.cases import get_wrapped_owner, get_owning_users
from corehq.apps.users.models import CommCareUser
from six.moves import range


class CaseUtilsTestCase(TestCase):

    def setUp(self):
        self.domain = 'test'

    def test_get_wrapped_user(self):
        user = CommCareUser.create(self.domain, 'wrapped-user-test', 'password')
        user.save()
        self.addCleanup(user.delete)
        wrapped = get_wrapped_owner(user._id)
        self.assertTrue(isinstance(wrapped, CommCareUser))

    def test_get_wrapped_group(self):
        group = Group(domain=self.domain, name='wrapped-group-test')
        group.save()
        self.addCleanup(group.delete)
        wrapped = get_wrapped_owner(group._id)
        self.assertTrue(isinstance(wrapped, Group))

    def test_owned_by_user(self):
        user = CommCareUser.create(self.domain, 'owned-user-test', 'password')
        user.save()
        self.addCleanup(user.delete)
        owners = get_owning_users(user._id)
        self.assertEqual(1, len(owners))
        self.assertEqual(owners[0]._id, user._id)
        self.assertTrue(isinstance(owners[0], CommCareUser))

    def test_owned_by_group(self):
        ids = []
        for i in range(5):
            user = CommCareUser.create(self.domain, 'owned-group-test-user-%s' % i, 'password')
            user.save()
            self.addCleanup(user.delete)
            ids.append(user._id)

        group = Group(domain=self.domain, name='owned-group-test-group', users=ids)
        group.save()
        self.addCleanup(group.delete)
        owners = get_owning_users(group._id)
        self.assertEqual(5, len(owners))
        ids_back = []
        for o in owners:
            self.assertTrue(isinstance(o, CommCareUser))
            ids_back.append(o._id)
        self.assertEqual(set(ids), set(ids_back))

@@ -300,16 +300,67 @@
  range%0A%0A
+from corehq.util.test_utils import generate_cases%0A%0A
 %0Aclass C
@@ -2045,8 +2045,251 @@
 _back))%0A
+%0A%0A@generate_cases(%0A    (%0A        (1, ),%0A        (1.0, ),%0A        (None, ),%0A        ('', ),%0A        ('foobar', ),%0A    ),%0A    CaseUtilsTestCase,%0A)%0Adef test_invalid_ids(self, invalid_id):%0A    self.assertEqual(None, get_wrapped_owner(invalid_id))%0A

add some failing test cases for get_wrapped_owner