Beispiel #1
0
    def test_can_monkey_patch_is_idempotent(self):
        patch_CursorWrapper_execute()

        with CaptureLastQuery() as cap, connection.cursor() as cursor:
            cursor.execute(
                "SELECT 1 FROM DUAL WHERE (/*QueryRewrite':label=hi*/1)")
        assert cap.query == "SELECT /*hi*/ 1 FROM DUAL WHERE (1)"
Beispiel #2
0
    def test_straight_join(self):
        with CaptureLastQuery() as cap:
            list(
                Author.objects.filter(
                    books__title__startswith='A').straight_join())

        assert cap.query.startswith("SELECT STRAIGHT_JOIN ")
Beispiel #3
0
    def test_straight_join_with_distinct(self):
        with CaptureLastQuery() as cap:
            list(
                Author.objects.filter(tutor=None).distinct().values(
                    'books__title').straight_join())

        assert cap.query.startswith("SELECT DISTINCT STRAIGHT_JOIN ")
Beispiel #4
0
 def test_use_index(self):
     name_idx = index_name(Author, 'name')
     with CaptureLastQuery() as cap:
         list(Author.objects.filter(name__gt='').use_index(name_idx))
     assert ('USE INDEX (`' + name_idx + '`)') in cap.query
     used = used_indexes(cap.query)
     assert len(used) == 0 or name_idx in used
Beispiel #5
0
 def test_complex_query_1(self):
     with CaptureLastQuery() as cap:
         list(Author.objects.distinct().straight_join().filter(
             books__title__startswith="A").exclude(
                 books__id__lte=1).prefetch_related('tutees').filter(
                     bio__gt='').exclude(bio__startswith='Once upon'))
     assert cap.query.startswith("SELECT DISTINCT STRAIGHT_JOIN ")
Beispiel #6
0
 def test_label_and_straight_join(self):
     with CaptureLastQuery() as cap:
         list(
             Author.objects.label(
                 "QueryHintTests.test_label_and").straight_join().all())
     assert cap.query.startswith(
         "SELECT /*QueryHintTests.test_label_and*/ STRAIGHT_JOIN ", )
Beispiel #7
0
 def test_adding_many(self):
     with CaptureLastQuery() as cap:
         list(Author.objects.straight_join().sql_cache().sql_big_result().
              sql_buffer_result())
     assert cap.query.startswith(
         "SELECT STRAIGHT_JOIN SQL_BIG_RESULT SQL_BUFFER_RESULT SQL_CACHE ",
     )
Beispiel #8
0
 def test_force_index(self):
     name_idx = index_name(Author, 'name')
     with CaptureLastQuery() as cap:
         list(Author.objects.filter(name__gt='')
                            .force_index(name_idx))
     assert ('FORCE INDEX (`' + name_idx + '`)') in cap.query
     assert name_idx in used_indexes(cap.query)
Beispiel #9
0
 def test_use_index_table_name(self):
     extra_table = 'testapp_authorextra'
     with CaptureLastQuery() as cap:
         list(
             Author.objects.select_related('authorextra').use_index(
                 'PRIMARY', table_name=extra_table))
     assert '`' + extra_table + '` USE INDEX (`PRIMARY`) ' in cap.query
Beispiel #10
0
 def test_label_twice(self):
     with CaptureLastQuery() as cap:
         list(
             Author.objects.label("QueryHintTests").label(
                 "test_label_twice").all())
     assert cap.query.startswith(
         "SELECT /*QueryHintTests*/ /*test_label_twice*/ ", )
Beispiel #11
0
    def test_index_hint_force_order_by(self):
        name_idx = index_name(Author, 'name')
        with CaptureLastQuery() as cap:
            list(Author.objects.force_index(name_idx, for_='ORDER BY')
                               .order_by('name'))

        assert ('FORCE INDEX FOR ORDER BY (`' + name_idx + "`)") in cap.query
        assert name_idx in used_indexes(cap.query)
