Example #1
0
 def test_cached_queryset_corner_cases(self):
     test_items = []
     for i in range(4):
         item = self.get_test_item()
         item.subject = 'Item %s' % i
         item.save()
         test_items.append(item)
     qs = QuerySet(
         folder_collection=FolderCollection(account=self.account, folders=[self.test_folder])
     ).filter(categories__contains=self.categories).order_by('subject')
     with self.assertRaises(MultipleObjectsReturned):
         qs.get()  # Get with a full cache
     self.assertEqual(qs[2].subject, 'Item 2')  # Index with a full cache
     self.assertEqual(qs[-2].subject, 'Item 2')  # Negative index with a full cache
     qs.delete()  # Delete with a full cache
     self.assertEqual(qs.count(), 0)  # QuerySet is empty after delete
     self.assertEqual(list(qs.none()), [])
Example #2
0
    def test_querysets(self):
        test_items = []
        for i in range(4):
            item = self.get_test_item()
            item.subject = 'Item %s' % i
            item.save()
            test_items.append(item)
        qs = QuerySet(folder_collection=FolderCollection(
            account=self.account, folders=[self.test_folder])).filter(
                categories__contains=self.categories)
        test_cat = self.categories[0]
        self.assertEqual(
            set((i.subject, i.categories[0]) for i in qs),
            {('Item 0', test_cat), ('Item 1', test_cat), ('Item 2', test_cat),
             ('Item 3', test_cat)})
        self.assertEqual([(i.subject, i.categories[0]) for i in qs.none()], [])
        self.assertEqual([(i.subject, i.categories[0])
                          for i in qs.filter(subject__startswith='Item 2')],
                         [('Item 2', test_cat)])
        self.assertEqual(
            set((i.subject, i.categories[0])
                for i in qs.exclude(subject__startswith='Item 2')),
            {('Item 0', test_cat), ('Item 1', test_cat), ('Item 3', test_cat)})
        self.assertEqual(
            set((i.subject, i.categories) for i in qs.only('subject')),
            {('Item 0', None), ('Item 1', None), ('Item 2', None),
             ('Item 3', None)})
        self.assertEqual([(i.subject, i.categories[0])
                          for i in qs.order_by('subject')],
                         [('Item 0', test_cat), ('Item 1', test_cat),
                          ('Item 2', test_cat), ('Item 3', test_cat)])
        self.assertEqual(  # Test '-some_field' syntax for reverse sorting
            [(i.subject, i.categories[0]) for i in qs.order_by('-subject')],
            [('Item 3', test_cat), ('Item 2', test_cat), ('Item 1', test_cat),
             ('Item 0', test_cat)])
        self.assertEqual(  # Test ordering on a field that we don't need to fetch
            [(i.subject, i.categories[0])
             for i in qs.order_by('-subject').only('categories')],
            [(None, test_cat), (None, test_cat), (None, test_cat),
             (None, test_cat)])
        self.assertEqual([(i.subject, i.categories[0])
                          for i in qs.order_by('subject').reverse()],
                         [('Item 3', test_cat), ('Item 2', test_cat),
                          ('Item 1', test_cat), ('Item 0', test_cat)])
        with self.assertRaises(ValueError):
            list(qs.values([]))
        self.assertEqual([i for i in qs.order_by('subject').values('subject')],
                         [{
                             'subject': 'Item 0'
                         }, {
                             'subject': 'Item 1'
                         }, {
                             'subject': 'Item 2'
                         }, {
                             'subject': 'Item 3'
                         }])

        # Test .values() in combinations of 'id' and 'changekey', which are handled specially
        self.assertEqual(list(qs.order_by('subject').values('id')), [{
            'id': i.id
        } for i in test_items])
        self.assertEqual(list(qs.order_by('subject').values('changekey')),
                         [{
                             'changekey': i.changekey
                         } for i in test_items])
        self.assertEqual(
            list(qs.order_by('subject').values('id', 'changekey')),
            [{k: getattr(i, k)
              for k in ('id', 'changekey')} for i in test_items])

        self.assertEqual(set(i for i in qs.values_list('subject')),
                         {('Item 0', ), ('Item 1', ), ('Item 2', ),
                          ('Item 3', )})

        # Test .values_list() in combinations of 'id' and 'changekey', which are handled specially
        self.assertEqual(list(qs.order_by('subject').values_list('id')),
                         [(i.id, ) for i in test_items])
        self.assertEqual(list(qs.order_by('subject').values_list('changekey')),
                         [(i.changekey, ) for i in test_items])
        self.assertEqual(
            list(qs.order_by('subject').values_list('id', 'changekey')),
            [(i.id, i.changekey) for i in test_items])

        self.assertEqual(set(i.subject for i in qs.only('subject')),
                         {'Item 0', 'Item 1', 'Item 2', 'Item 3'})

        # Test .only() in combinations of 'id' and 'changekey', which are handled specially
        self.assertEqual(
            list((i.id, ) for i in qs.order_by('subject').only('id')),
            [(i.id, ) for i in test_items])
        self.assertEqual(
            list((i.changekey, )
                 for i in qs.order_by('subject').only('changekey')),
            [(i.changekey, ) for i in test_items])
        self.assertEqual(
            list((i.id, i.changekey)
                 for i in qs.order_by('subject').only('id', 'changekey')),
            [(i.id, i.changekey) for i in test_items])

        with self.assertRaises(ValueError):
            list(qs.values_list('id', 'changekey', flat=True))
        with self.assertRaises(AttributeError):
            list(qs.values_list('id', xxx=True))
        self.assertEqual(
            list(qs.order_by('subject').values_list('id', flat=True)),
            [i.id for i in test_items])
        self.assertEqual(
            list(qs.order_by('subject').values_list('changekey', flat=True)),
            [i.changekey for i in test_items])
        self.assertEqual(set(i for i in qs.values_list('subject', flat=True)),
                         {'Item 0', 'Item 1', 'Item 2', 'Item 3'})
        self.assertEqual(
            qs.values_list('subject', flat=True).get(subject='Item 2'),
            'Item 2')
        self.assertEqual(
            set((i.subject, i.categories[0])
                for i in qs.exclude(subject__startswith='Item 2')),
            {('Item 0', test_cat), ('Item 1', test_cat), ('Item 3', test_cat)})
        # Test that we can sort on a field that we don't want
        self.assertEqual([
            i.categories[0] for i in qs.only('categories').order_by('subject')
        ], [test_cat, test_cat, test_cat, test_cat])
        # Test iterator
        self.assertEqual(
            set((i.subject, i.categories[0]) for i in qs.iterator()),
            {('Item 0', test_cat), ('Item 1', test_cat), ('Item 2', test_cat),
             ('Item 3', test_cat)})
        # Test that iterator() preserves the result format
        self.assertEqual(
            set((i[0], i[1][0])
                for i in qs.values_list('subject', 'categories').iterator()),
            {('Item 0', test_cat), ('Item 1', test_cat), ('Item 2', test_cat),
             ('Item 3', test_cat)})
        self.assertEqual(qs.get(subject='Item 3').subject, 'Item 3')
        with self.assertRaises(DoesNotExist):
            qs.get(subject='Item XXX')
        with self.assertRaises(MultipleObjectsReturned):
            qs.get(subject__startswith='Item')
        # len() and count()
        self.assertEqual(qs.count(), 4)
        # Indexing and slicing
        self.assertTrue(isinstance(qs[0], self.ITEM_CLASS))
        self.assertEqual(len(list(qs[1:3])), 2)
        self.assertEqual(qs.count(), 4)
        with self.assertRaises(IndexError):
            print(qs[99999])
        # Exists
        self.assertEqual(qs.exists(), True)
        self.assertEqual(qs.filter(subject='Test XXX').exists(), False)
        self.assertEqual(
            qs.filter(subject__startswith='Item').delete(),
            [True, True, True, True])
