Ensure that an embedded document is properly returned from different manners of querying.
(self)
| 1578 | assert ORDER_BY_KEY not in q.get_ops()[0][CMD_QUERY_KEY] |
| 1579 | |
| 1580 | def test_find_embedded(self): |
| 1581 | """Ensure that an embedded document is properly returned from |
| 1582 | different manners of querying. |
| 1583 | """ |
| 1584 | |
| 1585 | class User(EmbeddedDocument): |
| 1586 | name = StringField() |
| 1587 | |
| 1588 | class BlogPost(Document): |
| 1589 | content = StringField() |
| 1590 | author = EmbeddedDocumentField(User) |
| 1591 | |
| 1592 | BlogPost.drop_collection() |
| 1593 | |
| 1594 | user = User(name="Test User") |
| 1595 | BlogPost.objects.create(author=user, content="Had a good coffee today...") |
| 1596 | |
| 1597 | result = BlogPost.objects.first() |
| 1598 | assert isinstance(result.author, User) |
| 1599 | assert result.author.name == "Test User" |
| 1600 | |
| 1601 | result = BlogPost.objects.get(author__name=user.name) |
| 1602 | assert isinstance(result.author, User) |
| 1603 | assert result.author.name == "Test User" |
| 1604 | |
| 1605 | result = BlogPost.objects.get(author={"name": user.name}) |
| 1606 | assert isinstance(result.author, User) |
| 1607 | assert result.author.name == "Test User" |
| 1608 | |
| 1609 | # Fails, since the string is not a type that is able to represent the |
| 1610 | # author's document structure (should be dict) |
| 1611 | with pytest.raises(InvalidQueryError): |
| 1612 | BlogPost.objects.get(author=user.name) |
| 1613 | |
| 1614 | def test_find_empty_embedded(self): |
| 1615 | """Ensure that you can save and find an empty embedded document.""" |
nothing calls this directly
no test coverage detected