Exemple #1
0
    def test_icontains(self):
        from testapp.models import Simple
        from django.core.management import call_command
        from django.db.models import Count

        call_command('syncdb')

        names = (
            'Start 1',
            '2 Start',
            'Art 2',
            '123 ART',
            )
        for n in names:
            s = Simple(name=n)
            s.save()

        qs = Simple.objects.filter(name__icontains='art')
        q = qs.aggregate(n = Count('id'))
        self.assertEqual(q['n'], len(names))

        qs = Simple.objects.filter(name__icontains='start')
        q = qs.aggregate(n = Count('id'))
        self.assertEqual(q['n'], 2)

        qs = Simple.objects.filter(name__icontains='art ')
        q = qs.aggregate(n = Count('id'))
        self.assertEqual(q['n'], 2)
Exemple #2
0
    def testutf8(self):
        '''utf-8 in, unicode out.'''

        from testapp.models import Simple, Parent, Aunt, GrandParent
        from django.core.management import call_command

        call_command('syncdb')

        unicode_cafe  = u"caf" + unichr(0x00E9)
        utf8_cafe = unicode_cafe.encode('utf8')

        print "cafe =", unicode_cafe.encode('utf8')
        
        s = Simple(name=unicode_cafe)
        s.save()

        #
        # When we retrieve object, name should now be unicode.
        # 

        o = Simple.objects.get(pk=1)
        django_cafe = o.name

        self.assertEqual(type(django_cafe), type(unicode_cafe))
        self.assertEqual(django_cafe, unicode_cafe)
Exemple #3
0
    def test_icontains(self):
        from testapp.models import Simple
        from django.core.management import call_command
        from django.db.models import Count

        call_command('syncdb')

        names = (
            'Start 1',
            '2 Start',
            'Art 2',
            '123 ART',
        )
        for n in names:
            s = Simple(name=n)
            s.save()

        qs = Simple.objects.filter(name__icontains='art')
        q = qs.aggregate(n=Count('id'))
        self.assertEqual(q['n'], len(names))

        qs = Simple.objects.filter(name__icontains='start')
        q = qs.aggregate(n=Count('id'))
        self.assertEqual(q['n'], 2)

        qs = Simple.objects.filter(name__icontains='art ')
        q = qs.aggregate(n=Count('id'))
        self.assertEqual(q['n'], 2)
Exemple #4
0
    def testutf8(self):
        '''utf-8 in, unicode out.'''

        from testapp.models import Simple, Parent, Aunt, GrandParent
        from django.core.management import call_command

        call_command('syncdb')

        unicode_cafe = u"caf" + unichr(0x00E9)
        utf8_cafe = unicode_cafe.encode('utf8')

        print "cafe =", unicode_cafe.encode('utf8')

        s = Simple(name=unicode_cafe)
        s.save()

        #
        # When we retrieve object, name should now be unicode.
        #

        o = Simple.objects.get(pk=1)
        django_cafe = o.name

        self.assertEqual(type(django_cafe), type(unicode_cafe))
        self.assertEqual(django_cafe, unicode_cafe)
Exemple #5
0
    def test_safeunicode(self):
        '''Slug fields have type SafeUnicode, we need to support
        this field type.'''

        from testapp.models import Simple
        from django.core.management import call_command
        from django.utils.safestring import SafeUnicode

        call_command('syncdb')

        n = SafeUnicode('a name')
        s = Simple(name=n)
        s.save()
Exemple #6
0
    def test_safeunicode(self):
        '''Slug fields have type SafeUnicode, we need to support
        this field type.'''

        from testapp.models import Simple
        from django.core.management import call_command
        from django.utils.safestring import SafeUnicode

        call_command('syncdb')

        n = SafeUnicode('a name')
        s = Simple(name=n)
        s.save()
Exemple #7
0
    def test_autocommit_like_behavior(self):
        '''Django's default behavior is "like" auto commit.

        From Django docs, "Managing database transactions":

            Django's default behavior is to run with an open
            transaction which it commits automatically when any
            built-in, data-altering model function is called. For
            example, if you call model.save() or model.delete(),
            the change will be committed immediately.

            This is much like the auto-commit setting for most
            databases.

        Note the "much like" phrase.  Initially, I thought they
        meant you should turn on autocommit on the database
        connection, but that didn't work.  They mean that
        internally Django has code to mimic autocommit.
        '''

        from testapp.models import Simple
        from django.core.management import call_command
        from django.core.validators import ValidationError
        import django

        # full_clean() (used by this test) is new to Django 1.2
        maj = django.VERSION[0]
        min = django.VERSION[1]
        self.assertTrue(maj > 1 or (maj == 1 and min >= 2))

        call_command('syncdb')

        s = Simple(name='mark')
        s.save()
        s = Simple(name='mark')
        try:
            s.full_clean()
            s.save()
        except ValidationError:
            pass

        # first one should be saved.
        s = Simple.objects.get(name='mark')
        self.assertTrue(s is not None)
