|
| 1 | +""" |
| 2 | +A column copied by a foreign key keeps the parent's core type. |
| 3 | +
|
| 4 | +Core types (``int32``, ``int16``, ``uuid``, ...) are recorded in the column comment as |
| 5 | +``:type:``. A foreign key copies the parent's primary-key columns into the child; the copy |
| 6 | +must carry the same marker, or the child's heading reports the native SQL type and every |
| 7 | +table derived from it (a jobs table, a further child) inherits the loss. |
| 8 | +""" |
| 9 | + |
| 10 | +import logging |
| 11 | + |
| 12 | +import pytest |
| 13 | + |
| 14 | +import datajoint as dj |
| 15 | + |
| 16 | + |
| 17 | +class _Records(logging.Handler): |
| 18 | + def __init__(self): |
| 19 | + super().__init__(level=logging.WARNING) |
| 20 | + self.messages = [] |
| 21 | + |
| 22 | + def emit(self, record): |
| 23 | + self.messages.append(record.getMessage()) |
| 24 | + |
| 25 | + |
| 26 | +@pytest.mark.backend_agnostic |
| 27 | +def test_fk_columns_keep_core_types(connection_by_backend, backend, prefix): |
| 28 | + schema = dj.Schema(f"{prefix}_fk_core_types_{backend}", connection=connection_by_backend) |
| 29 | + try: |
| 30 | + |
| 31 | + @schema |
| 32 | + class Subject(dj.Manual): |
| 33 | + definition = """ |
| 34 | + subject_id : int32 |
| 35 | + """ |
| 36 | + |
| 37 | + @schema |
| 38 | + class Session(dj.Manual): |
| 39 | + definition = """ |
| 40 | + -> Subject |
| 41 | + session_idx : int16 |
| 42 | + """ |
| 43 | + |
| 44 | + @schema |
| 45 | + class Summary(dj.Manual): |
| 46 | + definition = """ |
| 47 | + -> Session |
| 48 | + --- |
| 49 | + n : int32 |
| 50 | + """ |
| 51 | + |
| 52 | + assert Subject.heading["subject_id"].original_type == "int32" |
| 53 | + # inherited one level down |
| 54 | + assert Session.heading["subject_id"].original_type == "int32" |
| 55 | + assert Session.heading["session_idx"].original_type == "int16" |
| 56 | + # inherited two levels down |
| 57 | + assert Summary.heading["subject_id"].original_type == "int32" |
| 58 | + assert Summary.heading["session_idx"].original_type == "int16" |
| 59 | + # the comment text itself is unchanged |
| 60 | + assert Summary.heading["subject_id"].comment == Subject.heading["subject_id"].comment |
| 61 | + finally: |
| 62 | + schema.drop(prompt=False) |
| 63 | + |
| 64 | + |
| 65 | +@pytest.mark.backend_agnostic |
| 66 | +def test_fk_columns_keep_uuid(connection_by_backend, backend, prefix): |
| 67 | + schema = dj.Schema(f"{prefix}_fk_core_types_uuid_{backend}", connection=connection_by_backend) |
| 68 | + try: |
| 69 | + |
| 70 | + @schema |
| 71 | + class Item(dj.Manual): |
| 72 | + definition = """ |
| 73 | + item_id : uuid # identifier of the item |
| 74 | + """ |
| 75 | + |
| 76 | + @schema |
| 77 | + class Tag(dj.Manual): |
| 78 | + definition = """ |
| 79 | + -> Item |
| 80 | + tag : varchar(16) |
| 81 | + """ |
| 82 | + |
| 83 | + assert Tag.heading["item_id"].uuid |
| 84 | + assert Tag.heading["item_id"].original_type == "uuid" |
| 85 | + assert Tag.heading["item_id"].comment == "identifier of the item" |
| 86 | + finally: |
| 87 | + schema.drop(prompt=False) |
| 88 | + |
| 89 | + |
| 90 | +@pytest.mark.backend_agnostic |
| 91 | +def test_jobs_table_keeps_inherited_core_types(connection_by_backend, backend, prefix): |
| 92 | + """A jobs table built from inherited keys declares core types and warns about nothing.""" |
| 93 | + schema = dj.Schema(f"{prefix}_fk_core_types_jobs_{backend}", connection=connection_by_backend) |
| 94 | + records = _Records() |
| 95 | + logger = logging.getLogger("datajoint") |
| 96 | + try: |
| 97 | + |
| 98 | + @schema |
| 99 | + class Subject(dj.Manual): |
| 100 | + definition = """ |
| 101 | + subject_id : int32 |
| 102 | + """ |
| 103 | + |
| 104 | + @schema |
| 105 | + class Session(dj.Manual): |
| 106 | + definition = """ |
| 107 | + -> Subject |
| 108 | + session_idx : int16 |
| 109 | + """ |
| 110 | + |
| 111 | + @schema |
| 112 | + class Summary(dj.Computed): |
| 113 | + definition = """ |
| 114 | + -> Session |
| 115 | + --- |
| 116 | + n : int32 |
| 117 | + """ |
| 118 | + |
| 119 | + def make(self, key): |
| 120 | + self.insert1(dict(key, n=1)) |
| 121 | + |
| 122 | + logger.addHandler(records) |
| 123 | + jobs = Summary.jobs |
| 124 | + assert jobs.heading["subject_id"].original_type == "int32" |
| 125 | + assert jobs.heading["session_idx"].original_type == "int16" |
| 126 | + native = [m for m in records.messages if m.startswith("Native type")] |
| 127 | + assert native == [], native |
| 128 | + finally: |
| 129 | + logger.removeHandler(records) |
| 130 | + schema.drop(prompt=False) |
0 commit comments