示例#1
0
 def test_float_serialization(self):
     """Tests that float values serialize and deserialize intact"""
     sc = Score(score=3.4)
     sc.save()
     serial_str = serializers.serialize(self.serializer_name, [sc])
     deserial_objs = list(
         serializers.deserialize(self.serializer_name, serial_str))
     self.assertEqual(deserial_objs[0].object.score,
                      Approximate(3.4, places=1))
示例#2
0
    def test_lefthand_addition(self):
        # LH Addition of floats and integers
        Number.objects.filter(pk=self.n.pk).update(integer=F('integer') + 15,
                                                   float=F('float') + 42.7)

        self.assertEqual(Number.objects.get(pk=self.n.pk).integer, 57)
        self.assertEqual(
            Number.objects.get(pk=self.n.pk).float,
            Approximate(58.200, places=3))
示例#3
0
    def test_right_hand_division(self):
        # RH Division of floats and integers
        Number.objects.filter(pk=self.n.pk).update(integer=640 / F('integer'),
                                                   float=42.7 / F('float'))

        self.assertEqual(Number.objects.get(pk=self.n.pk).integer, 15)
        self.assertEqual(
            Number.objects.get(pk=self.n.pk).float, Approximate(2.755,
                                                                places=3))
示例#4
0
 def test_righthand_power(self):
     # RH Powert arithmetic operation on floats and integers
     Number.objects.filter(pk=self.n.pk).update(integer=2**F('integer'),
                                                float=1.5**F('float'))
     self.assertEqual(
         Number.objects.get(pk=self.n.pk).integer, 4398046511104)
     self.assertEqual(
         Number.objects.get(pk=self.n.pk).float,
         Approximate(536.308, places=3))
示例#5
0
    def test_right_hand_subtraction(self):
        Number.objects.filter(pk=self.n.pk).update(integer=15 - F('integer'),
                                                   float=42.7 - F('float'))

        # RH Subtraction of floats and integers
        self.assertEqual(Number.objects.get(pk=self.n.pk).integer, -27)
        self.assertEqual(
            Number.objects.get(pk=self.n.pk).float,
            Approximate(27.200, places=3))
示例#6
0
    def test_right_hand_multiplication(self):
        # RH Multiplication of floats and integers
        Number.objects.filter(pk=self.n.pk).update(integer=15 * F('integer'),
                                                   float=42.7 * F('float'))

        self.assertEqual(Number.objects.get(pk=self.n.pk).integer, 630)
        self.assertEqual(
            Number.objects.get(pk=self.n.pk).float,
            Approximate(661.850, places=3))
示例#7
0
    def test_lefthand_bitwise_or(self):
        # LH Bitwise or on integers
        Number.objects.filter(pk=self.n.pk).update(
            integer=F('integer').bitor(48))

        self.assertEqual(Number.objects.get(pk=self.n.pk).integer, 58)
        self.assertEqual(
            Number.objects.get(pk=self.n.pk).float,
            Approximate(15.500, places=3))
示例#8
0
    def test_lefthand_division(self):
        # LH Division of floats and integers
        Number.objects.filter(pk=self.n.pk).update(integer=F('integer') / 2,
                                                   float=F('float') / 42.7)

        self.assertEqual(Number.objects.get(pk=self.n.pk).integer, 21)
        self.assertEqual(
            Number.objects.get(pk=self.n.pk).float, Approximate(0.363,
                                                                places=3))
示例#9
0
    def test_lefthand_multiplication(self):
        # Multiplication of floats and integers
        Number.objects.filter(pk=self.n.pk).update(integer=F('integer') * 15,
                                                   float=F('float') * 42.7)

        self.assertEqual(Number.objects.get(pk=self.n.pk).integer, 630)
        self.assertEqual(
            Number.objects.get(pk=self.n.pk).float,
            Approximate(661.850, places=3))
示例#10
0
    def test_lefthand_subtraction(self):
        # LH Subtraction of floats and integers
        Number.objects.filter(pk=self.n.pk).update(integer=F('integer') - 15,
                                                   float=F('float') - 42.7)

        self.assertEqual(Number.objects.get(pk=self.n.pk).integer, 27)
        self.assertEqual(
            Number.objects.get(pk=self.n.pk).float,
            Approximate(-27.200, places=3))
