Skip to content
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

Add missing database tests #193

Merged
merged 4 commits into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions builder/store/database/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ func NewEventStore() EventStore {
}
}

func NewEventStoreWithDB(db *DB) EventStore {
return &eventStoreImpl{
db: db,
}
}

func (s *eventStoreImpl) Save(ctx context.Context, event Event) error {
return assertAffectedOneRow(s.db.Core.NewInsert().Model(&event).Exec(ctx))
}
Expand Down
39 changes: 39 additions & 0 deletions builder/store/database/event_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package database_test

import (
"context"
"testing"

"github.com/stretchr/testify/require"
"opencsg.com/csghub-server/builder/store/database"
"opencsg.com/csghub-server/common/tests"
)

func TestEventStore_Save(t *testing.T) {
db := tests.InitTestDB()
defer db.Close()
ctx := context.TODO()

store := database.NewEventStoreWithDB(db)
err := store.Save(ctx, database.Event{
Module: "m1",
})
require.Nil(t, err)
event := &database.Event{}
err = db.Core.NewSelect().Model(event).Where("module=?", "m1").Scan(ctx)
require.Nil(t, err)
require.Equal(t, "m1", event.Module)

err = store.BatchSave(ctx, []database.Event{
{Module: "m2"},
{Module: "m3"},
})
require.Nil(t, err)
err = db.Core.NewSelect().Model(event).Where("module=?", "m2").Scan(ctx)
require.Nil(t, err)
require.Equal(t, "m2", event.Module)
err = db.Core.NewSelect().Model(event).Where("module=?", "m3").Scan(ctx)
require.Nil(t, err)
require.Equal(t, "m3", event.Module)

}
8 changes: 7 additions & 1 deletion builder/store/database/lfs_lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ func NewLfsLockStore() LfsLockStore {
}
}

func NewLfsLockStoreWithDB(db *DB) LfsLockStore {
return &lfsLockStoreImpl{
db: db,
}
}

