Skip to content

Commit

Permalink
util: Use dedicated test package for unit tests
Browse files Browse the repository at this point in the history
This uses a dedicated test package as opposed to including tests as part
of the same `util` package.

The idea with this pattern is that it allows us to test how a regular
test implementation would actually use the functions. It also, deters
us from testing specifics within the implementations which is not ideal.
  • Loading branch information
JAORMX committed Aug 28, 2023
1 parent 9efc4d2 commit 2129e70
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 9 deletions.
6 changes: 4 additions & 2 deletions internal/util/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package util
package util_test

import (
"strconv"
Expand All @@ -22,6 +22,8 @@ import (
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"

"github.com/stacklok/mediator/internal/util"
)

func TestGetConfigValue(t *testing.T) {
Expand Down Expand Up @@ -96,7 +98,7 @@ func TestGetConfigValue(t *testing.T) {
}
}

result := GetConfigValue(tc.key, tc.flagName, cmd, tc.defaultValue)
result := util.GetConfigValue(tc.key, tc.flagName, cmd, tc.defaultValue)
assert.Equal(t, tc.expected, result)
})
}
Expand Down
12 changes: 7 additions & 5 deletions internal/util/random_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@
// It does make a good example of how to use the generated client code
// for others to use as a reference.

package util
package util_test

import (
"testing"

"github.com/stretchr/testify/require"

"github.com/stacklok/mediator/internal/util"
)

func TestRandomInt(t *testing.T) {
Expand All @@ -33,15 +35,15 @@ func TestRandomInt(t *testing.T) {
min := int64(1)
max := int64(10)
seed := int64(12345)
randomInt := RandomInt(min, max, seed)
randomInt := util.RandomInt(min, max, seed)
require.GreaterOrEqual(t, randomInt, min)
require.LessOrEqual(t, randomInt, max)
}

func TestRandomString(t *testing.T) {
t.Parallel()
seed := int64(12345)
randomString := RandomString(10, seed)
randomString := util.RandomString(10, seed)
require.NotEmpty(t, randomString)
require.Len(t, randomString, 10)
}
Expand All @@ -50,7 +52,7 @@ func TestRandomEmail(t *testing.T) {
t.Parallel()

seed := int64(12345)
email := RandomEmail(seed)
email := util.RandomEmail(seed)
require.NotEmpty(t, email)
require.Contains(t, email, "@")
require.Contains(t, email, ".")
Expand All @@ -61,7 +63,7 @@ func TestRandomName(t *testing.T) {
t.Parallel()

seed := int64(12345)
name := RandomName(seed)
name := util.RandomName(seed)
require.NotEmpty(t, name)
require.Len(t, name, 10)
}
6 changes: 4 additions & 2 deletions internal/util/statuses_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,22 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package util
package util_test

import (
"fmt"
"testing"

"github.com/stretchr/testify/require"
"google.golang.org/grpc/codes"

"github.com/stacklok/mediator/internal/util"
)

func TestNiceStatusCreation(t *testing.T) {
t.Parallel()

s := GetNiceStatus(codes.OK)
s := util.GetNiceStatus(codes.OK)
require.Equal(t, codes.OK, s.Code)
require.Equal(t, "OK", s.Name)
require.Equal(t, "OK", s.Description)
Expand Down

0 comments on commit 2129e70

Please sign in to comment.