1
+ import pytest
2
+ from pydantic import BaseModel
3
+ from fastcrud .paginated .schemas import create_list_response , create_paginated_response
4
+
5
+
6
+ class TestSchema (BaseModel ):
7
+ name : str
8
+ value : int
9
+
10
+
11
+ def test_create_list_response_default_key ():
12
+ ResponseModel = create_list_response (TestSchema )
13
+
14
+ # Test model creation
15
+ assert ResponseModel .__name__ == "DynamicListResponse"
16
+
17
+ # Test with valid data
18
+ data = [{"name" : "test" , "value" : 1 }, {"name" : "test2" , "value" : 2 }]
19
+ response = ResponseModel (data = data )
20
+ assert len (response .data ) == 2
21
+ assert response .data [0 ].name == "test"
22
+ assert response .data [1 ].value == 2
23
+
24
+ # Test with empty list
25
+ empty_response = ResponseModel (data = [])
26
+ assert len (empty_response .data ) == 0
27
+
28
+
29
+ def test_create_list_response_custom_key ():
30
+ ResponseModel = create_list_response (TestSchema , response_key = "items" )
31
+
32
+ # Test model creation
33
+ assert ResponseModel .__name__ == "DynamicListResponse"
34
+
35
+ # Test with valid data
36
+ data = [{"name" : "test" , "value" : 1 }]
37
+ response = ResponseModel (items = data )
38
+ assert len (response .items ) == 1
39
+ assert response .items [0 ].name == "test"
40
+ assert response .items [0 ].value == 1
41
+
42
+
43
+ def test_create_list_response_validation ():
44
+ ResponseModel = create_list_response (TestSchema )
45
+
46
+ # Test invalid data
47
+ with pytest .raises (ValueError ):
48
+ ResponseModel (data = [{"invalid_field" : "test" }])
49
+
50
+
51
+ def test_create_paginated_response_default_key ():
52
+ ResponseModel = create_paginated_response (TestSchema )
53
+
54
+ # Test model creation
55
+ assert ResponseModel .__name__ == "DynamicPaginatedResponse"
56
+
57
+ # Test with valid data
58
+ response = ResponseModel (
59
+ data = [{"name" : "test" , "value" : 1 }],
60
+ total_count = 1 ,
61
+ has_more = False ,
62
+ page = 1 ,
63
+ items_per_page = 10
64
+ )
65
+
66
+ assert len (response .data ) == 1
67
+ assert response .data [0 ].name == "test"
68
+ assert response .total_count == 1
69
+ assert response .has_more is False
70
+ assert response .page == 1
71
+ assert response .items_per_page == 10
72
+
73
+
74
+ def test_create_paginated_response_custom_key ():
75
+ ResponseModel = create_paginated_response (TestSchema , response_key = "items" )
76
+
77
+ # Test with valid data
78
+ response = ResponseModel (
79
+ items = [{"name" : "test" , "value" : 1 }],
80
+ total_count = 1 ,
81
+ has_more = False ,
82
+ page = 1 ,
83
+ items_per_page = 10
84
+ )
85
+
86
+ assert len (response .items ) == 1
87
+ assert response .items [0 ].name == "test"
88
+
89
+
90
+ def test_create_paginated_response_optional_fields ():
91
+ ResponseModel = create_paginated_response (TestSchema )
92
+
93
+ # Test with minimal required fields
94
+ response = ResponseModel (
95
+ data = [{"name" : "test" , "value" : 1 }],
96
+ total_count = 1 ,
97
+ has_more = False
98
+ )
99
+
100
+ assert response .page is None
101
+ assert response .items_per_page is None
102
+
103
+
104
+ def test_create_paginated_response_validation ():
105
+ ResponseModel = create_paginated_response (TestSchema )
106
+
107
+ # Test missing required fields
108
+ with pytest .raises (ValueError ):
109
+ ResponseModel (
110
+ data = [{"name" : "test" , "value" : 1 }],
111
+ has_more = False # missing total_count
112
+ )
113
+
114
+ # Test invalid data structure
115
+ with pytest .raises (ValueError ):
116
+ ResponseModel (
117
+ data = [{"invalid_field" : "test" }],
118
+ total_count = 1 ,
119
+ has_more = False
120
+ )
121
+
122
+
123
+ def test_create_paginated_response_empty_list ():
124
+ ResponseModel = create_paginated_response (TestSchema )
125
+
126
+ response = ResponseModel (
127
+ data = [],
128
+ total_count = 0 ,
129
+ has_more = False ,
130
+ page = 1 ,
131
+ items_per_page = 10
132
+ )
133
+
134
+ assert len (response .data ) == 0
135
+ assert response .total_count == 0
136
+ assert response .has_more is False
0 commit comments