type LfsLock struct {
ID int64 `bun:",pk,autoincrement" json:"id"`
RepositoryID int64 `bun:",notnull" json:"repository_id"`
Expand All @@ -37,7 +43,7 @@ func (s *lfsLockStoreImpl) FindByID(ctx context.Context, ID int64) (*LfsLock, er
err := s.db.Operator.Core.NewSelect().
Model(&lfsLock).
Relation("User").
Where("id = ?", ID).
Where("lfs_lock.id = ?", ID).
Scan(ctx)
if err != nil {
return nil, err
Expand Down
47 changes: 47 additions & 0 deletions builder/store/database/lfs_lock_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package database_test

import (
"context"
"testing"

"github.com/stretchr/testify/require"
"opencsg.com/csghub-server/builder/store/database"
"opencsg.com/csghub-server/common/tests"
)

func TestLfsLockStore_CRUD(t *testing.T) {
db := tests.InitTestDB()
defer db.Close()
ctx := context.TODO()

store := database.NewLfsLockStoreWithDB(db)
_, err := store.Create(ctx, database.LfsLock{
RepositoryID: 123,
Path: "foo/bar",
})
require.Nil(t, err)

lock := &database.LfsLock{}
err = db.Core.NewSelect().Model(lock).Scan(ctx)
require.Nil(t, err)
require.Equal(t, "foo/bar", lock.Path)

lock, err = store.FindByID(ctx, lock.ID)
require.Nil(t, err)
require.Equal(t, "foo/bar", lock.Path)

lock, err = store.FindByPath(ctx, 123, "foo/bar")
require.Nil(t, err)
require.Equal(t, "foo/bar", lock.Path)

ls, err := store.FindByRepoID(ctx, 123, 1, 10)
require.Nil(t, err)
require.Equal(t, 1, len(ls))
require.Equal(t, "foo/bar", ls[0].Path)

err = store.RemoveByID(ctx, lock.ID)
require.Nil(t, err)
_, err = store.FindByID(ctx, lock.ID)
require.NotNil(t, err)

}
10 changes: 8 additions & 2 deletions builder/store/database/lfs_meta_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ func NewLfsMetaObjectStore() LfsMetaObjectStore {
}
}

func NewLfsMetaObjectStoreWithDB(db *DB) LfsMetaObjectStore {
return &lfsMetaObjectStoreImpl{
db: db,
}
}

type LfsMetaObject struct {
ID int64 `bun:",pk,autoincrement" json:"user_id"`
Oid string `bun:",notnull" json:"oid"`
Expand Down Expand Up @@ -70,10 +76,10 @@ func (s *lfsMetaObjectStoreImpl) Create(ctx context.Context, lfsObj LfsMetaObjec
}

func (s *lfsMetaObjectStoreImpl) RemoveByOid(ctx context.Context, oid string, repoID int64) error {
err := s.db.Operator.Core.NewDelete().
_, err := s.db.Operator.Core.NewDelete().
Model(&LfsMetaObject{}).
Where("oid = ? and repository_id= ?", oid, repoID).
Scan(ctx)
Exec(ctx)

return err
}
Expand Down
82 changes: 82 additions & 0 deletions builder/store/database/lfs_meta_object_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package database_test

import (
"context"
"testing"

"github.com/stretchr/testify/require"
"opencsg.com/csghub-server/builder/store/database"
"opencsg.com/csghub-server/common/tests"
)

func TestLfsMetaStore_CRUD(t *testing.T) {
db := tests.InitTestDB()
defer db.Close()
ctx := context.TODO()

store := database.NewLfsMetaObjectStoreWithDB(db)
_, err := store.Create(ctx, database.LfsMetaObject{
RepositoryID: 123,
Oid: "foobar",
})
require.Nil(t, err)

obj := &database.LfsMetaObject{}
err = db.Core.NewSelect().Model(obj).Scan(ctx)
require.Nil(t, err)
require.Equal(t, "foobar", obj.Oid)

obj, err = store.FindByOID(ctx, 123, "foobar")
require.Nil(t, err)
require.Equal(t, "foobar", obj.Oid)

objs, err := store.FindByRepoID(ctx, 123)
require.Nil(t, err)
require.Equal(t, 1, len(objs))
require.Equal(t, "foobar", objs[0].Oid)

// update
_, err = store.UpdateOrCreate(ctx, database.LfsMetaObject{
RepositoryID: 123,
Oid: "foobar",
Size: 999,
})
require.Nil(t, err)
obj, err = store.FindByOID(ctx, 123, "foobar")
require.Nil(t, err)
require.Equal(t, 999, int(obj.Size))

// create
_, err = store.UpdateOrCreate(ctx, database.LfsMetaObject{
RepositoryID: 456,
Oid: "bar",
Size: 998,
})
require.Nil(t, err)
obj, err = store.FindByOID(ctx, 456, "bar")
require.Nil(t, err)
require.Equal(t, 998, int(obj.Size))

err = store.BulkUpdateOrCreate(ctx, []database.LfsMetaObject{
{RepositoryID: 123, Oid: "foobar", Size: 1},
{RepositoryID: 456, Oid: "bar", Size: 2},
{RepositoryID: 789, Oid: "barfoo", Size: 3},
})
require.Nil(t, err)

obj, err = store.FindByOID(ctx, 123, "foobar")
require.Nil(t, err)
require.Equal(t, 1, int(obj.Size))
obj, err = store.FindByOID(ctx, 456, "bar")
require.Nil(t, err)
require.Equal(t, 2, int(obj.Size))
obj, err = store.FindByOID(ctx, 789, "barfoo")
require.Nil(t, err)
require.Equal(t, 3, int(obj.Size))

err = store.RemoveByOid(ctx, "foobar", 123)
require.Nil(t, err)
_, err = store.FindByOID(ctx, 123, "foobar")
require.NotNil(t, err)

}
4 changes: 4 additions & 0 deletions builder/store/database/llm_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ func NewLLMConfigStore() LLMConfigStore {
return &lLMConfigStoreImpl{db: defaultDB}
}

func NewLLMConfigStoreWithDB(db *DB) LLMConfigStore {
return &lLMConfigStoreImpl{db: db}
}

func (s *lLMConfigStoreImpl) GetOptimization(ctx context.Context) (*LLMConfig, error) {
var config LLMConfig
err := s.db.Operator.Core.NewSelect().Model(&config).Where("type = 1 and enabled = true").Limit(1).Scan(ctx)
Expand Down
40 changes: 40 additions & 0 deletions builder/store/database/llm_config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package database_test

import (
"context"
"testing"

"github.com/stretchr/testify/require"
"opencsg.com/csghub-server/builder/store/database"
"opencsg.com/csghub-server/common/tests"
)

func TestLLMConfigStore_GetOptimization(t *testing.T) {
db := tests.InitTestDB()
defer db.Close()
ctx := context.TODO()

store := database.NewLLMConfigStoreWithDB(db)
_, err := db.Core.NewInsert().Model(&database.LLMConfig{
Type: 1,
Enabled: true,
ModelName: "c1",
}).Exec(ctx)
require.Nil(t, err)
_, err = db.Core.NewInsert().Model(&database.LLMConfig{
Type: 2,
Enabled: true,
ModelName: "c2",
}).Exec(ctx)
require.Nil(t, err)
_, err = db.Core.NewInsert().Model(&database.LLMConfig{
Type: 1,
Enabled: false,
ModelName: "c3",
}).Exec(ctx)
require.Nil(t, err)

cfg, err := store.GetOptimization(ctx)
require.Nil(t, err)
require.Equal(t, "c1", cfg.ModelName)
}
6 changes: 6 additions & 0 deletions builder/store/database/member.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ func NewMemberStore() MemberStore {
}
}

func NewMemberStoreWithDB(db *DB) MemberStore {
return &memberStoreImpl{
db: db,
}
}

// Member is the relationship between a user and an organization.
type Member struct {
ID int64 `bun:",pk,autoincrement" json:"id"`
Expand Down
46 changes: 46 additions & 0 deletions builder/store/database/member_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package database_test

import (
"context"
"testing"

"github.com/stretchr/testify/require"
"opencsg.com/csghub-server/builder/store/database"
"opencsg.com/csghub-server/common/tests"
)

func TestMemberStore_CRUD(t *testing.T) {
db := tests.InitTestDB()
defer db.Close()
ctx := context.TODO()

store := database.NewMemberStoreWithDB(db)

err := store.Add(ctx, 123, 456, "foo")
require.Nil(t, err)
mem := &database.Member{}
err = db.Core.NewSelect().Model(mem).Where("user_id=?", 456).Scan(ctx)
require.Nil(t, err)
require.Equal(t, "foo", mem.Role)

mem, err = store.Find(ctx, 123, 456)
require.Nil(t, err)
require.Equal(t, "foo", mem.Role)

ms, err := store.UserMembers(ctx, 456)
require.Nil(t, err)
require.Equal(t, 1, len(ms))
require.Equal(t, "foo", ms[0].Role)

ms, count, err := store.OrganizationMembers(ctx, 123, 10, 1)
require.Nil(t, err)
require.Equal(t, 1, len(ms))
require.Equal(t, 1, count)
require.Equal(t, "foo", ms[0].Role)

err = store.Delete(ctx, 123, 456, "foo")
require.Nil(t, err)
_, err = store.Find(ctx, 123, 456)
require.NotNil(t, err)

}
8 changes: 7 additions & 1 deletion builder/store/database/mirror.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ func NewMirrorStore() MirrorStore {
}
}

func NewMirrorStoreWithDB(db *DB) MirrorStore {
return &mirrorStoreImpl{
db: db,
}
}

type Mirror struct {
ID int64 `bun:",pk,autoincrement" json:"id"`
Interval string `bun:",notnull" json:"interval"`
Expand Down Expand Up @@ -185,7 +191,7 @@ func (s *mirrorStoreImpl) WithPaginationWithRepository(ctx context.Context) ([]M
var mirrors []Mirror
err := s.db.Operator.Core.NewSelect().
Model(&mirrors).
Relation("Repositoy").
Relation("Repository").
Scan(ctx)
if err != nil {
return nil, err
Expand Down
6 changes: 6 additions & 0 deletions builder/store/database/mirror_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ func NewMirrorSourceStore() MirrorSourceStore {
}
}

func NewMirrorSourceStoreWithDB(db *DB) MirrorSourceStore {
return &mirrorSourceStoreImpl{
db: db,
}
}

type MirrorSource struct {
ID int64 `bun:",pk,autoincrement" json:"id"`
SourceName string `bun:",notnull,unique" json:"source_name"`
Expand Down
Loading
Loading