Skip to content

Commit

Permalink
asd
Browse files Browse the repository at this point in the history
  • Loading branch information
chiaryan committed Nov 4, 2024
1 parent b0c13d3 commit 0aed69e
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 9 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -209,4 +209,6 @@ jobs:
- name: Deploy to Firebase
uses: w9jds/firebase-action@master
with:
args: emulators:start --only firestore
args: emulators:start --only firestore --project demo-test-project
env:
GCP_SA_KEY: "{}"
45 changes: 37 additions & 8 deletions apps/question-service/tests/read_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@ import (
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"question-service/handlers"
"question-service/utils"
"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"
)

Expand Down Expand Up @@ -49,18 +48,47 @@ func initFirestore(ctx context.Context) (*firestore.Client, error) {
return client, nil
}

var ctx = context.Background()
var client *firestore.Client

func TestMain(m *testing.M) {
err := godotenv.Load(filepath.Join("../", ".env"))
// fmt.Printf("test")
// Set FIRESTORE_EMULATOR_HOST environment variable.
err := os.Setenv("FIRESTORE_EMULATOR_HOST", "localhost:8390")
if err != nil {
// TODO: Handle error.
log.Fatalf("could not set env %v", err)
}
// Create client as usual.
client, err = firestore.NewClient(ctx, "my-project-id")
if err != nil {
log.Fatalf("Error loading .env file")
// TODO: Handle error.
log.Fatalf("could not create client %v", err)
}
service = createService()
defer service.Client.Close()
exitCode := m.Run()
os.Exit(exitCode)
defer client.Close()

m.Run()
os.Exit(0)
}

func setup(t *testing.T) string {
// Repopulate document
utils.Populate(client)

// Read the document
coll := client.Collection("questions")
if coll == nil {
t.Fatalf("Failed to get CollectionRef")
}
docRef, err := coll.DocumentRefs(ctx).Next()
if err != nil {
t.Fatalf("Failed to get DocRef: %v", err)
}
return docRef.ID
}
func Test_Read(t *testing.T) {
id = setup(t)

res := httptest.NewRecorder()

// adds chi context
Expand Down Expand Up @@ -94,4 +122,5 @@ func Test_ReadNotFound(t *testing.T) {
if res.Result().StatusCode != 404 {
t.Fatalf("expected status code 404 but got response %v", res.Result())
}

}
11 changes: 11 additions & 0 deletions apps/question-service/tests/wtf.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package tests

type A struct{}

func (a A) Wat() {

}

func (a A) Wot() {
a.Wat()
}

0 comments on commit 0aed69e

Please sign in to comment.