Skip to content

Commit 0cb2f84

Browse files
s35560claude
andcommitted
test(account,feature): make UpdatedAt assertions falsifiable
In account_test.go the InDelta assertions could not fail: NewAccountV2 and the setup AddSearchFilter calls already stamp UpdatedAt with the current time, so removing the stamp from the mutator under test still passed. Backdate UpdatedAt before the call and branch on whether the mutator is expected to stamp it, which also covers the error paths leaving the field untouched. In TestChangeRulesOrder the expected value was captured once before the loop over a shared, mutated feature, so the assertion only held because the stamping case happened to be last. Read the previous value inside the loop instead. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
1 parent 3601042 commit 0cb2f84

2 files changed

Lines changed: 37 additions & 7 deletions

File tree

pkg/account/domain/account_test.go

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ import (
2525
"github.com/bucketeer-io/bucketeer/v2/proto/common"
2626
)
2727

28+
func backdateUpdatedAt(a *AccountV2) int64 {
29+
a.UpdatedAt = time.Now().Add(-time.Hour).Unix()
30+
return a.UpdatedAt
31+
}
32+
2833
func TestNewMemberAccount(t *testing.T) {
2934
envRoles := []*proto.AccountV2_EnvironmentRole{
3035
{
@@ -460,6 +465,7 @@ func TestAddSearchFilter(t *testing.T) {
460465
[]string{"team"},
461466
account.OrganizationId,
462467
account.OrganizationRole, account.EnvironmentRoles)
468+
backdatedUpdatedAt := backdateUpdatedAt(a)
463469
for _, f := range p.expectedFilters {
464470
_, err := a.AddSearchFilter(f.Name, f.Query, f.FilterTargetType, f.EnvironmentId, f.DefaultFilter)
465471
assert.Nil(t, err)
@@ -474,7 +480,11 @@ func TestAddSearchFilter(t *testing.T) {
474480
assert.Equal(t, account.OrganizationId, a.OrganizationId)
475481
assert.Equal(t, account.OrganizationRole, a.OrganizationRole)
476482
assert.Equal(t, account.EnvironmentRoles, a.EnvironmentRoles)
477-
assert.InDelta(t, time.Now().Unix(), a.UpdatedAt, 5)
483+
if len(p.expectedFilters) > 0 {
484+
assert.InDelta(t, time.Now().Unix(), a.UpdatedAt, 5)
485+
} else {
486+
assert.Equal(t, backdatedUpdatedAt, a.UpdatedAt)
487+
}
478488

479489
assert.Equal(t, len(p.expectedFilters), len(a.SearchFilters))
480490
for i, f := range p.expectedFilters {
@@ -627,6 +637,7 @@ func TestChangeSearchFilterName(t *testing.T) {
627637
if len(a.SearchFilters) > 0 {
628638
updateFilterId = a.SearchFilters[(len(a.SearchFilters) / 2)].Id
629639
}
640+
backdatedUpdatedAt := backdateUpdatedAt(a)
630641
err := a.ChangeSearchFilterName(updateFilterId, p.updateFilterName)
631642
assert.Equal(t, err, p.error)
632643

@@ -640,7 +651,11 @@ func TestChangeSearchFilterName(t *testing.T) {
640651
assert.Equal(t, account.OrganizationId, a.OrganizationId)
641652
assert.Equal(t, account.OrganizationRole, a.OrganizationRole)
642653
assert.Equal(t, account.EnvironmentRoles, a.EnvironmentRoles)
643-
assert.InDelta(t, time.Now().Unix(), a.UpdatedAt, 5)
654+
if p.error == nil {
655+
assert.InDelta(t, time.Now().Unix(), a.UpdatedAt, 5)
656+
} else {
657+
assert.Equal(t, backdatedUpdatedAt, a.UpdatedAt)
658+
}
644659

645660
assert.Equal(t, len(p.expectedFilters), len(a.SearchFilters))
646661
for i, f := range p.expectedFilters {
@@ -781,6 +796,7 @@ func TestChangeSearchFilterQuery(t *testing.T) {
781796
if len(a.SearchFilters) > 0 {
782797
updateFilterId = a.SearchFilters[(len(a.SearchFilters) / 2)].Id
783798
}
799+
backdatedUpdatedAt := backdateUpdatedAt(a)
784800
err := a.ChangeSearchFilterQuery(updateFilterId, p.updateFilterQuery)
785801
assert.Equal(t, err, p.error)
786802

@@ -794,7 +810,11 @@ func TestChangeSearchFilterQuery(t *testing.T) {
794810
assert.Equal(t, account.OrganizationId, a.OrganizationId)
795811
assert.Equal(t, account.OrganizationRole, a.OrganizationRole)
796812
assert.Equal(t, account.EnvironmentRoles, a.EnvironmentRoles)
797-
assert.InDelta(t, time.Now().Unix(), a.UpdatedAt, 5)
813+
if p.error == nil {
814+
assert.InDelta(t, time.Now().Unix(), a.UpdatedAt, 5)
815+
} else {
816+
assert.Equal(t, backdatedUpdatedAt, a.UpdatedAt)
817+
}
798818

799819
assert.Equal(t, len(p.expectedFilters), len(a.SearchFilters))
800820
for i, f := range p.expectedFilters {
@@ -939,6 +959,7 @@ func TestChangeDefaultSearchFilter(t *testing.T) {
939959
Id: updateFilterId,
940960
DefaultFilter: p.updateDefaultFilter,
941961
}
962+
backdatedUpdatedAt := backdateUpdatedAt(a)
942963
err := a.ChangeDefaultSearchFilter(
943964
updateFilter.Id,
944965
updateFilter.DefaultFilter)
@@ -954,7 +975,11 @@ func TestChangeDefaultSearchFilter(t *testing.T) {
954975
assert.Equal(t, account.OrganizationId, a.OrganizationId)
955976
assert.Equal(t, account.OrganizationRole, a.OrganizationRole)
956977
assert.Equal(t, account.EnvironmentRoles, a.EnvironmentRoles)
957-
assert.InDelta(t, time.Now().Unix(), a.UpdatedAt, 5)
978+
if p.error == nil {
979+
assert.InDelta(t, time.Now().Unix(), a.UpdatedAt, 5)
980+
} else {
981+
assert.Equal(t, backdatedUpdatedAt, a.UpdatedAt)
982+
}
958983

959984
assert.Equal(t, len(p.expectedFilters), len(a.SearchFilters))
960985
for i, f := range p.expectedFilters {
@@ -1076,6 +1101,7 @@ func TestDeleteSearchFilter(t *testing.T) {
10761101
if len(a.SearchFilters) > 0 {
10771102
deleteFilterId = a.SearchFilters[(len(a.SearchFilters) / 2)].Id
10781103
}
1104+
backdatedUpdatedAt := backdateUpdatedAt(a)
10791105
err := a.DeleteSearchFilter(deleteFilterId)
10801106
assert.Equal(t, err, p.error)
10811107

@@ -1089,7 +1115,11 @@ func TestDeleteSearchFilter(t *testing.T) {
10891115
assert.Equal(t, account.OrganizationId, a.OrganizationId)
10901116
assert.Equal(t, account.OrganizationRole, a.OrganizationRole)
10911117
assert.Equal(t, account.EnvironmentRoles, a.EnvironmentRoles)
1092-
assert.InDelta(t, time.Now().Unix(), a.UpdatedAt, 5)
1118+
if p.error == nil {
1119+
assert.InDelta(t, time.Now().Unix(), a.UpdatedAt, 5)
1120+
} else {
1121+
assert.Equal(t, backdatedUpdatedAt, a.UpdatedAt)
1122+
}
10931123

10941124
assert.Equal(t, len(p.expectedFilters), len(a.SearchFilters))
10951125
if len(a.SearchFilters) > 0 {

pkg/feature/domain/feature_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -785,7 +785,6 @@ func TestChangeRuleStrategyToFixed(t *testing.T) {
785785
func TestChangeRulesOrder(t *testing.T) {
786786
t.Helper()
787787
f := makeFeature("test-feature")
788-
initialUpdatedAt := f.UpdatedAt
789788
patterns := []*struct {
790789
ruleIDs []string
791790
expected []string
@@ -815,12 +814,13 @@ func TestChangeRulesOrder(t *testing.T) {
815814
},
816815
}
817816
for _, p := range patterns {
817+
updatedAtBefore := f.UpdatedAt
818818
err := f.ChangeRulesOrder(p.ruleIDs)
819819
assert.Equal(t, p.expectedError, err)
820820
if p.expectUpdatedAtNow {
821821
assert.InDelta(t, time.Now().Unix(), f.UpdatedAt, 5)
822822
} else {
823-
assert.Equal(t, initialUpdatedAt, f.UpdatedAt)
823+
assert.Equal(t, updatedAtBefore, f.UpdatedAt)
824824
}
825825
for i := range f.Rules {
826826
if p.expected[i] != f.Rules[i].Id {

0 commit comments

Comments
 (0)