Skip to content

Commit 0535cb9

Browse files
committed
test: Setting columnar and use_numpy for given query
1 parent b9494c3 commit 0535cb9

File tree

1 file changed

+132
-0
lines changed

1 file changed

+132
-0
lines changed

tests/backends/tests.py

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""Tests related to django.db.backends that haven't been organized."""
22
import datetime
3+
import importlib
34
import threading
45
import unittest
56
import warnings
@@ -560,6 +561,137 @@ def test_timezone_none_use_tz_false(self):
560561
connection.init_connection_state()
561562

562563

564+
def check_numpy():
565+
"""Check if numpy is installed."""
566+
spec = importlib.util.find_spec("numpy")
567+
return spec is not None
568+
569+
570+
class ColumnarTestCase(TransactionTestCase):
571+
available_apps = ["backends"]
572+
databases = {"default", "s2r1"}
573+
574+
def test_columnar_query(self):
575+
sql = """
576+
SELECT number, number*2, number*3, number*4, number*5
577+
FROM system.numbers
578+
LIMIT 10
579+
"""
580+
with connections["s2r1"].cursor() as cursorWrapper:
581+
with cursorWrapper.cursor.set_query_args(columnar=True) as cursor:
582+
cursor.execute(sql)
583+
self.assertEqual(
584+
cursor.fetchall(),
585+
[
586+
(0, 1, 2, 3, 4, 5, 6, 7, 8, 9),
587+
(0, 2, 4, 6, 8, 10, 12, 14, 16, 18),
588+
(0, 3, 6, 9, 12, 15, 18, 21, 24, 27),
589+
(0, 4, 8, 12, 16, 20, 24, 28, 32, 36),
590+
(0, 5, 10, 15, 20, 25, 30, 35, 40, 45),
591+
],
592+
)
593+
594+
cursor.execute(sql)
595+
self.assertEqual(
596+
cursor.fetchmany(2),
597+
[
598+
(0, 1, 2, 3, 4, 5, 6, 7, 8, 9),
599+
(0, 2, 4, 6, 8, 10, 12, 14, 16, 18),
600+
],
601+
)
602+
603+
actual_results = [
604+
r
605+
for results in iter(lambda: cursor.fetchmany(2), [])
606+
for r in results
607+
]
608+
self.assertEqual(
609+
actual_results,
610+
[
611+
(0, 3, 6, 9, 12, 15, 18, 21, 24, 27),
612+
(0, 4, 8, 12, 16, 20, 24, 28, 32, 36),
613+
(0, 5, 10, 15, 20, 25, 30, 35, 40, 45),
614+
],
615+
)
616+
617+
cursor.execute(sql)
618+
self.assertEqual(
619+
cursor.fetchone(),
620+
(0, 1, 2, 3, 4, 5, 6, 7, 8, 9),
621+
)
622+
623+
@unittest.skipUnless(check_numpy(), "numpy is not installed")
624+
def test_use_numpy_query(self):
625+
sql = """
626+
SELECT toDateTime32('2022-01-01 01:00:05', 'UTC'), number, number*2.5
627+
FROM system.numbers
628+
LIMIT 3
629+
"""
630+
import numpy as np
631+
632+
with connections["s2r1"].cursor() as cursorWrapper:
633+
with cursorWrapper.cursor.set_query_args(
634+
columnar=True, use_numpy=True
635+
) as cursor:
636+
cursor.execute(sql)
637+
np.testing.assert_equal(
638+
cursor.fetchall(),
639+
[
640+
np.array(
641+
[
642+
np.datetime64("2022-01-01T01:00:05"),
643+
np.datetime64("2022-01-01T01:00:05"),
644+
np.datetime64("2022-01-01T01:00:05"),
645+
],
646+
dtype="datetime64[s]",
647+
),
648+
np.array([0, 1, 2], dtype=np.uint64),
649+
np.array([0, 2.5, 5.0], dtype=np.float64),
650+
],
651+
)
652+
653+
cursor.execute(sql)
654+
np.testing.assert_equal(
655+
cursor.fetchmany(2),
656+
[
657+
np.array(
658+
[
659+
np.datetime64("2022-01-01T01:00:05"),
660+
np.datetime64("2022-01-01T01:00:05"),
661+
np.datetime64("2022-01-01T01:00:05"),
662+
],
663+
dtype="datetime64[s]",
664+
),
665+
np.array([0, 1, 2], dtype=np.uint64),
666+
],
667+
)
668+
669+
actual_results = [
670+
r
671+
for results in iter(lambda: cursor.fetchmany(2), [])
672+
for r in results
673+
]
674+
np.testing.assert_equal(
675+
actual_results,
676+
[
677+
np.array([0, 2.5, 5], dtype=np.float64),
678+
],
679+
)
680+
681+
cursor.execute(sql)
682+
np.testing.assert_equal(
683+
cursor.fetchone(),
684+
np.array(
685+
[
686+
np.datetime64("2022-01-01T01:00:05"),
687+
np.datetime64("2022-01-01T01:00:05"),
688+
np.datetime64("2022-01-01T01:00:05"),
689+
],
690+
dtype="datetime64[s]",
691+
),
692+
)
693+
694+
563695
# These tests aren't conditional because it would require differentiating
564696
# between MySQL+InnoDB and MySQL+MYISAM (something we currently can't do).
565697
class FkConstraintsTests(TransactionTestCase):

0 commit comments

Comments
 (0)