Пример #1
0
    async def test_single_when(self):
        category = Case(When(intnum__gte=8, then="big"), default="default")
        sql = IntFields.all().annotate(category=category).values(
            "intnum", "category").sql()

        dialect = self.db.schema_generator.DIALECT
        if dialect == "mysql":
            expected_sql = "SELECT `intnum` `intnum`,CASE WHEN `intnum`>=8 THEN 'big' ELSE 'default' END `category` FROM `intfields`"
        else:
            expected_sql = 'SELECT "intnum" "intnum",CASE WHEN "intnum">=8 THEN \'big\' ELSE \'default\' END "category" FROM "intfields"'
        self.assertEqual(sql, expected_sql)
Пример #2
0
    async def test_q_object_when(self):
        category = Case(When(Q(intnum__gt=2, intnum__lt=8), then="middle"),
                        default="default")
        sql = IntFields.all().annotate(category=category).values(
            "intnum", "category").sql()

        dialect = self.db.schema_generator.DIALECT
        if dialect == "mysql":
            expected_sql = "SELECT `intnum` `intnum`,CASE WHEN `intnum`>2 AND `intnum`<8 THEN 'middle' ELSE 'default' END `category` FROM `intfields`"
        else:
            expected_sql = 'SELECT "intnum" "intnum",CASE WHEN "intnum">2 AND "intnum"<8 THEN \'middle\' ELSE \'default\' END "category" FROM "intfields"'
        self.assertEqual(sql, expected_sql)
Пример #3
0
 async def test_case_when_in_where(self):
     category = Case(When(intnum__gte=8, then="big"),
                     When(intnum__lte=2, then="small"),
                     default="middle")
     sql = (IntFields.all().annotate(category=category).filter(
         category__in=["big", "small"]).values("intnum").sql())
     dialect = self.db.schema_generator.DIALECT
     if dialect == "mysql":
         expected_sql = "SELECT `intnum` `intnum` FROM `intfields` WHERE CASE WHEN `intnum`>=8 THEN 'big' WHEN `intnum`<=2 THEN 'small' ELSE 'middle' END IN ('big','small')"
     else:
         expected_sql = "SELECT \"intnum\" \"intnum\" FROM \"intfields\" WHERE CASE WHEN \"intnum\">=8 THEN 'big' WHEN \"intnum\"<=2 THEN 'small' ELSE 'middle' END IN ('big','small')"
     self.assertEqual(sql, expected_sql)
Пример #4
0
    async def test_func_default(self):
        category = Case(When(intnum__gte=8, then=8),
                        default=Coalesce("intnum_null", 10))
        sql = IntFields.all().annotate(category=category).values(
            "intnum", "category").sql()

        dialect = self.db.schema_generator.DIALECT
        if dialect == "mysql":
            expected_sql = "SELECT `intnum` `intnum`,CASE WHEN `intnum`>=8 THEN 8 ELSE COALESCE(`intnum_null`,10) END `category` FROM `intfields`"
        else:
            expected_sql = 'SELECT "intnum" "intnum",CASE WHEN "intnum">=8 THEN 8 ELSE COALESCE("intnum_null",10) END "category" FROM "intfields"'
        self.assertEqual(sql, expected_sql)
Пример #5
0
    async def test_AE_default(self):
        # AE: ArithmeticExpression
        category = Case(When(intnum__gte=8, then=8), default=F("intnum") + 1)
        sql = IntFields.all().annotate(category=category).values(
            "intnum", "category").sql()

        dialect = self.db.schema_generator.DIALECT
        if dialect == "mysql":
            expected_sql = "SELECT `intnum` `intnum`,CASE WHEN `intnum`>=8 THEN 8 ELSE `intnum`+1 END `category` FROM `intfields`"
        else:
            expected_sql = 'SELECT "intnum" "intnum",CASE WHEN "intnum">=8 THEN 8 ELSE "intnum"+1 END "category" FROM "intfields"'
        self.assertEqual(sql, expected_sql)
Пример #6
0
 async def test_sqlite_func_rand(self):
     sql = IntFields.all().annotate(randnum=SqliteRandom()).values("intnum", "randnum").sql()
     expected_sql = 'SELECT "intnum" "intnum",RANDOM() "randnum" FROM "intfields"'
     self.assertEqual(sql, expected_sql)
Пример #7
0
 async def test_mysql_func_rand_with_seed(self):
     sql = IntFields.all().annotate(randnum=Rand(0)).values("intnum", "randnum").sql()
     expected_sql = "SELECT `intnum` `intnum`,RAND(0) `randnum` FROM `intfields`"
     self.assertEqual(sql, expected_sql)
Пример #8
0
    async def test_async_iter(self):
        counter = 0
        async for _ in IntFields.all():
            counter += 1

        self.assertEqual(await IntFields.all().count(), counter)
Пример #9
0
 async def test_get_raw_sql(self):
     sql = IntFields.all().sql()
     self.assertRegex(sql, r"^SELECT.+FROM.+")