def test_requires_field_names(self): with pytest.raises(ValueError) as excinfo: index_name(Author) assert ( "At least one field name required" in str(excinfo.value) )
def test_requires_field_names(self): with pytest.raises(ValueError) as excinfo: index_name(Author) assert ( "At least one field name required" in six.text_type(excinfo.value) )
def test_invalid_kwarg(self): with pytest.raises(ValueError) as excinfo: index_name(Author, 'name', nonexistent_kwarg=True) assert ( "The only supported keyword argument is 'using'" in six.text_type(excinfo.value) )
def test_invalid_kwarg(self): with pytest.raises(ValueError) as excinfo: index_name(Author, 'name', nonexistent_kwarg=True) assert ( "The only supported keyword argument is 'using'" in str(excinfo.value) )
def test_requires_real_field_names(self): with pytest.raises(ValueError) as excinfo: index_name(Author, 'nonexistent') assert ( "Fields do not exist: nonexistent" in six.text_type(excinfo.value) )
def test_requires_real_field_names(self): with pytest.raises(ValueError) as excinfo: index_name(Author, 'nonexistent') assert ( "Fields do not exist: nonexistent" in str(excinfo.value) )
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
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
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
def setUp(self): super(HandlerMultipartIndexTests, self).setUp() self.smith1 = AuthorMultiIndex.objects.create(name="John Smith", country="Scotland") self.smith2 = AuthorMultiIndex.objects.create(name="John Smith", country="England") self.index_name = index_name(AuthorMultiIndex, "name", "country")
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)
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
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
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)
def setUp(self): super(HandlerMultipartIndexTests, self).setUp() self.smith1 = AuthorMultiIndex.objects.create(name='John Smith', country='Scotland') self.smith2 = AuthorMultiIndex.objects.create(name='John Smith', country='England') self.index_name = index_name(AuthorMultiIndex, 'name', 'country')
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
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)
def test_force_index_inner_query(self): title_idx = index_name(Book, "title") with CaptureLastQuery() as cap: list( Author.objects.annotate( has_books=Exists( Book.objects.filter( author_id=OuterRef("id"), title__gt="" ).force_index(title_idx, table_name="testapp_book") ) ).filter(has_books=True) ) assert ("FORCE INDEX (`" + title_idx + "`)") in cap.query used = used_indexes(cap.query) assert title_idx in used
def test_secondary_multiple_fields_non_existent(self): with pytest.raises(KeyError): index_name(AuthorMultiIndex, 'country', 'id')
def test_secondary_multiple_fields_non_existent_reversed_existent(self): # Checks that order is preserved with pytest.raises(KeyError): index_name(AuthorMultiIndex, 'country', 'name')
def test_secondary_multiple_fields(self): name = index_name(AuthorMultiIndex, 'name', 'country') assert name.startswith('testapp_authormultiindex')
def test_primary_key(self): assert index_name(Author, 'id') == 'PRIMARY'
def setUp(self): self.smith1 = AuthorMultiIndex.objects.create(name='John Smith', country='Scotland') self.smith2 = AuthorMultiIndex.objects.create(name='John Smith', country='England') self.index_name = index_name(AuthorMultiIndex, 'name', 'country')
def test_primary_key_using_other(self): assert index_name(Author, "id", using="other") == "PRIMARY"
def test_secondary_single_field(self): name = index_name(Author, "name") assert name.startswith("testapp_author_")
def test_secondary_single_field(self): name = index_name(Author, 'name') assert name.startswith('testapp_author_')
def test_primary_key(self): assert index_name(Author, "id") == "PRIMARY"
def test_index_does_not_exist(self): with pytest.raises(KeyError) as excinfo: index_name(Author, 'bio') assert "There is no index on (bio)" in six.text_type(excinfo.value)
def test_primary_key_using_other(self): assert index_name(Author, 'id', using='other') == 'PRIMARY'
def test_ignore_index(self): name_idx = index_name(Author, "name") with CaptureLastQuery() as cap: list(Author.objects.filter(name__gt="").ignore_index(name_idx)) assert ("IGNORE INDEX (`" + name_idx + "`)") in cap.query assert name_idx not in used_indexes(cap.query)
def test_index_does_not_exist(self): with pytest.raises(KeyError) as excinfo: index_name(Author, 'bio') assert "There is no index on (bio)" in str(excinfo.value)
def test_secondary_multiple_fields(self): name = index_name(AuthorMultiIndex, "name", "country") assert name.startswith("testapp_authormultiindex")