Skip to content

Commit

Permalink
Better structure the code for testability.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mathieu Leduc-Hamel committed Apr 6, 2021
1 parent 5bfd4cf commit b0db95b
Show file tree
Hide file tree
Showing 9 changed files with 117 additions and 27 deletions.
14 changes: 13 additions & 1 deletion pkg/app/faktory.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/mlhamel/trieugene/pkg/jobs"
"github.com/mlhamel/trieugene/pkg/store"
rougecombien "github.com/mlhamel/trieugene/services/rougecombien/pkg/jobs"
"github.com/mlhamel/trieugene/services/rougecombien/pkg/scraper"
"github.com/pior/runnable"
)

Expand All @@ -22,10 +23,21 @@ func NewFaktory(cfg *config.Config, store store.Store) runnable.Runnable {

manager := jobs.NewFaktoryManager(cfg)

httpScraper := scraper.NewHttpScraper(cfg)
parser := scraper.NewParser(cfg, httpScraper)

csvJob := rougecombien.NewCsvJob(&rougecombien.CsvJobKwargs{
Cfg: cfg,
Manager: manager,
StoreJob: csvStoreJob,
Scraper: httpScraper,
Parser: parser,
})

manager.Register(jsonStoreJob)
manager.Register(csvStoreJob)
manager.Register(rougecombien.NewJsonJob(cfg, manager, jsonStoreJob))
manager.Register(rougecombien.NewCsvJob(cfg, manager, csvStoreJob))
manager.Register(csvJob)

return &Faktory{
cfg: cfg,
Expand Down
27 changes: 27 additions & 0 deletions pkg/jobs/dummy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package jobs

import (
"context"

"github.com/mlhamel/trieugene/pkg/config"
)

type DummyManager struct {
cfg *config.Config
}

func NewDummyManager(cfg *config.Config) Manager {
return &DummyManager{}
}

func (f *DummyManager) Register(job Job) error {
return nil
}

func (d *DummyManager) Perform(job Job, msgs ...*Message) error {
return nil
}

func (f *DummyManager) Run(ctx context.Context) error {
return nil
}
20 changes: 20 additions & 0 deletions pkg/jobs/expects_jobs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package jobs

import "context"

type ExpectsJob struct {
Called bool
}

func (e *ExpectsJob) Kind() string {
return "expected-job"
}

func (e *ExpectsJob) Perform(ctx context.Context, args ...interface{}) error {
e.Called = true
return nil
}

func (e *ExpectsJob) Run(ctx context.Context, args ...interface{}) error {
return e.Perform(ctx, args)
}
24 changes: 22 additions & 2 deletions services/rougecombien/pkg/apps/rougecombien.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,33 @@ func (r *Rougecombien) Run(ctx context.Context) error {
func (r *Rougecombien) RunDevelopment(ctx context.Context) error {
run(r.setupDevelopment())

return r.manager.Perform(jobs.NewCsvJob(r.cfg, r.manager, r.csvStoreJob), &trieugene.Message{})
httpScraper := scraper.NewHttpScraper(r.cfg)
parser := scraper.NewParser(r.cfg, httpScraper)

job := jobs.NewCsvJob(&jobs.CsvJobKwargs{
Cfg: r.cfg,
Manager: r.manager,
StoreJob: r.csvStoreJob,
Scraper: httpScraper,
Parser: parser,
})

return r.manager.Perform(job, &trieugene.Message{})
}

func (r *Rougecombien) RunInline(ctx context.Context) error {
run(r.setupDevelopment())

job := jobs.NewCsvJob(r.cfg, r.manager, r.csvStoreJob)
httpScraper := scraper.NewHttpScraper(r.cfg)
parser := scraper.NewParser(r.cfg, httpScraper)

job := jobs.NewCsvJob(&jobs.CsvJobKwargs{
Cfg: r.cfg,
Manager: r.manager,
StoreJob: r.csvStoreJob,
Scraper: httpScraper,
Parser: parser,
})

return job.Run(ctx, &trieugene.Message{})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,20 @@ import (
"github.com/mlhamel/trieugene/services/rougecombien/pkg/scraper"
)

type CsvJobKwargs struct {
Cfg *config.Config
Manager trieugene.Manager
StoreJob trieugene.Job
Parser scraper.Parser
Scraper scraper.Scraper
}

type CsvJob struct {
cfg *config.Config
manager trieugene.Manager
storejob trieugene.Job
kwargs *CsvJobKwargs
}

func NewCsvJob(cfg *config.Config, manager trieugene.Manager, storejob trieugene.Job) trieugene.Job {
return &CsvJob{
cfg: cfg,
manager: manager,
storejob: storejob,
}
func NewCsvJob(kwargs *CsvJobKwargs) trieugene.Job {
return &CsvJob{kwargs: kwargs}
}

func (o *CsvJob) Kind() string {
Expand All @@ -29,10 +31,7 @@ func (o *CsvJob) Kind() string {
func (o *CsvJob) Perform(ctx context.Context, args ...interface{}) error {
var results = make(map[int][]scraper.Result)

httpScraper := scraper.NewHttpScraper(o.cfg)
parser := scraper.NewParser(o.cfg, httpScraper)

err := parser.Run(ctx, func(ctx context.Context, result scraper.Result) error {
err := o.kwargs.Parser.Run(ctx, func(ctx context.Context, result scraper.Result) error {
if len(results[result.TakenAt.Day()]) <= 0 {
results[result.TakenAt.Day()] = []scraper.Result{}
}
Expand All @@ -49,13 +48,13 @@ func (o *CsvJob) Perform(ctx context.Context, args ...interface{}) error {
for _, result := range values {
messages = append(messages, trieugene.Message{
ID: result.Sha1(),
Kind: o.storejob.Kind(),
Kind: o.kwargs.StoreJob.Kind(),
ProcessedAt: result.ScrapedAt.Unix(),
HappenedAt: result.TakenAt.Unix(),
Value: result.Outflow,
})
}
o.storejob.Run(ctx, messages...)
o.kwargs.StoreJob.Run(ctx, messages...)
}

return nil
Expand Down
9 changes: 0 additions & 9 deletions services/rougecombien/pkg/jobs/csv_test.go

This file was deleted.

File renamed without changes.
21 changes: 21 additions & 0 deletions services/rougecombien/pkg/scraper/dummy_scraper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package scraper

import (
"context"

"github.com/mlhamel/trieugene/pkg/config"
)

type DummyScraper struct {
cfg *config.Config
}

func NewDummyScraper(cfg *config.Config) Scraper {
return &HttpScraper{
cfg: cfg,
}
}

func (d *DummyScraper) Run(ctx context.Context) ([][]string, error) {
return [][]string{}, nil
}

0 comments on commit b0db95b

Please sign in to comment.