|
| 1 | +import importlib |
1 | 2 | import re |
| 3 | +import sys |
| 4 | +import time |
2 | 5 |
|
3 | 6 | import pytest |
4 | 7 |
|
| 8 | +import datajoint as dj |
5 | 9 |
|
6 | 10 | from tests import schema as schema_any_module |
7 | 11 | from tests.schema_alter import LOCALS_ALTER, Experiment, Parent |
@@ -52,3 +56,100 @@ def test_alter_part(self, schema_alter): |
52 | 56 | table=Parent.Grandchild, |
53 | 57 | attribute_sql=r"`grandchild_id` .* DEFAULT NULL[^,]*", |
54 | 58 | ) |
| 59 | + |
| 60 | + |
| 61 | +# A pipeline module that declares its tables at import, like the one in #1602. |
| 62 | +# Its schema is activated by the test, on the test's connection. |
| 63 | +PIPELINE_MODULE = ''' |
| 64 | +import datajoint as dj |
| 65 | +
|
| 66 | +schema = dj.Schema() |
| 67 | +
|
| 68 | +
|
| 69 | +@schema |
| 70 | +class Item(dj.Manual): |
| 71 | + definition = """ |
| 72 | + item : int32 |
| 73 | + """ |
| 74 | +
|
| 75 | +
|
| 76 | +@schema |
| 77 | +class Measurement(dj.Manual): |
| 78 | + definition = """ |
| 79 | + -> Item |
| 80 | + --- |
| 81 | + value : float32 |
| 82 | + """ |
| 83 | +
|
| 84 | + class Note(dj.Part): |
| 85 | + definition = """ |
| 86 | + -> master |
| 87 | + --- |
| 88 | + note : varchar(64) |
| 89 | + """ |
| 90 | +''' |
| 91 | + |
| 92 | + |
| 93 | +@pytest.fixture |
| 94 | +def schema_by_backend(connection_by_backend, db_creds_by_backend): |
| 95 | + """Create a fresh schema per test, parameterized across backends.""" |
| 96 | + backend = db_creds_by_backend["backend"] |
| 97 | + test_id = str(int(time.time() * 1000))[-8:] |
| 98 | + schema = dj.Schema(f"djtest_alter_{backend}_{test_id}"[:64], connection=connection_by_backend) |
| 99 | + yield schema |
| 100 | + schema.drop(prompt=False) |
| 101 | + |
| 102 | + |
| 103 | +@pytest.fixture |
| 104 | +def pipeline(schema_by_backend, tmp_path, monkeypatch): |
| 105 | + """Import the pipeline module from tmp_path, its tables declared in the test schema.""" |
| 106 | + (tmp_path / "alter_pipeline.py").write_text(PIPELINE_MODULE) |
| 107 | + monkeypatch.syspath_prepend(str(tmp_path)) |
| 108 | + try: |
| 109 | + module = importlib.import_module("alter_pipeline") |
| 110 | + module.schema.activate(schema_by_backend.database, connection=schema_by_backend.connection) |
| 111 | + yield module |
| 112 | + finally: |
| 113 | + sys.modules.pop("alter_pipeline", None) |
| 114 | + |
| 115 | + |
| 116 | +# Each alter adds an attribute: changing an attribute's type emits MODIFY, which |
| 117 | +# PostgreSQL does not accept. Foreign keys resolve before any SQL is generated. |
| 118 | + |
| 119 | + |
| 120 | +def test_alter_from_another_module(pipeline): |
| 121 | + """alter() resolves foreign keys in the declaration context, not in the caller's namespace (#1602).""" |
| 122 | + # This module has no `Item`, so resolving `-> Item` here would fail |
| 123 | + pipeline.Measurement.definition = """ |
| 124 | + -> Item |
| 125 | + --- |
| 126 | + value : float32 |
| 127 | + unit = "" : varchar(16) |
| 128 | + """ |
| 129 | + pipeline.Measurement.alter(prompt=False) |
| 130 | + assert pipeline.Measurement.heading.secondary_attributes == ["value", "unit"] |
| 131 | + |
| 132 | + |
| 133 | +def test_alter_part_from_another_module(pipeline): |
| 134 | + """A Part table still resolves `-> master` in its declaration context.""" |
| 135 | + pipeline.Measurement.Note.definition = """ |
| 136 | + -> master |
| 137 | + --- |
| 138 | + note : varchar(64) |
| 139 | + author = "" : varchar(32) |
| 140 | + """ |
| 141 | + pipeline.Measurement.Note.alter(prompt=False) |
| 142 | + assert pipeline.Measurement.Note.heading.secondary_attributes == ["note", "author"] |
| 143 | + |
| 144 | + |
| 145 | +def test_alter_explicit_context(pipeline): |
| 146 | + """An explicit context takes precedence over the declaration context.""" |
| 147 | + # `Parent` resolves only in the explicit context |
| 148 | + pipeline.Measurement.definition = """ |
| 149 | + -> Parent |
| 150 | + --- |
| 151 | + value : float32 |
| 152 | + unit = "" : varchar(16) |
| 153 | + """ |
| 154 | + pipeline.Measurement.alter(prompt=False, context={"Parent": pipeline.Item}) |
| 155 | + assert pipeline.Measurement.heading.secondary_attributes == ["value", "unit"] |
0 commit comments