Source

django-mssql-ado-merge / tests / test_main / regressiontests / models.py

Full commit
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
import datetime
import decimal
from django.db import models
from django.test import TestCase
from django.core.paginator import Paginator
from sqlserver_ado.fields import BigAutoField, BigIntegerField, BigForeignKey

from adodbapi import IntegrityError

class Bug19Table(models.Model):
    """ A simple model for testing string comparisons.
    
    >>> choices = (
    ...     "no_slashes", "no_slashes_2",
    ...     r"C:\some_folder\somefile.ext", r"\some_folder",
    ...     "some_folder"+'\0x5c', # 0x5c is a backslash
    ...     "[brackets]",
    ...     )
    >>> for c in choices:
    ...     Bug19Table.objects.create(choice=c).save()
    >>> len(Bug19Table.objects.all())
    6    
    >>> len(Bug19Table.objects.filter(choice__contains="shes"))
    2
    >>> len(Bug19Table.objects.filter(choice__endswith="shes"))
    1
    >>> len(Bug19Table.objects.filter(choice__contains=r"der\som"))
    1
    >>> len(Bug19Table.objects.filter(choice__endswith=r"der\somefile.ext"))
    1
    >>> len(Bug19Table.objects.filter(choice__contains="["))
    1
    """
    choice = models.TextField(max_length=200)
    
    def __unicode__(self):
        return self.choice


class Bug21Table(models.Model):
    """
    Test adding decimals as actual types or as strings.
    
    >>> Bug21Table(a='decimal as decimal', d=decimal.Decimal('12.34')).save()
    >>> Bug21Table(a='decimal as string', d=u'56.78').save()
    >>> len(list(Bug21Table.objects.all()))
    2
    """
    a = models.CharField(max_length=50)
    d = models.DecimalField(max_digits=5, decimal_places=2)

class Bug27Table(models.Model):
    """
    Test that extra/select works, and doesn't interfere with the 
    limit/offset implementation.
    
    >>> Bug27Table(a=100).save()
    >>> Bug27Table(a=101).save()
    >>> len(list(Bug27Table.objects.all()))
    2
    
    >>> objs = list(Bug27Table.objects.extra(select={'alias_for_a':'a'}).order_by('a'))
    >>> objs[0].alias_for_a
    100

    >>> objs[1].alias_for_a
    101
    """
    
    a = models.IntegerField()

class Bug23Table(models.Model):
    """
    Test inserting mixed NULL and non-NULL values.

    >>> Bug23Table(mycharfield1=None, mycharfield2="text2", myintfield=1).save()
    >>> Bug23Table(mycharfield1="text1", mycharfield2=None, myintfield=1).save()
    >>> Bug23Table(mycharfield1="text1", mycharfield2="text2", myintfield=None).save()
    >>> Bug23Table(mycharfield1=None, mycharfield2=None, myintfield=1).save()
    >>> Bug23Table(mycharfield1=None, mycharfield2="text2", myintfield=None).save()
    >>> Bug23Table(mycharfield1="text1", mycharfield2=None, myintfield=None).save()
    >>> Bug23Table(mycharfield1=None, mycharfield2=None, myintfield=None).save()
    >>> objs = list(Bug23Table.objects.all())
    >>> len(objs)
    7
    >>> len([obj for obj in objs if obj.mycharfield1=="text1"])
    3
    >>> len([obj for obj in objs if obj.mycharfield2=="text2"])
    3
    >>> len([obj for obj in objs if obj.myintfield==1])
    3
    """
    mycharfield1 = models.CharField(max_length=100, null=True)
    mycharfield2 = models.CharField(max_length=50, null=True)
    myintfield = models.IntegerField(null=True)


# Bug 26 tables, RelatedA and RelatedB
class RelatedB(models.Model):
    a = models.CharField(max_length=50)
    b = models.CharField(max_length=50)
    c = models.CharField(max_length=50)

class RelatedA(models.Model):
    a = models.CharField(max_length=50)
    b = models.ForeignKey(RelatedB)

class Bug26TestCase(TestCase):
    """Test that slicing queries w/ duplicate column names works."""

    def testWithDuplicateColumnNames(self):
        b = RelatedB(a='this is a value', b="valueb", c="valuec")
        b.save()
        
        RelatedA(a="valuea", b=b).save()
        RelatedA(a="valuea", b=b).save()

        items = RelatedA.objects.select_related()[1:2]
        self.assertEqual(len(items), 1)
        
