Ensure that dict types work as expected.
(self)
| 72 | post.validate() |
| 73 | |
| 74 | def test_general_things(self): |
| 75 | """Ensure that dict types work as expected.""" |
| 76 | |
| 77 | class BlogPost(Document): |
| 78 | info = DictField() |
| 79 | |
| 80 | BlogPost.drop_collection() |
| 81 | |
| 82 | post = BlogPost(info={"title": "test"}) |
| 83 | post.save() |
| 84 | |
| 85 | post = BlogPost() |
| 86 | post.info = {"title": "dollar_sign", "details": {"te$t": "test"}} |
| 87 | post.save() |
| 88 | |
| 89 | post = BlogPost() |
| 90 | post.info = {"details": {"test": "test"}} |
| 91 | post.save() |
| 92 | |
| 93 | post = BlogPost() |
| 94 | post.info = {"details": {"test": 3}} |
| 95 | post.save() |
| 96 | |
| 97 | assert BlogPost.objects.count() == 4 |
| 98 | assert BlogPost.objects.filter(info__title__exact="test").count() == 1 |
| 99 | assert BlogPost.objects.filter(info__details__test__exact="test").count() == 1 |
| 100 | |
| 101 | post = BlogPost.objects.filter(info__title__exact="dollar_sign").first() |
| 102 | assert "te$t" in post["info"]["details"] |
| 103 | |
| 104 | # Confirm handles non strings or non existing keys |
| 105 | assert BlogPost.objects.filter(info__details__test__exact=5).count() == 0 |
| 106 | assert BlogPost.objects.filter(info__made_up__test__exact="test").count() == 0 |
| 107 | |
| 108 | post = BlogPost.objects.create(info={"title": "original"}) |
| 109 | post.info.update({"title": "updated"}) |
| 110 | post.save() |
| 111 | post.reload() |
| 112 | assert "updated" == post.info["title"] |
| 113 | |
| 114 | post.info.setdefault("authors", []) |
| 115 | post.save() |
| 116 | post.reload() |
| 117 | assert post.info["authors"] == [] |
| 118 | |
| 119 | def test_dictfield_dump_document_with_inheritance__cls(self): |
| 120 | """Ensure a DictField can handle another document's dump.""" |