Skip to content

Commit

Permalink
update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
erka committed Sep 18, 2024
1 parent 6f8d028 commit 444f31a
Show file tree
Hide file tree
Showing 14 changed files with 83 additions and 82 deletions.
16 changes: 8 additions & 8 deletions build/testing/integration/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -664,7 +664,7 @@ func API(t *testing.T, ctx context.Context, opts integration.TestOpts) {
}

assert.Equal(t, ruleTwo.Id, distribution.RuleId)
assert.Equal(t, float32(100), distribution.Rollout)
assert.InDelta(t, 100, distribution.Rollout, 0)

distribution, err = client.Flipt().CreateDistribution(ctx, &flipt.CreateDistributionRequest{
NamespaceKey: namespace.Key,
Expand Down Expand Up @@ -733,7 +733,7 @@ func API(t *testing.T, ctx context.Context, opts integration.TestOpts) {
assert.Equal(t, "boolean_disabled", rolloutSegment.FlagKey)
assert.Equal(t, int32(1), rolloutSegment.Rank)
assert.Equal(t, "everyone", rolloutSegment.Rule.(*flipt.Rollout_Segment).Segment.SegmentKey)
assert.Equal(t, true, rolloutSegment.Rule.(*flipt.Rollout_Segment).Segment.Value)
assert.True(t, rolloutSegment.Rule.(*flipt.Rollout_Segment).Segment.Value)

anotherRolloutSegment, err := client.Flipt().CreateRollout(ctx, &flipt.CreateRolloutRequest{
NamespaceKey: namespace.Key,
Expand All @@ -753,7 +753,7 @@ func API(t *testing.T, ctx context.Context, opts integration.TestOpts) {
assert.Equal(t, "boolean_disabled", anotherRolloutSegment.FlagKey)
assert.Equal(t, int32(2), anotherRolloutSegment.Rank)
assert.Equal(t, "another-segment", anotherRolloutSegment.Rule.(*flipt.Rollout_Segment).Segment.SegmentKey)
assert.Equal(t, false, anotherRolloutSegment.Rule.(*flipt.Rollout_Segment).Segment.Value)
assert.False(t, anotherRolloutSegment.Rule.(*flipt.Rollout_Segment).Segment.Value)

updatedAnotherRolloutSegment, err := client.Flipt().UpdateRollout(ctx, &flipt.UpdateRolloutRequest{
Id: anotherRolloutSegment.Id,
Expand All @@ -773,7 +773,7 @@ func API(t *testing.T, ctx context.Context, opts integration.TestOpts) {
assert.Equal(t, int32(2), updatedAnotherRolloutSegment.Rank)
assert.Contains(t, updatedAnotherRolloutSegment.Rule.(*flipt.Rollout_Segment).Segment.SegmentKeys, "segment")
assert.Contains(t, updatedAnotherRolloutSegment.Rule.(*flipt.Rollout_Segment).Segment.SegmentKeys, "another-segment")
assert.Equal(t, false, updatedAnotherRolloutSegment.Rule.(*flipt.Rollout_Segment).Segment.Value)
assert.False(t, updatedAnotherRolloutSegment.Rule.(*flipt.Rollout_Segment).Segment.Value)

rolloutThreshold, err := client.Flipt().CreateRollout(ctx, &flipt.CreateRolloutRequest{
NamespaceKey: namespace.Key,
Expand All @@ -793,8 +793,8 @@ func API(t *testing.T, ctx context.Context, opts integration.TestOpts) {
assert.Equal(t, "boolean_disabled", rolloutThreshold.FlagKey)
assert.Equal(t, "50% disabled", rolloutThreshold.Description)
assert.Equal(t, int32(3), rolloutThreshold.Rank)
assert.Equal(t, float32(50.0), rolloutThreshold.Rule.(*flipt.Rollout_Threshold).Threshold.Percentage)
assert.Equal(t, true, rolloutThreshold.Rule.(*flipt.Rollout_Threshold).Threshold.Value)
assert.InDelta(t, 50.0, rolloutThreshold.Rule.(*flipt.Rollout_Threshold).Threshold.Percentage, 0)
assert.True(t, rolloutThreshold.Rule.(*flipt.Rollout_Threshold).Threshold.Value)

t.Log(`Ensure multiple rollouts with same rank can not be created`)

Expand Down Expand Up @@ -874,8 +874,8 @@ func API(t *testing.T, ctx context.Context, opts integration.TestOpts) {
assert.Equal(t, "boolean_disabled", updatedRollout.FlagKey)
assert.Equal(t, "50% enabled", updatedRollout.Description)
assert.Equal(t, int32(3), updatedRollout.Rank)
assert.Equal(t, float32(50.0), updatedRollout.Rule.(*flipt.Rollout_Threshold).Threshold.Percentage)
assert.Equal(t, false, updatedRollout.Rule.(*flipt.Rollout_Threshold).Threshold.Value)
assert.InDelta(t, 50.0, updatedRollout.Rule.(*flipt.Rollout_Threshold).Threshold.Percentage, 0)
assert.False(t, updatedRollout.Rule.(*flipt.Rollout_Threshold).Threshold.Value)

t.Run("Cannot change rollout type", func(t *testing.T) {
_, err := client.Flipt().UpdateRollout(ctx, &flipt.UpdateRolloutRequest{
Expand Down
24 changes: 12 additions & 12 deletions build/testing/integration/readonly/readonly_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,9 +237,9 @@ func TestReadOnly(t *testing.T) {
require.Len(t, rule.Distributions, 2)

assert.NotEmpty(t, rule.Distributions[0].Id)
assert.Equal(t, float32(50.0), rule.Distributions[0].Rollout)
assert.InDelta(t, 50.0, rule.Distributions[0].Rollout, 0)
assert.NotEmpty(t, rule.Distributions[1].Id)
assert.Equal(t, float32(50.0), rule.Distributions[1].Rollout)
assert.InDelta(t, 50.0, rule.Distributions[1].Rollout, 0)
})

t.Run("ListRules", func(t *testing.T) {
Expand Down Expand Up @@ -268,9 +268,9 @@ func TestReadOnly(t *testing.T) {
require.Len(t, rule.Distributions, 2)

assert.NotEmpty(t, rule.Distributions[0].Id)
assert.Equal(t, float32(50.0), rule.Distributions[0].Rollout)
assert.InDelta(t, 50.0, rule.Distributions[0].Rollout, 0)
assert.NotEmpty(t, rule.Distributions[1].Id)
assert.Equal(t, float32(50.0), rule.Distributions[1].Rollout)
assert.InDelta(t, 50.0, rule.Distributions[1].Rollout, 0)

t.Run("Paginated (page size 10)", func(t *testing.T) {
var (
Expand Down Expand Up @@ -373,7 +373,7 @@ func TestReadOnly(t *testing.T) {
})
require.NoError(t, err)

assert.Equal(t, true, response.Match)
assert.True(t, response.Match)
assert.Equal(t, "variant_002", response.Value)
assert.Equal(t, flipt.EvaluationReason_MATCH_EVALUATION_REASON, response.Reason)
})
Expand Down Expand Up @@ -401,11 +401,11 @@ func TestReadOnly(t *testing.T) {
require.NoError(t, err)
require.Len(t, response.Responses, 2)

assert.Equal(t, true, response.Responses[0].Match)
assert.True(t, response.Responses[0].Match)
assert.Equal(t, "variant_002", response.Responses[0].Value)
assert.Equal(t, flipt.EvaluationReason_MATCH_EVALUATION_REASON, response.Responses[0].Reason)

assert.Equal(t, true, response.Responses[1].Match)
assert.True(t, response.Responses[1].Match)
assert.Equal(t, "variant_001", response.Responses[1].Value)
assert.Equal(t, flipt.EvaluationReason_MATCH_EVALUATION_REASON, response.Responses[1].Reason)
})
Expand All @@ -424,7 +424,7 @@ func TestReadOnly(t *testing.T) {
})
require.NoError(t, err)

assert.Equal(t, true, response.Match)
assert.True(t, response.Match)
assert.Equal(t, "flag_001", response.FlagKey)
assert.Equal(t, "variant_002", response.VariantKey)
assert.Equal(t, evaluation.EvaluationReason_MATCH_EVALUATION_REASON, response.Reason)
Expand All @@ -443,7 +443,7 @@ func TestReadOnly(t *testing.T) {
})
require.NoError(t, err)

assert.Equal(t, true, response.Match)
assert.True(t, response.Match)
assert.Equal(t, "variant_002", response.VariantKey)
assert.Equal(t, "flag_variant_and_segments", response.FlagKey)
assert.Equal(t, evaluation.EvaluationReason_MATCH_EVALUATION_REASON, response.Reason)
Expand All @@ -462,7 +462,7 @@ func TestReadOnly(t *testing.T) {
})
require.NoError(t, err)

assert.Equal(t, false, response.Match)
assert.False(t, response.Match)
assert.Empty(t, response.VariantKey)
assert.Equal(t, evaluation.EvaluationReason_UNKNOWN_EVALUATION_REASON, response.Reason)
})
Expand All @@ -476,7 +476,7 @@ func TestReadOnly(t *testing.T) {
})
require.NoError(t, err)

assert.Equal(t, true, response.Match)
assert.True(t, response.Match)
assert.Equal(t, "variant_001", response.VariantKey)
assert.Equal(t, "flag_using_entity_id", response.FlagKey)
assert.Equal(t, evaluation.EvaluationReason_MATCH_EVALUATION_REASON, response.Reason)
Expand Down Expand Up @@ -521,7 +521,7 @@ func TestReadOnly(t *testing.T) {
})
require.NoError(t, err)

assert.Equal(t, true, response.Match)
assert.True(t, response.Match)
assert.Equal(t, "flag_no_distributions", response.FlagKey)
assert.Equal(t, evaluation.EvaluationReason_MATCH_EVALUATION_REASON, response.Reason)
assert.Contains(t, response.SegmentKeys, "segment_001")
Expand Down
6 changes: 3 additions & 3 deletions internal/cache/memory/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,15 @@ func TestDelete(t *testing.T) {
require.NoError(t, err)

v, ok, err := c.Get(ctx, "key")
assert.NoError(t, err)
require.NoError(t, err)
assert.True(t, ok)
assert.Equal(t, []byte("value"), v)

err = c.Delete(ctx, "key")
assert.NoError(t, err)
require.NoError(t, err)

v, ok, err = c.Get(ctx, "key")
assert.NoError(t, err)
require.NoError(t, err)
assert.False(t, ok)
assert.Nil(t, v)
}
16 changes: 8 additions & 8 deletions internal/cache/redis/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,20 @@ func TestGet(t *testing.T) {
defer teardown()

err := c.Set(ctx, "key", []byte("value"))
assert.NoError(t, err)
require.NoError(t, err)

v, ok, err := c.Get(ctx, "key")
assert.NoError(t, err)
require.NoError(t, err)
assert.True(t, ok)
assert.Equal(t, []byte("value"), v)

v, ok, err = c.Get(ctx, "foo")
assert.NoError(t, err)
require.NoError(t, err)
assert.False(t, ok)
assert.Nil(t, v)

v, ok, err = c.Get(ctx, "key")
assert.NoError(t, err)
require.NoError(t, err)
assert.True(t, ok)
assert.Equal(t, []byte("value"), v)
}
Expand All @@ -64,18 +64,18 @@ func TestDelete(t *testing.T) {
defer teardown()

err := c.Set(ctx, "key", []byte("value"))
assert.NoError(t, err)
require.NoError(t, err)

v, ok, err := c.Get(ctx, "key")
assert.NoError(t, err)
require.NoError(t, err)
assert.True(t, ok)
assert.Equal(t, []byte("value"), v)

err = c.Delete(ctx, "key")
assert.NoError(t, err)
require.NoError(t, err)

v, ok, err = c.Get(ctx, "key")
assert.NoError(t, err)
require.NoError(t, err)
assert.False(t, ok)
assert.Nil(t, v)
}
Expand Down
3 changes: 2 additions & 1 deletion internal/cmd/grpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.flipt.io/flipt/internal/config"
"go.flipt.io/flipt/internal/info"
"go.uber.org/zap/zaptest"
Expand All @@ -19,7 +20,7 @@ func TestNewGRPCServer(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)
s, err := NewGRPCServer(ctx, zaptest.NewLogger(t), cfg, info.Flipt{}, false)
assert.NoError(t, err)
require.NoError(t, err)
t.Cleanup(func() {
err := s.Shutdown(ctx)
assert.NoError(t, err)
Expand Down
11 changes: 6 additions & 5 deletions internal/cmd/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/go-chi/chi/v5"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

const (
Expand Down Expand Up @@ -40,21 +41,21 @@ func TestTrailingSlashMiddleware(t *testing.T) {

// Request with the middleware on.
req, err := http.NewRequestWithContext(context.TODO(), http.MethodGet, fmt.Sprintf("%s/hello/", s.URL), nil)
assert.NoError(t, err)
require.NoError(t, err)
req.Header.Set(tsoHeader, "on")

res, err := http.DefaultClient.Do(req)
assert.NoError(t, err)
require.NoError(t, err)

assert.Equal(t, http.StatusOK, res.StatusCode)
require.Equal(t, http.StatusOK, res.StatusCode)
res.Body.Close()

// Request with the middleware off.
req, err = http.NewRequestWithContext(context.TODO(), http.MethodGet, fmt.Sprintf("%s/hello/", s.URL), nil)
assert.NoError(t, err)
require.NoError(t, err)

res, err = http.DefaultClient.Do(req)
assert.NoError(t, err)
require.NoError(t, err)

assert.Equal(t, http.StatusNotFound, res.StatusCode)
res.Body.Close()
Expand Down
10 changes: 5 additions & 5 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func TestScheme(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
assert.Equal(t, want, scheme.String())
json, err := scheme.MarshalJSON()
assert.NoError(t, err)
require.NoError(t, err)
assert.JSONEq(t, fmt.Sprintf("%q", want), string(json))
})
}
Expand Down Expand Up @@ -90,7 +90,7 @@ func TestCacheBackend(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
assert.Equal(t, want, backend.String())
json, err := backend.MarshalJSON()
assert.NoError(t, err)
require.NoError(t, err)
assert.JSONEq(t, fmt.Sprintf("%q", want), string(json))
})
}
Expand Down Expand Up @@ -128,7 +128,7 @@ func TestTracingExporter(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
assert.Equal(t, want, exporter.String())
json, err := exporter.MarshalJSON()
assert.NoError(t, err)
require.NoError(t, err)
assert.JSONEq(t, fmt.Sprintf("%q", want), string(json))
})
}
Expand Down Expand Up @@ -176,7 +176,7 @@ func TestDatabaseProtocol(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
assert.Equal(t, want, protocol.String())
json, err := protocol.MarshalJSON()
assert.NoError(t, err)
require.NoError(t, err)
assert.JSONEq(t, fmt.Sprintf("%q", want), string(json))
})
}
Expand Down Expand Up @@ -209,7 +209,7 @@ func TestLogEncoding(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
assert.Equal(t, want, encoding.String())
json, err := encoding.MarshalJSON()
assert.NoError(t, err)
require.NoError(t, err)
assert.JSONEq(t, fmt.Sprintf("%q", want), string(json))
})
}
Expand Down
2 changes: 1 addition & 1 deletion internal/server/audit/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func testDistributionHelper(t *testing.T, d *flipt.Distribution) {
assert.Equal(t, nd.Id, d.Id)
assert.Equal(t, nd.RuleId, d.RuleId)
assert.Equal(t, nd.VariantId, d.VariantId)
assert.Equal(t, nd.Rollout, d.Rollout)
assert.InDelta(t, nd.Rollout, d.Rollout, 0)
}
func TestDistribution(t *testing.T) {
d := &flipt.Distribution{
Expand Down
4 changes: 1 addition & 3 deletions internal/server/authn/method/github/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
"github.com/h2non/gock"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.flipt.io/flipt/errors"
"go.flipt.io/flipt/internal/config"
middleware "go.flipt.io/flipt/internal/server/middleware/grpc"
"go.flipt.io/flipt/internal/storage/authn/memory"
Expand Down Expand Up @@ -174,8 +173,7 @@ func Test_Server(t *testing.T) {
JSON([]githubSimpleOrganization{{Login: "flipt-io"}})

_, err := client.Callback(ctx, &auth.CallbackRequest{Code: "github_code"})
uerr := errors.ErrUnauthenticatedf("request was not authenticated")
assert.ErrorAs(t, err, &uerr)
assert.ErrorContains(t, err, "request was not authenticated")
})

t.Run("should authorize successfully when the user is a member of one of the allowed organizations and it's inside the allowed team", func(t *testing.T) {
Expand Down
2 changes: 2 additions & 0 deletions internal/storage/authn/sql/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ func TestAuthentication_CreateAuthentication(t *testing.T) {

clientToken, created, err := store.CreateAuthentication(ctx, test.req)
if test.expectedErrAs != nil {
// nolint:testifylint
require.ErrorAs(t, err, test.expectedErrAs)
return
}
Expand Down Expand Up @@ -202,6 +203,7 @@ func TestAuthentication_GetAuthenticationByClientToken(t *testing.T) {
t.Run(test.name, func(t *testing.T) {
retrieved, err := storeFn(commonOpts(t)...).GetAuthenticationByClientToken(ctx, clientToken)
if expectedErrAs != nil {
// nolint:testifylint
require.ErrorAs(t, err, expectedErrAs)
return
}
Expand Down
5 changes: 2 additions & 3 deletions internal/storage/authn/testing/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,8 @@ func TestAuthenticationStoreHarness(t *testing.T, fn func(t *testing.T) storagea

auth, err := store.GetAuthenticationByClientToken(ctx, created[99].Token)
var expected errors.ErrNotFound
if !assert.ErrorAs(t, err, &expected, "authentication still exists in the database") {
t.Log("Auth still exists", auth)
}
require.ErrorAs(t, err, &expected, "authentication still exists in the database")
assert.Nil(t, auth)
})

t.Run("Delete by method Token with before expired constraint", func(t *testing.T) {
Expand Down
Loading

0 comments on commit 444f31a

Please sign in to comment.