mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
feat: Add database data generator to make fakedbs easier to populate (#5922)
* feat: Add database data generator to make fakedbs easier to populate
This commit is contained in:
@@ -13,6 +13,7 @@ import (
|
||||
|
||||
"github.com/coder/coder/coderd/database"
|
||||
"github.com/coder/coder/coderd/database/databasefake"
|
||||
"github.com/coder/coder/coderd/database/dbgen"
|
||||
"github.com/coder/coder/coderd/prometheusmetrics"
|
||||
"github.com/coder/coder/codersdk"
|
||||
"github.com/coder/coder/testutil"
|
||||
@@ -23,63 +24,50 @@ func TestActiveUsers(t *testing.T) {
|
||||
|
||||
for _, tc := range []struct {
|
||||
Name string
|
||||
Database func() database.Store
|
||||
Database func(t *testing.T) database.Store
|
||||
Count int
|
||||
}{{
|
||||
Name: "None",
|
||||
Database: func() database.Store {
|
||||
Database: func(t *testing.T) database.Store {
|
||||
return databasefake.New()
|
||||
},
|
||||
Count: 0,
|
||||
}, {
|
||||
Name: "One",
|
||||
Database: func() database.Store {
|
||||
Database: func(t *testing.T) database.Store {
|
||||
db := databasefake.New()
|
||||
_, _ = db.InsertAPIKey(context.Background(), database.InsertAPIKeyParams{
|
||||
UserID: uuid.New(),
|
||||
LastUsed: database.Now(),
|
||||
LoginType: database.LoginTypePassword,
|
||||
Scope: database.APIKeyScopeAll,
|
||||
dbgen.APIKey(t, db, database.APIKey{
|
||||
LastUsed: database.Now(),
|
||||
})
|
||||
return db
|
||||
},
|
||||
Count: 1,
|
||||
}, {
|
||||
Name: "OneWithExpired",
|
||||
Database: func() database.Store {
|
||||
Database: func(t *testing.T) database.Store {
|
||||
db := databasefake.New()
|
||||
_, _ = db.InsertAPIKey(context.Background(), database.InsertAPIKeyParams{
|
||||
UserID: uuid.New(),
|
||||
LastUsed: database.Now(),
|
||||
LoginType: database.LoginTypePassword,
|
||||
Scope: database.APIKeyScopeAll,
|
||||
|
||||
dbgen.APIKey(t, db, database.APIKey{
|
||||
LastUsed: database.Now(),
|
||||
})
|
||||
|
||||
// Because this API key hasn't been used in the past hour, this shouldn't
|
||||
// add to the user count.
|
||||
_, _ = db.InsertAPIKey(context.Background(), database.InsertAPIKeyParams{
|
||||
UserID: uuid.New(),
|
||||
LastUsed: database.Now().Add(-2 * time.Hour),
|
||||
LoginType: database.LoginTypePassword,
|
||||
Scope: database.APIKeyScopeAll,
|
||||
dbgen.APIKey(t, db, database.APIKey{
|
||||
LastUsed: database.Now().Add(-2 * time.Hour),
|
||||
})
|
||||
return db
|
||||
},
|
||||
Count: 1,
|
||||
}, {
|
||||
Name: "Multiple",
|
||||
Database: func() database.Store {
|
||||
Database: func(t *testing.T) database.Store {
|
||||
db := databasefake.New()
|
||||
_, _ = db.InsertAPIKey(context.Background(), database.InsertAPIKeyParams{
|
||||
UserID: uuid.New(),
|
||||
LastUsed: database.Now(),
|
||||
LoginType: database.LoginTypePassword,
|
||||
Scope: database.APIKeyScopeAll,
|
||||
dbgen.APIKey(t, db, database.APIKey{
|
||||
LastUsed: database.Now(),
|
||||
})
|
||||
_, _ = db.InsertAPIKey(context.Background(), database.InsertAPIKeyParams{
|
||||
UserID: uuid.New(),
|
||||
LastUsed: database.Now(),
|
||||
LoginType: database.LoginTypePassword,
|
||||
Scope: database.APIKeyScopeAll,
|
||||
dbgen.APIKey(t, db, database.APIKey{
|
||||
LastUsed: database.Now(),
|
||||
})
|
||||
return db
|
||||
},
|
||||
@@ -89,7 +77,7 @@ func TestActiveUsers(t *testing.T) {
|
||||
t.Run(tc.Name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
registry := prometheus.NewRegistry()
|
||||
cancel, err := prometheusmetrics.ActiveUsers(context.Background(), registry, tc.Database(), time.Millisecond)
|
||||
cancel, err := prometheusmetrics.ActiveUsers(context.Background(), registry, tc.Database(t), time.Millisecond)
|
||||
require.NoError(t, err)
|
||||
t.Cleanup(cancel)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user