def test_select_star(self): command = SelectCommand(connections['default'], FormattingTestModel.objects.all().query) sql = generate_sql_representation(command) expected = """ SELECT (*) FROM {} """.format(FormattingTestModel._meta.db_table).strip() self.assertEqual(expected, sql)
def test_limit_applied(self): command = SelectCommand(connections['default'], FormattingTestModel.objects.all()[10:15].query) sql = generate_sql_representation(command) expected = """ SELECT (*) FROM {} OFFSET 10 LIMIT 5 """.format(FormattingTestModel._meta.db_table).strip() self.assertEqual(expected, sql)
def test_unicode_error(self): command = SelectCommand( connections['default'], FormattingTestModel.objects.filter(field2=u"Jacqu\xe9s").query) sql = generate_sql_representation(command) expected = u""" SELECT (*) FROM {} WHERE (field2='Jacqu\xe9s') """.format(FormattingTestModel._meta.db_table).strip() self.assertEqual(expected, sql)
def test_ordering_applied(self): command = SelectCommand( connections['default'], FormattingTestModel.objects.order_by("-field1").query) sql = generate_sql_representation(command) expected = """ SELECT (*) FROM {} ORDER BY field1 DESC """.format(FormattingTestModel._meta.db_table).strip() self.assertEqual(expected, sql) command = SelectCommand( connections['default'], FormattingTestModel.objects.order_by("field1", "-field2").query) sql = generate_sql_representation(command) expected = """ SELECT (*) FROM {} ORDER BY field1, field2 DESC """.format(FormattingTestModel._meta.db_table).strip() self.assertEqual(expected, sql)
def test_select_in(self): """ We don't build explicit IN queries, only multiple OR branches there is essentially no difference between the two """ command = SelectCommand( connections['default'], FormattingTestModel.objects.filter(field1__in=[1, 2]).query) sql = generate_sql_representation(command) expected = """ SELECT (*) FROM {} WHERE (field1=2) OR (field1=1) """.format(FormattingTestModel._meta.db_table).strip() self.assertEqual(expected, sql)