Skip to content

Commit

Permalink
chore: fixed some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
freak12techno committed Oct 19, 2024
1 parent 8225f42 commit 1c91663
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 27 deletions.
44 changes: 42 additions & 2 deletions pkg/database/stub.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type StubDatabase struct {
Proposals map[string]map[string]*types.Proposal
Votes map[string]map[string]map[string]*types.Vote
LastBlockHeight map[string]map[string]int64
Mutes []*types.Mute
}

func (d *StubDatabase) Init() {
Expand Down Expand Up @@ -150,17 +151,56 @@ func (d *StubDatabase) UpsertLastBlockHeight(
}

func (d *StubDatabase) UpsertMute(mute *types.Mute) error {
if d.Mutes == nil {
d.Mutes = []*types.Mute{}
}

for _, otherMute := range d.Mutes {
if otherMute.LabelsEqual(mute) {
otherMute.Expires = mute.Expires
otherMute.Comment = mute.Comment
return nil
}

Check warning on line 163 in pkg/database/stub.go

View check run for this annotation

Codecov / codecov/patch

pkg/database/stub.go#L159-L163

Added lines #L159 - L163 were not covered by tests
}

d.Mutes = append(d.Mutes, mute)

return nil
}

func (d *StubDatabase) GetAllMutes() ([]*types.Mute, error) {
return []*types.Mute{}, nil
if d.Mutes == nil {
return []*types.Mute{}, nil
}

return d.Mutes, nil
}

func (d *StubDatabase) DeleteMute(mute *types.Mute) (bool, error) {
return true, nil
if d.Mutes == nil {
return false, nil
}

Check warning on line 182 in pkg/database/stub.go

View check run for this annotation

Codecov / codecov/patch

pkg/database/stub.go#L181-L182

Added lines #L181 - L182 were not covered by tests

for index, otherMute := range d.Mutes {
if otherMute.LabelsEqual(mute) {
d.Mutes = append(d.Mutes[:index], d.Mutes[index+1:]...)
return true, nil
}
}

return false, nil
}

func (d *StubDatabase) IsMuted(chain, proposalID string) (bool, error) {
if d.Mutes == nil {
return false, nil
}

Check warning on line 197 in pkg/database/stub.go

View check run for this annotation

Codecov / codecov/patch

pkg/database/stub.go#L196-L197

Added lines #L196 - L197 were not covered by tests

for _, mute := range d.Mutes {
if mute.Matches(chain, proposalID) {
return true, nil
}
}

return false, nil
}
71 changes: 46 additions & 25 deletions pkg/mutes/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,58 +28,79 @@ func TestMuteManagerAddAndDeleteMuteIsMuted(t *testing.T) {
})
require.NoError(t, err)

assert.True(t, manager.IsEntryMuted(events.VotedEvent{
Chain: &types.Chain{Name: "chain"},
Proposal: types.Proposal{ID: "proposal"},
}))
assert.False(t, manager.IsEntryMuted(events.VotedEvent{
Chain: &types.Chain{Name: "chain2"},
Proposal: types.Proposal{ID: "proposal"},
}))
muted1, err1 := manager.IsEntryMuted(events.VotedEvent{
Chain: &types.Chain{Name: "chain"},
})

assert.True(t, muted1)
require.NoError(t, err1)

muted2, err2 := manager.IsEntryMuted(events.VotedEvent{
Chain: &types.Chain{Name: "chain2"},
})
assert.False(t, muted2)
require.NoError(t, err2)

deleted, err := manager.DeleteMute(&types.Mute{
Chain: null.StringFrom("chain"),
})
assert.True(t, deleted)
require.NoError(t, err)

assert.False(t, manager.IsEntryMuted(events.VotedEvent{
muted3, err3 := manager.IsEntryMuted(events.VotedEvent{
Chain: &types.Chain{Name: "chain"},
Proposal: types.Proposal{ID: "proposal"},
}))
assert.False(t, manager.IsEntryMuted(events.VotedEvent{
})
assert.False(t, muted3)
require.NoError(t, err3)

muted4, err4 := manager.IsEntryMuted(events.VotedEvent{
Chain: &types.Chain{Name: "chain2"},
Proposal: types.Proposal{ID: "proposal"},
}))
})
assert.False(t, muted4)
require.NoError(t, err4)

deleted2, err2 := manager.DeleteMute(&types.Mute{
Chain: null.StringFrom("chain"),
})
assert.False(t, deleted2)
require.NoError(t, err2)
}

func TestMuteManagerIsMutedNoPath(t *testing.T) {
func TestMuteManagerIsNotAlert(t *testing.T) {
t.Parallel()

log := logger.GetNopLogger()
db := &databasePkg.StubDatabase{}
manager := NewMutesManager(log, db)

err := manager.AddMute(&types.Mute{
Chain: null.StringFrom("chain"),
Expires: time.Now().Add(time.Hour),
muted, err := manager.IsEntryMuted(events.ProposalsQueryErrorEvent{
Chain: &types.Chain{Name: "chain"},
})
require.NoError(t, err)

assert.False(t, manager.IsEntryMuted(events.VotedEvent{
Chain: &types.Chain{Name: "chain"},
Proposal: types.Proposal{ID: "proposal"},
}))
require.NoError(t, err)
assert.False(t, muted)
}

func TestMuteManagerIsNotAlert(t *testing.T) {
func TestMuteManagerGetAllMutes(t *testing.T) {
t.Parallel()

log := logger.GetNopLogger()
db := &databasePkg.StubDatabase{}
manager := NewMutesManager(log, db)

assert.False(t, manager.IsEntryMuted(events.ProposalsQueryErrorEvent{
Chain: &types.Chain{Name: "chain"},
}))
allMutes1, err1 := manager.GetAllMutes()
assert.Empty(t, allMutes1)
require.NoError(t, err1)

err := manager.AddMute(&types.Mute{
Chain: null.StringFrom("chain"),
Expires: time.Now().Add(time.Hour),
})
require.NoError(t, err)

allMutes2, err2 := manager.GetAllMutes()
assert.NotEmpty(t, allMutes2)
require.NoError(t, err2)
}

0 comments on commit 1c91663

Please sign in to comment.