File size: 8,213 Bytes
77f10a3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
from typing import Optional
from enum import Enum

from pydantic import BaseModel, Field

from comfy.comfy_types.node_typing import IO
from comfy_api_nodes.mapper_utils import model_field_to_node_input


def test_model_field_to_float_input():
    """Tests mapping a float field with constraints."""

    class ModelWithFloatField(BaseModel):
        cfg_scale: Optional[float] = Field(
            default=0.5,
            description="Flexibility in video generation",
            ge=0.0,
            le=1.0,
            multiple_of=0.001,
        )

    expected_output = (
        IO.FLOAT,
        {
            "default": 0.5,
            "tooltip": "Flexibility in video generation",
            "min": 0.0,
            "max": 1.0,
            "step": 0.001,
        },
    )

    actual_output = model_field_to_node_input(
        IO.FLOAT, ModelWithFloatField, "cfg_scale"
    )

    assert actual_output[0] == expected_output[0]
    assert actual_output[1] == expected_output[1]


def test_model_field_to_float_input_no_constraints():
    """Tests mapping a float field with no constraints."""

    class ModelWithFloatField(BaseModel):
        cfg_scale: Optional[float] = Field(default=0.5)

    expected_output = (
        IO.FLOAT,
        {
            "default": 0.5,
        },
    )

    actual_output = model_field_to_node_input(
        IO.FLOAT, ModelWithFloatField, "cfg_scale"
    )

    assert actual_output[0] == expected_output[0]
    assert actual_output[1] == expected_output[1]


def test_model_field_to_int_input():
    """Tests mapping an int field with constraints."""

    class ModelWithIntField(BaseModel):
        num_frames: Optional[int] = Field(
            default=10,
            description="Number of frames to generate",
            ge=1,
            le=100,
            multiple_of=1,
        )

    expected_output = (
        IO.INT,
        {
            "default": 10,
            "tooltip": "Number of frames to generate",
            "min": 1,
            "max": 100,
            "step": 1,
        },
    )

    actual_output = model_field_to_node_input(IO.INT, ModelWithIntField, "num_frames")

    assert actual_output[0] == expected_output[0]
    assert actual_output[1] == expected_output[1]


def test_model_field_to_string_input():
    """Tests mapping a string field."""

    class ModelWithStringField(BaseModel):
        prompt: Optional[str] = Field(
            default="A beautiful sunset over a calm ocean",
            description="A prompt for the video generation",
        )

    expected_output = (
        IO.STRING,
        {
            "default": "A beautiful sunset over a calm ocean",
            "tooltip": "A prompt for the video generation",
        },
    )

    actual_output = model_field_to_node_input(IO.STRING, ModelWithStringField, "prompt")

    assert actual_output[0] == expected_output[0]
    assert actual_output[1] == expected_output[1]


def test_model_field_to_string_input_multiline():
    """Tests mapping a string field."""

    class ModelWithStringField(BaseModel):
        prompt: Optional[str] = Field(
            default="A beautiful sunset over a calm ocean",
            description="A prompt for the video generation",
        )

    expected_output = (
        IO.STRING,
        {
            "default": "A beautiful sunset over a calm ocean",
            "tooltip": "A prompt for the video generation",
            "multiline": True,
        },
    )

    actual_output = model_field_to_node_input(
        IO.STRING, ModelWithStringField, "prompt", multiline=True
    )

    assert actual_output[0] == expected_output[0]
    assert actual_output[1] == expected_output[1]


def test_model_field_to_combo_input():
    """Tests mapping a combo field."""

    class MockEnum(str, Enum):
        option_1 = "option 1"
        option_2 = "option 2"
        option_3 = "option 3"

    class ModelWithComboField(BaseModel):
        model_name: Optional[MockEnum] = Field("option 1", description="Model Name")

    expected_output = (
        IO.COMBO,
        {
            "options": ["option 1", "option 2", "option 3"],
            "default": "option 1",
            "tooltip": "Model Name",
        },
    )

    actual_output = model_field_to_node_input(
        IO.COMBO, ModelWithComboField, "model_name", enum_type=MockEnum
    )

    assert actual_output[0] == expected_output[0]
    assert actual_output[1] == expected_output[1]


def test_model_field_to_combo_input_no_options():
    """Tests mapping a combo field with no options."""

    class ModelWithComboField(BaseModel):
        model_name: Optional[str] = Field(description="Model Name")

    expected_output = (
        IO.COMBO,
        {
            "tooltip": "Model Name",
        },
    )

    actual_output = model_field_to_node_input(
        IO.COMBO, ModelWithComboField, "model_name"
    )

    assert actual_output[0] == expected_output[0]
    assert actual_output[1] == expected_output[1]


def test_model_field_to_image_input():
    """Tests mapping an image field."""

    class ModelWithImageField(BaseModel):
        image: Optional[str] = Field(
            default=None,
            description="An image for the video generation",
        )

    expected_output = (
        IO.IMAGE,
        {
            "default": None,
            "tooltip": "An image for the video generation",
        },
    )

    actual_output = model_field_to_node_input(IO.IMAGE, ModelWithImageField, "image")

    assert actual_output[0] == expected_output[0]
    assert actual_output[1] == expected_output[1]


def test_model_field_to_node_input_no_description():
    """Tests mapping a field with no description."""

    class ModelWithNoDescriptionField(BaseModel):
        field: Optional[str] = Field(default="default value")

    expected_output = (
        IO.STRING,
        {
            "default": "default value",
        },
    )

    actual_output = model_field_to_node_input(
        IO.STRING, ModelWithNoDescriptionField, "field"
    )

    assert actual_output[0] == expected_output[0]
    assert actual_output[1] == expected_output[1]


def test_model_field_to_node_input_no_default():
    """Tests mapping a field with no default."""

    class ModelWithNoDefaultField(BaseModel):
        field: Optional[str] = Field(description="A field with no default")

    expected_output = (
        IO.STRING,
        {
            "tooltip": "A field with no default",
        },
    )

    actual_output = model_field_to_node_input(
        IO.STRING, ModelWithNoDefaultField, "field"
    )

    assert actual_output[0] == expected_output[0]
    assert actual_output[1] == expected_output[1]


def test_model_field_to_node_input_no_metadata():
    """Tests mapping a field with no metadata or properties defined on the schema."""

    class ModelWithNoMetadataField(BaseModel):
        field: Optional[str] = Field()

    expected_output = (
        IO.STRING,
        {},
    )

    actual_output = model_field_to_node_input(
        IO.STRING, ModelWithNoMetadataField, "field"
    )

    assert actual_output[0] == expected_output[0]
    assert actual_output[1] == expected_output[1]


def test_model_field_to_node_input_default_is_none():
    """

    Tests mapping a field with a default of `None`.

    I.e., the default field should be included as the schema explicitly sets it to `None`.

    """

    class ModelWithNoneDefaultField(BaseModel):
        field: Optional[str] = Field(
            default=None, description="A field with a default of None"
        )

    expected_output = (
        IO.STRING,
        {
            "default": None,
            "tooltip": "A field with a default of None",
        },
    )

    actual_output = model_field_to_node_input(
        IO.STRING, ModelWithNoneDefaultField, "field"
    )

    assert actual_output[0] == expected_output[0]
    assert actual_output[1] == expected_output[1]