Skip to content

Commit bcd29d3

Browse files
committed
merge n11
1 parent da6001d commit bcd29d3

File tree

2 files changed

+48
-51
lines changed

2 files changed

+48
-51
lines changed

.github/workflows/test.yml

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,16 @@ jobs:
4343
with:
4444
go-version: '1.23.x'
4545

46-
- name: Install dependencies
46+
- name: Install Go dependencies
4747
run: |
4848
cd ./apps/question-service
4949
go mod tidy
5050
51-
- name: Run tests
52-
run: |
53-
cd ./apps/question-service
54-
go test ./...
51+
- name: Install firebase tools
52+
run: curl -sL firebase.tools | bash
53+
54+
- name: Run Go tests with Firebase emulator
55+
run: firebase emulators:exec --only firestore 'cd ./apps/question-service'
5556

5657
frontend-unit-tests:
5758
runs-on: ubuntu-latest
@@ -83,7 +84,7 @@ jobs:
8384
cd ./apps/frontend
8485
pnpm test
8586
86-
test:
87+
test-docker-compose:
8788
runs-on: ubuntu-latest
8889

8990
steps:

apps/question-service/tests/read_test.go

Lines changed: 41 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -2,74 +2,74 @@ package tests
22

33
import (
44
"context"
5-
"fmt"
65
"log"
76
"net/http"
87
"net/http/httptest"
98
"os"
10-
"path/filepath"
119
"question-service/handlers"
10+
"question-service/utils"
1211
"strings"
1312
"testing"
1413

1514
"cloud.google.com/go/firestore"
16-
firebase "firebase.google.com/go/v4"
1715
"github.com/go-chi/chi/v5"
18-
"github.com/joho/godotenv"
19-
"google.golang.org/api/option"
2016
)
2117

2218
var service *handlers.Service
19+
var ctx = context.Background()
2320

24-
func createService() *handlers.Service {
25-
26-
ctx := context.Background()
27-
client, err := initFirestore(ctx)
28-
21+
func TestMain(m *testing.M) {
22+
// Set FIRESTORE_EMULATOR_HOST environment variable.
23+
err := os.Setenv("FIRESTORE_EMULATOR_HOST", "localhost:8080")
2924
if err != nil {
30-
log.Fatalf("failed to initialize Firestore: %v", err)
25+
log.Fatalf("could not set env %v", err)
3126
}
27+
// Create client.
28+
client, err := firestore.NewClient(ctx, "my-project-id")
29+
service = &handlers.Service{Client: client}
3230

33-
return &handlers.Service{Client: client}
34-
}
35-
36-
// initFirestore initializes the Firestore client
37-
func initFirestore(ctx context.Context) (*firestore.Client, error) {
38-
credentialsPath := "../" + os.Getenv("FIREBASE_CREDENTIAL_PATH")
39-
opt := option.WithCredentialsFile(credentialsPath)
40-
app, err := firebase.NewApp(ctx, nil, opt)
4131
if err != nil {
42-
return nil, fmt.Errorf("failed to initialize Firebase App: %v", err)
32+
log.Fatalf("could not create client %v", err)
4333
}
34+
defer client.Close()
4435

45-
client, err := app.Firestore(ctx)
46-
if err != nil {
47-
return nil, fmt.Errorf("failed to get Firestore client: %v", err)
48-
}
49-
return client, nil
36+
m.Run()
37+
os.Exit(0)
5038
}
5139

52-
func TestMain(m *testing.M) {
53-
err := godotenv.Load(filepath.Join("../", ".env"))
40+
// Sets up the firestore emulator with the sample questions
41+
// This repopulates the db
42+
// Returns the docref of one of the questions if a test need it
43+
func setupDb(t *testing.T) string {
44+
// Repopulate document
45+
utils.Populate(service.Client)
46+
47+
coll := service.Client.Collection("questions")
48+
if coll == nil {
49+
t.Fatalf("Failed to get CollectionRef")
50+
}
51+
docRef, err := coll.DocumentRefs(ctx).Next()
5452
if err != nil {
55-
log.Fatalf("Error loading .env file")
53+
t.Fatalf("Failed to get DocRef: %v", err)
5654
}
57-
service = createService()
58-
defer service.Client.Close()
59-
exitCode := m.Run()
60-
os.Exit(exitCode)
55+
return docRef.ID
6156
}
6257

63-
func Test_Read(t *testing.T) {
64-
res := httptest.NewRecorder()
65-
58+
func ReadRequestWithId(id string) *http.Request {
6659
// adds chi context
6760
// https://stackoverflow.com/questions/54580582/testing-chi-routes-w-path-variables
6861
rctx := chi.NewRouteContext()
69-
rctx.URLParams.Add("docRefID", "6SdbW4Awcfm5x0UQtWmg")
62+
rctx.URLParams.Add("docRefID", id)
7063

71-
req := httptest.NewRequest(http.MethodGet, "http://localhost:8080/questions/6SdbW4Awcfm5x0UQtWmg", strings.NewReader(""))
64+
req := httptest.NewRequest(http.MethodGet, "http://localhost:12345/questions/"+id, strings.NewReader(""))
7265
req = req.WithContext(context.WithValue(req.Context(), chi.RouteCtxKey, rctx))
66+
return req
67+
}
68+
func Test_Read(t *testing.T) {
69+
id := setupDb(t)
70+
71+
res := httptest.NewRecorder()
72+
req := ReadRequestWithId(id)
7373

7474
service.ReadQuestion(res, req)
7575

@@ -79,19 +79,15 @@ func Test_Read(t *testing.T) {
7979
}
8080

8181
func Test_ReadNotFound(t *testing.T) {
82-
res := httptest.NewRecorder()
83-
84-
// adds chi context
85-
// https://stackoverflow.com/questions/54580582/testing-chi-routes-w-path-variables
86-
rctx := chi.NewRouteContext()
87-
rctx.URLParams.Add("docRefID", "not-found-docref")
82+
setupDb(t)
8883

89-
req := httptest.NewRequest(http.MethodGet, "http://localhost:8080/questions/not-found-docref", strings.NewReader(""))
90-
req = req.WithContext(context.WithValue(req.Context(), chi.RouteCtxKey, rctx))
84+
res := httptest.NewRecorder()
85+
req := ReadRequestWithId("invalid-docref")
9186

9287
service.ReadQuestion(res, req)
9388

9489
if res.Result().StatusCode != 404 {
9590
t.Fatalf("expected status code 404 but got response %v", res.Result())
9691
}
92+
9793
}

0 commit comments

Comments
 (0)