Skip to content

Commit

Permalink
chore: removed MustJSONUnmarshal, added db tests
Browse files Browse the repository at this point in the history
  • Loading branch information
freak12techno committed Jan 19, 2025
1 parent ca801d4 commit 372cd7e
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 29 deletions.
5 changes: 4 additions & 1 deletion pkg/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,10 @@ func (d *Database) FindLastEventsByType(
}

Check warning on line 390 in pkg/database/database.go

View check run for this annotation

Codecov / codecov/patch

pkg/database/database.go#L388-L390

Added lines #L388 - L390 were not covered by tests

event := eventsPkg.MapEventTypesToEvent(eventType)
utils.MustJSONUnmarshal(payload, &event)
if unmarshallErr := json.Unmarshal(payload, &event); unmarshallErr != nil {
d.logger.Error().Err(unmarshallErr).Msg("Could not unmarshal event!")
return nil, unmarshallErr
}

newEvent := types.HistoricalEvent{
Chain: chain,
Expand Down
62 changes: 62 additions & 0 deletions pkg/database/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -460,3 +460,65 @@ func TestDatabaseInitUnsupported(t *testing.T) {
})
database.Init()
}

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

logger := loggerPkg.GetNopLogger()
client := NewStubDatabaseClient()
database := NewDatabase(*logger, configPkg.DatabaseConfig{})
database.SetClient(client)

client.Mock.
ExpectQuery("SELECT event, height, validator, payload, time FROM events").
WillReturnError(errors.New("custom error"))

_, err := database.FindLastEventsByType("chain", constants.GetEventNames())
require.Error(t, err)
require.ErrorContains(t, err, "custom error")
}

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

logger := loggerPkg.GetNopLogger()
client := NewStubDatabaseClient()
database := NewDatabase(*logger, configPkg.DatabaseConfig{})
database.SetClient(client)

client.Mock.
ExpectQuery("SELECT event, height, validator, payload, time FROM events").
WillReturnRows(sqlmock.
NewRows([]string{"event", "height", "validator", "payload", "time"}).
AddRow("test", 123, "test", "test", time.Now()),
)

_, err := database.FindLastEventsByType("chain", constants.GetEventNames())
require.Error(t, err)
require.ErrorContains(t, err, "invalid character 'e' in literal")
}

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

logger := loggerPkg.GetNopLogger()
client := NewStubDatabaseClient()
database := NewDatabase(*logger, configPkg.DatabaseConfig{})
database.SetClient(client)

client.Mock.
ExpectQuery("SELECT event, height, validator, payload, time FROM events").
WillReturnRows(sqlmock.
NewRows([]string{"event", "height", "validator", "payload", "time"}).
AddRow(
constants.EventValidatorActive,
123,
"validator",
utils.MustJSONMarshall(events.ValidatorActive{Validator: &types.Validator{}}),
time.Now(),
),
)

_, err := database.FindLastEventsByType("chain", constants.GetEventNames())
require.NoError(t, err)
}
7 changes: 0 additions & 7 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,10 +235,3 @@ func MustJSONMarshall(v any) []byte {

return bytes
}

func MustJSONUnmarshal(data []byte, v any) {
err := json.Unmarshal(data, v)
if err != nil {
panic(err)
}
}
21 changes: 0 additions & 21 deletions pkg/utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,24 +296,3 @@ func TestMustMarshallOk(t *testing.T) {
bytes := MustJSONMarshall(map[string]string{"key": "value"})
require.JSONEq(t, "{\"key\":\"value\"}", string(bytes))
}

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

defer func() {
if r := recover(); r == nil {
require.Fail(t, "Expected to have a panic here!")
}
}()

out := map[string]string{}
MustJSONUnmarshal([]byte("123"), &out)
}

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

out := map[string]string{}
MustJSONUnmarshal([]byte("{\"key\":\"value\"}"), &out)
require.Equal(t, map[string]string{"key": "value"}, out)
}

0 comments on commit 372cd7e

Please sign in to comment.