Exemplo n.º 1
0
 def test_folder_query_set_failures(self):
     with self.assertRaises(ValueError):
         FolderQuerySet('XXX')
     fld_qs = SingleFolderQuerySet(account=self.account, folder=self.account.inbox)
     with self.assertRaises(InvalidField):
         fld_qs.only('XXX')
     with self.assertRaises(InvalidField):
         list(fld_qs.filter(XXX='XXX'))
Exemplo n.º 2
0
 def test_folder_query_set_failures(self):
     with self.assertRaises(TypeError) as e:
         FolderQuerySet("XXX")
     self.assertEqual(
         e.exception.args[0],
         "'folder_collection' 'XXX' must be of type <class 'exchangelib.folders.collections.FolderCollection'>",
     )
     # Test FolderQuerySet._copy_cls()
     self.assertEqual(list(FolderQuerySet(FolderCollection(account=self.account, folders=[])).only("name")), [])
     fld_qs = SingleFolderQuerySet(account=self.account, folder=self.account.inbox)
     with self.assertRaises(InvalidField) as e:
         fld_qs.only("XXX")
     self.assertIn("Unknown field 'XXX' on folders", e.exception.args[0])
     with self.assertRaises(InvalidField) as e:
         list(fld_qs.filter(XXX="XXX"))
     self.assertIn("Unknown field path 'XXX' on folders", e.exception.args[0])
Exemplo n.º 3
0
    def test_get_folders_with_distinguished_id(self):
        # Test that we return an Inbox instance and not a generic Messages or Folder instance when we call GetFolder
        # with a DistinguishedFolderId instance with an ID of Inbox.DISTINGUISHED_FOLDER_ID.
        inbox_folder_id = DistinguishedFolderId(
            id=Inbox.DISTINGUISHED_FOLDER_ID,
            mailbox=Mailbox(email_address=self.account.primary_smtp_address))
        inbox = list(
            GetFolder(account=self.account).call(
                folders=[inbox_folder_id],
                shape='IdOnly',
                additional_fields=[],
            ))[0]
        self.assertIsInstance(inbox, Inbox)

        # Test via SingleFolderQuerySet
        inbox = SingleFolderQuerySet(account=self.account,
                                     folder=inbox_folder_id).resolve()
        self.assertIsInstance(inbox, Inbox)
Exemplo n.º 4
0
    def test_folder_query_set(self):
        # Create a folder hierarchy and test a folder queryset
        #
        # -f0
        #  - f1
        #  - f2
        #    - f21
        #    - f22
        f0 = Folder(parent=self.account.inbox, name=get_random_string(16)).save()
        f1 = Folder(parent=f0, name=get_random_string(16)).save()
        f2 = Folder(parent=f0, name=get_random_string(16)).save()
        f21 = Folder(parent=f2, name=get_random_string(16)).save()
        f22 = Folder(parent=f2, name=get_random_string(16)).save()
        folder_qs = SingleFolderQuerySet(account=self.account, folder=f0)
        try:
            # Test all()
            self.assertSetEqual(
                set(f.name for f in folder_qs.all()),
                {f.name for f in (f1, f2, f21, f22)}
            )

            # Test only()
            self.assertSetEqual(
                set(f.name for f in folder_qs.only('name').all()),
                {f.name for f in (f1, f2, f21, f22)}
            )
            self.assertSetEqual(
                set(f.child_folder_count for f in folder_qs.only('name').all()),
                {None}
            )
            # Test depth()
            self.assertSetEqual(
                set(f.name for f in folder_qs.depth(SHALLOW).all()),
                {f.name for f in (f1, f2)}
            )

            # Test filter()
            self.assertSetEqual(
                set(f.name for f in folder_qs.filter(name=f1.name)),
                {f.name for f in (f1,)}
            )
            self.assertSetEqual(
                set(f.name for f in folder_qs.filter(name__in=[f1.name, f2.name])),
                {f.name for f in (f1, f2)}
            )

            # Test get()
            self.assertEqual(folder_qs.get(id=f2.id).name, f2.name)
            self.assertEqual(folder_qs.get(id=f2.id, changekey=f2.changekey).name, f2.name)
            self.assertEqual(
                folder_qs.get(name=f2.name).child_folder_count,
                2
            )
            self.assertEqual(
                folder_qs.filter(name=f2.name).get().child_folder_count,
                2
            )
            self.assertEqual(
                folder_qs.only('name').get(name=f2.name).name,
                f2.name
            )
            self.assertEqual(
                folder_qs.only('name').get(name=f2.name).child_folder_count,
                None
            )
            with self.assertRaises(DoesNotExist):
                folder_qs.get(name=get_random_string(16))
            with self.assertRaises(MultipleObjectsReturned):
                folder_qs.get()
        finally:
            f0.wipe()
            f0.delete()