Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
D
django-rest-framework
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
edx
django-rest-framework
Commits
4db23cae
Commit
4db23cae
authored
Sep 22, 2014
by
Tom Christie
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Tweaks to DecimalField
parent
249253a1
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
165 additions
and
79 deletions
+165
-79
rest_framework/fields.py
+6
-5
tests/test_fields.py
+159
-74
No files found.
rest_framework/fields.py
View file @
4db23cae
...
@@ -521,7 +521,12 @@ class DecimalField(Field):
...
@@ -521,7 +521,12 @@ class DecimalField(Field):
return
value
return
value
def
to_representation
(
self
,
value
):
def
to_representation
(
self
,
value
):
if
isinstance
(
value
,
decimal
.
Decimal
):
if
value
in
(
None
,
''
):
return
None
if
not
isinstance
(
value
,
decimal
.
Decimal
):
value
=
decimal
.
Decimal
(
value
)
context
=
decimal
.
getcontext
()
.
copy
()
context
=
decimal
.
getcontext
()
.
copy
()
context
.
prec
=
self
.
max_digits
context
.
prec
=
self
.
max_digits
quantized
=
value
.
quantize
(
quantized
=
value
.
quantize
(
...
@@ -532,10 +537,6 @@ class DecimalField(Field):
...
@@ -532,10 +537,6 @@ class DecimalField(Field):
return
quantized
return
quantized
return
'{0:f}'
.
format
(
quantized
)
return
'{0:f}'
.
format
(
quantized
)
if
not
self
.
coerce_to_string
:
return
value
return
'
%.*
f'
%
(
self
.
max_decimal_places
,
value
)
# Date & time fields...
# Date & time fields...
...
...
tests/test_fields.py
View file @
4db23cae
...
@@ -14,31 +14,35 @@ def get_items(mapping_or_list_of_two_tuples):
...
@@ -14,31 +14,35 @@ def get_items(mapping_or_list_of_two_tuples):
return
mapping_or_list_of_two_tuples
return
mapping_or_list_of_two_tuples
class
ValidAndInvali
dValues
:
class
Fiel
dValues
:
"""
"""
Base class for testing valid and invalid input values.
Base class for testing valid and invalid input values.
"""
"""
def
test_valid_
value
s
(
self
):
def
test_valid_
input
s
(
self
):
"""
"""
Ensure that valid values return the expected validated data.
Ensure that valid values return the expected validated data.
"""
"""
for
input_value
,
expected_output
in
get_items
(
self
.
valid_
mapping
s
):
for
input_value
,
expected_output
in
get_items
(
self
.
valid_
input
s
):
assert
self
.
field
.
run_validation
(
input_value
)
==
expected_output
assert
self
.
field
.
run_validation
(
input_value
)
==
expected_output
def
test_invalid_
value
s
(
self
):
def
test_invalid_
input
s
(
self
):
"""
"""
Ensure that invalid values raise the expected validation error.
Ensure that invalid values raise the expected validation error.
"""
"""
for
input_value
,
expected_failure
in
get_items
(
self
.
invalid_
mapping
s
):
for
input_value
,
expected_failure
in
get_items
(
self
.
invalid_
input
s
):
with
pytest
.
raises
(
fields
.
ValidationError
)
as
exc_info
:
with
pytest
.
raises
(
fields
.
ValidationError
)
as
exc_info
:
self
.
field
.
run_validation
(
input_value
)
self
.
field
.
run_validation
(
input_value
)
assert
exc_info
.
value
.
messages
==
expected_failure
assert
exc_info
.
value
.
messages
==
expected_failure
def
test_outputs
(
self
):
for
output_value
,
expected_output
in
get_items
(
self
.
outputs
):
assert
self
.
field
.
to_representation
(
output_value
)
==
expected_output
# Boolean types...
# Boolean types...
class
TestBooleanField
(
ValidAndInvali
dValues
):
class
TestBooleanField
(
Fiel
dValues
):
valid_
mapping
s
=
{
valid_
input
s
=
{
'true'
:
True
,
'true'
:
True
,
'false'
:
False
,
'false'
:
False
,
'1'
:
True
,
'1'
:
True
,
...
@@ -48,73 +52,92 @@ class TestBooleanField(ValidAndInvalidValues):
...
@@ -48,73 +52,92 @@ class TestBooleanField(ValidAndInvalidValues):
True
:
True
,
True
:
True
,
False
:
False
,
False
:
False
,
}
}
invalid_
mapping
s
=
{
invalid_
input
s
=
{
'foo'
:
[
'`foo` is not a valid boolean.'
]
'foo'
:
[
'`foo` is not a valid boolean.'
]
}
}
outputs
=
{
'true'
:
True
,
'false'
:
False
,
'1'
:
True
,
'0'
:
False
,
1
:
True
,
0
:
False
,
True
:
True
,
False
:
False
,
'other'
:
True
}
field
=
fields
.
BooleanField
()
field
=
fields
.
BooleanField
()
# String types...
# String types...
class
TestCharField
(
ValidAndInvali
dValues
):
class
TestCharField
(
Fiel
dValues
):
valid_
mapping
s
=
{
valid_
input
s
=
{
1
:
'1'
,
1
:
'1'
,
'abc'
:
'abc'
'abc'
:
'abc'
}
}
invalid_
mapping
s
=
{
invalid_
input
s
=
{
''
:
[
'This field may not be blank.'
]
''
:
[
'This field may not be blank.'
]
}
}
outputs
=
{
1
:
'1'
,
'abc'
:
'abc'
}
field
=
fields
.
CharField
()
field
=
fields
.
CharField
()
class
TestEmailField
(
ValidAndInvali
dValues
):
class
TestEmailField
(
Fiel
dValues
):
valid_
mapping
s
=
{
valid_
input
s
=
{
'example@example.com'
:
'example@example.com'
,
'example@example.com'
:
'example@example.com'
,
' example@example.com '
:
'example@example.com'
,
' example@example.com '
:
'example@example.com'
,
}
}
invalid_
mapping
s
=
{
invalid_
input
s
=
{
'example.com'
:
[
'Enter a valid email address.'
]
'example.com'
:
[
'Enter a valid email address.'
]
}
}
outputs
=
{}
field
=
fields
.
EmailField
()
field
=
fields
.
EmailField
()
class
TestRegexField
(
ValidAndInvali
dValues
):
class
TestRegexField
(
Fiel
dValues
):
valid_
mapping
s
=
{
valid_
input
s
=
{
'a9'
:
'a9'
,
'a9'
:
'a9'
,
}
}
invalid_
mapping
s
=
{
invalid_
input
s
=
{
'A9'
:
[
"This value does not match the required pattern."
]
'A9'
:
[
"This value does not match the required pattern."
]
}
}
outputs
=
{}
field
=
fields
.
RegexField
(
regex
=
'[a-z][0-9]'
)
field
=
fields
.
RegexField
(
regex
=
'[a-z][0-9]'
)
class
TestSlugField
(
ValidAndInvali
dValues
):
class
TestSlugField
(
Fiel
dValues
):
valid_
mapping
s
=
{
valid_
input
s
=
{
'slug-99'
:
'slug-99'
,
'slug-99'
:
'slug-99'
,
}
}
invalid_
mapping
s
=
{
invalid_
input
s
=
{
'slug 99'
:
[
"Enter a valid 'slug' consisting of letters, numbers, underscores or hyphens."
]
'slug 99'
:
[
"Enter a valid 'slug' consisting of letters, numbers, underscores or hyphens."
]
}
}
outputs
=
{}
field
=
fields
.
SlugField
()
field
=
fields
.
SlugField
()
class
TestURLField
(
ValidAndInvali
dValues
):
class
TestURLField
(
Fiel
dValues
):
valid_
mapping
s
=
{
valid_
input
s
=
{
'http://example.com'
:
'http://example.com'
,
'http://example.com'
:
'http://example.com'
,
}
}
invalid_
mapping
s
=
{
invalid_
input
s
=
{
'example.com'
:
[
'Enter a valid URL.'
]
'example.com'
:
[
'Enter a valid URL.'
]
}
}
outputs
=
{}
field
=
fields
.
URLField
()
field
=
fields
.
URLField
()
# Number types...
# Number types...
class
TestIntegerField
(
ValidAndInvali
dValues
):
class
TestIntegerField
(
Fiel
dValues
):
"""
"""
Valid and invalid values for `IntegerField`.
Valid and invalid values for `IntegerField`.
"""
"""
valid_
mapping
s
=
{
valid_
input
s
=
{
'1'
:
1
,
'1'
:
1
,
'0'
:
0
,
'0'
:
0
,
1
:
1
,
1
:
1
,
...
@@ -122,36 +145,45 @@ class TestIntegerField(ValidAndInvalidValues):
...
@@ -122,36 +145,45 @@ class TestIntegerField(ValidAndInvalidValues):
1.0
:
1
,
1.0
:
1
,
0.0
:
0
0.0
:
0
}
}
invalid_
mapping
s
=
{
invalid_
input
s
=
{
'abc'
:
[
'A valid integer is required.'
]
'abc'
:
[
'A valid integer is required.'
]
}
}
outputs
=
{
'1'
:
1
,
'0'
:
0
,
1
:
1
,
0
:
0
,
1.0
:
1
,
0.0
:
0
}
field
=
fields
.
IntegerField
()
field
=
fields
.
IntegerField
()
class
TestMinMaxIntegerField
(
ValidAndInvali
dValues
):
class
TestMinMaxIntegerField
(
Fiel
dValues
):
"""
"""
Valid and invalid values for `IntegerField` with min and max limits.
Valid and invalid values for `IntegerField` with min and max limits.
"""
"""
valid_
mapping
s
=
{
valid_
input
s
=
{
'1'
:
1
,
'1'
:
1
,
'3'
:
3
,
'3'
:
3
,
1
:
1
,
1
:
1
,
3
:
3
,
3
:
3
,
}
}
invalid_
mapping
s
=
{
invalid_
input
s
=
{
0
:
[
'Ensure this value is greater than or equal to 1.'
],
0
:
[
'Ensure this value is greater than or equal to 1.'
],
4
:
[
'Ensure this value is less than or equal to 3.'
],
4
:
[
'Ensure this value is less than or equal to 3.'
],
'0'
:
[
'Ensure this value is greater than or equal to 1.'
],
'0'
:
[
'Ensure this value is greater than or equal to 1.'
],
'4'
:
[
'Ensure this value is less than or equal to 3.'
],
'4'
:
[
'Ensure this value is less than or equal to 3.'
],
}
}
outputs
=
{}
field
=
fields
.
IntegerField
(
min_value
=
1
,
max_value
=
3
)
field
=
fields
.
IntegerField
(
min_value
=
1
,
max_value
=
3
)
class
TestFloatField
(
ValidAndInvali
dValues
):
class
TestFloatField
(
Fiel
dValues
):
"""
"""
Valid and invalid values for `FloatField`.
Valid and invalid values for `FloatField`.
"""
"""
valid_
mapping
s
=
{
valid_
input
s
=
{
'1'
:
1.0
,
'1'
:
1.0
,
'0'
:
0.0
,
'0'
:
0.0
,
1
:
1.0
,
1
:
1.0
,
...
@@ -159,17 +191,25 @@ class TestFloatField(ValidAndInvalidValues):
...
@@ -159,17 +191,25 @@ class TestFloatField(ValidAndInvalidValues):
1.0
:
1.0
,
1.0
:
1.0
,
0.0
:
0.0
,
0.0
:
0.0
,
}
}
invalid_
mapping
s
=
{
invalid_
input
s
=
{
'abc'
:
[
"A valid number is required."
]
'abc'
:
[
"A valid number is required."
]
}
}
outputs
=
{
'1'
:
1.0
,
'0'
:
0.0
,
1
:
1.0
,
0
:
0.0
,
1.0
:
1.0
,
0.0
:
0.0
,
}
field
=
fields
.
FloatField
()
field
=
fields
.
FloatField
()
class
TestMinMaxFloatField
(
ValidAndInvali
dValues
):
class
TestMinMaxFloatField
(
Fiel
dValues
):
"""
"""
Valid and invalid values for `FloatField` with min and max limits.
Valid and invalid values for `FloatField` with min and max limits.
"""
"""
valid_
mapping
s
=
{
valid_
input
s
=
{
'1'
:
1
,
'1'
:
1
,
'3'
:
3
,
'3'
:
3
,
1
:
1
,
1
:
1
,
...
@@ -177,20 +217,21 @@ class TestMinMaxFloatField(ValidAndInvalidValues):
...
@@ -177,20 +217,21 @@ class TestMinMaxFloatField(ValidAndInvalidValues):
1.0
:
1.0
,
1.0
:
1.0
,
3.0
:
3.0
,
3.0
:
3.0
,
}
}
invalid_
mapping
s
=
{
invalid_
input
s
=
{
0.9
:
[
'Ensure this value is greater than or equal to 1.'
],
0.9
:
[
'Ensure this value is greater than or equal to 1.'
],
3.1
:
[
'Ensure this value is less than or equal to 3.'
],
3.1
:
[
'Ensure this value is less than or equal to 3.'
],
'0.0'
:
[
'Ensure this value is greater than or equal to 1.'
],
'0.0'
:
[
'Ensure this value is greater than or equal to 1.'
],
'3.1'
:
[
'Ensure this value is less than or equal to 3.'
],
'3.1'
:
[
'Ensure this value is less than or equal to 3.'
],
}
}
outputs
=
{}
field
=
fields
.
FloatField
(
min_value
=
1
,
max_value
=
3
)
field
=
fields
.
FloatField
(
min_value
=
1
,
max_value
=
3
)
class
TestDecimalField
(
ValidAndInvali
dValues
):
class
TestDecimalField
(
Fiel
dValues
):
"""
"""
Valid and invalid values for `DecimalField`.
Valid and invalid values for `DecimalField`.
"""
"""
valid_
mapping
s
=
{
valid_
input
s
=
{
'12.3'
:
Decimal
(
'12.3'
),
'12.3'
:
Decimal
(
'12.3'
),
'0.1'
:
Decimal
(
'0.1'
),
'0.1'
:
Decimal
(
'0.1'
),
10
:
Decimal
(
'10'
),
10
:
Decimal
(
'10'
),
...
@@ -198,7 +239,7 @@ class TestDecimalField(ValidAndInvalidValues):
...
@@ -198,7 +239,7 @@ class TestDecimalField(ValidAndInvalidValues):
12.3
:
Decimal
(
'12.3'
),
12.3
:
Decimal
(
'12.3'
),
0.1
:
Decimal
(
'0.1'
),
0.1
:
Decimal
(
'0.1'
),
}
}
invalid_
mapping
s
=
(
invalid_
input
s
=
(
(
'abc'
,
[
"A valid number is required."
]),
(
'abc'
,
[
"A valid number is required."
]),
(
Decimal
(
'Nan'
),
[
"A valid number is required."
]),
(
Decimal
(
'Nan'
),
[
"A valid number is required."
]),
(
Decimal
(
'Inf'
),
[
"A valid number is required."
]),
(
Decimal
(
'Inf'
),
[
"A valid number is required."
]),
...
@@ -206,63 +247,98 @@ class TestDecimalField(ValidAndInvalidValues):
...
@@ -206,63 +247,98 @@ class TestDecimalField(ValidAndInvalidValues):
(
'0.01'
,
[
"Ensure that there are no more than 1 decimal places."
]),
(
'0.01'
,
[
"Ensure that there are no more than 1 decimal places."
]),
(
123
,
[
"Ensure that there are no more than 2 digits before the decimal point."
])
(
123
,
[
"Ensure that there are no more than 2 digits before the decimal point."
])
)
)
outputs
=
{
'1'
:
'1.0'
,
'0'
:
'0.0'
,
'1.09'
:
'1.1'
,
'0.04'
:
'0.0'
,
1
:
'1.0'
,
0
:
'0.0'
,
Decimal
(
'1.0'
):
'1.0'
,
Decimal
(
'0.0'
):
'0.0'
,
Decimal
(
'1.09'
):
'1.1'
,
Decimal
(
'0.04'
):
'0.0'
,
}
field
=
fields
.
DecimalField
(
max_digits
=
3
,
decimal_places
=
1
)
field
=
fields
.
DecimalField
(
max_digits
=
3
,
decimal_places
=
1
)
class
TestMinMaxDecimalField
(
ValidAndInvali
dValues
):
class
TestMinMaxDecimalField
(
Fiel
dValues
):
"""
"""
Valid and invalid values for `DecimalField` with min and max limits.
Valid and invalid values for `DecimalField` with min and max limits.
"""
"""
valid_
mapping
s
=
{
valid_
input
s
=
{
'10.0'
:
10.0
,
'10.0'
:
10.0
,
'20.0'
:
20.0
,
'20.0'
:
20.0
,
}
}
invalid_
mapping
s
=
{
invalid_
input
s
=
{
'9.9'
:
[
'Ensure this value is greater than or equal to 10.'
],
'9.9'
:
[
'Ensure this value is greater than or equal to 10.'
],
'20.1'
:
[
'Ensure this value is less than or equal to 20.'
],
'20.1'
:
[
'Ensure this value is less than or equal to 20.'
],
}
}
outputs
=
{}
field
=
fields
.
DecimalField
(
field
=
fields
.
DecimalField
(
max_digits
=
3
,
decimal_places
=
1
,
max_digits
=
3
,
decimal_places
=
1
,
min_value
=
10
,
max_value
=
20
min_value
=
10
,
max_value
=
20
)
)
class
TestNoStringCoercionDecimalField
(
FieldValues
):
"""
Output values for `DecimalField` with `coerce_to_string=False`.
"""
valid_inputs
=
{}
invalid_inputs
=
{}
outputs
=
{
1.09
:
Decimal
(
'1.1'
),
0.04
:
Decimal
(
'0.0'
),
'1.09'
:
Decimal
(
'1.1'
),
'0.04'
:
Decimal
(
'0.0'
),
Decimal
(
'1.09'
):
Decimal
(
'1.1'
),
Decimal
(
'0.04'
):
Decimal
(
'0.0'
),
}
field
=
fields
.
DecimalField
(
max_digits
=
3
,
decimal_places
=
1
,
coerce_to_string
=
False
)
# Date & time fields...
# Date & time fields...
class
TestDateField
(
ValidAndInvali
dValues
):
class
TestDateField
(
Fiel
dValues
):
"""
"""
Valid and invalid values for `DateField`.
Valid and invalid values for `DateField`.
"""
"""
valid_
mapping
s
=
{
valid_
input
s
=
{
'2001-01-01'
:
datetime
.
date
(
2001
,
1
,
1
),
'2001-01-01'
:
datetime
.
date
(
2001
,
1
,
1
),
datetime
.
date
(
2001
,
1
,
1
):
datetime
.
date
(
2001
,
1
,
1
),
datetime
.
date
(
2001
,
1
,
1
):
datetime
.
date
(
2001
,
1
,
1
),
}
}
invalid_
mapping
s
=
{
invalid_
input
s
=
{
'abc'
:
[
'Date has wrong format. Use one of these formats instead: YYYY[-MM[-DD]]'
],
'abc'
:
[
'Date has wrong format. Use one of these formats instead: YYYY[-MM[-DD]]'
],
'2001-99-99'
:
[
'Date has wrong format. Use one of these formats instead: YYYY[-MM[-DD]]'
],
'2001-99-99'
:
[
'Date has wrong format. Use one of these formats instead: YYYY[-MM[-DD]]'
],
datetime
.
datetime
(
2001
,
1
,
1
,
12
,
00
):
[
'Expected a date but got a datetime.'
],
datetime
.
datetime
(
2001
,
1
,
1
,
12
,
00
):
[
'Expected a date but got a datetime.'
],
}
}
outputs
=
{}
field
=
fields
.
DateField
()
field
=
fields
.
DateField
()
class
TestCustomInputFormatDateField
(
ValidAndInvali
dValues
):
class
TestCustomInputFormatDateField
(
Fiel
dValues
):
"""
"""
Valid and invalid values for `DateField` with a cutom input format.
Valid and invalid values for `DateField` with a cutom input format.
"""
"""
valid_
mapping
s
=
{
valid_
input
s
=
{
'1 Jan 2001'
:
datetime
.
date
(
2001
,
1
,
1
),
'1 Jan 2001'
:
datetime
.
date
(
2001
,
1
,
1
),
}
}
invalid_
mapping
s
=
{
invalid_
input
s
=
{
'2001-01-01'
:
[
'Date has wrong format. Use one of these formats instead: DD [Jan-Dec] YYYY'
]
'2001-01-01'
:
[
'Date has wrong format. Use one of these formats instead: DD [Jan-Dec] YYYY'
]
}
}
outputs
=
{}
field
=
fields
.
DateField
(
input_formats
=
[
'
%
d
%
b
%
Y'
])
field
=
fields
.
DateField
(
input_formats
=
[
'
%
d
%
b
%
Y'
])
class
TestDateTimeField
(
ValidAndInvali
dValues
):
class
TestDateTimeField
(
Fiel
dValues
):
"""
"""
Valid and invalid values for `DateTimeField`.
Valid and invalid values for `DateTimeField`.
"""
"""
valid_
mapping
s
=
{
valid_
input
s
=
{
'2001-01-01 13:00'
:
datetime
.
datetime
(
2001
,
1
,
1
,
13
,
00
,
tzinfo
=
timezone
.
UTC
()),
'2001-01-01 13:00'
:
datetime
.
datetime
(
2001
,
1
,
1
,
13
,
00
,
tzinfo
=
timezone
.
UTC
()),
'2001-01-01T13:00'
:
datetime
.
datetime
(
2001
,
1
,
1
,
13
,
00
,
tzinfo
=
timezone
.
UTC
()),
'2001-01-01T13:00'
:
datetime
.
datetime
(
2001
,
1
,
1
,
13
,
00
,
tzinfo
=
timezone
.
UTC
()),
'2001-01-01T13:00Z'
:
datetime
.
datetime
(
2001
,
1
,
1
,
13
,
00
,
tzinfo
=
timezone
.
UTC
()),
'2001-01-01T13:00Z'
:
datetime
.
datetime
(
2001
,
1
,
1
,
13
,
00
,
tzinfo
=
timezone
.
UTC
()),
...
@@ -270,81 +346,87 @@ class TestDateTimeField(ValidAndInvalidValues):
...
@@ -270,81 +346,87 @@ class TestDateTimeField(ValidAndInvalidValues):
datetime
.
datetime
(
2001
,
1
,
1
,
13
,
00
):
datetime
.
datetime
(
2001
,
1
,
1
,
13
,
00
,
tzinfo
=
timezone
.
UTC
()),
datetime
.
datetime
(
2001
,
1
,
1
,
13
,
00
):
datetime
.
datetime
(
2001
,
1
,
1
,
13
,
00
,
tzinfo
=
timezone
.
UTC
()),
datetime
.
datetime
(
2001
,
1
,
1
,
13
,
00
,
tzinfo
=
timezone
.
UTC
()):
datetime
.
datetime
(
2001
,
1
,
1
,
13
,
00
,
tzinfo
=
timezone
.
UTC
()),
datetime
.
datetime
(
2001
,
1
,
1
,
13
,
00
,
tzinfo
=
timezone
.
UTC
()):
datetime
.
datetime
(
2001
,
1
,
1
,
13
,
00
,
tzinfo
=
timezone
.
UTC
()),
}
}
invalid_
mapping
s
=
{
invalid_
input
s
=
{
'abc'
:
[
'Datetime has wrong format. Use one of these formats instead: YYYY-MM-DDThh:mm[:ss[.uuuuuu]][+HH:MM|-HH:MM|Z]'
],
'abc'
:
[
'Datetime has wrong format. Use one of these formats instead: YYYY-MM-DDThh:mm[:ss[.uuuuuu]][+HH:MM|-HH:MM|Z]'
],
'2001-99-99T99:00'
:
[
'Datetime has wrong format. Use one of these formats instead: YYYY-MM-DDThh:mm[:ss[.uuuuuu]][+HH:MM|-HH:MM|Z]'
],
'2001-99-99T99:00'
:
[
'Datetime has wrong format. Use one of these formats instead: YYYY-MM-DDThh:mm[:ss[.uuuuuu]][+HH:MM|-HH:MM|Z]'
],
datetime
.
date
(
2001
,
1
,
1
):
[
'Expected a datetime but got a date.'
],
datetime
.
date
(
2001
,
1
,
1
):
[
'Expected a datetime but got a date.'
],
}
}
outputs
=
{}
field
=
fields
.
DateTimeField
(
default_timezone
=
timezone
.
UTC
())
field
=
fields
.
DateTimeField
(
default_timezone
=
timezone
.
UTC
())
class
TestCustomInputFormatDateTimeField
(
ValidAndInvali
dValues
):
class
TestCustomInputFormatDateTimeField
(
Fiel
dValues
):
"""
"""
Valid and invalid values for `DateTimeField` with a cutom input format.
Valid and invalid values for `DateTimeField` with a cutom input format.
"""
"""
valid_
mapping
s
=
{
valid_
input
s
=
{
'1:35pm, 1 Jan 2001'
:
datetime
.
datetime
(
2001
,
1
,
1
,
13
,
35
,
tzinfo
=
timezone
.
UTC
()),
'1:35pm, 1 Jan 2001'
:
datetime
.
datetime
(
2001
,
1
,
1
,
13
,
35
,
tzinfo
=
timezone
.
UTC
()),
}
}
invalid_
mapping
s
=
{
invalid_
input
s
=
{
'2001-01-01T20:50'
:
[
'Datetime has wrong format. Use one of these formats instead: hh:mm[AM|PM], DD [Jan-Dec] YYYY'
]
'2001-01-01T20:50'
:
[
'Datetime has wrong format. Use one of these formats instead: hh:mm[AM|PM], DD [Jan-Dec] YYYY'
]
}
}
outputs
=
{}
field
=
fields
.
DateTimeField
(
default_timezone
=
timezone
.
UTC
(),
input_formats
=
[
'
%
I:
%
M
%
p,
%
d
%
b
%
Y'
])
field
=
fields
.
DateTimeField
(
default_timezone
=
timezone
.
UTC
(),
input_formats
=
[
'
%
I:
%
M
%
p,
%
d
%
b
%
Y'
])
class
TestNaiveDateTimeField
(
ValidAndInvali
dValues
):
class
TestNaiveDateTimeField
(
Fiel
dValues
):
"""
"""
Valid and invalid values for `DateTimeField` with naive datetimes.
Valid and invalid values for `DateTimeField` with naive datetimes.
"""
"""
valid_
mapping
s
=
{
valid_
input
s
=
{
datetime
.
datetime
(
2001
,
1
,
1
,
13
,
00
,
tzinfo
=
timezone
.
UTC
()):
datetime
.
datetime
(
2001
,
1
,
1
,
13
,
00
),
datetime
.
datetime
(
2001
,
1
,
1
,
13
,
00
,
tzinfo
=
timezone
.
UTC
()):
datetime
.
datetime
(
2001
,
1
,
1
,
13
,
00
),
'2001-01-01 13:00'
:
datetime
.
datetime
(
2001
,
1
,
1
,
13
,
00
),
'2001-01-01 13:00'
:
datetime
.
datetime
(
2001
,
1
,
1
,
13
,
00
),
}
}
invalid_mappings
=
{}
invalid_inputs
=
{}
outputs
=
{}
field
=
fields
.
DateTimeField
(
default_timezone
=
None
)
field
=
fields
.
DateTimeField
(
default_timezone
=
None
)
class
TestTimeField
(
ValidAndInvali
dValues
):
class
TestTimeField
(
Fiel
dValues
):
"""
"""
Valid and invalid values for `TimeField`.
Valid and invalid values for `TimeField`.
"""
"""
valid_
mapping
s
=
{
valid_
input
s
=
{
'13:00'
:
datetime
.
time
(
13
,
00
),
'13:00'
:
datetime
.
time
(
13
,
00
),
datetime
.
time
(
13
,
00
):
datetime
.
time
(
13
,
00
),
datetime
.
time
(
13
,
00
):
datetime
.
time
(
13
,
00
),
}
}
invalid_
mapping
s
=
{
invalid_
input
s
=
{
'abc'
:
[
'Time has wrong format. Use one of these formats instead: hh:mm[:ss[.uuuuuu]]'
],
'abc'
:
[
'Time has wrong format. Use one of these formats instead: hh:mm[:ss[.uuuuuu]]'
],
'99:99'
:
[
'Time has wrong format. Use one of these formats instead: hh:mm[:ss[.uuuuuu]]'
],
'99:99'
:
[
'Time has wrong format. Use one of these formats instead: hh:mm[:ss[.uuuuuu]]'
],
}
}
outputs
=
{}
field
=
fields
.
TimeField
()
field
=
fields
.
TimeField
()
class
TestCustomInputFormatTimeField
(
ValidAndInvali
dValues
):
class
TestCustomInputFormatTimeField
(
Fiel
dValues
):
"""
"""
Valid and invalid values for `TimeField` with a custom input format.
Valid and invalid values for `TimeField` with a custom input format.
"""
"""
valid_
mapping
s
=
{
valid_
input
s
=
{
'1:00pm'
:
datetime
.
time
(
13
,
00
),
'1:00pm'
:
datetime
.
time
(
13
,
00
),
}
}
invalid_
mapping
s
=
{
invalid_
input
s
=
{
'13:00'
:
[
'Time has wrong format. Use one of these formats instead: hh:mm[AM|PM]'
],
'13:00'
:
[
'Time has wrong format. Use one of these formats instead: hh:mm[AM|PM]'
],
}
}
outputs
=
{}
field
=
fields
.
TimeField
(
input_formats
=
[
'
%
I:
%
M
%
p'
])
field
=
fields
.
TimeField
(
input_formats
=
[
'
%
I:
%
M
%
p'
])
# Choice types...
# Choice types...
class
TestChoiceField
(
ValidAndInvali
dValues
):
class
TestChoiceField
(
Fiel
dValues
):
"""
"""
Valid and invalid values for `ChoiceField`.
Valid and invalid values for `ChoiceField`.
"""
"""
valid_
mapping
s
=
{
valid_
input
s
=
{
'poor'
:
'poor'
,
'poor'
:
'poor'
,
'medium'
:
'medium'
,
'medium'
:
'medium'
,
'good'
:
'good'
,
'good'
:
'good'
,
}
}
invalid_
mapping
s
=
{
invalid_
input
s
=
{
'awful'
:
[
'`awful` is not a valid choice.'
]
'awful'
:
[
'`awful` is not a valid choice.'
]
}
}
outputs
=
{}
field
=
fields
.
ChoiceField
(
field
=
fields
.
ChoiceField
(
choices
=
[
choices
=
[
(
'poor'
,
'Poor quality'
),
(
'poor'
,
'Poor quality'
),
...
@@ -354,19 +436,20 @@ class TestChoiceField(ValidAndInvalidValues):
...
@@ -354,19 +436,20 @@ class TestChoiceField(ValidAndInvalidValues):
)
)
class
TestChoiceFieldWithType
(
ValidAndInvali
dValues
):
class
TestChoiceFieldWithType
(
Fiel
dValues
):
"""
"""
Valid and invalid values for a `Choice` field that uses an integer type,
Valid and invalid values for a `Choice` field that uses an integer type,
instead of a char type.
instead of a char type.
"""
"""
valid_
mapping
s
=
{
valid_
input
s
=
{
'1'
:
1
,
'1'
:
1
,
3
:
3
,
3
:
3
,
}
}
invalid_
mapping
s
=
{
invalid_
input
s
=
{
5
:
[
'`5` is not a valid choice.'
],
5
:
[
'`5` is not a valid choice.'
],
'abc'
:
[
'`abc` is not a valid choice.'
]
'abc'
:
[
'`abc` is not a valid choice.'
]
}
}
outputs
=
{}
field
=
fields
.
ChoiceField
(
field
=
fields
.
ChoiceField
(
choices
=
[
choices
=
[
(
1
,
'Poor quality'
),
(
1
,
'Poor quality'
),
...
@@ -376,35 +459,37 @@ class TestChoiceFieldWithType(ValidAndInvalidValues):
...
@@ -376,35 +459,37 @@ class TestChoiceFieldWithType(ValidAndInvalidValues):
)
)
class
TestChoiceFieldWithListChoices
(
ValidAndInvali
dValues
):
class
TestChoiceFieldWithListChoices
(
Fiel
dValues
):
"""
"""
Valid and invalid values for a `Choice` field that uses a flat list for the
Valid and invalid values for a `Choice` field that uses a flat list for the
choices, rather than a list of pairs of (`value`, `description`).
choices, rather than a list of pairs of (`value`, `description`).
"""
"""
valid_
mapping
s
=
{
valid_
input
s
=
{
'poor'
:
'poor'
,
'poor'
:
'poor'
,
'medium'
:
'medium'
,
'medium'
:
'medium'
,
'good'
:
'good'
,
'good'
:
'good'
,
}
}
invalid_
mapping
s
=
{
invalid_
input
s
=
{
'awful'
:
[
'`awful` is not a valid choice.'
]
'awful'
:
[
'`awful` is not a valid choice.'
]
}
}
outputs
=
{}
field
=
fields
.
ChoiceField
(
choices
=
(
'poor'
,
'medium'
,
'good'
))
field
=
fields
.
ChoiceField
(
choices
=
(
'poor'
,
'medium'
,
'good'
))
class
TestMultipleChoiceField
(
ValidAndInvali
dValues
):
class
TestMultipleChoiceField
(
Fiel
dValues
):
"""
"""
Valid and invalid values for `MultipleChoiceField`.
Valid and invalid values for `MultipleChoiceField`.
"""
"""
valid_
mapping
s
=
{
valid_
input
s
=
{
():
set
(),
():
set
(),
(
'aircon'
,):
set
([
'aircon'
]),
(
'aircon'
,):
set
([
'aircon'
]),
(
'aircon'
,
'manual'
):
set
([
'aircon'
,
'manual'
]),
(
'aircon'
,
'manual'
):
set
([
'aircon'
,
'manual'
]),
}
}
invalid_
mapping
s
=
{
invalid_
input
s
=
{
'abc'
:
[
'Expected a list of items but got type `str`'
],
'abc'
:
[
'Expected a list of items but got type `str`'
],
(
'aircon'
,
'incorrect'
):
[
'`incorrect` is not a valid choice.'
]
(
'aircon'
,
'incorrect'
):
[
'`incorrect` is not a valid choice.'
]
}
}
outputs
=
{}
field
=
fields
.
MultipleChoiceField
(
field
=
fields
.
MultipleChoiceField
(
choices
=
[
choices
=
[
(
'aircon'
,
'AirCon'
),
(
'aircon'
,
'AirCon'
),
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment