Skip to content

Instantly share code, notes, and snippets.

@funnydman
Created January 2, 2020 09:20
Show Gist options
  • Save funnydman/f664b8da7a21bfdc92bf7832407da8bd to your computer and use it in GitHub Desktop.
Save funnydman/f664b8da7a21bfdc92bf7832407da8bd to your computer and use it in GitHub Desktop.
differences in order_by behaviour
# settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'helloworld',
'PASSWORD': 'adminhris',
'USER': 'adminhris',
'HOST': 'localhost'
},
'sql_lite': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': 'test'
}
}
# -----------------
# models.py
class TestModel(models.Model):
name = models.CharField(max_length=256)
age = models.PositiveSmallIntegerField(null=True, blank=True)
start_date = models.DateTimeField(null=True, blank=True)
# -----------------
from datetime import datetime, timedelta
from django.test import TestCase
from core.models import TestModel
class MyTestCase(TestCase):
databases = ('default',)
def setUp(self):
TestModel.objects.create(name='first', start_date=datetime.today())
TestModel.objects.create(name='second', start_date=datetime.today() + timedelta(days=10))
TestModel.objects.create(name='third')
def test_something_default(self):
tm = TestModel.objects.using('default').order_by('-start_date')
self.assertEqual(tm.first().name, 'third')
class MyTestCaseLite(TestCase):
databases = ('sql_lite',)
def setUp(self):
TestModel.objects.using('sql_lite').create(name='first', start_date=datetime.today())
TestModel.objects.using('sql_lite').create(name='second', start_date=datetime.today() + timedelta(days=10))
TestModel.objects.using('sql_lite').create(name='third')
def test_something_sql_lite(self):
tm = TestModel.objects.using('sql_lite').order_by('-start_date')
self.assertEqual(tm.first().name, 'second')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment