Skip to content

Commit 85a6add

Browse files
committed
Feature: copy and override metadata.Fields
1 parent d544ad4 commit 85a6add

File tree

3 files changed

+36
-4
lines changed

3 files changed

+36
-4
lines changed

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919

2020
dependencies = [
21-
'yhttp >= 5.2, < 6',
21+
'yhttp >= 5.3, < 6',
2222
'yhttp-dbmanager >= 4, < 5',
2323
'sqlalchemy >= 2.0.32',
2424
]

tests/test_metadata.py

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,13 @@ def post(req):
5050
assert response.json == dict(id=2, title='foo', alias='OOF')
5151

5252

53-
def test_metadata_args():
54-
col = m.String('foo', length=(0, 20)).column()
53+
def test_metadata_example():
54+
field = m.String('foo', length=(0, 20), example='123')
55+
assert field.example == '123'
56+
57+
58+
def test_metadata_column():
59+
col = m.String('foo', length=(0, 20), example='123').column()
5560
assert isinstance(col, MappedColumn)
5661
assert isinstance(col.column.type, sa.String)
5762
assert col.column.type.length == 20
@@ -64,3 +69,21 @@ def test_metadata_args():
6469
col = m.Integer('foo').column()
6570
assert isinstance(col, MappedColumn)
6671
assert isinstance(col.column.type, sa.Integer)
72+
73+
74+
def test_metadata_override():
75+
foo = m.String('foo', length=(0, 20), example='123')
76+
bar = foo(name='bar')
77+
assert bar.name == 'bar'
78+
assert bar.length == (0, 20)
79+
assert bar.example == '123'
80+
81+
bar = foo(name='bar', length=(1, 10))
82+
assert bar.name == 'bar'
83+
assert bar.length == (1, 10)
84+
assert bar.example == '123'
85+
86+
bar = bar(example='321')
87+
assert bar.name == 'bar'
88+
assert bar.length == (1, 10)
89+
assert bar.example == '321'

yhttp/ext/sqlalchemy/metadata.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,18 @@
77

88

99
class FieldMixin(metaclass=abc.ABCMeta):
10-
def __init__(self, *args, **kwargs):
10+
def __init__(self, *args, example=None, **kwargs):
11+
self.example = example
1112
super().__init__(*args, **kwargs)
1213

14+
def __call__(self, *, example=None, **kwargs):
15+
kwargs['example'] = self.example if example is None else example
16+
17+
if 'name' not in kwargs:
18+
kwargs['name'] = self.name
19+
20+
return super(FieldMixin, self).__call__(**kwargs)
21+
1322
@property
1423
@abc.abstractmethod
1524
def _satype(self):

0 commit comments

Comments
 (0)