|
| 1 | +""" |
| 2 | +Regression tests for issues 386, 449, 484, and 558 — all related to processing complex aggregations and projections. |
| 3 | +""" |
| 4 | + |
| 5 | +import pytest |
| 6 | +import datajoint as dj |
| 7 | +from . import PREFIX |
| 8 | +import uuid |
| 9 | +from .schema_uuid import Topic, Item, top_level_namespace_id |
| 10 | +from .schema_aggr_regress import R, Q, S, A, B, X, LOCALS_AGGR_REGRESS |
| 11 | + |
| 12 | + |
| 13 | +@pytest.fixture(scope="function") |
| 14 | +def schema_aggr_reg(connection_test): |
| 15 | + context = LOCALS_AGGR_REGRESS |
| 16 | + schema = dj.Schema( |
| 17 | + PREFIX + "_aggr_regress", |
| 18 | + context=context, |
| 19 | + connection=connection_test, |
| 20 | + ) |
| 21 | + schema(R) |
| 22 | + schema(Q) |
| 23 | + schema(S) |
| 24 | + yield schema |
| 25 | + schema.drop() |
| 26 | + |
| 27 | + |
| 28 | +@pytest.fixture(scope="function") |
| 29 | +def schema_aggr_reg_with_abx(connection_test): |
| 30 | + context = LOCALS_AGGR_REGRESS |
| 31 | + schema = dj.Schema( |
| 32 | + PREFIX + "_aggr_regress_with_abx", |
| 33 | + context=context, |
| 34 | + connection=connection_test, |
| 35 | + ) |
| 36 | + schema(R) |
| 37 | + schema(Q) |
| 38 | + schema(S) |
| 39 | + schema(A) |
| 40 | + schema(B) |
| 41 | + schema(X) |
| 42 | + yield schema |
| 43 | + schema.drop() |
| 44 | + |
| 45 | + |
| 46 | +def test_issue386(schema_aggr_reg): |
| 47 | + """ |
| 48 | + --------------- ISSUE 386 ------------------- |
| 49 | + Issue 386 resulted from the loss of aggregated attributes when the aggregation was used as the restrictor |
| 50 | + Q & (R.aggr(S, n='count(*)') & 'n=2') |
| 51 | + Error: Unknown column 'n' in HAVING |
| 52 | + """ |
| 53 | + result = R.aggr(S, n="count(*)") & "n=10" |
| 54 | + result = Q & result |
| 55 | + result.fetch() |
| 56 | + |
| 57 | + |
| 58 | +def test_issue449(schema_aggr_reg): |
| 59 | + """ |
| 60 | + ---------------- ISSUE 449 ------------------ |
| 61 | + Issue 449 arises from incorrect group by attributes after joining with a dj.U() |
| 62 | + """ |
| 63 | + result = dj.U("n") * R.aggr(S, n="max(s)") |
| 64 | + result.fetch() |
| 65 | + |
| 66 | + |
| 67 | +def test_issue484(schema_aggr_reg): |
| 68 | + """ |
| 69 | + ---------------- ISSUE 484 ----------------- |
| 70 | + Issue 484 |
| 71 | + """ |
| 72 | + q = dj.U().aggr(S, n="max(s)") |
| 73 | + n = q.fetch("n") |
| 74 | + n = q.fetch1("n") |
| 75 | + q = dj.U().aggr(S, n="avg(s)") |
| 76 | + result = dj.U().aggr(q, m="max(n)") |
| 77 | + result.fetch() |
| 78 | + |
| 79 | + |
| 80 | +def test_union_join(schema_aggr_reg_with_abx): |
| 81 | + """ |
| 82 | + This test fails if it runs after TestIssue558. |
| 83 | +
|
| 84 | + https://github.com/datajoint/datajoint-python/issues/930 |
| 85 | + """ |
| 86 | + A.insert(zip([100, 200, 300, 400, 500, 600])) |
| 87 | + B.insert([(100, 11), (200, 22), (300, 33), (400, 44)]) |
| 88 | + q1 = B & "id < 300" |
| 89 | + q2 = B & "id > 300" |
| 90 | + |
| 91 | + expected_data = [ |
| 92 | + {"id": 0, "id2": 5}, |
| 93 | + {"id": 1, "id2": 6}, |
| 94 | + {"id": 2, "id2": 7}, |
| 95 | + {"id": 3, "id2": 8}, |
| 96 | + {"id": 4, "id2": 9}, |
| 97 | + {"id": 100, "id2": 11}, |
| 98 | + {"id": 200, "id2": 22}, |
| 99 | + {"id": 400, "id2": 44}, |
| 100 | + ] |
| 101 | + |
| 102 | + assert ((q1 + q2) * A).fetch(as_dict=True) == expected_data |
| 103 | + |
| 104 | + |
| 105 | +class TestIssue558: |
| 106 | + """ |
| 107 | + --------------- ISSUE 558 ------------------ |
| 108 | + Issue 558 resulted from the fact that DataJoint saves subqueries and often combines a restriction followed |
| 109 | + by a projection into a single SELECT statement, which in several unusual cases produces unexpected results. |
| 110 | + """ |
| 111 | + |
| 112 | + def test_issue558_part1(self, schema_aggr_reg_with_abx): |
| 113 | + q = (A - B).proj(id2="3") |
| 114 | + assert len(A - B) == len(q) |
| 115 | + |
| 116 | + def test_issue558_part2(self, schema_aggr_reg_with_abx): |
| 117 | + d = dict(id=3, id2=5) |
| 118 | + assert len(X & d) == len((X & d).proj(id2="3")) |
| 119 | + |
| 120 | + |
| 121 | +def test_left_join_len(schema_uuid): |
| 122 | + Topic().add("jeff") |
| 123 | + Item.populate() |
| 124 | + Topic().add("jeff2") |
| 125 | + Topic().add("jeff3") |
| 126 | + q = Topic.join( |
| 127 | + Item - dict(topic_id=uuid.uuid5(top_level_namespace_id, "jeff")), left=True |
| 128 | + ) |
| 129 | + qf = q.fetch() |
| 130 | + assert len(q) == len(qf) |
0 commit comments