Skip to content

Commit b74b285

Browse files
committed
fixes
1 parent a924ac8 commit b74b285

File tree

5 files changed

+16
-238
lines changed

5 files changed

+16
-238
lines changed

django_mongodb_backend/fields/embedded_model_array.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,13 @@ def deconstruct(self):
2424

2525
def get_db_prep_value(self, value, connection, prepared=False):
2626
if isinstance(value, list | tuple):
27+
# Must call get_db_prep_save() rather than get_db_prep_value()
28+
# to transform model instances to dicts.
2729
return [self.base_field.get_db_prep_save(i, connection) for i in value]
30+
if value is not None:
31+
raise TypeError(
32+
f"Expected list of {self.embedded_model!r} instances, not {type(value)!r}."
33+
)
2834
return value
2935

3036
def formfield(self, **kwargs):

django_mongodb_backend/forms/fields/embedded_model_array.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ def __init__(self, model, prefix, max_length=None, length=None, *args, **kwargs)
2828

2929
def clean(self, value):
3030
if not value:
31-
# TODO: null or empty list?
3231
return []
3332
formset = self.formset(value, prefix=self.prefix)
3433
if not formset.is_valid():

tests/model_fields_/test_embedded_model_array.py

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,12 @@
1-
from django.core.exceptions import FieldError, ValidationError
1+
from django.core.exceptions import FieldError
22
from django.db import models
33
from django.test import SimpleTestCase, TestCase
44
from django.test.utils import isolate_apps
55

66
from django_mongodb_backend.fields import EmbeddedModelArrayField
77
from django_mongodb_backend.models import EmbeddedModel
88

9-
from .models import (
10-
Data,
11-
Holder,
12-
Movie,
13-
Review,
14-
)
9+
from .models import Movie, Review
1510

1611

1712
class MethodTests(SimpleTestCase):
@@ -23,17 +18,14 @@ def test_deconstruct(self):
2318
self.assertEqual(kwargs, {"embedded_model": "Data", "null": True})
2419

2520
def test_get_db_prep_save_invalid(self):
26-
msg = "Expected instance of type <class 'model_fields_.models.Data'>, " "not <class 'int'>."
21+
msg = "Expected list of <class 'model_fields_.models.Review'> instances, not <class 'int'>."
2722
with self.assertRaisesMessage(TypeError, msg):
28-
Holder(data=42).save()
29-
30-
def test_validate(self):
31-
obj = Holder(data=Data(integer=None))
32-
# This isn't quite right because "integer" is the subfield of data
33-
# that's non-null.
34-
msg = "{'data': ['This field cannot be null.']}"
35-
with self.assertRaisesMessage(ValidationError, msg):
36-
obj.full_clean()
23+
Movie(reviews=42).save()
24+
25+
def test_get_db_prep_save_invalid_list(self):
26+
msg = "Expected instance of type <class 'model_fields_.models.Review'>, not <class 'int'>."
27+
with self.assertRaisesMessage(TypeError, msg):
28+
Movie(reviews=[42]).save()
3729

3830

3931
class ModelTests(TestCase):

tests/model_forms_/models.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class Book(models.Model):
2828
publisher = EmbeddedModelField(Publisher)
2929

3030

31+
# EmbeddedModelArrayField
3132
class Review(EmbeddedModel):
3233
title = models.CharField(max_length=255)
3334
rating = models.IntegerField()

tests/model_forms_/test_embedded_model_array.py

