blob: cca419be0acf4448529cc331f0da0ef5fccba5dc [file] [log] [blame]
package db
import (
"context"
"testing"
"time"
"github.com/stretchr/testify/require"
"go.skia.org/infra/go/firestore/testutils"
"go.skia.org/infra/go/sktest"
)
const (
testDBKey = "test_key"
testIssueId = int64(100)
)
// newDBClientForTesting returns a FirestoreDB client and ensures that it will
// connect to the Firestore emulator. The Client's instance name will be
// randomized to ensure concurrent tests don't interfere with each other. It also
// returns a CleanupFunc that closes the Client.
func newDBClientForTesting(ctx context.Context, t sktest.TestingT) *FirestoreDB {
c, cleanup := testutils.NewClientForTesting(ctx, t)
t.Cleanup(cleanup)
return &FirestoreDB{
client: c,
collectionName: NpmAuditDataCol,
}
}
func TestGetFromDB_EmptyResults_NoErrors(t *testing.T) {
ctx := context.Background()
db := newDBClientForTesting(ctx, t)
// DB should be empty.
cherrypickData, err := db.GetFromDB(ctx, testDBKey)
require.Nil(t, cherrypickData)
require.NoError(t, err)
}
func TestPutInDB_OneEntry_ExpectedChangeNum(t *testing.T) {
db := newDBClientForTesting(context.Background(), t)
ctx := context.Background()
created := time.Now().UTC()
// DB should be empty. Add one entry.
err := db.PutInDB(ctx, testDBKey, testIssueId, created)
require.NoError(t, err)
// Get the added entry and assert.
npmAuditData, err := db.GetFromDB(ctx, testDBKey)
require.Equal(t, testIssueId, npmAuditData.IssueId)
require.Equal(t, created.Format("2006-01-02"), npmAuditData.Created.Format("2006-01-02"))
require.NoError(t, err)
}