Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: unit test for testing KindUnexpected case in Mongo query function #1883

Merged
merged 4 commits into from
Sep 25, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions pkg/storage/mongo/mongo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package mongo
import (
"bytes"
"context"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"io"
"os"
"testing"
Expand Down Expand Up @@ -97,6 +100,51 @@ func TestQueryKindNotFoundErrorCases(t *testing.T) {
require.Equal(t, errors.KindNotFound, errors.Kind(err))
}
}

func TestQueryKindUnexpectedErrorCases(t *testing.T) {
// Prepare error documents
// If there is any error in preparation phase, skip this test
ctx := context.Background()
mongoStorage := getStorage(t)
uri := os.Getenv("ATHENS_MONGO_STORAGE_URL")
client, err := mongo.Connect(ctx, options.Client().ApplyURI(uri))
if err != nil {
t.SkipNow()
}
defer client.Disconnect(ctx)
coll := client.Database("athens").Collection("modules")
errDocs := []struct {
Module string `bson:"module"`
Version string `bson:"version"`
Mod interface{} `bson:"mod"`
Info interface{} `bson:"info"`
}{
{"model1", "v1.1.1", 12345678, "error document with integer mod"},
{"model2", "v2.0.0", true, "error document with boolean mod"},
}
// In case some docs were inserted into the collection before, using upsert instead.
for _, errDoc := range errDocs {
filter := bson.D{{"module", errDoc.Module}, {"version", errDoc.Version}}
update := bson.D{{"$set", bson.D{{"mod", errDoc.Mod}, {"info", errDoc.Info}}}}
opts := options.Update().SetUpsert(true).SetBypassDocumentValidation(true)
_, err = coll.UpdateOne(ctx, filter, update, opts)
if err != nil {
t.SkipNow()
}
}
testCases := []struct {
modName string
version string
}{
{"model1", "v1.1.1"}, // test case: decode integer to []byte
{"model2", "v2.0.0"}, // test case: decode boolean to []byte
}
for _, test := range testCases {
_, err := query(ctx, mongoStorage, test.modName, test.version)
require.Error(t, err)
require.Equal(t, errors.KindUnexpected, errors.Kind(err))
}
}
func TestNewStorageWithDefaultOverrides(t *testing.T) {
url := os.Getenv("ATHENS_MONGO_STORAGE_URL")

Expand Down