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

Misc Tests #60

Merged
merged 1 commit into from
Jan 3, 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
34 changes: 34 additions & 0 deletions internal/task/task_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,25 @@
package task

import (
"context"
"errors"
"testing"

"github.com/rs/zerolog"
"github.com/stretchr/testify/require"
)

func TestTaskMap(t *testing.T) {
t.Parallel()
require := require.New(t)

tm := TaskMap{
"test": NoOpTask,
}
require.NotNil(tm.Get("test"), "Task not registered")
require.Nil(tm.Get("test2"), "Task registered when it shouldn't be")
}

func TestConcurrentTaskMap(t *testing.T) {
t.Parallel()
require := require.New(t)
Expand All @@ -21,3 +35,23 @@ func TestConcurrentTaskMap(t *testing.T) {
require.Nil(tm.Get("test2"), "Task registered when it shouldn't be")

}

func Test_NoOpTask(t *testing.T) {
t.Parallel()
require := require.New(t)
logger := zerolog.New(nil)
ctx := logger.WithContext(context.Background())
m, err := NoOpTask(ctx, nil, func(progress float64) error {
require.Equal(1.0, progress)
return nil
})
require.NoError(err)
require.NotEmpty(m)

expectedErr := errors.New("")
_, err2 := NoOpTask(ctx, nil, func(progress float64) error {
return expectedErr
})
require.Error(err2)
require.Equal(expectedErr, err2)
}
26 changes: 25 additions & 1 deletion internal/viewer/viewer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,22 @@ import (
"github.com/stretchr/testify/assert"
)

func Test_EmptyViewer(t *testing.T) {
assert := assert.New(t)
ctx := NewContext(context.Background(), nil, nil)
v := FromContext(ctx)
assert.False(v.IsAdmin())
assert.Nil(v.User())
assert.Nil(v.Permissions())
}

func Test_NoViewer(t *testing.T) {
assert := assert.New(t)
ctx := context.Background()
v := FromContext(ctx)
assert.Nil(v)
}

func Test_UserViewerImplementsViewer(t *testing.T) {
assert := assert.New(t)
assert.NotPanics(func() {
Expand Down Expand Up @@ -87,6 +103,9 @@ func Test_NewContext(t *testing.T) {
u, ok := v.User()
assert.True(ok, "FromContext should return the user")
assert.Equal(expectedID, u.ID, "FromContext should return the same user")
p, ok := v.Permissions()
assert.True(ok, "FromContext should return the permissions")
assert.Equal(true, p.Admin, "FromContext should return the same permissions")
assert.True(v.IsAdmin(), "FromContext should return the same permissions")
}

Expand All @@ -101,7 +120,12 @@ func Test_NewSystemAdminContext(t *testing.T) {
t.FailNow()
}
assert.Implements((*Viewer)(nil), v, "FromContext should return a Viewer")
_, ok := v.User()
u, ok := v.User()
assert.Nil(u, "FromContext should not return a user")
assert.False(ok, "FromContext should not return a user")
assert.True(v.IsAdmin(), "FromContext should return admin permissions")

p, ok := v.Permissions()
assert.Nil(p, "FromContext should not return permissions")
assert.False(ok, "FromContext should not return permissions")
}
Loading