Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge DB fixes from dev #71

Merged
merged 8 commits into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ name: Go

on:
push:
branches: [ main ]
branches:
- main
- dev
pull_request:
branches: [ main ]

jobs:

Expand All @@ -21,5 +22,20 @@ jobs:
- name: Build
run: go build -v ./...

- uses: google-github-actions/setup-gcloud@v2
with:
version: '>= 486.0.0'
install_components: 'cloud-firestore-emulator'

- name: Start Firestore emulator
run: |
gcloud emulators firestore start --host-port=[::1]:8410 &

until curl '[::1]:8410' 2>/dev/null; do
sleep 1
done

- name: Test
run: go test -v ./...
env:
FIRESTORE_EMULATOR_HOST: '[::1]:8410'
8 changes: 3 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
./pairing_bot
.*
TODO.txt
*.bak
pairing-bot
# Compiled binary
/pairing-bot
/pairing_bot
113 changes: 113 additions & 0 deletions database_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package main

import (
"context"
"crypto/rand"
"fmt"
"maps"
"math/big"
"testing"

"cloud.google.com/go/firestore"
)

func fakeProjectID(t *testing.T) string {
return fmt.Sprintf("fake-project-%d", randInt64(t))
}

func randInt64(t *testing.T) int64 {
int64Max := int64(1<<63 - 1)

n, err := rand.Int(rand.Reader, big.NewInt(int64Max))
if err != nil {
t.Fatal(err)
}
return n.Int64()
}

func testFirestoreClient(t *testing.T, ctx context.Context, projectID string) *firestore.Client {
client, err := firestore.NewClient(ctx, projectID)
if err != nil {
t.Fatal(err)
}

t.Cleanup(func() {
if err := client.Close(); err != nil {
t.Logf("Error closing Firestore client: %v", err)
}
})

return client
}

func TestFirestoreRecurserDB(t *testing.T) {
t.Run("round-trip new recurser", func(t *testing.T) {
ctx := context.Background()
projectID := fakeProjectID(t)

client := testFirestoreClient(t, ctx, projectID)
recursers := &FirestoreRecurserDB{client}

recurser := Recurser{
id: randInt64(t),
name: "Your Name",
email: "[email protected]",
isSkippingTomorrow: false,
schedule: map[string]any{
"monday": false,
"tuesday": false,
"wednesday": false,
"thursday": false,
"friday": false,
"saturday": false,
"sunday": false,
},
isSubscribed: false,
currentlyAtRC: false,
}

err := recursers.Set(ctx, recurser.id, recurser)
if err != nil {
t.Fatal(err)
}

// GetByUserID forces isSubscribed to be `true`, because that's implied by
// the record's existence in the DB in the first place.
expected := recurser
expected.isSubscribed = true

// GetByUserID can update the name and email address if our record is stale.
// These values are the same, so this call *does not* trigger that update.
unchanged, err := recursers.GetByUserID(ctx, recurser.id, recurser.email, recurser.name)
if err != nil {
t.Fatal(err)
}

if !unchanged.Equal(expected) {
t.Errorf("values not equal:\nactual: %+v\nexpected: %+v", unchanged, expected)
}

// These values are different, so this call *does* trigger an update.
changed, err := recursers.GetByUserID(ctx, recurser.id, "[email protected]", "My Name")
if err != nil {
t.Fatal(err)
}

expected.email = "[email protected]"
expected.name = "My Name"

if !changed.Equal(expected) {
t.Errorf("values not equal:\nactual: %+v\nexpected: %+v", unchanged, expected)
}
})
}

func (r Recurser) Equal(s Recurser) bool {
return r.id == s.id &&
r.name == s.name &&
r.email == s.email &&
r.isSkippingTomorrow == s.isSkippingTomorrow &&
maps.Equal(r.schedule, s.schedule) &&
r.isSubscribed == s.isSubscribed &&
r.currentlyAtRC == s.currentlyAtRC
}
5 changes: 5 additions & 0 deletions dev.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env bash

set -euo pipefail

exec gcloud emulators firestore start --host-port="$FIRESTORE_EMULATOR_HOST"
34 changes: 8 additions & 26 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ func main() {
// and the file:line (without the full path- we don't have directories.)
log.SetFlags(log.Ldate | log.Ltime | log.LUTC | log.Lshortfile)

// setting up database connection: 2 clients encapsulated into PairingLogic struct

ctx := context.Background()

appEnv := os.Getenv("APP_ENV")
Expand All @@ -34,48 +32,32 @@ func main() {
log.Println("Running pairing bot in the testing environment for development")
}

rc, err := firestore.NewClient(ctx, projectId)
if err != nil {
log.Panic(err)
}
defer rc.Close()

ac, err := firestore.NewClient(ctx, projectId)
if err != nil {
log.Panic(err)
}
defer ac.Close()

pc, err := firestore.NewClient(ctx, projectId)
if err != nil {
log.Panic(err)
}
defer pc.Close()

revc, err := firestore.NewClient(ctx, projectId)
// Set up database wrappers. The Firestore client has a connection pool, so
// we can share this one DB handle among all the collection helpers.
db, err := firestore.NewClient(ctx, projectId)
if err != nil {
log.Panic(err)
}
defer revc.Close()
defer db.Close()

rdb := &FirestoreRecurserDB{
client: rc,
client: db,
}

rcapi := RecurseAPI{
rcAPIURL: "https://www.recurse.com/api/v1",
}

adb := &FirestoreAPIAuthDB{
client: ac,
client: db,
}

pdb := &FirestorePairingsDB{
client: pc,
client: db,
}

revdb := &FirestoreReviewDB{
client: revc,
client: db,
}

ur := &zulipUserRequest{}
Expand Down
Loading