Skip to content

Commit

Permalink
update prose test
Browse files Browse the repository at this point in the history
  • Loading branch information
qingyang-hu committed Aug 11, 2023
1 parent 064428a commit 4585537
Showing 1 changed file with 124 additions and 60 deletions.
184 changes: 124 additions & 60 deletions mongo/search_index_prose_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (

"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/internal/assert"
"go.mongodb.org/mongo-driver/internal/require"
"go.mongodb.org/mongo-driver/internal/uuid"
"go.mongodb.org/mongo-driver/mongo/options"
)
Expand All @@ -24,30 +25,14 @@ func newTestCollection(t *testing.T, client *Client) *Collection {
t.Helper()

id, err := uuid.New()
assert.NoError(t, err)
require.NoError(t, err)
collName := fmt.Sprintf("col%s", id.String())
return client.Database("test").Collection(collName)
}

func getDocument(t *testing.T, ctx context.Context, view SearchIndexView, index string) bson.Raw {
t.Helper()

for {
cursor, err := view.List(ctx, &index, nil)
assert.NoError(t, err, "failed to list")

if !cursor.Next(ctx) {
return nil
}
if cursor.Current.Lookup("queryable").Boolean() {
return cursor.Current
}
t.Logf("cursor: %s, sleep 5 seconds...", cursor.Current.String())
time.Sleep(5 * time.Second)
}
}

func TestSearchIndexProse(t *testing.T) {
t.Parallel()

const timeout = 5 * time.Minute

ctx := context.Background()
Expand All @@ -66,10 +51,13 @@ func TestSearchIndexProse(t *testing.T) {
ctx := context.Background()

collection := newTestCollection(t, client)
defer collection.Drop(ctx)
defer func() {
err = collection.Drop(ctx)
assert.NoError(t, err, "failed to drop collection")
}()

_, err = collection.InsertOne(ctx, bson.D{})
assert.NoError(t, err, "failed to insert")
require.NoError(t, err, "failed to insert")

view := collection.SearchIndexes()

Expand All @@ -80,14 +68,28 @@ func TestSearchIndexProse(t *testing.T) {
Name: &searchName,
}
index, err := view.CreateOne(ctx, model)
assert.NoError(t, err, "failed to create index")
assert.Equal(t, searchName, index, "unmatched name")
require.NoError(t, err, "failed to create index")
require.Equal(t, searchName, index, "unmatched name")

var doc bson.Raw
for doc == nil {
cursor, err := view.List(ctx, &index, nil)
require.NoError(t, err, "failed to list")

doc := getDocument(t, ctx, view, searchName)
assert.NotNil(t, doc, "got empty document")
if !cursor.Next(ctx) {
break
}
if cursor.Current.Lookup("queryable").Boolean() {
doc = cursor.Current
} else {
t.Logf("cursor: %s, sleep 5 seconds...", cursor.Current.String())
time.Sleep(5 * time.Second)
}
}
require.NotNil(t, doc, "got empty document")
assert.Equal(t, searchName, doc.Lookup("name").StringValue(), "unmatched name")
expected, err := bson.Marshal(definition)
assert.NoError(t, err, "failed to marshal definition")
require.NoError(t, err, "failed to marshal definition")
actual := doc.Lookup("latestDefinition").Value
assert.Equal(t, expected, actual, "unmatched definition")
})
Expand All @@ -96,10 +98,13 @@ func TestSearchIndexProse(t *testing.T) {
ctx := context.Background()

collection := newTestCollection(t, client)
defer collection.Drop(ctx)
defer func() {
err = collection.Drop(ctx)
assert.NoError(t, err, "failed to drop collection")
}()

_, err = collection.InsertOne(ctx, bson.D{})
assert.NoError(t, err, "failed to insert")
require.NoError(t, err, "failed to insert")

view := collection.SearchIndexes()

Expand All @@ -117,32 +122,50 @@ func TestSearchIndexProse(t *testing.T) {
},
}
indexes, err := view.CreateMany(ctx, models)
assert.NoError(t, err, "failed to create index")
assert.Equal(t, len(indexes), 2, "expected 2 indexes")
assert.Contains(t, indexes, searchName0)
assert.Contains(t, indexes, searchName1)
require.NoError(t, err, "failed to create index")
require.Equal(t, len(indexes), 2, "expected 2 indexes")
require.Contains(t, indexes, searchName0)
require.Contains(t, indexes, searchName1)

getDocument := func(index string) bson.Raw {
t.Helper()

for {
cursor, err := view.List(ctx, &index, nil)
require.NoError(t, err, "failed to list")

if !cursor.Next(ctx) {
return nil
}
if cursor.Current.Lookup("queryable").Boolean() {
return cursor.Current
}
t.Logf("cursor: %s, sleep 5 seconds...", cursor.Current.String())
time.Sleep(5 * time.Second)
}
}

