-
Notifications
You must be signed in to change notification settings - Fork 1.3k
[usage] store more data for in usage entry #12736
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
// Copyright (c) 2022 Gitpod GmbH. All rights reserved. | ||
// Licensed under the GNU Affero General Public License (AGPL). | ||
// See License-AGPL.txt in the project root for license information. | ||
|
||
package dbtest | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/gitpod-io/gitpod/usage/pkg/db" | ||
"github.com/google/uuid" | ||
"github.com/stretchr/testify/require" | ||
"gorm.io/gorm" | ||
) | ||
|
||
type User struct { | ||
ID uuid.UUID `gorm:"primary_key;column:id;type:char;size:36;"` | ||
AvatarURL string `gorm:"column:avatarUrl;type:char;size:255;"` | ||
Name string `gorm:"column:name;type:char;size:255;"` | ||
FullName string `gorm:"column:fullName;type:char;size:255;"` | ||
CreationDate db.VarcharTime `gorm:"column:creationDate;type:varchar;size:255;"` | ||
|
||
// user has more field but we don't care here as they are just used in tests. | ||
} | ||
|
||
func (user *User) TableName() string { | ||
return "d_b_user" | ||
} | ||
|
||
func NewUser(t *testing.T, user User) User { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's bit of an anti-pattern to have a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it's in a testing package There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry, I got confused. The User model definition should be moved out of the |
||
t.Helper() | ||
|
||
result := User{ | ||
ID: uuid.New(), | ||
AvatarURL: "https://avatars.githubusercontent.com/u/9071", | ||
Name: "HomerJSimpson", | ||
FullName: "Homer Simpson", | ||
CreationDate: db.NewVarcharTime(time.Now()), | ||
} | ||
|
||
if user.ID != uuid.Nil { | ||
result.ID = user.ID | ||
} | ||
|
||
if user.AvatarURL != "" { | ||
result.AvatarURL = user.AvatarURL | ||
} | ||
if user.Name != "" { | ||
result.Name = user.Name | ||
} | ||
if user.FullName != "" { | ||
result.FullName = user.FullName | ||
} | ||
|
||
return result | ||
} | ||
|
||
func CreatUser(t *testing.T, conn *gorm.DB, user ...User) []User { | ||
t.Helper() | ||
|
||
var records []User | ||
var ids []uuid.UUID | ||
for _, u := range user { | ||
record := NewUser(t, u) | ||
records = append(records, record) | ||
ids = append(ids, record.ID) | ||
} | ||
|
||
require.NoError(t, conn.CreateInBatches(&records, 1000).Error) | ||
|
||
t.Cleanup(func() { | ||
require.NoError(t, conn.Where(ids).Delete(&User{}).Error) | ||
}) | ||
|
||
return records | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
package dbtest | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
"testing" | ||
"time" | ||
|
@@ -23,7 +24,7 @@ func NewWorkspaceInstance(t *testing.T, instance db.WorkspaceInstance) db.Worksp | |
t.Helper() | ||
|
||
id := uuid.New() | ||
if instance.ID.ID() != 0 { // empty value | ||
if instance.ID != uuid.Nil { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Cool! I didn't know about this. |
||
id = instance.ID | ||
} | ||
|
||
|
@@ -120,3 +121,32 @@ func CreateWorkspaceInstances(t *testing.T, conn *gorm.DB, instances ...db.Works | |
|
||
return records | ||
} | ||
|
||
// ListWorkspaceInstancesInRange filters out instances by workspaceID to make tests robust and work only on their own data | ||
func ListWorkspaceInstancesInRange(t *testing.T, conn *gorm.DB, from, to time.Time, workspaceID string) []db.WorkspaceInstanceForUsage { | ||
all, err := db.ListWorkspaceInstancesInRange(context.Background(), conn, from, to) | ||
require.NoError(t, err) | ||
return filterByWorkspaceId(all, workspaceID) | ||
} | ||
|
||
func FindStoppedWorkspaceInstancesInRange(t *testing.T, conn *gorm.DB, from, to time.Time, workspaceID string) []db.WorkspaceInstanceForUsage { | ||
all, err := db.FindStoppedWorkspaceInstancesInRange(context.Background(), conn, from, to) | ||
require.NoError(t, err) | ||
return filterByWorkspaceId(all, workspaceID) | ||
} | ||
|
||
func FindRunningWorkspaceInstances(t *testing.T, conn *gorm.DB, workspaceID string) []db.WorkspaceInstanceForUsage { | ||
all, err := db.FindRunningWorkspaceInstances(context.Background(), conn) | ||
require.NoError(t, err) | ||
return filterByWorkspaceId(all, workspaceID) | ||
} | ||
|
||
func filterByWorkspaceId(all []db.WorkspaceInstanceForUsage, workspaceID string) []db.WorkspaceInstanceForUsage { | ||
result := []db.WorkspaceInstanceForUsage{} | ||
for _, candidate := range all { | ||
if candidate.WorkspaceID == workspaceID { | ||
result = append(result, candidate) | ||
} | ||
} | ||
return result | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be in the
db
package, not thedbtest
packageThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I put it here, because we don't need this in production code, it's only used for setting up testing state in DB.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see. I guess I can live with this, but I'd generally define it on the
db
package as someone else will come along and not find the User object there so they would go and define it again