|
1 | 1 | """Tests related to django.db.backends that haven't been organized."""
|
2 | 2 | import datetime
|
| 3 | +import importlib |
3 | 4 | import threading
|
4 | 5 | import unittest
|
5 | 6 | import warnings
|
@@ -560,6 +561,137 @@ def test_timezone_none_use_tz_false(self):
|
560 | 561 | connection.init_connection_state()
|
561 | 562 |
|
562 | 563 |
|
| 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 | + |
563 | 695 | # These tests aren't conditional because it would require differentiating
|
564 | 696 | # between MySQL+InnoDB and MySQL+MYISAM (something we currently can't do).
|
565 | 697 | class FkConstraintsTests(TransactionTestCase):
|
|
0 commit comments