forked from anvilco/python-anvil
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_models.py
More file actions
264 lines (209 loc) · 8.05 KB
/
test_models.py
File metadata and controls
264 lines (209 loc) · 8.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
import base64
import pytest
from pydantic import BaseModel
from typing import Any, List, Optional
from python_anvil.models import FileCompatibleBaseModel
def test_file_compat_base_model_handles_regular_data():
class TestModel(FileCompatibleBaseModel):
name: str
value: int
model = TestModel(name="test", value=42)
data = model.model_dump()
assert data == {"name": "test", "value": 42}
def test_file_compat_base_model_preserves_file_objects():
class FileModel(FileCompatibleBaseModel):
file: Any = None
# Create a test file object
with open(__file__, 'rb') as test_file:
model = FileModel(file=test_file)
data = model.model_dump()
# Verify we got a dictionary with the expected structure
assert isinstance(data['file'], dict)
assert 'data' in data['file']
assert 'mimetype' in data['file']
assert 'filename' in data['file']
# Verify the content matches
with open(__file__, 'rb') as original_file:
original_content = original_file.read()
decoded_content = base64.b64decode(data['file']['data'].encode('utf-8'))
assert (
decoded_content == original_content
), "File content should match original"
def test_file_compat_base_model_validates_types():
class TestModel(FileCompatibleBaseModel):
name: str
age: int
# Should work with valid types
model = TestModel(name="Alice", age=30)
assert model.name == "Alice"
assert model.age == 30
# Should raise validation error for wrong types
with pytest.raises(ValueError):
TestModel(name="Alice", age="thirty")
def test_file_compat_base_model_handles_optional_fields():
class TestModel(FileCompatibleBaseModel):
required: str
optional: Optional[str] = None
# Should work with just required field
model = TestModel(required="test")
assert model.required == "test"
assert model.optional is None
# Should work with both fields
model = TestModel(required="test", optional="present")
assert model.optional == "present"
def test_file_compat_base_model_handles_nested_models():
class NestedModel(BaseModel):
value: str
class ParentModel(FileCompatibleBaseModel):
nested: NestedModel
nested = NestedModel(value="test")
model = ParentModel(nested=nested)
data = model.model_dump()
assert data == {"nested": {"value": "test"}}
def test_file_compat_base_model_handles_lists():
class TestModel(FileCompatibleBaseModel):
items: List[str]
model = TestModel(items=["a", "b", "c"])
data = model.model_dump()
assert data == {"items": ["a", "b", "c"]}
def test_document_upload_handles_file_objects():
# pylint: disable-next=import-outside-toplevel
from python_anvil.api_resources.payload import DocumentUpload, SignatureField
# Create a sample signature field
field = SignatureField(
id="sig1",
type="signature",
page_num=1,
rect={"x": 100.0, "y": 100.0, "width": 100.0},
)
# Test with a file object
with open(__file__, 'rb') as test_file:
doc = DocumentUpload(
id="doc1", title="Test Document", file=test_file, fields=[field]
)
data = doc.model_dump()
# Verify file is converted to expected dictionary format
assert isinstance(data['file'], dict)
assert 'data' in data['file']
assert 'mimetype' in data['file']
assert 'filename' in data['file']
# Verify content matches
with open(__file__, 'rb') as original_file:
original_content = original_file.read()
decoded_content = base64.b64decode(data['file']['data'].encode('utf-8'))
assert decoded_content == original_content
# Verify other fields are correct
assert data['id'] == "doc1"
assert data['title'] == "Test Document"
assert len(data['fields']) == 1
assert data['fields'][0]['id'] == "sig1"
def test_create_etch_packet_payload_handles_nested_file_objects():
# pylint: disable-next=import-outside-toplevel
from python_anvil.api_resources.payload import (
CreateEtchPacketPayload,
DocumentUpload,
EtchSigner,
SignatureField,
)
# Create a sample signature field
field = SignatureField(
id="sig1",
type="signature",
page_num=1,
rect={"x": 100.0, "y": 100.0, "width": 100.0},
)
# Create a signer
signer = EtchSigner(
name="Test Signer",
email="[email protected]",
fields=[{"file_id": "doc1", "field_id": "sig1"}],
)
# Test with a file object
with open(__file__, 'rb') as test_file:
# Create a DocumentUpload instance
doc = DocumentUpload(
id="doc1", title="Test Document", file=test_file, fields=[field]
)
# Create the packet payload
packet = CreateEtchPacketPayload(
name="Test Packet", signers=[signer], files=[doc], is_test=True
)
# Dump the model
data = packet.model_dump()
# Verify the structure
assert data['name'] == "Test Packet"
assert len(data['files']) == 1
assert len(data['signers']) == 1
# Verify file handling in the nested DocumentUpload
file_data = data['files'][0]
assert file_data['id'] == "doc1"
assert file_data['title'] == "Test Document"
assert isinstance(file_data['file'], dict)
assert 'data' in file_data['file']
assert 'mimetype' in file_data['file']
assert 'filename' in file_data['file']
# Verify content matches
with open(__file__, 'rb') as original_file:
original_content = original_file.read()
decoded_content = base64.b64decode(
file_data['file']['data'].encode('utf-8')
)
assert decoded_content == original_content
def test_create_etch_packet_payload_handles_multiple_files():
# pylint: disable-next=import-outside-toplevel
from python_anvil.api_resources.payload import (
CreateEtchPacketPayload,
DocumentUpload,
EtchSigner,
SignatureField,
)
# Create signature fields
field1 = SignatureField(
id="sig1",
type="signature",
page_num=1,
rect={"x": 100.0, "y": 100.0, "width": 100.0},
)
field2 = SignatureField(
id="sig2",
type="signature",
page_num=1,
rect={"x": 200.0, "y": 200.0, "width": 100.0},
)
signer = EtchSigner(
name="Test Signer",
email="[email protected]",
fields=[
{"file_id": "doc1", "field_id": "sig1"},
{"file_id": "doc2", "field_id": "sig2"},
],
)
# Test with multiple file objects
with open(__file__, 'rb') as test_file1, open(__file__, 'rb') as test_file2:
doc1 = DocumentUpload(
id="doc1", title="Test Document 1", file=test_file1, fields=[field1]
)
doc2 = DocumentUpload(
id="doc2", title="Test Document 2", file=test_file2, fields=[field2]
)
packet = CreateEtchPacketPayload(
name="Test Packet", signers=[signer], files=[doc1, doc2], is_test=True
)
data = packet.model_dump()
# Verify structure
assert len(data['files']) == 2
# Verify both files are properly handled
for i, file_data in enumerate(data['files'], 1):
assert file_data['id'] == f"doc{i}"
assert file_data['title'] == f"Test Document {i}"
assert isinstance(file_data['file'], dict)
assert 'data' in file_data['file']
assert 'mimetype' in file_data['file']
assert 'filename' in file_data['file']
# Verify content matches
with open(__file__, 'rb') as original_file:
original_content = original_file.read()
decoded_content = base64.b64decode(
file_data['file']['data'].encode('utf-8')
)
assert decoded_content == original_content