Skip to content

Commit

Permalink
chore: add app tests
Browse files Browse the repository at this point in the history
  • Loading branch information
freak12techno committed Jul 6, 2024
1 parent 32e8cb1 commit 2ab8510
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 4 deletions.
14 changes: 10 additions & 4 deletions pkg/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type App struct {
ReportGenerator *report.Generator
StateGenerator *state.Generator
ReportDispatcher *report.Dispatcher
StopChannel chan bool
}

func NewApp(configPath string, filesystem fs.FS, version string) *App {
Expand Down Expand Up @@ -93,6 +94,7 @@ func NewApp(configPath string, filesystem fs.FS, version string) *App {
ReportGenerator: reportGenerator,
StateGenerator: stateGenerator,
ReportDispatcher: reportDispatcher,
StopChannel: make(chan bool),
}
}

Expand All @@ -103,15 +105,19 @@ func (a *App) Start() {
}

c := cron.New()
if _, err := c.AddFunc(a.Config.Interval, func() {
a.Report()
}); err != nil {
if _, err := c.AddFunc(a.Config.Interval, a.Report); err != nil {
a.Logger.Panic().Err(err).Msg("Error processing cron pattern")
}
c.Start()
a.Logger.Info().Str("interval", a.Config.Interval).Msg("Scheduled proposals reporting")

select {}
<-a.StopChannel
a.Logger.Info().Msg("Shutting down...")
c.Stop()
}

func (a *App) Stop() {
a.StopChannel <- true
}

func (a *App) Report() {
Expand Down
123 changes: 123 additions & 0 deletions pkg/app_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package pkg

import (
"main/pkg/fs"
reportersPkg "main/pkg/reporters"
"sync"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

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

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

filesystem := &fs.TestFS{}

NewApp("notexisting.toml", filesystem, "1.2.3")
}

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

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

filesystem := &fs.TestFS{}

NewApp("config-invalid.toml", filesystem, "1.2.3")
}

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

filesystem := &fs.TestFS{}

app := NewApp("config-with-warnings.toml", filesystem, "1.2.3")
require.NotNil(t, app)
}

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

filesystem := &fs.TestFS{}

app := NewApp("config-valid.toml", filesystem, "1.2.3")
require.NotNil(t, app)
}

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

filesystem := &fs.TestFS{}

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

app := NewApp("config-valid.toml", filesystem, "1.2.3")
app.ReportDispatcher.Reporters = []reportersPkg.Reporter{&reportersPkg.TestReporter{WithInitFail: true}}

app.Start()
require.NotNil(t, app)
}

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

filesystem := &fs.TestFS{}

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

app := NewApp("config-valid.toml", filesystem, "1.2.3")
app.Config.Interval = "invalid"

app.Start()
require.NotNil(t, app)
}

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

filesystem := &fs.TestFS{}
app := NewApp("config-valid.toml", filesystem, "1.2.3")
app.ReportDispatcher.Reporters = []reportersPkg.Reporter{&reportersPkg.TestReporter{}}

var wg sync.WaitGroup
wg.Add(1)

go func() {
app.Start()
wg.Done()
}()

app.Stop()
wg.Wait()
assert.True(t, true)
}

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

filesystem := &fs.TestFS{}
app := NewApp("config-valid.toml", filesystem, "1.2.3")
app.Report()

assert.True(t, true)
}

0 comments on commit 2ab8510

Please sign in to comment.