Example #3
0
    def test_querysets(self):
        test_items = []
        for i in range(4):
            item = self.get_test_item()
            item.subject = f"Item {i}"
            item.save()
            test_items.append(item)
        qs = QuerySet(folder_collection=FolderCollection(
            account=self.account, folders=[self.test_folder])).filter(
                categories__contains=self.categories)
        test_cat = self.categories[0]
        self.assertEqual(
            {(i.subject, i.categories[0])
             for i in qs},
            {("Item 0", test_cat), ("Item 1", test_cat), ("Item 2", test_cat),
             ("Item 3", test_cat)},
        )
        self.assertEqual([(i.subject, i.categories[0]) for i in qs.none()], [])
        self.assertEqual([(i.subject, i.categories[0])
                          for i in qs.filter(subject__startswith="Item 2")],
                         [("Item 2", test_cat)])
        self.assertEqual(
            {(i.subject, i.categories[0])
             for i in qs.exclude(subject__startswith="Item 2")},
            {("Item 0", test_cat), ("Item 1", test_cat), ("Item 3", test_cat)},
        )
        self.assertEqual(
            {(i.subject, i.categories)
             for i in qs.only("subject")},
            {("Item 0", None), ("Item 1", None), ("Item 2", None),
             ("Item 3", None)},
        )
        self.assertEqual(
            [(i.subject, i.categories[0]) for i in qs.order_by("subject")],
            [("Item 0", test_cat), ("Item 1", test_cat), ("Item 2", test_cat),
             ("Item 3", test_cat)],
        )
        self.assertEqual(  # Test '-some_field' syntax for reverse sorting
            [(i.subject, i.categories[0]) for i in qs.order_by("-subject")],
            [("Item 3", test_cat), ("Item 2", test_cat), ("Item 1", test_cat),
             ("Item 0", test_cat)],
        )
        self.assertEqual(  # Test ordering on a field that we don't need to fetch
            [(i.subject, i.categories[0])
             for i in qs.order_by("-subject").only("categories")],
            [(None, test_cat), (None, test_cat), (None, test_cat),
             (None, test_cat)],
        )
        self.assertEqual(
            [(i.subject, i.categories[0])
             for i in qs.order_by("subject").reverse()],
            [("Item 3", test_cat), ("Item 2", test_cat), ("Item 1", test_cat),
             ("Item 0", test_cat)],
        )
        with self.assertRaises(TypeError):
            list(qs.values([]))
        self.assertEqual(
            list(qs.order_by("subject").values("subject")),
            [{
                "subject": "Item 0"
            }, {
                "subject": "Item 1"
            }, {
                "subject": "Item 2"
            }, {
                "subject": "Item 3"
            }],
        )

        # Test .values() in combinations of 'id' and 'changekey', which are handled specially
        self.assertEqual(list(qs.order_by("subject").values("id")), [{
            "id": i.id
        } for i in test_items])
        self.assertEqual(list(qs.order_by("subject").values("changekey")),
                         [{
                             "changekey": i.changekey
                         } for i in test_items])
        self.assertEqual(
            list(qs.order_by("subject").values("id", "changekey")),
            [{k: getattr(i, k)
              for k in ("id", "changekey")} for i in test_items],
        )

        self.assertEqual(set(qs.values_list("subject")), {("Item 0", ),
                                                          ("Item 1", ),
                                                          ("Item 2", ),
                                                          ("Item 3", )})

        # Test .values_list() in combinations of 'id' and 'changekey', which are handled specially
        self.assertEqual(list(qs.order_by("subject").values_list("id")),
                         [(i.id, ) for i in test_items])
        self.assertEqual(list(qs.order_by("subject").values_list("changekey")),
                         [(i.changekey, ) for i in test_items])
        self.assertEqual(
            list(qs.order_by("subject").values_list("id", "changekey")),
            [(i.id, i.changekey) for i in test_items])

        self.assertEqual({i.subject
                          for i in qs.only("subject")},
                         {"Item 0", "Item 1", "Item 2", "Item 3"})

        # Test .only() in combinations of 'id' and 'changekey', which are handled specially
        self.assertEqual([(i.id, ) for i in qs.order_by("subject").only("id")],
                         [(i.id, ) for i in test_items])
        self.assertEqual([(i.changekey, )
                          for i in qs.order_by("subject").only("changekey")],
                         [(i.changekey, ) for i in test_items])
        self.assertEqual(
            [(i.id, i.changekey)
             for i in qs.order_by("subject").only("id", "changekey")],
            [(i.id, i.changekey) for i in test_items],
        )

        with self.assertRaises(ValueError):
            list(qs.values_list("id", "changekey", flat=True))
        with self.assertRaises(AttributeError):
            list(qs.values_list("id", xxx=True))
        self.assertEqual(
            list(qs.order_by("subject").values_list("id", flat=True)),
            [i.id for i in test_items])
        self.assertEqual(
            list(qs.order_by("subject").values_list("changekey", flat=True)),
            [i.changekey for i in test_items])
        self.assertEqual(set(qs.values_list("subject", flat=True)),
                         {"Item 0", "Item 1", "Item 2", "Item 3"})
        self.assertEqual(
            qs.values_list("subject", flat=True).get(subject="Item 2"),
            "Item 2")
        self.assertEqual(
            {(i.subject, i.categories[0])
             for i in qs.exclude(subject__startswith="Item 2")},
            {("Item 0", test_cat), ("Item 1", test_cat), ("Item 3", test_cat)},
        )
        # Test that we can sort on a field that we don't want
        self.assertEqual(
            [
                i.categories[0]
                for i in qs.only("categories").order_by("subject")
            ],
            [test_cat, test_cat, test_cat, test_cat],
        )
        self.assertEqual(qs.get(subject="Item 3").subject, "Item 3")
        with self.assertRaises(DoesNotExist):
            qs.get(subject="Item XXX")
        with self.assertRaises(MultipleObjectsReturned):
            qs.get(subject__startswith="Item")
        # len() and count()
        self.assertEqual(qs.count(), 4)
        # Indexing and slicing
        self.assertIsInstance(qs[0], self.ITEM_CLASS)
        self.assertEqual(len(list(qs[1:3])), 2)
        self.assertEqual(qs.count(), 4)
        with self.assertRaises(IndexError):
            print(qs[99999])
        # Exists
        self.assertEqual(qs.exists(), True)
        self.assertEqual(qs.filter(subject="Test XXX").exists(), False)
        self.assertEqual(
            qs.filter(subject__startswith="Item").delete(),
            [True, True, True, True])