Lines changed: 0 additions & 220 deletions
Original file line numberDiff line numberDiff line change
@@ -379,223 +379,3 @@ def test_rendering_initial(self):
379379
id="id_reviews-MIN_NUM_FORMS"><input type="hidden"
380380
name="reviews-MAX_NUM_FORMS" value="1000" id="id_reviews-MAX_NUM_FORMS">""",
381381
)
382-
383-
384-
# class NestedFormTests(TestCase):
385-
# def test_update(self):
386-
# book = Book.objects.create(
387-
# title="Learning MongoDB",
388-
# publisher=Publisher(
389-
# name="Random House", address=Address(city="NYC", state="NY", zip_code="10001")
390-
# ),
391-
# )
392-
# data = {
393-
# "title": "Learning MongoDB!",
394-
# "publisher-name": "Random House!",
395-
# "publisher-address-po_box": "",
396-
# "publisher-address-city": "New York City",
397-
# "publisher-address-state": "NY",
398-
# "publisher-address-zip_code": "10001",
399-
# }
400-
# form = BookForm(data, instance=book)
401-
# self.assertTrue(form.is_valid())
402-
# form.save()
403-
# book.refresh_from_db()
404-
# self.assertEqual(book.title, "Learning MongoDB!")
405-
# self.assertEqual(book.publisher.name, "Random House!")
406-
# self.assertEqual(book.publisher.address.city, "New York City")
407-
408-
# def test_some_missing_data(self):
409-
# """A required field (zip_code) is missing."""
410-
# book = Book.objects.create(
411-
# title="Learning MongoDB",
412-
# publisher=Publisher(
413-
# name="Random House", address=Address(city="NYC", state="NY", zip_code="10001")
414-
# ),
415-
# )
416-
# data = {
417-
# "title": "Learning MongoDB!",
418-
# "publisher-name": "Random House!",
419-
# "publisher-address-po_box": "",
420-
# "publisher-address-city": "New York City",
421-
# "publisher-address-state": "NY",
422-
# "publisher-address-zip_code": "",
423-
# }
424-
# form = BookForm(data, instance=book)
425-
# self.assertFalse(form.is_valid())
426-
# self.assertEqual(form.errors["publisher"], ["Enter all required values."])
427-
# self.assertHTMLEqual(
428-
# str(form),
429-
# """
430-
# <div>
431-
# <label for="id_title">Title:</label>
432-
# <input type="text" name="title" value="Learning MongoDB!" maxlength="50"
433-
# required id="id_title">
434-
# </div>
435-
# <div>
436-
# <fieldset>
437-
# <legend>Publisher:</legend>
438-
# <ul class="errorlist">
439-
# <li>Enter all required values.</li>
440-
# </ul>
441-
# <div>
442-
# <label for="id_publisher-name">Name:</label>
443-
# <input type="text" name="publisher-name" value="Random House!" maxlength="50"
444-
# required id="id_publisher-name">
445-
# </div>
446-
# <div>
447-
# <fieldset>
448-
# <legend>Address:</legend>
449-
# <div>
450-
# <label for="id_publisher-address-po_box">PO Box:</label>
451-
# <input type="text" name="publisher-address-po_box" maxlength="50"
452-
# id="id_publisher-address-po_box">
453-
# </div>
454-
# <div>
455-
# <label for="id_publisher-address-city">City:</label>
456-
# <input type="text" name="publisher-address-city" value="New York City"
457-
# maxlength="20" required id="id_publisher-address-city">
458-
# </div>
459-
# <div>
460-
# <label for="id_publisher-address-state">State:</label>
461-
# <input type="text" name="publisher-address-state" value="NY"
462-
# maxlength="2" required id="id_publisher-address-state">
463-
# </div>
464-
# <div>
465-
# <label for="id_publisher-address-zip_code">Zip code:</label>
466-
# <input type="number" name="publisher-address-zip_code"
467-
# required id="id_publisher-address-zip_code">
468-
# </div>
469-
# </fieldset>
470-
# </div>
471-
# </fieldset>
472-
# </div>""",
473-
# )
474-
475-
# def test_invalid_field_data(self):
476-
# """A field's data (state) is too long."""
477-
# book = Book.objects.create(
478-
# title="Learning MongoDB",
479-
# publisher=Publisher(
480-
# name="Random House", address=Address(city="NYC", state="NY", zip_code="10001")
481-
# ),
482-
# )
483-
# data = {
484-
# "title": "Learning MongoDB!",
485-
# "publisher-name": "Random House!",
486-
# "publisher-address-po_box": "",
487-
# "publisher-address-city": "New York City",
488-
# "publisher-address-state": "TOO LONG",
489-
# "publisher-address-zip_code": "10001",
490-
# }
491-
# form = BookForm(data, instance=book)
492-
# self.assertFalse(form.is_valid())
493-
# self.assertEqual(
494-
# form.errors["publisher"],
495-
# ["Ensure this value has at most 2 characters (it has 8)."],
496-
# )
497-
# self.assertHTMLEqual(
498-
# str(form),
499-
# """
500-
# <div>
501-
# <label for="id_title">Title:</label>
502-
# <input type="text" name="title" value="Learning MongoDB!"
503-
# maxlength="50" required id="id_title">
504-
# </div>
505-
# <div>
506-
# <fieldset>
507-
# <legend>Publisher:</legend>
508-
# <ul class="errorlist">
509-
# <li>Ensure this value has at most 2 characters (it has 8).</li>
510-
# </ul>
511-
# <div>
512-
# <label for="id_publisher-name">Name:</label>
513-
# <input type="text" name="publisher-name" value="Random House!"
514-
# maxlength="50" required id="id_publisher-name">
515-
# </div>
516-
# <div>
517-
# <fieldset>
518-
# <legend>Address:</legend>
519-
# <div>
520-
# <label for="id_publisher-address-po_box">PO Box:</label>
521-
# <input type="text" name="publisher-address-po_box"
522-
# maxlength="50" id="id_publisher-address-po_box">
523-
# </div>
524-
# <div>
525-
# <label for="id_publisher-address-city">City:</label>
526-
# <input type="text" name="publisher-address-city" value="New York City"
527-
# maxlength="20" required id="id_publisher-address-city">
528-
# </div>
529-
# <div>
530-
# <label for="id_publisher-address-state">State:</label>
531-
# <input type="text" name="publisher-address-state" value="TOO LONG"
532-
# maxlength="2" required id="id_publisher-address-state">
533-
# </div>
534-
# <div>
535-
# <label for="id_publisher-address-zip_code">Zip code:</label>
536-
# <input type="number" name="publisher-address-zip_code" value="10001"
537-
# required id="id_publisher-address-zip_code">
538-
# </div>
539-
# </fieldset>
540-
# </div>
541-
# </fieldset>
542-
# </div>""",
543-
# )
544-
545-
# def test_all_missing_data(self):
546-
# """An embedded model with all data missing triggers a required error."""
547-
# book = Book.objects.create(
548-
# title="Learning MongoDB",
549-
# publisher=Publisher(
550-
# name="Random House", address=Address(city="NYC", state="NY", zip_code="10001")
551-
# ),
552-
# )
553-
# data = {
554-
# "title": "Learning MongoDB!",
555-
# "publisher-name": "Random House!",
556-
# "publisher-address-po_box": "",
557-
# "publisher-address-city": "",
558-
# "publisher-address-state": "",
559-
# "publisher-address-zip_code": "",
560-
# }
561-
# form = BookForm(data, instance=book)
562-
# self.assertFalse(form.is_valid())
563-
# self.assertEqual(form.errors["publisher"], ["This field is required."])
564-
565-
# def test_rendering(self):
566-
# form = MovieForm()
567-
# print(str(form.fields["reviews"].get_bound_field(form, "reviews")))
568-
# self.assertHTMLEqual(
569-
# str(form.fields["reviews"].get_bound_field(form, "reviews")),
570-
# """
571-
# <div>
572-
# <label for="id_publisher-name">Name:</label>
573-
# <input type="text" name="publisher-name" maxlength="50"
574-
# required id="id_publisher-name">
575-
# </div>
576-
# <div>
577-
# <fieldset>
578-
# <legend>Address:</legend>
579-
# <div>
580-
# <label for="id_publisher-address-po_box">PO Box:</label>
581-
# <input type="text" name="publisher-address-po_box" maxlength="50"
582-
# id="id_publisher-address-po_box">
583-
# </div>
584-
# <div>
585-
# <label for="id_publisher-address-city">City:</label>
586-
# <input type="text" name="publisher-address-city" maxlength="20"
587-
# required id="id_publisher-address-city">
588-
# </div>
589-
# <div>
590-
# <label for="id_publisher-address-state">State:</label>
591-
# <input type="text" name="publisher-address-state" maxlength="2"
592-
# required id="id_publisher-address-state">
593-
# </div>
594-
# <div>
595-
# <label for="id_publisher-address-zip_code">Zip code:</label>
596-
# <input type="number" name="publisher-address-zip_code"
597-
# required id="id_publisher-address-zip_code">
598-
# </div>
599-
# </fieldset>
600-
# </div>""",
601-
# )

0 commit comments

Comments
 (0)