Skip to content

Commit

Permalink
dev: Add round-trip test for Recurser collection
Browse files Browse the repository at this point in the history
  • Loading branch information
jdkaplan committed Aug 6, 2024
1 parent 27126de commit e212195
Showing 1 changed file with 113 additions and 0 deletions.
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
}

0 comments on commit e212195

Please sign in to comment.