Skip to content

Commit

Permalink
radio: add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Wessie committed Feb 21, 2024
1 parent da73d78 commit 62299cf
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 13 deletions.
8 changes: 6 additions & 2 deletions radio.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,17 +113,21 @@ func (up UserPermissions) Has(perm UserPermission) bool {
// Done in a way that it expects all permissions to be a single string or []byte
// separated by a comma
func (upp *UserPermissions) Scan(src interface{}) error {
if upp == nil {
return fmt.Errorf("nil found in Scan")
}

*upp = make(UserPermissions)
up := *upp

switch perms := src.(type) {
case []byte:
for _, p := range bytes.Split(perms, []byte(",")) {
up[UserPermission(p)] = struct{}{}
up[UserPermission(bytes.TrimSpace(p))] = struct{}{}
}
case string:
for _, p := range strings.Split(perms, ",") {
up[UserPermission(p)] = struct{}{}
up[UserPermission(strings.TrimSpace(p))] = struct{}{}
}
case nil: // no permissions, we made the map above though
default:
Expand Down
132 changes: 121 additions & 11 deletions radio_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/leanovate/gopter/arbitrary"
"github.com/leanovate/gopter/gen"
"github.com/leanovate/gopter/prop"
"github.com/stretchr/testify/assert"
)

func TestSongEqualTo(t *testing.T) {
Expand Down Expand Up @@ -72,20 +73,24 @@ func TestSongRequestable(t *testing.T) {
}

func TestCalculateCooldown(t *testing.T) {
now := time.Now()

tests := []struct {
delay time.Duration
last time.Time
ok bool
expect time.Duration
delay time.Duration
last time.Time
ok bool
}{
{time.Hour, time.Now(), false},
{time.Hour, time.Now().Add(-time.Hour * 2), true},
{time.Hour, time.Now().Add(time.Hour * 2), false},
{time.Hour, time.Hour, now, false},
{0, time.Hour, now.Add(-time.Hour * 2), true},
{time.Hour, time.Hour, now.Add(time.Hour * 2), false},
{0, time.Hour, time.Time{}, true},
}

for _, test := range tests {
d, ok := CalculateCooldown(test.delay, test.last)
if ok != test.ok {
t.Errorf("failed %s on %s, returned: %s", test.last, test.delay, d)
if ok != test.ok || test.expect-d > time.Minute {
t.Errorf("failed %s on %s, returned: %s expected %s", test.last, test.delay, d, test.expect)
}
}
}
Expand All @@ -96,8 +101,113 @@ func TestCalculateRequestDelay(t *testing.T) {
a := arbitrary.DefaultArbitraries()
p := gopter.NewProperties(tp)

p.Property("+1 should be higher or equal", a.ForAll(func(i int) bool {
return CalculateRequestDelay(i) <= CalculateRequestDelay(i+1)
}))
p.Property("+1 should be higher or equal", a.ForAll(
func(i int) bool {
return CalculateRequestDelay(i) <= CalculateRequestDelay(i+1)
}))
p.Property("+1 should be higher or equal", prop.ForAll(
func(i int) bool {
return CalculateRequestDelay(i) <= CalculateRequestDelay(i+1)
},
gen.IntRange(0, 50),
))
p.TestingRun(t)
}

func TestUserPermissionsHas(t *testing.T) {
var up UserPermissions
// nil map should not panic and return false instead
assert.False(t, up.Has(PermActive))
up = make(UserPermissions)

// dev permission, but not active so this should be false too
up[PermDev] = struct{}{}
assert.False(t, up.Has(PermDev))

// now we're actually active and both Active and Dev previously
// added should return true
up[PermActive] = struct{}{}
assert.True(t, up.Has(PermActive))
assert.True(t, up.Has(PermDev))
// Dev permission also gives you a blanket permission on all other permissions
// so the above combination should give us true for any permission we throw at it
for _, perm := range AllUserPermissions() {
assert.True(t, up.Has(perm))
}
}

func TestUserPermissionsScan(t *testing.T) {
assert.Error(t, (*UserPermissions).Scan(nil, nil))

tests := []struct {
name string
input any
expectErr bool
expected UserPermissions
}{
{
name: "nil",
input: nil,
expectErr: false,
expected: UserPermissions{},
},
{
name: "something",
input: struct{ literallyAnyType any }{},
expectErr: true,
expected: UserPermissions{},
},
{
name: "string",
input: "active,dev,admin",
expectErr: false,
expected: UserPermissions{
PermActive: struct{}{},
PermDev: struct{}{},
PermAdmin: struct{}{},
},
},
{
name: "[]byte",
input: []byte("news,database_edit,pending_view"),
expectErr: false,
expected: UserPermissions{
PermPendingView: struct{}{},
PermNews: struct{}{},
PermDatabaseEdit: struct{}{},
},
},
{
name: "string with spaces",
input: "active, dev, admin",
expectErr: false,
expected: UserPermissions{
PermActive: struct{}{},
PermDev: struct{}{},
PermAdmin: struct{}{},
},
},
{
name: "[]byte with spaces",
input: []byte("news, database_edit, pending_view"),
expectErr: false,
expected: UserPermissions{
PermPendingView: struct{}{},
PermNews: struct{}{},
PermDatabaseEdit: struct{}{},
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
var up UserPermissions
err := up.Scan(test.input)
if test.expectErr {
assert.EqualError(t, err, "invalid argument passed to Scan")
} else {
assert.NoError(t, err)
}
assert.Equal(t, test.expected, up)
})
}
}

0 comments on commit 62299cf

Please sign in to comment.