func TestPairSuccess(t *testing.T) { filterRows := func(rows []optimus.Row, filterFn func(optimus.Row) (bool, error)) []optimus.Row { out := []optimus.Row{} for _, row := range rows { if f, _ := filterFn(row); f { out = append(out, row) } } return out } for _, joinTest := range joinTests { for _, joinFilter := range joinFilters { left := slice.New(joinTest.left) right := slice.New(joinTest.right) pair := Pair(right, joinTest.leftID, joinTest.rightID, joinFilter) table := optimus.Transform(left, pair) actual := tests.GetRows(table) assert.Equal(t, filterRows(joinTest.expected, joinFilter), actual) assert.Nil(t, table.Err()) } } }
func TestPairErrorsRowIdentifier(t *testing.T) { for _, joinHasherError := range joinHasherErrors { left := slice.New(joinHasherError.left) right := slice.New(joinHasherError.right) table := optimus.Transform(left, Pair(right, joinHasherError.leftID, joinHasherError.rightID, OuterJoin)) tests.Consumed(t, table) tests.Consumed(t, right) assert.EqualError(t, table.Err(), joinHasherError.expected) } }
func TestStable(t *testing.T) { input := []optimus.Row{ {"c": "a", "b": "a"}, {"c": "a", "b": "b"}, {"c": "a", "b": "c"}, {"c": "a", "b": "d"}, {"c": "a", "b": "e"}, {"c": "a", "b": "f"}, {"c": "a", "b": "g"}, {"c": "a", "b": "h"}, {"c": "a", "b": "i"}, {"c": "a", "b": "j"}, {"c": "a", "b": "k"}, {"c": "a", "b": "l"}, {"c": "a", "b": "m"}, {"c": "a", "b": "n"}, {"c": "a", "b": "o"}, {"c": "a", "b": "p"}, {"c": "a", "b": "q"}, {"c": "a", "b": "r"}, {"c": "a", "b": "s"}, {"c": "a", "b": "t"}, {"c": "a", "b": "u"}, {"c": "a", "b": "v"}, {"c": "a", "b": "w"}, {"c": "a", "b": "x"}, {"c": "a", "b": "y"}, {"c": "a", "b": "z"}, } table := optimus.Transform(slice.New(input), StableSort(byStringKey("c"))) actual := tests.GetRows(table) assert.Nil(t, table.Err()) assert.Equal(t, actual, input) }
func TestPairErrorsRightTable(t *testing.T) { left := slice.New([]optimus.Row{a, b, c}) right := errorSource.New(fmt.Errorf("garbage error")) table := optimus.Transform(left, Pair(right, KeyIdentifier(""), KeyIdentifier(""), OuterJoin)) tests.Consumed(t, table) tests.Consumed(t, right) assert.EqualError(t, table.Err(), "garbage error") }
func TestJoinErrors(t *testing.T) { left := slice.New([]optimus.Row{a, b, c}) right := errorTable.New(fmt.Errorf("garbage error")) table := optimus.Transform(left, Join(right, "", "", JoinType.Left)) tests.Consumed(t, table) tests.Consumed(t, right) assert.EqualError(t, table.Err(), "garbage error") }
func TestUniqueErrorForInvalidHeader(t *testing.T) { inputTable := slice.New([]optimus.Row{ {"header1": "value1", "header2": "value2"}, }) actualTable := optimus.Transform(inputTable, Unique(invalidHeaderHash)) tests.Consumed(t, actualTable) if actualTable.Err() == nil { t.Fatal("Expected actualTable to report an error") } }
// Test that chaining together multiple transforms behaves as expected func TestUniqueForMultipleHeaders(t *testing.T) { expected := []optimus.Row{ {"header1": "value1", "header2": "value2", "header3": "value1"}, } inputTable := slice.New([]optimus.Row{ {"header1": "value1", "header2": "value2", "header3": "value1"}, {"header1": "value1", "header2": "value2", "header3": "value2"}, }) actualTable := optimus.Transform(inputTable, Unique(multiHeaderHash)) actual := tests.HasRows(t, actualTable, 1) assert.Equal(t, expected, actual) }
func TestUniqueReturnsMultiple(t *testing.T) { expected := []optimus.Row{ {"header1": "value1", "header2": "value2"}, {"header1": "value2", "header2": "value4"}, {"header1": "value3", "header2": "value6"}, } inputTable := slice.New([]optimus.Row{ {"header1": "value1", "header2": "value2"}, {"header1": "value2", "header2": "value4"}, {"header1": "value3", "header2": "value6"}, }) actualTable := optimus.Transform(inputTable, Unique(singleHeaderHash)) actual := tests.HasRows(t, actualTable, 3) assert.Equal(t, expected, actual) }
func TestSort(t *testing.T) { for _, sortTest := range sortTests { for _, sort := range []optimus.TransformFunc{Sort(sortTest.less), StableSort(sortTest.less)} { input := slice.New(sortTest.input) table := optimus.Transform(input, sort) actual := tests.GetRows(table) if sortTest.err != nil { assert.Equal(t, sortTest.err, table.Err()) } else { assert.Equal(t, actual, sortTest.output) assert.Nil(t, table.Err()) } } } }
// Test that chaining together multiple transforms behaves as expected func TestChaining(t *testing.T) { input := []optimus.Row{ {"header1": "value1", "header2": "value2"}, {"header1": "value3", "header2": "value4"}, {"header1": "value5", "header2": "value6"}, } expected := []optimus.Row{ {"header4": "value1"}, {"header4": "value3"}, {"header4": "value5"}, } table := slice.New(input) transformedTable := New(table).Fieldmap( map[string][]string{"header1": {"header3"}}).Fieldmap(map[string][]string{"header3": {"header4"}}).Table() rows := tests.HasRows(t, transformedTable, 3) assert.Equal(t, expected, rows) }
func TestGroupBy(t *testing.T) { transform := GroupBy(KeyIdentifier("group")) input := []optimus.Row{ {"group": "1", "key": "a"}, {"group": 2, "key": "d"}, {"group": "3", "key": "g"}, {"group": "1", "key": 1}, {"group": 2, "key": 2}, {"group": "3", "key": 3}, {"group": "1", "key": 'b'}, {"group": 2, "key": 'e'}, {"group": "3", "key": 'h'}, {"group": "hello", "key": "world"}, {"group": "hello", "val": "gopher"}, } expected := []optimus.Row{ {"id": "1", "rows": []optimus.Row{input[0], input[3], input[6]}}, {"id": 2, "rows": []optimus.Row{input[1], input[4], input[7]}}, {"id": "3", "rows": []optimus.Row{input[2], input[5], input[8]}}, {"id": "hello", "rows": []optimus.Row{input[9], input[10]}}, } // groupBy makes no guarantees about what order the groups are outputted in. // Let's manually sort them based on the group name. sortByGroup := func(in []optimus.Row) []optimus.Row { out := make([]optimus.Row, len(in), len(in)) indexMap := map[interface{}]int{"1": 0, 2: 1, "3": 2, "hello": 3} for _, row := range in { out[indexMap[row["id"]]] = row } return out } actualTable := optimus.Transform(slice.New(input), transform) actual := tests.HasRows(t, actualTable, 4) assert.Equal(t, expected, sortByGroup(actual)) }
errorTable "gopkg.in/Clever/optimus.v3/sources/error" "gopkg.in/Clever/optimus.v3/sources/infinite" "gopkg.in/Clever/optimus.v3/sources/slice" "gopkg.in/Clever/optimus.v3/tests" ) var defaultInput = func() []optimus.Row { return []optimus.Row{ {"header1": "value1", "header2": "value2"}, {"header1": "value3", "header2": "value4"}, {"header1": "value5", "header2": "value6"}, } } var defaultSource = func() optimus.Table { return slice.New(defaultInput()) } var transformEqualities = []tests.TableCompareConfig{ { Name: "Fieldmap-MapSome", Actual: func(optimus.Table, interface{}) optimus.Table { return optimus.Transform(defaultSource(), Fieldmap(map[string][]string{"header1": {"header4"}})) }, Expected: func(optimus.Table, interface{}) optimus.Table { return slice.New([]optimus.Row{ {"header4": "value1"}, {"header4": "value3"}, {"header4": "value5"}, }) },
func TestAlphabetical(t *testing.T) { source := slice.New([]optimus.Row{{"a": "0", "b": "0", "c": "0", "d": "0", "e": "0", "f": "0"}}) actual := &bytes.Buffer{} assert.Nil(t, New(actual)(source)) assert.Equal(t, strings.Split(actual.String(), "\n")[0], "a,b,c,d,e,f") }
func TestNilValues(t *testing.T) { rows := []optimus.Row{{"field1": "val1", "field2": nil}} actual := &bytes.Buffer{} assert.Nil(t, New(actual)(slice.New(rows))) assert.Equal(t, tests.GetRows(csv.New(actual)), rows) }
"gopkg.in/Clever/optimus.v3/sources/slice" "gopkg.in/Clever/optimus.v3/tests" "gopkg.in/Clever/optimus.v3/transforms" "testing" ) var defaultInput = func() []optimus.Row { return []optimus.Row{ {"header1": "value1", "header2": "value2"}, {"header1": "value3", "header2": "value4"}, {"header1": "value5", "header2": "value6"}, } } var defaultSource = func() optimus.Table { return slice.New(defaultInput()) } var errorTransform = func(msg string) func(optimus.Row) (optimus.Row, error) { return func(optimus.Row) (optimus.Row, error) { return nil, errors.New(msg) } } // Test that chaining together multiple transforms behaves as expected func TestChaining(t *testing.T) { input := []optimus.Row{ {"header1": "value1", "header2": "value2"}, {"header1": "value3", "header2": "value4"}, {"header1": "value5", "header2": "value6"}, }