Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
Backend
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Container Registry
Analyze
Contributor analytics
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Arkindex
Backend
Commits
649aa33f
Commit
649aa33f
authored
2 years ago
by
Bastien Abadie
Browse files
Options
Downloads
Plain Diff
Merge branch 'fix-boolean-integer-field' into 'master'
Fix invalid default values for user configurations Closes
#1007
See merge request
!1677
parents
75cead9b
c186bc85
No related branches found
Branches containing commit
No related tags found
Tags containing commit
1 merge request
!1677
Fix invalid default values for user configurations
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
arkindex/dataimport/serializers/workers.py
+13
-8
13 additions, 8 deletions
arkindex/dataimport/serializers/workers.py
arkindex/dataimport/tests/test_workers.py
+28
-0
28 additions, 0 deletions
arkindex/dataimport/tests/test_workers.py
with
41 additions
and
8 deletions
arkindex/dataimport/serializers/workers.py
+
13
−
8
View file @
649aa33f
...
...
@@ -76,11 +76,11 @@ class UserConfigurationFieldSerializer(serializers.Serializer):
allowed_fields
=
[
'
title
'
,
'
type
'
,
'
required
'
,
'
default
'
,
'
choices
'
]
data_types
=
{
UserConfigurationFieldType
.
Int
:
serializers
.
IntegerField
,
UserConfigurationFieldType
.
Float
:
serializers
.
FloatField
,
UserConfigurationFieldType
.
String
:
serializers
.
CharField
,
UserConfigurationFieldType
.
Enum
:
serializers
.
ChoiceField
,
UserConfigurationFieldType
.
Boolean
:
serializers
.
BooleanField
UserConfigurationFieldType
.
Int
:
[
serializers
.
IntegerField
,
int
],
UserConfigurationFieldType
.
Float
:
[
serializers
.
FloatField
,
float
],
UserConfigurationFieldType
.
String
:
[
serializers
.
CharField
,
str
],
UserConfigurationFieldType
.
Enum
:
[
serializers
.
ChoiceField
,
None
],
UserConfigurationFieldType
.
Boolean
:
[
serializers
.
BooleanField
,
bool
]
}
for
field
in
data
:
...
...
@@ -94,15 +94,20 @@ class UserConfigurationFieldSerializer(serializers.Serializer):
field_type
=
data
.
get
(
'
type
'
)
choices
=
data
.
get
(
'
choices
'
)
if
choices
:
# Handle enums
if
choices
is
not
None
:
if
field_type
!=
UserConfigurationFieldType
.
Enum
:
errors
[
'
choices
'
].
append
(
'
The
"
choices
"
field can only be set for an
"
enum
"
type property.
'
)
# If the configuration parameter is of enum type, an eventual default value won't match the field type
if
default_value
and
default_value
not
in
choices
:
errors
[
'
default
'
].
append
(
f
'
{
default_value
}
is not an available choice.
'
)
elif
default_value
:
# Handle everything else
elif
default_value
is
not
None
:
try
:
data_type
=
data_types
[
field_type
]
data_type
,
data_class
=
data_types
[
field_type
]
if
not
isinstance
(
default_value
,
data_class
):
raise
ValidationError
data_type
().
to_internal_value
(
default_value
)
except
ValidationError
:
errors
[
'
default
'
].
append
(
f
'
Default value is not of type
{
field_type
.
value
}
.
'
)
...
...
This diff is collapsed.
Click to expand it.
arkindex/dataimport/tests/test_workers.py
+
28
−
0
View file @
649aa33f
...
...
@@ -937,6 +937,34 @@ class TestWorkersWorkerVersions(FixtureAPITestCase):
}
})
def
test_invalid_user_configuration_default_value
(
self
):
self
.
client
.
force_login
(
self
.
internal_user
)
cases
=
[
({
"
type
"
:
"
int
"
,
"
default
"
:
False
},
'
int
'
),
({
"
type
"
:
"
int
"
,
"
default
"
:
True
},
'
int
'
),
({
"
type
"
:
"
float
"
,
"
default
"
:
False
},
'
float
'
),
({
"
type
"
:
"
float
"
,
"
default
"
:
True
},
'
float
'
),
({
"
type
"
:
"
bool
"
,
"
default
"
:
0
},
'
bool
'
),
({
"
type
"
:
"
bool
"
,
"
default
"
:
1
},
'
bool
'
),
({
"
type
"
:
"
string
"
,
"
default
"
:
1
},
'
string
'
),
]
for
params
,
expected
in
cases
:
with
self
.
subTest
(
**
params
):
response
=
self
.
client
.
post
(
reverse
(
"
api:worker-versions
"
,
kwargs
=
{
"
pk
"
:
str
(
self
.
worker_2
.
id
)}),
data
=
{
"
revision
"
:
str
(
self
.
rev2
.
id
),
"
configuration
"
:
{
"
user_configuration
"
:
{
"
param
"
:
{
"
title
"
:
'
param
'
,
**
params
}
}
},
},
format
=
"
json
"
,
)
self
.
assertEqual
(
response
.
status_code
,
status
.
HTTP_400_BAD_REQUEST
)
self
.
assertEqual
(
response
.
json
(),
{
"
configuration
"
:
{
"
user_configuration
"
:
[{
'
param
'
:
{
'
default
'
:
[
f
'
Default value is not of type
{
expected
}
.
'
]}}]}})
def
test_retrieve_version_invalid_id
(
self
):
self
.
client
.
force_login
(
self
.
user
)
response
=
self
.
client
.
get
(
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment