Repository: googleapis/genai-toolbox
File: tests/mongodb/mongodb_integration_test.go (Go)

Code before:

			got, ok := body["result"].(string)
			if !ok {
				t.Fatalf("unable to find result in response body")
			}

			if got != tc.want {
				t.Fatalf("unexpected value: got %q, want %q", got, tc.want)
			}
		})
	}
}

func setupMongoDB(t *testing.T, ctx context.Context, database *mongo.Database) func(*testing.T) {
	collectionName := "test_collection"

	if err := database.Collection(collectionName).Drop(ctx); err != nil {
		t.Logf("Warning: failed to drop collection before setup: %v", err)
	}

	documents := []map[string]any{
		{"_id": 1, "id": 1, "name": "Alice", "email": ServiceAccountEmail},
		{"_id": 14, "id": 2, "name": "FakeAlice", "email": "fakeAlice@gmail.com"},
		{"_id": 2, "id": 2, "name": "Jane"},
		{"_id": 3, "id": 3, "name": "Sid"},
		// {"_id": 4, "id": 4, "name": nil},
		{"_id": 5, "id": 3, "name": "Alice", "email": "alice@gmail.com"},
		{"_id": 6, "id": 100, "name": "ToBeDeleted", "email": "bob@gmail.com"},
		{"_id": 7, "id": 101, "name": "ToBeDeleted", "email": "bob1@gmail.com"},
		{"_id": 8, "id": 101, "name": "ToBeDeleted", "email": "bob2@gmail.com"},
		{"_id": 9, "id": 300, "name": "ToBeUpdatedToBob", "email": "bob@gmail.com"},
		{"_id": 10, "id": 400, "name": "ToBeUpdatedToAlice", "email": "alice@gmail.com"},
		{"_id": 11, "id": 400, "name": "ToBeUpdatedToAlice", "email": "alice@gmail.com"},
		{"_id": 12, "id": 500, "name": "ToBeAggregated", "email": "agatha@gmail.com"},
		{"_id": 13, "id": 501, "name": "ToBeAggregated", "email": "agatha@gmail.com"},
	}
	for _, doc := range documents {
		_, err := database.Collection(collectionName).InsertOne(ctx, doc)
		if err != nil {
			t.Fatalf("unable to insert test data: %s", err)
		}
	}

	return func(t *testing.T) {
		// tear down test
		err := database.Collection(collectionName).Drop(ctx)
		if err != nil {
			t.Errorf("Teardown failed: %s", err)
		}
	}


Review comment (suggestion):
```suggestion
{"_id": 3, "id": 3, "name": "Sid"},
```

Code after:

			got, ok := body["result"].(string)
			if !ok {
				t.Fatalf("unable to find result in response body")
			}

			if got != tc.want {
				t.Fatalf("unexpected value: got %q, want %q", got, tc.want)
			}
		})
	}
}

func setupMongoDB(t *testing.T, ctx context.Context, database *mongo.Database) func(*testing.T) {
	collectionName := "test_collection"

	if err := database.Collection(collectionName).Drop(ctx); err != nil {
		t.Logf("Warning: failed to drop collection before setup: %v", err)
	}

	documents := []map[string]any{
		{"_id": 1, "id": 1, "name": "Alice", "email": ServiceAccountEmail},
		{"_id": 14, "id": 2, "name": "FakeAlice", "email": "fakeAlice@gmail.com"},
		{"_id": 2, "id": 2, "name": "Jane"},
		{"_id": 3, "id": 3, "name": "Sid"},
		{"_id": 5, "id": 3, "name": "Alice", "email": "alice@gmail.com"},
		{"_id": 6, "id": 100, "name": "ToBeDeleted", "email": "bob@gmail.com"},
		{"_id": 7, "id": 101, "name": "ToBeDeleted", "email": "bob1@gmail.com"},
		{"_id": 8, "id": 101, "name": "ToBeDeleted", "email": "bob2@gmail.com"},
		{"_id": 9, "id": 300, "name": "ToBeUpdatedToBob", "email": "bob@gmail.com"},
		{"_id": 10, "id": 400, "name": "ToBeUpdatedToAlice", "email": "alice@gmail.com"},
		{"_id": 11, "id": 400, "name": "ToBeUpdatedToAlice", "email": "alice@gmail.com"},
		{"_id": 12, "id": 500, "name": "ToBeAggregated", "email": "agatha@gmail.com"},
		{"_id": 13, "id": 501, "name": "ToBeAggregated", "email": "agatha@gmail.com"},
	}
	for _, doc := range documents {
		_, err := database.Collection(collectionName).InsertOne(ctx, doc)
		if err != nil {
			t.Fatalf("unable to insert test data: %s", err)
		}
	}

	return func(t *testing.T) {
		// tear down test
		err := database.Collection(collectionName).Drop(ctx)
		if err != nil {
			t.Errorf("Teardown failed: %s", err)
		}
	}

}