class Bug34DatetimeTable(models.Model):
    """Ensure that __year filters work with datetime fields.
    
    >>> Bug34DatetimeTable(posted=datetime.date(2007,1,1)).save()
    >>> Bug34DatetimeTable(posted=datetime.date(2008,2,2)).save()
    >>> Bug34DatetimeTable(posted=datetime.date(2009,3,3)).save()
    >>> len((Bug34DatetimeTable.objects.filter(posted__day=3)))
    1
    >>> len(list(Bug34DatetimeTable.objects.filter(posted__month=2)))
    1
    >>> len(list(Bug34DatetimeTable.objects.filter(posted__year=2008)))
    1
    >>> len(list(Bug34DatetimeTable.objects.filter(posted__year=2005)))
    0
    """
    posted = models.DateTimeField()
        
class Bug34DateTable(models.Model):
    """Ensure that __year filters work with date fields.
    
    >>> Bug34DateTable(posted=datetime.date(2007,1,1)).save()
    >>> Bug34DateTable(posted=datetime.date(2008,2,2)).save()
    >>> Bug34DateTable(posted=datetime.date(2009,3,3)).save()
    >>> len((Bug34DateTable.objects.filter(posted__day=3)))
    1
    >>> len(list(Bug34DateTable.objects.filter(posted__month=2)))
    1
    >>> len(list(Bug34DateTable.objects.filter(posted__year=2008)))
    1
    >>> len(list(Bug34DateTable.objects.filter(posted__year=2005)))
    0
    """
    posted = models.DateField()

class Bug35ATable(models.Model):
    """Ensure that multiple Postive Integer columns across tables don't 
    create duplicate constraint names.
    """
    int1 = models.PositiveIntegerField()
    int2 = models.PositiveIntegerField()
    int3 = models.PositiveSmallIntegerField()
    int4 = models.PositiveSmallIntegerField()

class Bug35BTable(models.Model):
    """Ensure that multiple Postive Integer columns across tables don't 
    create duplicate constraint names.
    """
    int1 = models.PositiveIntegerField()
    int2 = models.PositiveIntegerField()
    int3 = models.PositiveSmallIntegerField()
    int4 = models.PositiveSmallIntegerField()

class Bug35CModel(models.Model):
    """Ensure that multiple Postive Integer columns across tables don't 
    create duplicate constraint names when using inheritence.
    
    The test for Bug35 is just the creation of the tables; there are no
    other explicit tests.
    """
    age = models.PositiveIntegerField()

    class Meta:
        abstract = True

class Bug35C1Table(Bug35CModel):
    name = models.CharField(max_length=10)
    
class Bug35C2Table(Bug35CModel):
    name = models.CharField(max_length=10)


class Bug37ATable(models.Model):
    a = models.CharField(max_length=50)
    b = models.CharField(max_length=50)
    c = models.CharField(max_length=50)

class Bug37BTable(models.Model):
    d = models.CharField(max_length=50)
    a = models.ForeignKey(Bug37ATable)

class Bug37TestCase(TestCase):
    """Test that IntegrityErrors are raised when appropriate."""

    def testDuplicateKeysFails(self):
        Bug37ATable(pk=1, a='a', b='b', c='c').save(force_insert=True)
        try:
            Bug37ATable(pk=1, a='a', b='b', c='c').save(force_insert=True)
        except Exception, e:
            self.failUnless(isinstance(e, IntegrityError), 'Expected IntegrityError but got: %s' % type(e))
            
    def testDeleteRelatedRecordFails(self):
        a2 = Bug37ATable(a='a', b='b', c='c')
        a2.save()
        
        Bug37BTable(d='d', a=a2).save()
        try:
            a2.delete()
        except Exception, e:
            self.failUnless(isinstance(e, IntegrityError), 'Expected IntegrityError but got: %s' % type(e))

class Bug38Table(models.Model):
    d = models.DecimalField(max_digits=5, decimal_places=2)


class Bug41Table(models.Model):
    """
    Test that pagination works with extra/select columns.

    >>> Bug41Table(a=100).save()
    >>> Bug41Table(a=101).save()
    >>> Bug41Table(a=102).save()
    >>> len(list(Bug41Table.objects.all()))
    3

    >>> objs = Bug41Table.objects.extra(select={'alias_for_a':'[regressiontests_bug41table].[a]'}).order_by('alias_for_a')
    >>> all_objs = Paginator(objs, 1)
    >>> all_objs.count
    3
    >>> all_objs.num_pages
    3
    >>> page1 = all_objs.page(1)
    >>> page1.object_list[0].alias_for_a
    100
    >>> page2 = all_objs.page(2)
    >>> page2.object_list[0].alias_for_a
    101
    """

    a = models.IntegerField()

class Bug62Table(models.Model):
    email = models.CharField(max_length=255, blank=True)

class Bug58TableRecipe(models.Model):
    name = models.CharField(max_length=30)

class Bug58TableIngredient(models.Model):
    name = models.CharField(max_length=30)

class Bug58TableItem(models.Model):
    recipe = models.ForeignKey(Bug58TableRecipe, related_name='item')
    ingredient = models.ForeignKey(Bug58TableIngredient, related_name='item')
    amount = models.CharField(max_length=30)

class Bug58TestCase(TestCase):
    def testDistinctRelated(self):
        i1 = Bug58TableIngredient(name='bread')
        i1.save()
        i2 = Bug58TableIngredient(name='butter')
        i2.save()
        
        r = Bug58TableRecipe(name='toast')
        r.save()
        
        Bug58TableItem(recipe=r, ingredient=i1, amount='1 slice').save()
        Bug58TableItem(recipe=r, ingredient=i2, amount='1 Tbsp').save()

        q = list(Bug58TableRecipe.objects.filter(item__ingredient__name__in=['bread','butter']).distinct())
        self.assertEqual(len(q), 1)


class Bug62TestCase(TestCase):
    def testExclude(self):
        Bug62Table(email='').save()
        Bug62Table(email='abc').save()
        Bug62Table(email='').save()
        Bug62Table(email='def').save()
        
        q = list(Bug62Table.objects.exclude(email='abc'))
        self.assertEqual(len(q), 3)

    def testExcludeEmpty(self):
        Bug62Table(email='').save()
        Bug62Table(email='abc').save()
        Bug62Table(email='').save()
        Bug62Table(email='def').save()

        q = list(Bug62Table.objects.exclude(email=''))
        self.assertEqual(len(q), 2)

    def testExcludeEmptyUnicode(self):
        Bug62Table(email='').save()
        Bug62Table(email='abc').save()
        Bug62Table(email='').save()
        Bug62Table(email='def').save()
        
        q = list(Bug62Table.objects.exclude(email=u''))
        self.assertEqual(len(q), 2)

class Bug63Table(models.Model):
    """
    Test that the BigAutoField and BigIntegerField fields work.

    >>> Bug63Table(number=2147483648L).save()
    >>> len(list(Bug63Table.objects.all()))
    1
    >>> big = Bug63Table.objects.get(number=2147483648L)
    >>> big.number
    2147483648L
    """
    id = BigAutoField(primary_key=True)
    number = BigIntegerField()

class Bug64Table(models.Model):
    """
    Test that a BigForeignKey works as intended.
    
    >>> a = Bug63Table(number=2147483648L)
    >>> a.save()
    >>> b = Bug64Table(key=a)
    >>> b.save()
    >>> a == b.key
    True
    >>> b.key.number == 2147483648L
    True
    """
    
    key = BigForeignKey(Bug63Table)


class Bug66Table(models.Model):
    """
    Test that pagination works when pk uses db_column

    >>> Bug66Table(a=100).save()
    >>> Bug66Table(a=101).save()
    >>> Bug66Table(a=102).save()
    >>> len(list(Bug66Table.objects.all()))
    3

    >>> objs = Bug66Table.objects.all()
    >>> all_objs = Paginator(objs, 1)
    >>> all_objs.count
    3
    >>> all_objs.num_pages
    3
    >>> page1 = all_objs.page(1)
    >>> page1.object_list[0].a
    100
    >>> page2 = all_objs.page(2)
    >>> page2.object_list[0].a
    101
    """
    id = models.AutoField(primary_key=True, db_column='bug41id')
    a = models.IntegerField()
    

class Bug69Table1(models.Model):
    """
    Test that pagination works when db_column is specified but the text case
    does not match between tables.
    
    http://code.google.com/p/django-mssql/issues/detail?id=69
    """
    # db_column is lowercase here
    id = models.IntegerField(primary_key=True, db_column='table1id')
    

class Bug69Table2(models.Model):
    id = models.IntegerField(primary_key=True, db_column='Table2Id')
    # db_column is camelcase here
    related_obj = models.ForeignKey(Bug69Table1, db_column='Table1Id')
    

class MyAutoField(models.AutoField): 
    def get_internal_type(self):
        """
        Need to explicitly specify this internal type as AutoField so 
        it maps to a recognized database type. See Django ticket #13516
        """
        return "AutoField"

class Bug70Table(models.Model):
	"""
	Test that insert works with subclasses of AutoField.
	"""
	id = MyAutoField(primary_key=True, db_column="Table70Id")
	a = models.IntegerField()

class Bug93Table(models.Model):
    """
    Test that __year works.
    """
    d = models.DateField()
    dt = models.DateTimeField()

class IntegerIdTable(models.Model):
    """
    Simple table consisting of only an Integer ID primary key
    """
    id = models.IntegerField(primary_key=True)