Пример #1
0
 def test_dq_subquery_filter(self):
     """This subquery is a DjangoQuery subquery."""
     # import ipdb; ipdb.set_trace()
     len1 = len(
         list(DQ('(b.id, b.name) Book{b.id in ["(Book.id)"]} b').dicts()))
     len2 = len(list(DQ("(Book.id)").dicts()))
     self.assertEqual(len1, len2)
Пример #2
0
 def test_deletes(self):
     #  import ipdb; ipdb.set_trace()
     book_count = DQ("(count(Book.id)) Book").value()
     book_id = DQ("(Book.id)").limit(1).value()
     data = {"_model": "books.Book", "_pk": book_id}
     deletes([data])
     book_count_new = DQ("(count(Book.id)) Book").value()
     self.assertEqual(book_count, book_count_new + 1)
Пример #3
0
    def test_function_whitelist(self):
        with self.assertRaises(UnknownFunctionException):
            list(DQ("(b.id, pg_backend_pid()) Book b").tuples())

        with self.assertRaises(UnknownFunctionException):
            list(DQ("(b.id, asdfasdfasdf()) Book b").tuples())

        # this should be fine because `ceil()` is in our whitelist
        list(DQ("(b.id, ceil(b.price)) Book b").tuples())
Пример #4
0
    def test_in_list(self):
        """Test that IN (list) works."""

        # get available ids
        ids = list(DQ("(b.id) Book b").tuples())
        ids = [id[0] for id in ids]

        # take just three of them
        c = {"ids": ids[:3]}
        dq = DQ("(b.id, b.name) Book{b.id in '$(ids)'} b")
        r = list(dq.context(c).dicts())

        # make sure we got three of them
        self.assertEqual(len(r), 3)
Пример #5
0
 def test_complex_expression(self):
     dq = DQ("""
     (Publisher.name, max(Book.price) -
     avg(Book.price) as price_diff) Book b
     """)
     for t in dq.tuples():
         pass
Пример #6
0
 def test_aggregate(self):
     dq = DQ("(Publisher.name as publisher, count(b.id) as count) Book b")
     # make list of dicts
     pubs = [d for d in dq.dicts()]
     self.assertEqual(len(pubs), 2)
     self.assertTrue("publisher" in pubs[0])
     self.assertTrue("count" in pubs[0])
Пример #7
0
 def test_subquery_queryset(self):
     qs = Book.objects.all().only("id")
     ids = [rec.id for rec in qs]
     list(
         DQ("(b.name, b.price) Book{id in '@qs_sub'} b",
            names={
                "qs_sub": ids
            }).tuples())
Пример #8
0
    def test_whitelist(self):

        # make sure it lets us in
        wl = {"books": ["Book"]}
        schema = get_schema(connections["default"], whitelist=wl)
        self.assertIn("books.Book", schema)
        self.assertEqual(len(schema.keys()), 1)
        DQ("(b.name) Book{ilike(b.name, 'A%')} b", whitelist=wl).parse()

        # make sure it keeps us out
        wl = {"django.contrib.auth": ["User"]}
        schema = get_schema(connections["default"], whitelist=wl)
        self.assertIn("django.contrib.auth.User", schema)
        self.assertNotIn("books.Book", schema)
        self.assertEqual(len(schema.keys()), 1)

        wl = {"django.contrib.auth": ["User"]}
        with self.assertRaises(ModelNotFoundException):
            DQ("(b.name) Book{ilike(b.name, 'A%')} b", whitelist=wl).parse()
Пример #9
0
 def test_complex2(self):
     dq = DQ("""(b.name,
     b.price as price,
     0.2 as discount,
     b.price * 0.2 as discount_price,
     b.price - (b.price * 0.2) as diff,
     Publisher.name as publisher
     ) Book{b.price > 7} b""")
     for d in dq.json():
         json.loads(d)
Пример #10
0
    def test_dq_subquery_select(self):
        q = """(
            p.id as id,
            p.name,
            ["(count(b.id)) books.Book{b.publisher==Publisher.id} b"] as cnt
        ) books.Publisher p
        """
        data = list(DQ(q).dicts())
        # one entry for each publisher
        self.assertEqual(len(data), len(Publisher.objects.all()))

        # now check each individual value by getting
        # the same data with a different query
        s = """(
            count(b.id) as cnt
        ) books.Book{Publisher.id=='$(pubid)'} b
        """
        for rec in data:
            v = DQ(s).context({"pubid": rec["id"]}).value()
            self.assertEqual(v, rec["cnt"])
Пример #11
0
 def test_subquery_djangoquery(self):
     dq_sub = DQ("(b.id) Book{name == 'B*'} b", name="dq_sub")  # noqa: F841
     list(DQ("(b.name, b.price) Book{id in '@dq_sub'} b").tuples())
Пример #12
0
 def test_m2m(self):
     results = list(DQ("(b.name, b.authors.name) Book b").dicts())
     self.assertTrue("b_name" in results[0])
     self.assertTrue("b_authors_name" in results[0])
Пример #13
0
 def test_onetoone(self):
     #  import ipdb; ipdb.set_trace()
     results = list(
         DQ("(u.username, u.email, u.profile.company) User u").tuples())
     self.assertTrue(results[0][0] == "artemis")
Пример #14
0
 def test_subquery_2(self):
     pubs = DQ("(p.id) Publisher p", name="pubs")  # noqa: F841
     list(DQ("(b.name) Book{publisher in '@pubs'} b").tuples())
Пример #15
0
 def test_custom_functions(self):
     dq = DQ("""
     (sum(iif(b.rating >= 3, b.rating, 0)) as below_3,
     sum(iif(b.rating > 3, b.rating, 0)) as above_3) Book b
     """)
     list(dq.tuples())
Пример #16
0
 def test_implicit_model(self):
     dq = DQ("(Book.name, Book.id)")
     self.assertEquals(dq.count(), 10)
Пример #17
0
 def test_order_by(self):
     list(
         DQ("(b.name) Book b order by (-b.name, b.publisher, -b.id)").dicts(
         ))
Пример #18
0
 def test_expression_grouping(self):
     list(
         DQ("(b.id, b.name) Book{(b.id == 1 or b.id == 2) and b.id == 3} b "
            ).tuples())
Пример #19
0
 def test_parameter(self):
     dq = DQ("""(b.id, b.name) Book{b.id == 1 or
     regex(b.name, '$(mynamepattern)')} b """)
     list(dq.context({"mynamepattern": "B.*"}).tuples())
Пример #20
0
 def test_group_by(self):
     dq = DQ("(avg(b.price)) Book b")
     self.assertEqual(len([t for t in dq.tuples()]), 1)
     for t in dq.rewind().tuples():
         self.assertTrue(isinstance(t[0], Decimal))
Пример #21
0
 def test_count(self):
     dq = DQ("(b.id, b.name) Book b")
     self.assertEqual(dq.count(), 10)
Пример #22
0
 def test_csv(self):
     dq = DQ("(b.id, b.name) Book b")
     for r in dq.csv():
         self.assertTrue(isinstance(r, str))
Пример #23
0
 def test_aggregate_funcs(self):
     dq = DQ("(avg(b.price), max(b.price), min(b.price)) Book b")
     for r in dq.dicts():
         self.assertTrue(isinstance(r, dict))
Пример #24
0
 def test_json(self):
     dq = DQ("(b.id, b.name) Book b")
     for r in dq.json():
         self.assertTrue(isinstance(json.loads(r), dict))