-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
dev: Add round-trip test for Recurser collection (#64)
I want to be able to experiment with Pairing Bot locally, which means I'll need some way to run Firestore without also running all of Google Cloud. It turns out that there's an [emulator] inside of `gcloud` which is made exactly for this purpose! [emulator]: https://cloud.google.com/firestore/docs/emulator The Firestore Go SDK we're using automatically supports using this via the `FIRESTORE_EMULATOR_HOST` environment variable. ## Changes This sets up a single round-trip test to prove that this all works. I left space to add more because I'll be collecting them in the future as I learn about how Firestore works (and how we use it!). I also made these tests run on PRs to the `dev` branch. I intend to treat them as non-blocking there, but I do think it's nice to know that we've broken unit tests before deploying, even if it's just to the test bot. I also added a `dev.sh` script to help me remember how to start the emulator. It uses the same variable as the SDK so something like [direnv](https://direnv.net/) makes it "just work".
- Loading branch information
Showing
3 changed files
with
136 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |