Skip to content

Commit

Permalink
Fail errors immediately
Browse files Browse the repository at this point in the history
  • Loading branch information
philippgille committed Mar 5, 2024
1 parent d9d3b18 commit 31a1a17
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 125 deletions.
82 changes: 41 additions & 41 deletions collection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ func TestCollection_Add(t *testing.T) {
db := NewDB()
c, err := db.CreateCollection(name, metadata, embeddingFunc)
if err != nil {
t.Error("expected no error, got", err)
t.Fatal("expected no error, got", err)
}
if c == nil {
t.Error("expected collection, got nil")
t.Fatal("expected collection, got nil")
}

// Add documents
Expand Down Expand Up @@ -66,37 +66,37 @@ func TestCollection_Add(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
err = c.Add(ctx, ids, nil, metadatas, contents)
if err != nil {
t.Error("expected nil, got", err)
t.Fatal("expected nil, got", err)
}

// Check documents
if len(c.documents) != 2 {
t.Error("expected 2, got", len(c.documents))
t.Fatal("expected 2, got", len(c.documents))
}
for i, id := range ids {
doc, ok := c.documents[id]
if !ok {
t.Error("expected document, got nil")
t.Fatal("expected document, got nil")
}
if doc.ID != id {
t.Error("expected", id, "got", doc.ID)
t.Fatal("expected", id, "got", doc.ID)
}
if len(doc.Metadata) != 1 {
t.Error("expected 1, got", len(doc.Metadata))
t.Fatal("expected 1, got", len(doc.Metadata))
}
if !slices.Equal(doc.Embedding, vectors) {
t.Error("expected", vectors, "got", doc.Embedding)
t.Fatal("expected", vectors, "got", doc.Embedding)
}
if doc.Content != contents[i] {
t.Error("expected", contents[i], "got", doc.Content)
t.Fatal("expected", contents[i], "got", doc.Content)
}
}
// Metadata can't be accessed with the loop's i
if c.documents[ids[0]].Metadata["foo"] != "bar" {
t.Error("expected bar, got", c.documents[ids[0]].Metadata["foo"])
t.Fatal("expected bar, got", c.documents[ids[0]].Metadata["foo"])
}
if c.documents[ids[1]].Metadata["a"] != "b" {
t.Error("expected b, got", c.documents[ids[1]].Metadata["a"])
t.Fatal("expected b, got", c.documents[ids[1]].Metadata["a"])
}
})
}
Expand All @@ -115,10 +115,10 @@ func TestCollection_Add_Error(t *testing.T) {
db := NewDB()
c, err := db.CreateCollection(name, metadata, embeddingFunc)
if err != nil {
t.Error("expected no error, got", err)
t.Fatal("expected no error, got", err)
}
if c == nil {
t.Error("expected collection, got nil")
t.Fatal("expected collection, got nil")
}

// Add documents, provoking errors
Expand All @@ -129,27 +129,27 @@ func TestCollection_Add_Error(t *testing.T) {
// Empty IDs
err = c.Add(ctx, []string{}, embeddings, metadatas, contents)
if err == nil {
t.Error("expected error, got nil")
t.Fatal("expected error, got nil")
}
// Empty embeddings and contents (both at the same time!)
err = c.Add(ctx, ids, [][]float32{}, metadatas, []string{})
if err == nil {
t.Error("expected error, got nil")
t.Fatal("expected error, got nil")
}
// Bad embeddings length
err = c.Add(ctx, ids, [][]float32{vectors}, metadatas, contents)
if err == nil {
t.Error("expected error, got nil")
t.Fatal("expected error, got nil")
}
// Bad metadatas length
err = c.Add(ctx, ids, embeddings, []map[string]string{{"foo": "bar"}}, contents)
if err == nil {
t.Error("expected error, got nil")
t.Fatal("expected error, got nil")
}
// Bad contents length
err = c.Add(ctx, ids, embeddings, metadatas, []string{"hello world"})
if err == nil {
t.Error("expected error, got nil")
t.Fatal("expected error, got nil")
}
}

Expand All @@ -166,10 +166,10 @@ func TestCollection_AddConcurrently(t *testing.T) {
db := NewDB()
c, err := db.CreateCollection(name, metadata, embeddingFunc)
if err != nil {
t.Error("expected no error, got", err)
t.Fatal("expected no error, got", err)
}
if c == nil {
t.Error("expected collection, got nil")
t.Fatal("expected collection, got nil")
}

// Add documents
Expand Down Expand Up @@ -213,37 +213,37 @@ func TestCollection_AddConcurrently(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
err = c.AddConcurrently(ctx, ids, nil, metadatas, contents, 2)
if err != nil {
t.Error("expected nil, got", err)
t.Fatal("expected nil, got", err)
}

// Check documents
if len(c.documents) != 2 {
t.Error("expected 2, got", len(c.documents))
t.Fatal("expected 2, got", len(c.documents))
}
for i, id := range ids {
doc, ok := c.documents[id]
if !ok {
t.Error("expected document, got nil")
t.Fatal("expected document, got nil")
}
if doc.ID != id {
t.Error("expected", id, "got", doc.ID)
t.Fatal("expected", id, "got", doc.ID)
}
if len(doc.Metadata) != 1 {
t.Error("expected 1, got", len(doc.Metadata))
t.Fatal("expected 1, got", len(doc.Metadata))
}
if !slices.Equal(doc.Embedding, vectors) {
t.Error("expected", vectors, "got", doc.Embedding)
t.Fatal("expected", vectors, "got", doc.Embedding)
}
if doc.Content != contents[i] {
t.Error("expected", contents[i], "got", doc.Content)
t.Fatal("expected", contents[i], "got", doc.Content)
}
}
// Metadata can't be accessed with the loop's i
if c.documents[ids[0]].Metadata["foo"] != "bar" {
t.Error("expected bar, got", c.documents[ids[0]].Metadata["foo"])
t.Fatal("expected bar, got", c.documents[ids[0]].Metadata["foo"])
}
if c.documents[ids[1]].Metadata["a"] != "b" {
t.Error("expected b, got", c.documents[ids[1]].Metadata["a"])
t.Fatal("expected b, got", c.documents[ids[1]].Metadata["a"])
}
})
}
Expand All @@ -262,10 +262,10 @@ func TestCollection_AddConcurrently_Error(t *testing.T) {
db := NewDB()
c, err := db.CreateCollection(name, metadata, embeddingFunc)
if err != nil {
t.Error("expected no error, got", err)
t.Fatal("expected no error, got", err)
}
if c == nil {
t.Error("expected collection, got nil")
t.Fatal("expected collection, got nil")
}

// Add documents, provoking errors
Expand All @@ -276,32 +276,32 @@ func TestCollection_AddConcurrently_Error(t *testing.T) {
// Empty IDs
err = c.AddConcurrently(ctx, []string{}, embeddings, metadatas, contents, 2)
if err == nil {
t.Error("expected error, got nil")
t.Fatal("expected error, got nil")
}
// Empty embeddings and contents (both at the same time!)
err = c.AddConcurrently(ctx, ids, [][]float32{}, metadatas, []string{}, 2)
if err == nil {
t.Error("expected error, got nil")
t.Fatal("expected error, got nil")
}
// Bad embeddings length
err = c.AddConcurrently(ctx, ids, [][]float32{vectors}, metadatas, contents, 2)
if err == nil {
t.Error("expected error, got nil")
t.Fatal("expected error, got nil")
}
// Bad metadatas length
err = c.AddConcurrently(ctx, ids, embeddings, []map[string]string{{"foo": "bar"}}, contents, 2)
if err == nil {
t.Error("expected error, got nil")
t.Fatal("expected error, got nil")
}
// Bad contents length
err = c.AddConcurrently(ctx, ids, embeddings, metadatas, []string{"hello world"}, 2)
if err == nil {
t.Error("expected error, got nil")
t.Fatal("expected error, got nil")
}
// Bad concurrency
err = c.AddConcurrently(ctx, ids, embeddings, metadatas, contents, 0)
if err == nil {
t.Error("expected error, got nil")
t.Fatal("expected error, got nil")
}
}

Expand All @@ -315,10 +315,10 @@ func TestCollection_Count(t *testing.T) {
}
c, err := db.CreateCollection(name, metadata, embeddingFunc)
if err != nil {
t.Error("expected no error, got", err)
t.Fatal("expected no error, got", err)
}
if c == nil {
t.Error("expected collection, got nil")
t.Fatal("expected collection, got nil")
}

// Add documents
Expand All @@ -327,11 +327,11 @@ func TestCollection_Count(t *testing.T) {
contents := []string{"hello world", "hallo welt"}
err = c.Add(context.Background(), ids, nil, metadatas, contents)
if err != nil {
t.Error("expected nil, got", err)
t.Fatal("expected nil, got", err)
}

// Check count
if c.Count() != 2 {
t.Error("expected 2, got", c.Count())
t.Fatal("expected 2, got", c.Count())
}
}
Loading

0 comments on commit 31a1a17

Please sign in to comment.