示例#11
0
 def test_annotate_reserved_word(self):
     """
     Regression #18333 - Ensure annotated column name is properly quoted.
     """
     vals = Book.objects.annotate(select=Count('authors__id')).aggregate(Sum('select'), Avg('select'))
     self.assertEqual(vals, {
         'select__sum': 10,
         'select__avg': Approximate(1.666, places=2),
     })
示例#12
0
    def test_complex_expressions(self):
        """
        Complex expressions of different connection types are possible.
        """
        n = Number.objects.create(integer=10, float=123.45)
        self.assertEqual(Number.objects.filter(pk=n.pk)
            .update(float=F('integer') + F('float') * 2), 1)

        self.assertEqual(Number.objects.get(pk=n.pk).integer, 10)
        self.assertEqual(Number.objects.get(pk=n.pk).float, Approximate(256.900, places=3))
示例#13
0
    def test_related_aggregate(self):
        vals = Author.objects.aggregate(Avg("friends__age"))
        self.assertEqual(vals,
                         {'friends__age__avg': Approximate(34.07, places=2)})

        vals = Book.objects.filter(rating__lt=4.5).aggregate(
            Avg("authors__age"))
        self.assertEqual(vals,
                         {'authors__age__avg': Approximate(38.2857, places=2)})

        vals = Author.objects.all().filter(name__contains="a").aggregate(
            Avg("book__rating"))
        self.assertEqual(vals, {'book__rating__avg': 4.0})

        vals = Book.objects.aggregate(Sum("publisher__num_awards"))
        self.assertEqual(vals, {'publisher__num_awards__sum': 30})

        vals = Publisher.objects.aggregate(Sum("book__price"))
        self.assertEqual(vals, {'book__price__sum': Decimal('270.27')})
示例#14
0
    def test_right_hand_addition(self):
        # Right hand operators
        Number.objects.filter(pk=self.n.pk).update(integer=15 + F('integer'),
                                                   float=42.7 + F('float'))

        # RH Addition of floats and integers
        self.assertEqual(Number.objects.get(pk=self.n.pk).integer, 57)
        self.assertEqual(
            Number.objects.get(pk=self.n.pk).float,
            Approximate(58.200, places=3))
示例#15
0
    def test_combine_different_types(self):
        with self.assertRaisesMessage(FieldError, 'Expression contains mixed types. You must set output_field'):
            Book.objects.annotate(sums=Sum('rating') + Sum('pages') + Sum('price')).get(pk=self.b4.pk)

        b1 = Book.objects.annotate(sums=Sum(F('rating') + F('pages') + F('price'),
                                   output_field=IntegerField())).get(pk=self.b4.pk)
        self.assertEqual(b1.sums, 383)

        b2 = Book.objects.annotate(sums=Sum(F('rating') + F('pages') + F('price'),
                                   output_field=FloatField())).get(pk=self.b4.pk)
        self.assertEqual(b2.sums, 383.69)

        b3 = Book.objects.annotate(sums=Sum(F('rating') + F('pages') + F('price'),
                                   output_field=DecimalField())).get(pk=self.b4.pk)
        self.assertEqual(b3.sums, Approximate(Decimal("383.69"), places=2))
示例#16
0
文件: tests.py 项目: trudger/django
    def test_aggrate_annotation(self):
        # Aggregates can be composed over annotations.
        # The return type is derived from the composed aggregate
        vals = Book.objects.all().annotate(num_authors=Count('authors__id')).aggregate(Max('pages'), Max('price'), Sum('num_authors'), Avg('num_authors'))
        self.assertEqual(vals, {
            'num_authors__sum': 10,
            'num_authors__avg': Approximate(1.666, places=2),
            'pages__max': 1132,
            'price__max': Decimal("82.80")
        })

        # Regression for #15624 - Missing SELECT columns when using values, annotate
        # and aggregate in a single query
        self.assertEqual(
            Book.objects.annotate(c=Count('authors')).values('c').aggregate(Max('c')),
            {'c__max': 3}
        )
示例#17
0
    def test_annotate_m2m(self):
        books = Book.objects.filter(rating__lt=4.5).annotate(
            Avg("authors__age")).order_by("name")
        self.assertQuerysetEqual(
            books,
            [('Artificial Intelligence: A Modern Approach', 51.5),
             ('Practical Django Projects', 29.0),
             ('Python Web Development with Django', Approximate(30.3,
                                                                places=1)),
             ('Sams Teach Yourself Django in 24 Hours', 45.0)],
            lambda b: (b.name, b.authors__age__avg),
        )

        books = Book.objects.annotate(
            num_authors=Count("authors")).order_by("name")
        self.assertQuerysetEqual(books, [
            ('Artificial Intelligence: A Modern Approach', 2),
            ('Paradigms of Artificial Intelligence Programming: Case Studies in Common Lisp',
             1), ('Practical Django Projects', 1),
            ('Python Web Development with Django', 3),
            ('Sams Teach Yourself Django in 24 Hours', 1),
            ('The Definitive Guide to Django: Web Development Done Right', 2)
        ], lambda b: (b.name, b.num_authors))
示例#18
0
    def test_order_of_precedence(self):
        p1 = Book.objects.filter(rating=4).aggregate(avg_price=(Avg('price') + 2) * 3)
        self.assertEqual(p1, {'avg_price': Approximate(Decimal('148.18'), places=2)})

        p2 = Book.objects.filter(rating=4).aggregate(avg_price=Avg('price') + 2 * 3)
        self.assertEqual(p2, {'avg_price': Approximate(Decimal('53.39'), places=2)})
示例#19
0
 def test_avg_decimal_field(self):
     v = Book.objects.filter(rating=4).aggregate(avg_price=(Avg('price')))['avg_price']
     self.assertIsInstance(v, Decimal)
     self.assertEqual(v, Approximate(Decimal('47.39'), places=2))
示例#20
0
 def test_aggregate_annotation(self):
     vals = Book.objects.annotate(num_authors=Count("authors__id")).aggregate(Avg("num_authors"))
     self.assertEqual(vals, {"num_authors__avg": Approximate(1.66, places=1)})
示例#21
0
    def test_annotate_values(self):
        books = list(Book.objects.filter(pk=self.b1.pk).annotate(mean_age=Avg("authors__age")).values())
        self.assertEqual(
            books, [
                {
                    "contact_id": self.a1.id,
                    "id": self.b1.id,
                    "isbn": "159059725",
                    "mean_age": 34.5,
                    "name": "The Definitive Guide to Django: Web Development Done Right",
                    "pages": 447,
                    "price": Approximate(Decimal("30")),
                    "pubdate": datetime.date(2007, 12, 6),
                    "publisher_id": self.p1.id,
                    "rating": 4.5,
                }
            ]
        )

        books = (
            Book.objects
            .filter(pk=self.b1.pk)
            .annotate(mean_age=Avg('authors__age'))
            .values('pk', 'isbn', 'mean_age')
        )
        self.assertEqual(
            list(books), [
                {
                    "pk": self.b1.pk,
                    "isbn": "159059725",
                    "mean_age": 34.5,
                }
            ]
        )

        books = Book.objects.filter(pk=self.b1.pk).annotate(mean_age=Avg("authors__age")).values("name")
        self.assertEqual(
            list(books),
            [{'name': 'The Definitive Guide to Django: Web Development Done Right'}],
        )

        books = Book.objects.filter(pk=self.b1.pk).values().annotate(mean_age=Avg('authors__age'))
        self.assertEqual(
            list(books), [
                {
                    "contact_id": self.a1.id,
                    "id": self.b1.id,
                    "isbn": "159059725",
                    "mean_age": 34.5,
                    "name": "The Definitive Guide to Django: Web Development Done Right",
                    "pages": 447,
                    "price": Approximate(Decimal("30")),
                    "pubdate": datetime.date(2007, 12, 6),
                    "publisher_id": self.p1.id,
                    "rating": 4.5,
                }
            ]
        )

        books = (
            Book.objects
            .values("rating")
            .annotate(n_authors=Count("authors__id"), mean_age=Avg("authors__age"))
            .order_by("rating")
        )
        self.assertEqual(
            list(books), [
                {
                    "rating": 3.0,
                    "n_authors": 1,
                    "mean_age": 45.0,
                },
                {
                    "rating": 4.0,
                    "n_authors": 6,
                    "mean_age": Approximate(37.16, places=1)
                },
                {
                    "rating": 4.5,
                    "n_authors": 2,
                    "mean_age": 34.5,
                },
                {
                    "rating": 5.0,
                    "n_authors": 1,
                    "mean_age": 57.0,
                }
            ]
        )

        authors = Author.objects.annotate(Avg("friends__age")).order_by("name")
        self.assertQuerysetEqual(
            authors, [
                ('Adrian Holovaty', 32.0),
                ('Brad Dayley', None),
                ('Jacob Kaplan-Moss', 29.5),
                ('James Bennett', 34.0),
                ('Jeffrey Forcier', 27.0),
                ('Paul Bissex', 31.0),
                ('Peter Norvig', 46.0),
                ('Stuart Russell', 57.0),
                ('Wesley J. Chun', Approximate(33.66, places=1))
            ],
            lambda a: (a.name, a.friends__age__avg)
        )
