@@ -2,74 +2,74 @@ package tests
2
2
3
3
import (
4
4
"context"
5
- "fmt"
6
5
"log"
7
6
"net/http"
8
7
"net/http/httptest"
9
8
"os"
10
- "path/filepath"
11
9
"question-service/handlers"
10
+ "question-service/utils"
12
11
"strings"
13
12
"testing"
14
13
15
14
"cloud.google.com/go/firestore"
16
- firebase "firebase.google.com/go/v4"
17
15
"github.com/go-chi/chi/v5"
18
- "github.com/joho/godotenv"
19
- "google.golang.org/api/option"
20
16
)
21
17
22
18
var service * handlers.Service
19
+ var ctx = context .Background ()
23
20
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" )
29
24
if err != nil {
30
- log .Fatalf ("failed to initialize Firestore: %v" , err )
25
+ log .Fatalf ("could not set env %v" , err )
31
26
}
27
+ // Create client.
28
+ client , err := firestore .NewClient (ctx , "my-project-id" )
29
+ service = & handlers.Service {Client : client }
32
30
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 )
41
31
if err != nil {
42
- return nil , fmt . Errorf ( "failed to initialize Firebase App: %v" , err )
32
+ log . Fatalf ( "could not create client %v" , err )
43
33
}
34
+ defer client .Close ()
44
35
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 )
50
38
}
51
39
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 ()
54
52
if err != nil {
55
- log .Fatalf ("Error loading .env file" )
53
+ t .Fatalf ("Failed to get DocRef: %v" , err )
56
54
}
57
- service = createService ()
58
- defer service .Client .Close ()
59
- exitCode := m .Run ()
60
- os .Exit (exitCode )
55
+ return docRef .ID
61
56
}
62
57
63
- func Test_Read (t * testing.T ) {
64
- res := httptest .NewRecorder ()
65
-
58
+ func ReadRequestWithId (id string ) * http.Request {
66
59
// adds chi context
67
60
// https://stackoverflow.com/questions/54580582/testing-chi-routes-w-path-variables
68
61
rctx := chi .NewRouteContext ()
69
- rctx .URLParams .Add ("docRefID" , "6SdbW4Awcfm5x0UQtWmg" )
62
+ rctx .URLParams .Add ("docRefID" , id )
70
63
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 ("" ))
72
65
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 )
73
73
74
74
service .ReadQuestion (res , req )
75
75
@@ -79,19 +79,15 @@ func Test_Read(t *testing.T) {
79
79
}
80
80
81
81
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 )
88
83
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" )
91
86
92
87
service .ReadQuestion (res , req )
93
88
94
89
if res .Result ().StatusCode != 404 {
95
90
t .Fatalf ("expected status code 404 but got response %v" , res .Result ())
96
91
}
92
+
97
93
}
0 commit comments