var wg sync.WaitGroup
wg.Add(2)
go func() {
defer wg.Done()

doc := getDocument(t, ctx, view, searchName0)
assert.NotNil(t, doc, "got empty document")
doc := getDocument(searchName0)
require.NotNil(t, doc, "got empty document")
assert.Equal(t, searchName0, doc.Lookup("name").StringValue(), "unmatched name")
expected, err := bson.Marshal(definition)
assert.NoError(t, err, "failed to marshal definition")
require.NoError(t, err, "failed to marshal definition")
actual := doc.Lookup("latestDefinition").Value
assert.Equal(t, expected, actual, "unmatched definition")
}()
go func() {
defer wg.Done()

doc := getDocument(t, ctx, view, searchName1)
assert.NotNil(t, doc, "got empty document")
doc := getDocument(searchName1)
require.NotNil(t, doc, "got empty document")
assert.Equal(t, searchName1, doc.Lookup("name").StringValue(), "unmatched name")
expected, err := bson.Marshal(definition)
assert.NoError(t, err, "failed to marshal definition")
require.NoError(t, err, "failed to marshal definition")
actual := doc.Lookup("latestDefinition").Value
assert.Equal(t, expected, actual, "unmatched definition")
}()
Expand All @@ -153,10 +176,13 @@ func TestSearchIndexProse(t *testing.T) {
ctx := context.Background()

collection := newTestCollection(t, client)
defer collection.Drop(ctx)
defer func() {
err = collection.Drop(ctx)
assert.NoError(t, err, "failed to drop collection")
}()

_, err = collection.InsertOne(ctx, bson.D{})
assert.NoError(t, err, "failed to insert")
require.NoError(t, err, "failed to insert")

view := collection.SearchIndexes()

Expand All @@ -167,18 +193,32 @@ func TestSearchIndexProse(t *testing.T) {
Name: &searchName,
}
index, err := view.CreateOne(ctx, model)
assert.NoError(t, err, "failed to create index")
assert.Equal(t, searchName, index, "unmatched name")
require.NoError(t, err, "failed to create index")
require.Equal(t, searchName, index, "unmatched name")

doc := getDocument(t, ctx, view, searchName)
assert.NotNil(t, doc, "got empty document")
assert.Equal(t, searchName, doc.Lookup("name").StringValue(), "unmatched name")
var doc bson.Raw
for doc == nil {
cursor, err := view.List(ctx, &index, nil)
require.NoError(t, err, "failed to list")

if !cursor.Next(ctx) {
break
}
if cursor.Current.Lookup("queryable").Boolean() {
doc = cursor.Current
} else {
t.Logf("cursor: %s, sleep 5 seconds...", cursor.Current.String())
time.Sleep(5 * time.Second)
}
}
require.NotNil(t, doc, "got empty document")
require.Equal(t, searchName, doc.Lookup("name").StringValue(), "unmatched name")

err = view.DropOne(ctx, searchName)
assert.NoError(t, err, "failed to drop index")
require.NoError(t, err, "failed to drop index")
for {
cursor, err := view.List(ctx, &index, nil)
assert.NoError(t, err, "failed to list")
require.NoError(t, err, "failed to list")

if !cursor.Next(ctx) {
break
Expand All @@ -192,10 +232,13 @@ func TestSearchIndexProse(t *testing.T) {
ctx := context.Background()

collection := newTestCollection(t, client)
defer collection.Drop(ctx)
defer func() {
err = collection.Drop(ctx)
assert.NoError(t, err, "failed to drop collection")
}()

_, err = collection.InsertOne(ctx, bson.D{})
assert.NoError(t, err, "failed to insert")
require.NoError(t, err, "failed to insert")

view := collection.SearchIndexes()

Expand All @@ -206,22 +249,40 @@ func TestSearchIndexProse(t *testing.T) {
Name: &searchName,
}
index, err := view.CreateOne(ctx, model)
assert.NoError(t, err, "failed to create index")
assert.Equal(t, searchName, index, "unmatched name")
require.NoError(t, err, "failed to create index")
require.Equal(t, searchName, index, "unmatched name")

getDocument := func(index string) bson.Raw {
t.Helper()

for {
cursor, err := view.List(ctx, &index, nil)
require.NoError(t, err, "failed to list")

if !cursor.Next(ctx) {
return nil
}
if cursor.Current.Lookup("queryable").Boolean() {
return cursor.Current
}
t.Logf("cursor: %s, sleep 5 seconds...", cursor.Current.String())
time.Sleep(5 * time.Second)
}
}

doc := getDocument(t, ctx, view, searchName)
assert.NotNil(t, doc, "got empty document")
assert.Equal(t, searchName, doc.Lookup("name").StringValue(), "unmatched name")
doc := getDocument(searchName)
require.NotNil(t, doc, "got empty document")
require.Equal(t, searchName, doc.Lookup("name").StringValue(), "unmatched name")

definition = bson.D{{"mappings", bson.D{{"dynamic", true}}}}
err = view.UpdateOne(ctx, searchName, definition)
assert.NoError(t, err, "failed to drop index")
doc = getDocument(t, ctx, view, searchName)
assert.NotNil(t, doc, "got empty document")
require.NoError(t, err, "failed to drop index")
doc = getDocument(searchName)
require.NotNil(t, doc, "got empty document")
assert.Equal(t, searchName, doc.Lookup("name").StringValue(), "unmatched name")
assert.Equal(t, "READY", doc.Lookup("status").StringValue(), "unexpected status")
expected, err := bson.Marshal(definition)
assert.NoError(t, err, "failed to marshal definition")
require.NoError(t, err, "failed to marshal definition")
actual := doc.Lookup("latestDefinition").Value
assert.Equal(t, expected, actual, "unmatched definition")
})
Expand All @@ -230,9 +291,12 @@ func TestSearchIndexProse(t *testing.T) {
ctx := context.Background()

collection := newTestCollection(t, client)
defer collection.Drop(ctx)
defer func() {
err = collection.Drop(ctx)
assert.NoError(t, err, "failed to drop collection")
}()

err = collection.SearchIndexes().DropOne(ctx, "foo")
assert.NoError(t, err)
require.NoError(t, err)
})
}

0 comments on commit 4585537

Please sign in to comment.