示例#22
0
 def test_multiple_aggregates(self):
     vals = Author.objects.aggregate(Sum("age"), Avg("age"))
     self.assertEqual(vals, {"age__sum": 337, "age__avg": Approximate(37.4, places=1)})
示例#23
0
 def test_single_aggregate(self):
     vals = Author.objects.aggregate(Avg("age"))
     self.assertEqual(vals, {"age__avg": Approximate(37.4, places=1)})
示例#24
0
    def test_right_hand_modulo(self):
        # RH Modulo arithmetic on integers
        Number.objects.filter(pk=self.n.pk).update(integer=69 % F('integer'))

        self.assertEqual(Number.objects.get(pk=self.n.pk).integer, 27)
        self.assertEqual(Number.objects.get(pk=self.n.pk).float, Approximate(15.500, places=3))
示例#25
0
 def test_lefthand_power(self):
     # LH Powert arithmetic operation on floats and integers
     Number.objects.filter(pk=self.n.pk).update(integer=F('integer') ** 2,
                                             float=F('float') ** 1.5)
     self.assertEqual(Number.objects.get(pk=self.n.pk).integer, 1764)
     self.assertEqual(Number.objects.get(pk=self.n.pk).float, Approximate(61.02, places=2))
示例#26
0
    def test_lefthand_modulo(self):
        # LH Modulo arithmetic on integers
        Number.objects.filter(pk=self.n.pk).update(integer=F('integer') % 20)

        self.assertEqual(Number.objects.get(pk=self.n.pk).integer, 2)
        self.assertEqual(Number.objects.get(pk=self.n.pk).float, Approximate(15.500, places=3))
