Skip to content

Commit 9428d2e

Browse files
committed
edits
1 parent 4371057 commit 9428d2e

File tree

7 files changed

+54
-70
lines changed

7 files changed

+54
-70
lines changed

django_mongodb_backend/forms/fields/array.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ def __init__(
2121
self.delimiter = delimiter
2222
super().__init__(**kwargs)
2323
if (min_length is not None or max_length is not None) and length is not None:
24+
invalid_param = "max_length" if max_length is not None else "min_length"
2425
raise ImproperlyConfigured(
25-
"SimpleArrayField param 'length' cannot be "
26-
"specified with 'max_length' or 'min_length'."
26+
f"The length and {invalid_param} parameters are mutually exclusive."
2727
)
2828
if min_length is not None:
2929
self.min_length = min_length

docs/source/ref/forms.rst

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,11 @@ Stores an :class:`~bson.objectid.ObjectId`.
9393

9494
.. attribute:: length
9595

96-
This is an optional argument which validates that the array reaches at
97-
exactly the stated length.
96+
This is an optional argument which validates that the array contains
97+
the stated number of items.
9898

99-
.. note::
100-
Defining ``length`` along with ``max_length`` or ``min_length``
101-
will raise an exception. Use ``length`` for fixed-length arrays
102-
and ``max_length`` / ``min_length`` for variable-length arrays
103-
with an upper or lower limit.
99+
``length`` may not be specified along with ``max_length`` or
100+
``min_length``.
104101

105102
.. attribute:: max_length
106103

docs/source/ref/models/fields.rst

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,9 @@ Some MongoDB-specific fields are available in ``django_mongodb_backend.fields``.
1111
.. class:: ArrayField(base_field, max_size=None, size=None, **options)
1212

1313
A field for storing lists of data. Most field types can be used, and you
14-
pass another field instance as the :attr:`base_field
15-
<ArrayField.base_field>`. You may also specify a :attr:`max_size
16-
<ArrayField.max_size>` or :attr:`size
17-
<ArrayField.size>`. ``ArrayField`` can be nested to store
18-
multi-dimensional arrays.
14+
pass another field instance as the :attr:`~ArrayField.base_field`. You may
15+
also specify a :attr:`~ArrayField.size` or :attr:`~ArrayField.max_size`.
16+
``ArrayField`` can be nested to store multi-dimensional arrays.
1917

2018
If you give the field a :attr:`~django.db.models.Field.default`, ensure
2119
it's a callable such as ``list`` (for an empty default) or a callable that
@@ -67,16 +65,14 @@ Some MongoDB-specific fields are available in ``django_mongodb_backend.fields``.
6765
If passed, the array will have a maximum size as specified, validated
6866
by forms and model validation, but not enforced by the database.
6967

68+
The ``max_size`` and ``size`` options are mutually exclusive.
69+
7070
.. attribute:: size
7171

7272
This is an optional argument.
7373

74-
If passed, the array will have size as specified, validated by forms and model validation, but not enforced by the database.
75-
76-
.. note::
77-
78-
Defining both ``size`` and ``max_size`` will raise an exception.
79-
Use ``size`` for fixed-length arrays and ``max_size`` for variable-length arrays with an upper limit.
74+
If passed, the array will have size as specified, validated by forms
75+
and model validation, but not enforced by the database.
8076

8177
Querying ``ArrayField``
8278
~~~~~~~~~~~~~~~~~~~~~~~

docs/source/releases/5.1.x.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ Django MongoDB Backend 5.1.x
88
*Unreleased*
99

1010
- Backward-incompatible: :class:`~django_mongodb_backend.fields.ArrayField`\'s
11-
``size`` argument is renamed to ``max_size``.
12-
- Added the ``size`` parameter to :class:`~django_mongodb_backend.fields.ArrayField`
13-
for enforcing fixed-length arrays.
11+
:attr:`~.ArrayField.size` parameter is renamed to
12+
:attr:`~.ArrayField.max_size`. The :attr:`~.ArrayField.size` parameter is now
13+
used to enforce fixed-length arrays.
1414
- Added support for :doc:`database caching </topics/cache>`.
1515
- Fixed ``QuerySet.raw_aggregate()`` field initialization when the document key
1616
order doesn't match the order of the model's fields.

tests/forms_tests_/test_array.py

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -117,44 +117,32 @@ def test_min_length_singular(self):
117117

118118
def test_size_length(self):
119119
field = SimpleArrayField(forms.CharField(max_length=27), length=4)
120-
with self.assertRaises(exceptions.ValidationError) as cm:
120+
msg = "List contains 3 items, it should contain 4."
121+
with self.assertRaisesMessage(exceptions.ValidationError, msg):
121122
field.clean(["a", "b", "c"])
122-
self.assertEqual(
123-
cm.exception.messages[0],
124-
"List contains 3 items, it should contain 4.",
125-
)
126-
with self.assertRaises(exceptions.ValidationError) as cm:
123+
msg = "List contains 5 items, it should contain 4."
124+
with self.assertRaisesMessage(exceptions.ValidationError, msg):
127125
field.clean(["a", "b", "c", "d", "e"])
128-
self.assertEqual(
129-
cm.exception.messages[0],
130-
"List contains 5 items, it should contain 4.",
131-
)
132126

133127
def test_size_length_singular(self):
134128
field = SimpleArrayField(forms.CharField(max_length=27), length=4)
135-
with self.assertRaises(exceptions.ValidationError) as cm:
129+
msg = "List contains 1 item, it should contain 4."
130+
with self.assertRaisesMessage(exceptions.ValidationError, msg):
136131
field.clean(["a"])
137-
self.assertEqual(
138-
cm.exception.messages[0],
139-
"List contains 1 item, it should contain 4.",
140-
)
141132

142133
def test_required(self):
143134
field = SimpleArrayField(forms.CharField(), required=True)
144135
with self.assertRaises(exceptions.ValidationError) as cm:
145136
field.clean("")
146137
self.assertEqual(cm.exception.messages[0], "This field is required.")
147138

148-
def test_misconfigured(self):
149-
msg = (
150-
"SimpleArrayField param 'length' cannot be specified with 'max_length' or 'min_length'."
151-
)
152-
with self.assertRaises(exceptions.ImproperlyConfigured) as cm:
139+
def test_length_and_max_min_length(self):
140+
msg = "The length and max_length parameters are mutually exclusive."
141+
with self.assertRaisesMessage(exceptions.ImproperlyConfigured, msg):
153142
SimpleArrayField(forms.CharField(), max_length=3, length=2)
154-
self.assertEqual(cm.exception.args[0], msg)
155-
with self.assertRaises(exceptions.ImproperlyConfigured) as cm:
143+
msg = "The length and min_length parameters are mutually exclusive."
144+
with self.assertRaisesMessage(exceptions.ImproperlyConfigured, msg):
156145
SimpleArrayField(forms.CharField(), min_length=3, length=2)
157-
self.assertEqual(cm.exception.args[0], msg)
158146

159147
def test_model_field_formfield(self):
160148
model_field = ArrayField(models.CharField(max_length=27))

tests/model_fields_/test_arrayfield.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -830,12 +830,9 @@ def test_with_max_size_singular(self):
830830
def test_with_size(self):
831831
field = ArrayField(models.IntegerField(), size=3)
832832
field.clean([1, 2, 3], None)
833-
with self.assertRaises(exceptions.ValidationError) as cm:
833+
msg = "List contains 4 items, it should contain 3."
834+
with self.assertRaisesMessage(exceptions.ValidationError, msg):
834835
field.clean([1, 2, 3, 4], None)
835-
self.assertEqual(
836-
cm.exception.messages[0],
837-
"List contains 4 items, it should contain 3.",
838-
)
839836

840837
def test_with_size_singular(self):
841838
field = ArrayField(models.IntegerField(), size=2)

tests/validators_/tests.py

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,28 @@
44
from django_mongodb_backend.validators import LengthValidator
55

66

7-
class TestValidators(SimpleTestCase):
8-
def test_validators(self):
9-
validator = LengthValidator(10)
10-
with self.assertRaises(ValidationError) as context_manager:
11-
validator([])
12-
self.assertEqual(
13-
context_manager.exception.messages, ["List contains 0 items, it should contain 10."]
14-
)
15-
with self.assertRaises(ValidationError) as context_manager:
16-
validator([1])
17-
self.assertEqual(
18-
context_manager.exception.messages, ["List contains 1 item, it should contain 10."]
19-
)
20-
with self.assertRaises(ValidationError) as context_manager:
21-
validator(list(range(11)))
22-
self.assertEqual(
23-
context_manager.exception.messages, ["List contains 11 items, it should contain 10."]
24-
)
25-
self.assertEqual(validator(list(range(10))), None)
7+
class TestLengthValidator(SimpleTestCase):
8+
validator = LengthValidator(10)
9+
10+
def test_empty(self):
11+
msg = "List contains 0 items, it should contain 10."
12+
with self.assertRaisesMessage(ValidationError, msg):
13+
self.validator([])
14+
15+
def test_singular(self):
16+
msg = "List contains 1 item, it should contain 10."
17+
with self.assertRaisesMessage(ValidationError, msg):
18+
self.validator([1])
19+
20+
def test_too_short(self):
21+
msg = "List contains 9 items, it should contain 10."
22+
with self.assertRaisesMessage(ValidationError, msg):
23+
self.validator([1, 2, 3, 4, 5, 6, 7, 8, 9])
24+
25+
def test_too_long(self):
26+
msg = "List contains 11 items, it should contain 10."
27+
with self.assertRaisesMessage(ValidationError, msg):
28+
self.validator(list(range(11)))
29+
30+
def test_valid(self):
31+
self.assertEqual(self.validator(list(range(10))), None)

0 commit comments

Comments
 (0)