Skip to content

Commit

Permalink
Add test for job using struct field
Browse files Browse the repository at this point in the history
  • Loading branch information
bcongdon committed Mar 19, 2019
1 parent 53db867 commit 1f4ad49
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,3 +162,58 @@ func TestLocalNoCrashOnNoResolvedInputFiles(t *testing.T) {

driver.Main()
}

type statefulJob struct {
filterWords *[]string
}

func (s statefulJob) Map(key, value string, emitter Emitter) {
for _, word := range strings.Fields(value) {
for _, filterWord := range *s.filterWords {
if filterWord == word {
emitter.Emit(word, "1")
}
}
}
}

func (statefulJob) Reduce(key string, values ValueIterator, emitter Emitter) {
count := 0
for range values.Iter() {
count++
}
emitter.Emit(key, fmt.Sprintf("%d", count))
}

func TestLocalStructFieldMapReduce(t *testing.T) {
tmpdir, err := ioutil.TempDir("", "test")
assert.Nil(t, err)
defer os.RemoveAll(tmpdir)

inputPath := filepath.Join(tmpdir, "test_input")
ioutil.WriteFile(inputPath, []byte("the test input\nthe input test\nfoo bar baz"), 0700)

jobStruct := statefulJob{filterWords: &[]string{"foo", "bar"}}
job := NewJob(jobStruct, jobStruct)
driver := NewDriver(
job,
WithInputs(tmpdir),
WithWorkingLocation(tmpdir),
)

driver.Main()

output, err := ioutil.ReadFile(filepath.Join(tmpdir, "output-part-0"))
assert.Nil(t, err)

keyVals := testOutputToKeyValues(string(output))
assert.Len(t, keyVals, 2)

correct := []keyValue{
{"foo", "1"},
{"bar", "1"},
}
for _, kv := range correct {
assert.Contains(t, keyVals, kv)
}
}

0 comments on commit 1f4ad49

Please sign in to comment.