From 62299cf70e769ba7e66e037024253036b2523f30 Mon Sep 17 00:00:00 2001 From: Wessie Date: Wed, 21 Feb 2024 02:54:28 +0000 Subject: [PATCH] radio: add more tests --- radio.go | 8 ++- radio_test.go | 132 +++++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 127 insertions(+), 13 deletions(-) diff --git a/radio.go b/radio.go index 9f2ff48a..14cb457b 100644 --- a/radio.go +++ b/radio.go @@ -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: diff --git a/radio_test.go b/radio_test.go index 91e0feb7..1a13ced9 100644 --- a/radio_test.go +++ b/radio_test.go @@ -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) { @@ -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) } } } @@ -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) + }) + } +}