Skip to content

db: migrate org.go to organizations.go with GORM #7538

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

Draft
wants to merge 29 commits into
base: main
Choose a base branch
from
Draft
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
orgsList
  • Loading branch information
unknwon committed Dec 17, 2023
commit cbd3c7a40b6e2d28c9e9af0374cff0b9751d3b89
45 changes: 41 additions & 4 deletions internal/db/organizations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ func TestOrganizations(t *testing.T) {
}{
{"Create", orgsCreate},
{"GetByName", orgsGetByName},
{"List", orgsList},
{"SearchByName", orgsSearchByName},
{"List", orgsList},
{"CountByUser", orgsCountByUser},
} {
t.Run(tc.name, func(t *testing.T) {
Expand Down Expand Up @@ -150,15 +150,17 @@ func orgsList(t *testing.T, ctx context.Context, db *organizations) {
tempPictureAvatarUploadPath := filepath.Join(os.TempDir(), "orgsList-tempPictureAvatarUploadPath")
conf.SetMockPicture(t, conf.PictureOpts{AvatarUploadPath: tempPictureAvatarUploadPath})

org1, err := db.Create(ctx, "org1", alice.ID, CreateOrganizationOptions{})
org1, err := db.Create(ctx, "org1-alice-owned", alice.ID, CreateOrganizationOptions{})
require.NoError(t, err)
org2, err := db.Create(ctx, "org2", alice.ID, CreateOrganizationOptions{})

org2, err := db.Create(ctx, "org2-alice-owned", alice.ID, CreateOrganizationOptions{})
require.NoError(t, err)
err = db.SetMemberVisibility(ctx, org2.ID, alice.ID, true)
require.NoError(t, err)
err = db.AddMember(ctx, org2.ID, bob.ID)
require.NoError(t, err)
err = db.SetMemberVisibility(ctx, org2.ID, alice.ID, true)

org3, err := db.Create(ctx, "org3-bob-owned", bob.ID, CreateOrganizationOptions{})
require.NoError(t, err)

tests := []struct {
Expand All @@ -182,6 +184,22 @@ func orgsList(t *testing.T, ctx context.Context, db *organizations) {
},
wantOrgNames: []string{org1.Name, org2.Name},
},
{
name: "only public ownership for a user",
opts: ListOrganizationsOptions{
OwnerID: alice.ID,
IncludePrivateMembers: false,
},
wantOrgNames: []string{org2.Name},
},
{
name: "all ownership for a user",
opts: ListOrganizationsOptions{
OwnerID: alice.ID,
IncludePrivateMembers: true,
},
wantOrgNames: []string{org1.Name, org2.Name},
},
{
name: "no membership for a non-existent user",
opts: ListOrganizationsOptions{
Expand All @@ -203,6 +221,25 @@ func orgsList(t *testing.T, ctx context.Context, db *organizations) {
assert.Equal(t, test.wantOrgNames, gotOrgNames)
})
}

t.Run("pagination", func(t *testing.T) {
got, err := db.List(ctx, ListOrganizationsOptions{Page: 1, PageSize: 1})
require.NoError(t, err)
require.Len(t, got, 1)
assert.Equal(t, org1.ID, got[0].ID)

got, err = db.List(ctx, ListOrganizationsOptions{Page: 2, PageSize: 1})
require.NoError(t, err)
require.Len(t, got, 1)
assert.Equal(t, org2.ID, got[0].ID)

got, err = db.List(ctx, ListOrganizationsOptions{Page: 1, PageSize: 4})
require.NoError(t, err)
require.Len(t, got, 3)
assert.Equal(t, org1.ID, got[0].ID)
assert.Equal(t, org2.ID, got[1].ID)
assert.Equal(t, org3.ID, got[2].ID)
})
}

func orgsSearchByName(t *testing.T, ctx context.Context, db *organizations) {
Expand Down