Beispiel #12
0
 def test_ignore_index_multiple(self):
     name_idx = index_name(AuthorMultiIndex, 'name')
     name_country_idx = index_name(AuthorMultiIndex, 'name', 'country')
     with CaptureLastQuery() as cap:
         list(
             AuthorMultiIndex.objects.filter(name__gt='').ignore_index(
                 name_idx, name_country_idx))
     assert ('IGNORE INDEX (`' + name_idx + '`,`' + name_country_idx + '`)'
             in cap.query)
     used = used_indexes(cap.query)
     assert name_idx not in used
     assert name_country_idx not in used
Beispiel #13
0
 def test_complex_query_2(self):
     subq = Book.objects.straight_join().filter(title__startswith="A")
     with CaptureLastQuery() as cap:
         list(Author.objects.straight_join().filter(books__in=subq))
     assert cap.query.startswith("SELECT STRAIGHT_JOIN ")
Beispiel #14
0
 def test_sql_buffer_result(self):
     with CaptureLastQuery() as cap:
         list(Author.objects.sql_buffer_result().all())
     assert cap.query.startswith("SELECT SQL_BUFFER_RESULT ")
Beispiel #15
0
 def test_ignore_index_primary(self):
     with CaptureLastQuery() as cap:
         list(Author.objects.filter(name__gt='').ignore_index('PRIMARY'))
     assert ('IGNORE INDEX (`PRIMARY`)') in cap.query
     assert 'PRIMARY' not in used_indexes(cap.query)
Beispiel #16
0
 def test_label_update(self):
     Author.objects.create(name='UPDATEME')
     with CaptureLastQuery() as cap:
         Author.objects.label("QueryHintTests").update(name='UPDATED')
     assert cap.query.startswith("UPDATE /*QueryHintTests*/ ")
Beispiel #17
0
 def test_label_star(self):
     with CaptureLastQuery() as cap:
         list(Author.objects.label("I'ma*").label("*").all())
     assert cap.query.startswith("SELECT /*I'ma**/ /***/ ")
Beispiel #18
0
 def test_label(self):
     with CaptureLastQuery() as cap:
         list(Author.objects.label("QueryHintTests.test_label").all())
     assert cap.query.startswith("SELECT /*QueryHintTests.test_label*/ ")
Beispiel #19
0
 def test_force_index_primary(self):
     with CaptureLastQuery() as cap:
         list(Author.objects.force_index('PRIMARY'))
     assert ('FORCE INDEX (`PRIMARY`)') in cap.query
     used = used_indexes(cap.query)
     assert len(used) == 0 or 'PRIMARY' in used
Beispiel #20
0
 def test_monkey_patch_can_be_disabled(self):
     query = "SELECT 1 FROM DUAL WHERE (/*QueryRewrite':STRAIGHT_JOIN*/1)"
     with CaptureLastQuery() as cap, connection.cursor() as cursor:
         cursor.execute(query)
     cap.query == query
Beispiel #21
0
 def test_sql_no_cache(self):
     with CaptureLastQuery() as cap:
         list(Author.objects.sql_no_cache().all())
     assert cap.query.startswith("SELECT SQL_NO_CACHE ")
Beispiel #22
0
 def test_use_index_none(self):
     with CaptureLastQuery() as cap:
         list(Author.objects.values_list('name').distinct().use_index())
     assert 'USE INDEX () ' in cap.query
     assert used_indexes(cap.query) == set()
Beispiel #23
0
    def test_can_disable_setting(self):
        with CaptureLastQuery() as cap:
            list(Author.objects.all().straight_join())

        assert not cap.query.startswith("SELECT STRAIGHT_JOIN ")
Beispiel #24
0
 def test_force_index_table_name_doesnt_exist_ignored(self):
     with CaptureLastQuery() as cap:
         list(
             Author.objects.select_related('authorextra').force_index(
                 'PRIMARY', table_name='nonexistent'))
     assert ' FORCE INDEX ' not in cap.query
Beispiel #25
0
 def test_it_is_monkey_patched(self):
     with CaptureLastQuery() as cap, connection.cursor() as cursor:
         cursor.execute("SELECT 1 FROM DUAL "
                        "WHERE (/*QueryRewrite':STRAIGHT_JOIN*/1)")
     assert cap.query == "SELECT STRAIGHT_JOIN 1 FROM DUAL WHERE (1)"