Exemple #8
0
    def test_iexact(self):
        from testapp.models import Simple
        from django.core.management import call_command
        from django.db.models import Count

        call_command('syncdb')

        names = (
            'iExactly this',
            'iExactly This',
            'iExactly This not',
            'not iExactly This',
            )
        for n in names:
            s = Simple(name=n)
            s.save()

        iexact_matches_n = len(names) - 2

        qs = Simple.objects.filter(name__iexact='iExactly this')
        q = qs.aggregate(n = Count('id'))
        self.assertEqual(q['n'], iexact_matches_n)
Exemple #9
0
    def test_iexact(self):
        from testapp.models import Simple
        from django.core.management import call_command
        from django.db.models import Count

        call_command('syncdb')

        names = (
            'iExactly this',
            'iExactly This',
            'iExactly This not',
            'not iExactly This',
        )
        for n in names:
            s = Simple(name=n)
            s.save()

        iexact_matches_n = len(names) - 2

        qs = Simple.objects.filter(name__iexact='iExactly this')
        q = qs.aggregate(n=Count('id'))
        self.assertEqual(q['n'], iexact_matches_n)
Exemple #10
0
    def test_loadfixture(self):
        '''Update sequence nextval's when a fixture is loaded.

        Note: Here's a great set of fixture unit tests:

        http://django-pyodbc.googlecode.com/svn/trunk/tests/fixtures/models.py
        '''

        from testapp.models import Simple
        from django.core import management
        from django.db.models import Count

        management.call_command('syncdb')
        management.call_command('loaddata', 'fixture1', verbosity=0)

        # Fixture should have loaded Two records.
        qs = Simple.objects.all()
        d = qs.aggregate(n = Count('id'))
        self.assertEqual(d['n'], 2)

        # We should be able to save a new record.
        s = Simple(name = 'abc xyz')
        s.save()
Exemple #11
0
    def test_loadfixture(self):
        '''Update sequence nextval's when a fixture is loaded.

        Note: Here's a great set of fixture unit tests:

        http://django-pyodbc.googlecode.com/svn/trunk/tests/fixtures/models.py
        '''

        from testapp.models import Simple
        from django.core import management
        from django.db.models import Count

        management.call_command('syncdb')
        management.call_command('loaddata', 'fixture1', verbosity=0)

        # Fixture should have loaded Two records.
        qs = Simple.objects.all()
        d = qs.aggregate(n=Count('id'))
        self.assertEqual(d['n'], 2)

        # We should be able to save a new record.
        s = Simple(name='abc xyz')
        s.save()
Exemple #12
0
    def test_startswith(self):
        from testapp.models import Simple
        from django.core.management import call_command
        from django.db.models import Count

        call_command('syncdb')

        names = (
            'start 1',
            'start 2',
            'start 12',
            'Start 12',
            )
        for n in names:
            s = Simple(name=n)
            s.save()

        qs = Simple.objects.filter(name__startswith='start')
        q = qs.aggregate(n = Count('id'))
        self.assertEqual(q['n'], 3)

        qs = Simple.objects.filter(name__startswith='start 1')
        q = qs.aggregate(n = Count('id'))
        self.assertEqual(q['n'], 2)

        qs = Simple.objects.filter(name__startswith='start 12')
        q = qs.aggregate(n = Count('id'))
        self.assertEqual(q['n'], 1)

        qs = Simple.objects.filter(name__startswith='start 3')
        q = qs.aggregate(n = Count('id'))
        self.assertEqual(q['n'], 0)

        qs = Simple.objects.filter(name__startswith='tart 1')
        q = qs.aggregate(n = Count('id'))
        self.assertEqual(q['n'], 0)
Exemple #13
0
    def test_startswith(self):
        from testapp.models import Simple
        from django.core.management import call_command
        from django.db.models import Count

        call_command('syncdb')

        names = (
            'start 1',
            'start 2',
            'start 12',
            'Start 12',
        )
        for n in names:
            s = Simple(name=n)
            s.save()

        qs = Simple.objects.filter(name__startswith='start')
        q = qs.aggregate(n=Count('id'))
        self.assertEqual(q['n'], 3)

        qs = Simple.objects.filter(name__startswith='start 1')
        q = qs.aggregate(n=Count('id'))
        self.assertEqual(q['n'], 2)

        qs = Simple.objects.filter(name__startswith='start 12')
        q = qs.aggregate(n=Count('id'))
        self.assertEqual(q['n'], 1)

        qs = Simple.objects.filter(name__startswith='start 3')
        q = qs.aggregate(n=Count('id'))
        self.assertEqual(q['n'], 0)

        qs = Simple.objects.filter(name__startswith='tart 1')
        q = qs.aggregate(n=Count('id'))
        self.assertEqual(q['n'], 0)