示例#27
0
文件: tests.py 项目: MaximusV/django
    def test_more_more(self):
        # Regression for #10113 - Fields mentioned in order_by() must be
        # included in the GROUP BY. This only becomes a problem when the
        # order_by introduces a new join.
        self.assertQuerysetEqual(
            Book.objects.annotate(num_authors=Count('authors')).order_by(
                'publisher__name', 'name'),
            [
                "Practical Django Projects",
                "The Definitive Guide to Django: Web Development Done Right",
                "Paradigms of Artificial Intelligence Programming: Case Studies in Common Lisp",
                "Artificial Intelligence: A Modern Approach",
                "Python Web Development with Django",
                "Sams Teach Yourself Django in 24 Hours",
            ], lambda b: b.name)

        # Regression for #10127 - Empty select_related() works with annotate
        qs = Book.objects.filter(rating__lt=4.5).select_related().annotate(
            Avg('authors__age'))
        self.assertQuerysetEqual(
            qs,
            [('Artificial Intelligence: A Modern Approach', 51.5,
              'Prentice Hall', 'Peter Norvig'),
             ('Practical Django Projects', 29.0, 'Apress', 'James Bennett'),
             ('Python Web Development with Django',
              Approximate(30.333,
                          places=2), 'Prentice Hall', 'Jeffrey Forcier'),
             ('Sams Teach Yourself Django in 24 Hours', 45.0, 'Sams',
              'Brad Dayley')], lambda b:
            (b.name, b.authors__age__avg, b.publisher.name, b.contact.name))

        # Regression for #10132 - If the values() clause only mentioned extra
        # (select=) columns, those columns are used for grouping
        qs = Book.objects.extra(select={
            'pub': 'publisher_id'
        }).values('pub').annotate(Count('id')).order_by('pub')
        self.assertQuerysetEqual(qs, [{
            'pub': 1,
            'id__count': 2
        }, {
            'pub': 2,
            'id__count': 1
        }, {
            'pub': 3,
            'id__count': 2
        }, {
            'pub': 4,
            'id__count': 1
        }], lambda b: b)

        qs = Book.objects.extra(select={
            'pub': 'publisher_id',
            'foo': 'pages'
        }).values('pub').annotate(Count('id')).order_by('pub')
        self.assertQuerysetEqual(qs, [{
            'pub': 1,
            'id__count': 2
        }, {
            'pub': 2,
            'id__count': 1
        }, {
            'pub': 3,
            'id__count': 2
        }, {
            'pub': 4,
            'id__count': 1
        }], lambda b: b)

        # Regression for #10182 - Queries with aggregate calls are correctly
        # realiased when used in a subquery
        ids = Book.objects.filter(pages__gt=100).annotate(
            n_authors=Count('authors')).filter(
                n_authors__gt=2).order_by('n_authors')
        self.assertQuerysetEqual(Book.objects.filter(id__in=ids), [
            "Python Web Development with Django",
        ], lambda b: b.name)

        # Regression for #15709 - Ensure each group_by field only exists once
        # per query
        qs = Book.objects.values('publisher').annotate(
            max_pages=Max('pages')).order_by()
        grouping, gb_params = qs.query.get_compiler(qs.db).get_grouping([], [])
        self.assertEqual(len(grouping), 1)
示例#28
0
 def test_aggregate_alias(self):
     vals = Store.objects.filter(name="Amazon.com").aggregate(amazon_mean=Avg("books__rating"))
     self.assertEqual(vals, {'amazon_mean': Approximate(4.08, places=2)})
示例#29
0
文件: tests.py 项目: MaximusV/django
    def test_stddev(self):
        self.assertEqual(Book.objects.aggregate(StdDev('pages')),
                         {'pages__stddev': Approximate(311.46, 1)})

        self.assertEqual(Book.objects.aggregate(StdDev('rating')),
                         {'rating__stddev': Approximate(0.60, 1)})

        self.assertEqual(Book.objects.aggregate(StdDev('price')),
                         {'price__stddev': Approximate(24.16, 2)})

        self.assertEqual(Book.objects.aggregate(StdDev('pages', sample=True)),
                         {'pages__stddev': Approximate(341.19, 2)})

        self.assertEqual(Book.objects.aggregate(StdDev('rating', sample=True)),
                         {'rating__stddev': Approximate(0.66, 2)})

        self.assertEqual(Book.objects.aggregate(StdDev('price', sample=True)),
                         {'price__stddev': Approximate(26.46, 1)})

        self.assertEqual(Book.objects.aggregate(Variance('pages')),
                         {'pages__variance': Approximate(97010.80, 1)})

        self.assertEqual(Book.objects.aggregate(Variance('rating')),
                         {'rating__variance': Approximate(0.36, 1)})

        self.assertEqual(Book.objects.aggregate(Variance('price')),
                         {'price__variance': Approximate(583.77, 1)})

        self.assertEqual(
            Book.objects.aggregate(Variance('pages', sample=True)),
            {'pages__variance': Approximate(116412.96, 1)})

        self.assertEqual(
            Book.objects.aggregate(Variance('rating', sample=True)),
            {'rating__variance': Approximate(0.44, 2)})

        self.assertEqual(
            Book.objects.aggregate(Variance('price', sample=True)),
            {'price__variance': Approximate(700.53, 2)})
示例#30
0
 def test_covar_pop_general(self):
     values = StatTestModel.objects.aggregate(
         covarpop=CovarPop(y='int2', x='int1'))
     self.assertEqual(values, {'covarpop': Approximate(-0.66, places=1)})