🌐 AI搜索 & 代理 主页
Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
orgsGetByName
  • Loading branch information
unknwon committed Dec 17, 2023
commit 26f8ddfca070be5d5609a9819d4f547dc4286bb9
28 changes: 28 additions & 0 deletions internal/db/organizations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func TestOrganizations(t *testing.T) {
test func(t *testing.T, ctx context.Context, db *organizations)
}{
{"Create", orgsCreate},
{"GetByName", orgsGetByName},
{"List", orgsList},
{"SearchByName", orgsSearchByName},
{"CountByUser", orgsCountByUser},
Expand Down Expand Up @@ -112,6 +113,33 @@ func orgsCreate(t *testing.T, ctx context.Context, db *organizations) {
assert.Equal(t, db.NowFunc().Format(time.RFC3339), got.Updated.UTC().Format(time.RFC3339))
}

func orgsGetByName(t *testing.T, ctx context.Context, db *organizations) {
t.Run("correct user type", func(t *testing.T) {
tempPictureAvatarUploadPath := filepath.Join(os.TempDir(), "usersGetByUsername-tempPictureAvatarUploadPath")
conf.SetMockPicture(t, conf.PictureOpts{AvatarUploadPath: tempPictureAvatarUploadPath})

org1, err := db.Create(ctx, "org1", 1, CreateOrganizationOptions{})
require.NoError(t, err)

got, err := db.GetByName(ctx, org1.Name)
require.NoError(t, err)
assert.Equal(t, org1.Name, got.Name)

_, err = db.GetByName(ctx, "bad_name")
wantErr := ErrOrganizationNotExist{args: errutil.Args{"name": "bad_name"}}
assert.Equal(t, wantErr, err)
})

t.Run("wrong user type", func(t *testing.T) {
alice, err := NewUsersStore(db.DB).Create(ctx, "alice", "alice@example.com", CreateUserOptions{})
require.NoError(t, err)

_, err = db.GetByName(ctx, alice.Name)
wantErr := ErrOrganizationNotExist{args: errutil.Args{"name": alice.Name}}
assert.Equal(t, wantErr, err)
})
}

func orgsList(t *testing.T, ctx context.Context, db *organizations) {
usersStore := NewUsersStore(db.DB)
alice, err := usersStore.Create(ctx, "alice", "alice@example.com", CreateUserOptions{})
Expand Down
4 changes: 2 additions & 2 deletions internal/db/users_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -797,9 +797,9 @@ func usersGetByUsername(t *testing.T, ctx context.Context, db *users) {
alice, err := db.Create(ctx, "alice", "alice@exmaple.com", CreateUserOptions{})
require.NoError(t, err)

user, err := db.GetByUsername(ctx, alice.Name)
got, err := db.GetByUsername(ctx, alice.Name)
require.NoError(t, err)
assert.Equal(t, alice.Name, user.Name)
assert.Equal(t, alice.Name, got.Name)

_, err = db.GetByUsername(ctx, "bad_username")
wantErr := ErrUserNotExist{args: errutil.Args{"name": "bad_username"}}
Expand Down