|
| 1 | +"""Tests for ``OrmarConfig.comment`` forwarding to ``sqlalchemy.Table``.""" |
| 2 | + |
| 3 | +import ormar |
| 4 | +from tests.lifespan import init_tests |
| 5 | +from tests.settings import create_config |
| 6 | + |
| 7 | +base_ormar_config = create_config() |
| 8 | + |
| 9 | + |
| 10 | +class CommentedModel(ormar.Model): |
| 11 | + ormar_config = base_ormar_config.copy( |
| 12 | + tablename="commented_models", |
| 13 | + comment="Stores commented things.", |
| 14 | + ) |
| 15 | + |
| 16 | + id: int = ormar.Integer(primary_key=True) |
| 17 | + name: str = ormar.String(max_length=100) |
| 18 | + |
| 19 | + |
| 20 | +class UncommentedModel(ormar.Model): |
| 21 | + ormar_config = base_ormar_config.copy(tablename="uncommented_models") |
| 22 | + |
| 23 | + id: int = ormar.Integer(primary_key=True) |
| 24 | + |
| 25 | + |
| 26 | +create_test_database = init_tests(base_ormar_config) |
| 27 | + |
| 28 | + |
| 29 | +def test_comment_forwarded_to_sqlalchemy_table(): |
| 30 | + assert CommentedModel.ormar_config.table.comment == "Stores commented things." |
| 31 | + |
| 32 | + |
| 33 | +def test_comment_defaults_to_none(): |
| 34 | + assert UncommentedModel.ormar_config.comment is None |
| 35 | + assert UncommentedModel.ormar_config.table.comment is None |
| 36 | + |
| 37 | + |
| 38 | +def test_copy_inherits_parent_comment_when_omitted(): |
| 39 | + parent = ormar.OrmarConfig( |
| 40 | + metadata=base_ormar_config.metadata, |
| 41 | + database=base_ormar_config.database, |
| 42 | + engine=base_ormar_config.engine, |
| 43 | + comment="parent comment", |
| 44 | + ) |
| 45 | + child = parent.copy(tablename="child") |
| 46 | + assert child.comment == "parent comment" |
| 47 | + |
| 48 | + |
| 49 | +def test_copy_can_explicitly_clear_parent_comment(): |
| 50 | + parent = ormar.OrmarConfig( |
| 51 | + metadata=base_ormar_config.metadata, |
| 52 | + database=base_ormar_config.database, |
| 53 | + engine=base_ormar_config.engine, |
| 54 | + comment="parent comment", |
| 55 | + ) |
| 56 | + child = parent.copy(tablename="child", comment=None) |
| 57 | + assert child.comment is None |
| 58 | + |
| 59 | + |
| 60 | +def test_copy_can_override_parent_comment(): |
| 61 | + parent = ormar.OrmarConfig( |
| 62 | + metadata=base_ormar_config.metadata, |
| 63 | + database=base_ormar_config.database, |
| 64 | + engine=base_ormar_config.engine, |
| 65 | + comment="parent comment", |
| 66 | + ) |
| 67 | + child = parent.copy(tablename="child", comment="child comment") |
| 68 | + assert child.comment == "child comment" |
0 commit comments