|
5 | 5 | import datetime |
6 | 6 | import numpy as np |
7 | 7 | import datajoint as dj |
| 8 | +from datajoint.errors import DataJointError |
8 | 9 | from .schema_simple import * |
9 | 10 | from .schema import * |
10 | 11 |
|
@@ -570,3 +571,94 @@ def test_union_multiple(schema_simp_pop): |
570 | 571 | y = set(zip(*q2.fetch("i", "j"))) |
571 | 572 | assert x == y |
572 | 573 | assert q1.fetch(as_dict=True) == q2.fetch(as_dict=True) |
| 574 | + |
| 575 | + |
| 576 | +class TestDjTop: |
| 577 | + """TODO: migrate""" |
| 578 | + |
| 579 | + def test_restrictions_by_top(self): |
| 580 | + a = L() & dj.Top() |
| 581 | + b = L() & dj.Top(order_by=["cond_in_l", "KEY"]) |
| 582 | + x = L() & dj.Top(5, "id_l desc", 4) & "cond_in_l=1" |
| 583 | + y = L() & "cond_in_l=1" & dj.Top(5, "id_l desc", 4) |
| 584 | + z = ( |
| 585 | + L() |
| 586 | + & dj.Top(None, order_by="id_l desc") |
| 587 | + & "cond_in_l=1" |
| 588 | + & dj.Top(5, "id_l desc") |
| 589 | + & ("id_l=20", "id_l=16", "id_l=17") |
| 590 | + & dj.Top(2, "id_l asc", 1) |
| 591 | + ) |
| 592 | + assert len(a) == 1 |
| 593 | + assert len(b) == 1 |
| 594 | + assert len(x) == 1 |
| 595 | + assert len(y) == 5 |
| 596 | + assert len(z) == 2 |
| 597 | + assert a.fetch(as_dict=True) == [ |
| 598 | + {"id_l": 0, "cond_in_l": 1}, |
| 599 | + ] |
| 600 | + assert b.fetch(as_dict=True) == [ |
| 601 | + {"id_l": 3, "cond_in_l": 0}, |
| 602 | + ] |
| 603 | + assert x.fetch(as_dict=True) == [{"id_l": 25, "cond_in_l": 1}] |
| 604 | + assert y.fetch(as_dict=True) == [ |
| 605 | + {"id_l": 16, "cond_in_l": 1}, |
| 606 | + {"id_l": 15, "cond_in_l": 1}, |
| 607 | + {"id_l": 11, "cond_in_l": 1}, |
| 608 | + {"id_l": 10, "cond_in_l": 1}, |
| 609 | + {"id_l": 5, "cond_in_l": 1}, |
| 610 | + ] |
| 611 | + assert z.fetch(as_dict=True) == [ |
| 612 | + {"id_l": 17, "cond_in_l": 1}, |
| 613 | + {"id_l": 20, "cond_in_l": 1}, |
| 614 | + ] |
| 615 | + |
| 616 | + def test_top_restriction_with_keywords(self): |
| 617 | + select = SelectPK() & dj.Top(limit=9, order_by=["select desc"]) |
| 618 | + key = KeyPK() & dj.Top(limit=9, order_by="key desc") |
| 619 | + assert select.fetch(as_dict=True) == [ |
| 620 | + {"id": 2, "select": 8}, |
| 621 | + {"id": 2, "select": 6}, |
| 622 | + {"id": 1, "select": 4}, |
| 623 | + {"id": 2, "select": 4}, |
| 624 | + {"id": 1, "select": 3}, |
| 625 | + {"id": 1, "select": 2}, |
| 626 | + {"id": 2, "select": 2}, |
| 627 | + {"id": 1, "select": 1}, |
| 628 | + {"id": 0, "select": 0}, |
| 629 | + ] |
| 630 | + assert key.fetch(as_dict=True) == [ |
| 631 | + {"id": 2, "key": 6}, |
| 632 | + {"id": 2, "key": 5}, |
| 633 | + {"id": 1, "key": 5}, |
| 634 | + {"id": 0, "key": 4}, |
| 635 | + {"id": 1, "key": 4}, |
| 636 | + {"id": 2, "key": 4}, |
| 637 | + {"id": 0, "key": 3}, |
| 638 | + {"id": 1, "key": 3}, |
| 639 | + {"id": 2, "key": 3}, |
| 640 | + ] |
| 641 | + |
| 642 | + def test_top_errors(self): |
| 643 | + with assert_raises(DataJointError) as err1: |
| 644 | + L() & ("cond_in_l=1", dj.Top()) |
| 645 | + with assert_raises(DataJointError) as err2: |
| 646 | + L() & dj.AndList(["cond_in_l=1", dj.Top()]) |
| 647 | + with assert_raises(TypeError) as err3: |
| 648 | + L() & dj.Top(limit="1") |
| 649 | + with assert_raises(TypeError) as err4: |
| 650 | + L() & dj.Top(order_by=1) |
| 651 | + with assert_raises(TypeError) as err5: |
| 652 | + L() & dj.Top(offset="1") |
| 653 | + assert ( |
| 654 | + "Invalid restriction type Top(limit=1, order_by=['KEY'], offset=0)" |
| 655 | + == str(err1.exception) |
| 656 | + ) |
| 657 | + assert ( |
| 658 | + "Invalid restriction type Top(limit=1, order_by=['KEY'], offset=0)" |
| 659 | + == str(err2.exception) |
| 660 | + ) |
| 661 | + assert "Top limit must be an integer" == str(err3.exception) |
| 662 | + assert "Top order_by attributes must all be strings" == str( |
| 663 | + err4.exception) |
| 664 | + assert "The offset argument must be an integer" == str(err5.exception) |
0 commit comments