1
1
import pytest
2
2
from fastui import components
3
3
from fastui .components import display
4
- from pydantic import BaseModel , Field
4
+ from pydantic import BaseModel , Field , computed_field
5
5
6
6
7
7
class User (BaseModel ):
8
8
id : int
9
9
name : str = Field (title = 'Name' )
10
10
11
+ @computed_field (title = 'Representation' )
12
+ @property
13
+ def representation (self ) -> str :
14
+ return f'{ self .id } : { self .name } '
15
+
11
16
12
17
users = [User (id = 1 , name = 'john' ), User (id = 2 , name = 'jack' )]
13
18
@@ -17,21 +22,40 @@ def test_table_no_columns():
17
22
18
23
# insert_assert(table.model_dump(by_alias=True, exclude_none=True))
19
24
assert table .model_dump (by_alias = True , exclude_none = True ) == {
20
- 'data' : [{'id' : 1 , 'name' : 'john' }, {'id' : 2 , 'name' : 'jack' }],
21
- 'columns' : [{'field' : 'id' }, {'field' : 'name' , 'title' : 'Name' }],
25
+ 'data' : [
26
+ {'id' : 1 , 'name' : 'john' , 'representation' : '1: john' },
27
+ {'id' : 2 , 'name' : 'jack' , 'representation' : '2: jack' },
28
+ ],
29
+ 'columns' : [
30
+ {'field' : 'id' },
31
+ {'field' : 'name' , 'title' : 'Name' },
32
+ {'field' : 'representation' , 'title' : 'Representation' },
33
+ ],
22
34
'type' : 'Table' ,
23
35
}
24
36
25
37
26
38
def test_table_columns ():
27
39
table = components .Table (
28
- data = users , columns = [display .DisplayLookup (field = 'id' , title = 'ID' ), display .DisplayLookup (field = 'name' )]
40
+ data = users ,
41
+ columns = [
42
+ display .DisplayLookup (field = 'id' , title = 'ID' ),
43
+ display .DisplayLookup (field = 'name' ),
44
+ display .DisplayLookup (field = 'representation' ),
45
+ ],
29
46
)
30
47
31
48
# insert_assert(table.model_dump(by_alias=True, exclude_none=True))
32
49
assert table .model_dump (by_alias = True , exclude_none = True ) == {
33
- 'data' : [{'id' : 1 , 'name' : 'john' }, {'id' : 2 , 'name' : 'jack' }],
34
- 'columns' : [{'title' : 'ID' , 'field' : 'id' }, {'field' : 'name' , 'title' : 'Name' }],
50
+ 'data' : [
51
+ {'id' : 1 , 'name' : 'john' , 'representation' : '1: john' },
52
+ {'id' : 2 , 'name' : 'jack' , 'representation' : '2: jack' },
53
+ ],
54
+ 'columns' : [
55
+ {'title' : 'ID' , 'field' : 'id' },
56
+ {'title' : 'Name' , 'field' : 'name' },
57
+ {'title' : 'Representation' , 'field' : 'representation' },
58
+ ],
35
59
'type' : 'Table' ,
36
60
}
37
61
@@ -47,7 +71,11 @@ def test_table_empty_data_model():
47
71
# insert_assert(table.model_dump(by_alias=True, exclude_none=True))
48
72
assert table .model_dump (by_alias = True , exclude_none = True ) == {
49
73
'data' : [],
50
- 'columns' : [{'field' : 'id' }, {'title' : 'Name' , 'field' : 'name' }],
74
+ 'columns' : [
75
+ {'field' : 'id' },
76
+ {'title' : 'Name' , 'field' : 'name' },
77
+ {'title' : 'Representation' , 'field' : 'representation' },
78
+ ],
51
79
'type' : 'Table' ,
52
80
}
53
81
@@ -57,7 +85,7 @@ def test_display_no_fields():
57
85
58
86
# insert_assert(d.model_dump(by_alias=True, exclude_none=True))
59
87
assert d .model_dump (by_alias = True , exclude_none = True ) == {
60
- 'data' : {'id' : 1 , 'name' : 'john' },
88
+ 'data' : {'id' : 1 , 'name' : 'john' , 'representation' : '1: john' },
61
89
'fields' : [{'field' : 'id' }, {'title' : 'Name' , 'field' : 'name' }],
62
90
'type' : 'Details' ,
63
91
}
@@ -70,7 +98,7 @@ def test_display_fields():
70
98
71
99
# insert_assert(d.model_dump(by_alias=True, exclude_none=True))
72
100
assert d .model_dump (by_alias = True , exclude_none = True ) == {
73
- 'data' : {'id' : 1 , 'name' : 'john' },
101
+ 'data' : {'id' : 1 , 'name' : 'john' , 'representation' : '1: john' },
74
102
'fields' : [{'title' : 'ID' , 'field' : 'id' }, {'title' : 'Name' , 'field' : 'name' }],
75
103
'type' : 'Details' ,
76
104
}
0 commit comments