|
1 | 1 | import datetime |
2 | | -import unittest |
3 | 2 |
|
4 | 3 | import pytest |
5 | 4 | from bson import SON, DBRef, ObjectId |
|
34 | 33 | StringField, |
35 | 34 | ValidationError, |
36 | 35 | ) |
37 | | -from mongoengine.base import ( |
38 | | - BaseField, |
39 | | - EmbeddedDocumentList, |
40 | | - _DocumentRegistry, |
41 | | -) |
| 36 | +from mongoengine.base import BaseField, EmbeddedDocumentList |
42 | 37 | from mongoengine.base.fields import _no_dereference_for_fields |
43 | 38 | from mongoengine.errors import DeprecatedError |
44 | 39 | from tests.utils import MongoDBTestCase |
@@ -1587,328 +1582,6 @@ class Mother(Document): |
1587 | 1582 | with pytest.raises(ValidationError): |
1588 | 1583 | brother.save() |
1589 | 1584 |
|
1590 | | - def test_generic_reference(self): |
1591 | | - """Ensure that a GenericReferenceField properly dereferences items.""" |
1592 | | - |
1593 | | - class Link(Document): |
1594 | | - title = StringField() |
1595 | | - meta = {"allow_inheritance": False} |
1596 | | - |
1597 | | - class Post(Document): |
1598 | | - title = StringField() |
1599 | | - |
1600 | | - class Bookmark(Document): |
1601 | | - bookmark_object = GenericReferenceField() |
1602 | | - |
1603 | | - Link.drop_collection() |
1604 | | - Post.drop_collection() |
1605 | | - Bookmark.drop_collection() |
1606 | | - |
1607 | | - link_1 = Link(title="Pitchfork") |
1608 | | - link_1.save() |
1609 | | - |
1610 | | - post_1 = Post(title="Behind the Scenes of the Pavement Reunion") |
1611 | | - post_1.save() |
1612 | | - |
1613 | | - bm = Bookmark(bookmark_object=post_1) |
1614 | | - bm.save() |
1615 | | - |
1616 | | - bm = Bookmark.objects(bookmark_object=post_1).first() |
1617 | | - |
1618 | | - assert bm.bookmark_object == post_1 |
1619 | | - assert isinstance(bm.bookmark_object, Post) |
1620 | | - |
1621 | | - bm.bookmark_object = link_1 |
1622 | | - bm.save() |
1623 | | - |
1624 | | - bm = Bookmark.objects(bookmark_object=link_1).first() |
1625 | | - |
1626 | | - assert bm.bookmark_object == link_1 |
1627 | | - assert isinstance(bm.bookmark_object, Link) |
1628 | | - |
1629 | | - def test_generic_reference_list(self): |
1630 | | - """Ensure that a ListField properly dereferences generic references.""" |
1631 | | - |
1632 | | - class Link(Document): |
1633 | | - title = StringField() |
1634 | | - |
1635 | | - class Post(Document): |
1636 | | - title = StringField() |
1637 | | - |
1638 | | - class User(Document): |
1639 | | - bookmarks = ListField(GenericReferenceField()) |
1640 | | - |
1641 | | - Link.drop_collection() |
1642 | | - Post.drop_collection() |
1643 | | - User.drop_collection() |
1644 | | - |
1645 | | - link_1 = Link(title="Pitchfork") |
1646 | | - link_1.save() |
1647 | | - |
1648 | | - post_1 = Post(title="Behind the Scenes of the Pavement Reunion") |
1649 | | - post_1.save() |
1650 | | - |
1651 | | - user = User(bookmarks=[post_1, link_1]) |
1652 | | - user.save() |
1653 | | - |
1654 | | - user = User.objects(bookmarks__all=[post_1, link_1]).first() |
1655 | | - |
1656 | | - assert user.bookmarks[0] == post_1 |
1657 | | - assert user.bookmarks[1] == link_1 |
1658 | | - |
1659 | | - def test_generic_reference_document_not_registered(self): |
1660 | | - """Ensure dereferencing out of the document registry throws a |
1661 | | - `NotRegistered` error. |
1662 | | - """ |
1663 | | - |
1664 | | - class Link(Document): |
1665 | | - title = StringField() |
1666 | | - |
1667 | | - class User(Document): |
1668 | | - bookmarks = ListField(GenericReferenceField()) |
1669 | | - |
1670 | | - Link.drop_collection() |
1671 | | - User.drop_collection() |
1672 | | - |
1673 | | - link_1 = Link(title="Pitchfork") |
1674 | | - link_1.save() |
1675 | | - |
1676 | | - user = User(bookmarks=[link_1]) |
1677 | | - user.save() |
1678 | | - |
1679 | | - # Mimic User and Link definitions being in a different file |
1680 | | - # and the Link model not being imported in the User file. |
1681 | | - _DocumentRegistry.unregister("Link") |
1682 | | - |
1683 | | - user = User.objects.first() |
1684 | | - try: |
1685 | | - user.bookmarks |
1686 | | - raise AssertionError("Link was removed from the registry") |
1687 | | - except NotRegistered: |
1688 | | - pass |
1689 | | - |
1690 | | - def test_generic_reference_is_none(self): |
1691 | | - class Person(Document): |
1692 | | - name = StringField() |
1693 | | - city = GenericReferenceField() |
1694 | | - |
1695 | | - Person.drop_collection() |
1696 | | - |
1697 | | - Person(name="Wilson Jr").save() |
1698 | | - assert repr(Person.objects(city=None)) == "[<Person: Person object>]" |
1699 | | - |
1700 | | - def test_generic_reference_choices(self): |
1701 | | - """Ensure that a GenericReferenceField can handle choices.""" |
1702 | | - |
1703 | | - class Link(Document): |
1704 | | - title = StringField() |
1705 | | - |
1706 | | - class Post(Document): |
1707 | | - title = StringField() |
1708 | | - |
1709 | | - class Bookmark(Document): |
1710 | | - bookmark_object = GenericReferenceField(choices=(Post,)) |
1711 | | - |
1712 | | - Link.drop_collection() |
1713 | | - Post.drop_collection() |
1714 | | - Bookmark.drop_collection() |
1715 | | - |
1716 | | - link_1 = Link(title="Pitchfork") |
1717 | | - link_1.save() |
1718 | | - |
1719 | | - post_1 = Post(title="Behind the Scenes of the Pavement Reunion") |
1720 | | - post_1.save() |
1721 | | - |
1722 | | - bm = Bookmark(bookmark_object=link_1) |
1723 | | - with pytest.raises(ValidationError): |
1724 | | - bm.validate() |
1725 | | - |
1726 | | - bm = Bookmark(bookmark_object=post_1) |
1727 | | - bm.save() |
1728 | | - |
1729 | | - bm = Bookmark.objects.first() |
1730 | | - assert bm.bookmark_object == post_1 |
1731 | | - |
1732 | | - def test_generic_reference_string_choices(self): |
1733 | | - """Ensure that a GenericReferenceField can handle choices as strings""" |
1734 | | - |
1735 | | - class Link(Document): |
1736 | | - title = StringField() |
1737 | | - |
1738 | | - class Post(Document): |
1739 | | - title = StringField() |
1740 | | - |
1741 | | - class Bookmark(Document): |
1742 | | - bookmark_object = GenericReferenceField(choices=("Post", Link)) |
1743 | | - |
1744 | | - Link.drop_collection() |
1745 | | - Post.drop_collection() |
1746 | | - Bookmark.drop_collection() |
1747 | | - |
1748 | | - link_1 = Link(title="Pitchfork") |
1749 | | - link_1.save() |
1750 | | - |
1751 | | - post_1 = Post(title="Behind the Scenes of the Pavement Reunion") |
1752 | | - post_1.save() |
1753 | | - |
1754 | | - bm = Bookmark(bookmark_object=link_1) |
1755 | | - bm.save() |
1756 | | - |
1757 | | - bm = Bookmark(bookmark_object=post_1) |
1758 | | - bm.save() |
1759 | | - |
1760 | | - bm = Bookmark(bookmark_object=bm) |
1761 | | - with pytest.raises(ValidationError): |
1762 | | - bm.validate() |
1763 | | - |
1764 | | - def test_generic_reference_choices_no_dereference(self): |
1765 | | - """Ensure that a GenericReferenceField can handle choices on |
1766 | | - non-derefenreced (i.e. DBRef) elements |
1767 | | - """ |
1768 | | - |
1769 | | - class Post(Document): |
1770 | | - title = StringField() |
1771 | | - |
1772 | | - class Bookmark(Document): |
1773 | | - bookmark_object = GenericReferenceField(choices=(Post,)) |
1774 | | - other_field = StringField() |
1775 | | - |
1776 | | - Post.drop_collection() |
1777 | | - Bookmark.drop_collection() |
1778 | | - |
1779 | | - post_1 = Post(title="Behind the Scenes of the Pavement Reunion") |
1780 | | - post_1.save() |
1781 | | - |
1782 | | - bm = Bookmark(bookmark_object=post_1) |
1783 | | - bm.save() |
1784 | | - |
1785 | | - bm = Bookmark.objects.get(id=bm.id) |
1786 | | - # bookmark_object is now a DBRef |
1787 | | - bm.other_field = "dummy_change" |
1788 | | - bm.save() |
1789 | | - |
1790 | | - def test_generic_reference_list_choices(self): |
1791 | | - """Ensure that a ListField properly dereferences generic references and |
1792 | | - respects choices. |
1793 | | - """ |
1794 | | - |
1795 | | - class Link(Document): |
1796 | | - title = StringField() |
1797 | | - |
1798 | | - class Post(Document): |
1799 | | - title = StringField() |
1800 | | - |
1801 | | - class User(Document): |
1802 | | - bookmarks = ListField(GenericReferenceField(choices=(Post,))) |
1803 | | - |
1804 | | - Link.drop_collection() |
1805 | | - Post.drop_collection() |
1806 | | - User.drop_collection() |
1807 | | - |
1808 | | - link_1 = Link(title="Pitchfork") |
1809 | | - link_1.save() |
1810 | | - |
1811 | | - post_1 = Post(title="Behind the Scenes of the Pavement Reunion") |
1812 | | - post_1.save() |
1813 | | - |
1814 | | - user = User(bookmarks=[link_1]) |
1815 | | - with pytest.raises(ValidationError): |
1816 | | - user.validate() |
1817 | | - |
1818 | | - user = User(bookmarks=[post_1]) |
1819 | | - user.save() |
1820 | | - |
1821 | | - user = User.objects.first() |
1822 | | - assert user.bookmarks == [post_1] |
1823 | | - |
1824 | | - def test_generic_reference_list_item_modification(self): |
1825 | | - """Ensure that modifications of related documents (through generic reference) don't influence on querying""" |
1826 | | - |
1827 | | - class Post(Document): |
1828 | | - title = StringField() |
1829 | | - |
1830 | | - class User(Document): |
1831 | | - username = StringField() |
1832 | | - bookmarks = ListField(GenericReferenceField()) |
1833 | | - |
1834 | | - Post.drop_collection() |
1835 | | - User.drop_collection() |
1836 | | - |
1837 | | - post_1 = Post(title="Behind the Scenes of the Pavement Reunion") |
1838 | | - post_1.save() |
1839 | | - |
1840 | | - user = User(bookmarks=[post_1]) |
1841 | | - user.save() |
1842 | | - |
1843 | | - post_1.title = "Title was modified" |
1844 | | - user.username = "New username" |
1845 | | - user.save() |
1846 | | - |
1847 | | - user = User.objects(bookmarks__all=[post_1]).first() |
1848 | | - |
1849 | | - assert user is not None |
1850 | | - assert user.bookmarks[0] == post_1 |
1851 | | - |
1852 | | - def test_generic_reference_filter_by_dbref(self): |
1853 | | - """Ensure we can search for a specific generic reference by |
1854 | | - providing its ObjectId. |
1855 | | - """ |
1856 | | - |
1857 | | - class Doc(Document): |
1858 | | - ref = GenericReferenceField() |
1859 | | - |
1860 | | - Doc.drop_collection() |
1861 | | - |
1862 | | - doc1 = Doc.objects.create() |
1863 | | - doc2 = Doc.objects.create(ref=doc1) |
1864 | | - |
1865 | | - doc = Doc.objects.get(ref=DBRef("doc", doc1.pk)) |
1866 | | - assert doc == doc2 |
1867 | | - |
1868 | | - def test_generic_reference_is_not_tracked_in_parent_doc(self): |
1869 | | - """Ensure that modifications of related documents (through generic reference) don't influence |
1870 | | - the owner changed fields (#1934) |
1871 | | - """ |
1872 | | - |
1873 | | - class Doc1(Document): |
1874 | | - name = StringField() |
1875 | | - |
1876 | | - class Doc2(Document): |
1877 | | - ref = GenericReferenceField() |
1878 | | - refs = ListField(GenericReferenceField()) |
1879 | | - |
1880 | | - Doc1.drop_collection() |
1881 | | - Doc2.drop_collection() |
1882 | | - |
1883 | | - doc1 = Doc1(name="garbage1").save() |
1884 | | - doc11 = Doc1(name="garbage11").save() |
1885 | | - doc2 = Doc2(ref=doc1, refs=[doc11]).save() |
1886 | | - |
1887 | | - doc2.ref.name = "garbage2" |
1888 | | - assert doc2._get_changed_fields() == [] |
1889 | | - |
1890 | | - doc2.refs[0].name = "garbage3" |
1891 | | - assert doc2._get_changed_fields() == [] |
1892 | | - assert doc2._delta() == ({}, {}) |
1893 | | - |
1894 | | - def test_generic_reference_field(self): |
1895 | | - """Ensure we can search for a specific generic reference by |
1896 | | - providing its DBRef. |
1897 | | - """ |
1898 | | - |
1899 | | - class Doc(Document): |
1900 | | - ref = GenericReferenceField() |
1901 | | - |
1902 | | - Doc.drop_collection() |
1903 | | - |
1904 | | - doc1 = Doc.objects.create() |
1905 | | - doc2 = Doc.objects.create(ref=doc1) |
1906 | | - |
1907 | | - assert isinstance(doc1.pk, ObjectId) |
1908 | | - |
1909 | | - doc = Doc.objects.get(ref=doc1.pk) |
1910 | | - assert doc == doc2 |
1911 | | - |
1912 | 1585 | def test_choices_allow_using_sets_as_choices(self): |
1913 | 1586 | """Ensure that sets can be used when setting choices""" |
1914 | 1587 |
|
@@ -2761,7 +2434,3 @@ class Group(Document): |
2761 | 2434 | group = Group.objects.first() |
2762 | 2435 | with _no_dereference_for_fields(group._fields["member"]): |
2763 | 2436 | assert isinstance(group.member, DBRef) |
2764 | | - |
2765 | | - |
2766 | | -if __name__ == "__main__": |
2767 | | - unittest.main() |
0 commit comments