Skip to content

Commit

Permalink
add "unit test" for ReadQuestion
Browse files Browse the repository at this point in the history
  • Loading branch information
chiaryan committed Nov 3, 2024
1 parent 5a8f8ff commit fdb4c1a
Showing 1 changed file with 71 additions and 0 deletions.
71 changes: 71 additions & 0 deletions apps/question-service/tests/read_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package tests

import (
"context"
"fmt"
"log"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"question-service/handlers"
"strings"
"testing"

"cloud.google.com/go/firestore"
firebase "firebase.google.com/go/v4"
"github.com/go-chi/chi/v5"
"github.com/joho/godotenv"
"google.golang.org/api/option"
)

func createService(t testing.TB) *handlers.Service {
err := godotenv.Load(filepath.Join("../", ".env"))
if err != nil {
log.Fatalf("Error loading .env file")
}

ctx := context.Background()
client, err := initFirestore(ctx)

if err != nil {
t.Fatalf("failed to initialize Firestore: %v", err)
}

return &handlers.Service{Client: client}
}

// initFirestore initializes the Firestore client
func initFirestore(ctx context.Context) (*firestore.Client, error) {
credentialsPath := "../" + os.Getenv("FIREBASE_CREDENTIAL_PATH")
opt := option.WithCredentialsFile(credentialsPath)
app, err := firebase.NewApp(ctx, nil, opt)
if err != nil {
return nil, fmt.Errorf("failed to initialize Firebase App: %v", err)
}

client, err := app.Firestore(ctx)
if err != nil {
return nil, fmt.Errorf("failed to get Firestore client: %v", err)
}
return client, nil
}

func Test_Read(t *testing.T) {
service := createService(t)
res := httptest.NewRecorder()

// adds chi context
// https://stackoverflow.com/questions/54580582/testing-chi-routes-w-path-variables
rctx := chi.NewRouteContext()
rctx.URLParams.Add("docRefID", "6SdbW4Awcfm5x0UQtWmg")

req := httptest.NewRequest(http.MethodGet, "http://localhost:8080/questions/6SdbW4Awcfm5x0UQtWmg", strings.NewReader(""))
req = req.WithContext(context.WithValue(req.Context(), chi.RouteCtxKey, rctx))

service.ReadQuestion(res, req)

if res.Result().StatusCode != 200 {
t.Fatalf("expected status code 200 but got %v", res.Result())
}
}

0 comments on commit fdb4c1a

Please sign in to comment.