def test_gin_parameters(self): index_name = "integer_array_gin_params" index = GinIndex( fields=["field"], name=index_name, fastupdate=True, gin_pending_list_limit=64, db_tablespace="pg_default", ) with connection.schema_editor() as editor: editor.add_index(IntegerArrayModel, index) self.assertIn( ") WITH (gin_pending_list_limit = 64, fastupdate = on) TABLESPACE", str(index.create_sql(IntegerArrayModel, editor)), ) constraints = self.get_constraints(IntegerArrayModel._meta.db_table) self.assertEqual(constraints[index_name]["type"], "gin") self.assertEqual( constraints[index_name]["options"], ["gin_pending_list_limit=64", "fastupdate=on"], ) with connection.schema_editor() as editor: editor.remove_index(IntegerArrayModel, index) self.assertNotIn( index_name, self.get_constraints(IntegerArrayModel._meta.db_table) )
def test_cast_search_vector_gin_index(self): index_name = "cast_search_vector_gin" index = GinIndex(Cast("field", SearchVectorField()), name=index_name) with connection.schema_editor() as editor: editor.add_index(TextFieldModel, index) sql = index.create_sql(TextFieldModel, editor) table = TextFieldModel._meta.db_table constraints = self.get_constraints(table) self.assertIn(index_name, constraints) self.assertIn(constraints[index_name]["type"], GinIndex.suffix) self.assertIs(sql.references_column(table, "field"), True) self.assertIn("::tsvector", str(sql)) with connection.schema_editor() as editor: editor.remove_index(TextFieldModel, index) self.assertNotIn(index_name, self.get_constraints(table))
def test_partial_gin_index_with_tablespace(self): with register_lookup(CharField, Length): index_name = 'char_field_gin_partial_idx' index = GinIndex( fields=['field'], name=index_name, condition=Q(field__length=40), db_tablespace='pg_default', ) with connection.schema_editor() as editor: editor.add_index(CharFieldModel, index) self.assertIn('TABLESPACE "pg_default" ', str(index.create_sql(CharFieldModel, editor))) constraints = self.get_constraints(CharFieldModel._meta.db_table) self.assertEqual(constraints[index_name]['type'], 'gin') with connection.schema_editor() as editor: editor.remove_index(CharFieldModel, index) self.assertNotIn(index_name, self.get_constraints(CharFieldModel._meta.db_table))