Exemple #14
0
    def test_autocommit_like_behavior(self):
        '''Django's default behavior is "like" auto commit.

        From Django docs, "Managing database transactions":

            Django's default behavior is to run with an open
            transaction which it commits automatically when any
            built-in, data-altering model function is called. For
            example, if you call model.save() or model.delete(),
            the change will be committed immediately.

            This is much like the auto-commit setting for most
            databases.

        Note the "much like" phrase.  Initially, I thought they
        meant you should turn on autocommit on the database
        connection, but that didn't work.  They mean that
        internally Django has code to mimic autocommit.
        '''

        from testapp.models import Simple
        from django.core.management import call_command
        from django.core.validators import ValidationError
        import django

        # full_clean() (used by this test) is new to Django 1.2
        maj = django.VERSION[0]
        min = django.VERSION[1]
        self.assertTrue(maj > 1 or (maj == 1 and min >= 2))
        
        call_command('syncdb')

        s = Simple(name='mark')
        s.save()
        s = Simple(name='mark')
        try:
            s.full_clean()    
            s.save()
        except ValidationError:
            pass

        # first one should be saved.
        s = Simple.objects.get(name='mark')
        self.assertTrue(s is not None)
Exemple #15
0
    def testcascadingdelete(self):
        '''in Django, deletes cascade.  test this works.'''

        from testapp.models import Simple, Parent, Aunt, GrandParent
        from django.core.management import call_command

        call_command('syncdb')

        s = Simple(name='one')
        s.save()
        s = Simple(name='two')
        s.save()

        #
        # Simple record should now be in database.
        #

        try:
            s = Simple.objects.get(name='one')
        except Simple.DoesNotExist:
            self.fail("didn't save Simple record")

        p = Parent(simple=s, name='p')
        p.save()
        try:
            tst = Parent.objects.get(name='p')
        except Parent.DoesNotExist:
            self.fail("didn't save Parent record")

        a = Aunt(simple=s, name='a')
        a.save()
        try:
            tst = Aunt.objects.get(name='a')
        except Aunt.DoesNotExist:
            self.fail("didn't save Aunt record")

        gp = GrandParent(parent=p, name='gp')
        gp.save()
        try:
            tst = GrandParent.objects.get(name='gp')
        except GrandParent.DoesNotExist:
            self.fail("didn't save GrandParent record")

        s.delete()

        #
        # Parent, Aunt and GrandParent records should now be
        # gone from database.
        #

        ok = False
        try:
            tst = Parent.objects.get(name='p')
        except Parent.DoesNotExist:
            ok = True
        self.failIf(not ok, "delete did not cascade");

        ok = False
        try:
            tst = GrandParent.objects.get(name='gp')
        except GrandParent.DoesNotExist:
            ok = True
        self.failIf(not ok, "delete did not cascade");

        ok = False
        try:
            tst = Aunt.objects.get(name='a')
        except Aunt.DoesNotExist:
            ok = True
        self.failIf(not ok, "delete did not cascade");
Exemple #16
0
    def testcascadingdelete(self):
        '''in Django, deletes cascade.  test this works.'''

        from testapp.models import Simple, Parent, Aunt, GrandParent
        from django.core.management import call_command

        call_command('syncdb')

        s = Simple(name='one')
        s.save()
        s = Simple(name='two')
        s.save()

        #
        # Simple record should now be in database.
        #

        try:
            s = Simple.objects.get(name='one')
        except Simple.DoesNotExist:
            self.fail("didn't save Simple record")

        p = Parent(simple=s, name='p')
        p.save()
        try:
            tst = Parent.objects.get(name='p')
        except Parent.DoesNotExist:
            self.fail("didn't save Parent record")

        a = Aunt(simple=s, name='a')
        a.save()
        try:
            tst = Aunt.objects.get(name='a')
        except Aunt.DoesNotExist:
            self.fail("didn't save Aunt record")

        gp = GrandParent(parent=p, name='gp')
        gp.save()
        try:
            tst = GrandParent.objects.get(name='gp')
        except GrandParent.DoesNotExist:
            self.fail("didn't save GrandParent record")

        s.delete()

        #
        # Parent, Aunt and GrandParent records should now be
        # gone from database.
        #

        ok = False
        try:
            tst = Parent.objects.get(name='p')
        except Parent.DoesNotExist:
            ok = True
        self.failIf(not ok, "delete did not cascade")

        ok = False
        try:
            tst = GrandParent.objects.get(name='gp')
        except GrandParent.DoesNotExist:
            ok = True
        self.failIf(not ok, "delete did not cascade")

        ok = False
        try:
            tst = Aunt.objects.get(name='a')
        except Aunt.DoesNotExist:
            ok = True
        self.failIf(not ok, "delete did not cascade")