Skip to content

Commit 29c5f26

Browse files
committed
[add] Included extra test to cover all DropIndex() conditions
1 parent 8e748b2 commit 29c5f26

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

redisearch/client_test.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -894,3 +894,66 @@ func TestClient_CreateIndex(t *testing.T) {
894894
assert.Equal(t, "Jon", docs[0].Properties["name"])
895895
assert.Equal(t, "John", docs[1].Properties["name"])
896896
}
897+
898+
func TestClient_DropIndex(t *testing.T) {
899+
c := createClient("drop-index-example")
900+
version, err := c.getRediSearchVersion()
901+
assert.Nil(t, err)
902+
if version <= 10699 {
903+
// DropIndex() is available for RediSearch 2.0+
904+
return
905+
}
906+
907+
// Create a schema
908+
schema := NewSchema(DefaultOptions).
909+
AddField(NewTextFieldOptions("name", TextFieldOptions{Sortable: true, PhoneticMatcher: PhoneticDoubleMetaphoneEnglish})).
910+
AddField(NewNumericField("age"))
911+
912+
// IndexDefinition is available for RediSearch 2.0+
913+
// In this example we will only index keys started by product:
914+
indexDefinition := NewIndexDefinition().AddPrefix("drop-index:")
915+
916+
// Add the Index Definition
917+
err = c.CreateIndexWithIndexDefinition(schema, indexDefinition)
918+
assert.Nil(t, err)
919+
920+
// Create docs with a name that has the same phonetic matcher
921+
vanillaConnection := c.pool.Get()
922+
vanillaConnection.Do("HSET", "drop-index:doc1", "name", "Jon", "age", 25)
923+
vanillaConnection.Do("HSET", "drop-index:doc2", "name", "John", "age", 20)
924+
925+
// Wait for all documents to be indexed
926+
info, _ := c.Info()
927+
for info.IsIndexing {
928+
time.Sleep(time.Second)
929+
info, _ = c.Info()
930+
}
931+
932+
// Drop index but keep docs
933+
err = c.DropIndex(false)
934+
assert.Nil(t, err)
935+
// Now that we don't have the index this should raise an error
936+
_, err = c.Info()
937+
assert.EqualError(t, err, "Unknown Index name")
938+
// Assert hashes still exist
939+
result, err := vanillaConnection.Do("EXISTS", "drop-index:doc1")
940+
assert.Equal(t, int64(1), result)
941+
result, err = vanillaConnection.Do("EXISTS", "drop-index:doc2")
942+
assert.Equal(t, int64(1), result)
943+
944+
// Create index again
945+
err = c.CreateIndexWithIndexDefinition(schema, indexDefinition)
946+
assert.Nil(t, err)
947+
// Drop index but keep docs
948+
err = c.DropIndex(true)
949+
assert.Nil(t, err)
950+
// Now that we don't have the index this should raise an error
951+
_, err = c.Info()
952+
assert.EqualError(t, err, "Unknown Index name")
953+
// Assert hashes still exist
954+
result, err = vanillaConnection.Do("EXISTS", "drop-index:doc1")
955+
assert.Equal(t, int64(0), result)
956+
result, err = vanillaConnection.Do("EXISTS", "drop-index:doc2")
957+
assert.Equal(t, int64(0), result)
958+
959+
}

0 commit comments

Comments
 (0)