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 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 TestTabSource(t *testing.T) { reader := csv.NewReader(bytes.NewBufferString(tabData)) reader.Comma = '\t' table := NewWithCsvReader(reader) assert.Equal(t, expected, tests.GetRows(table)) assert.Nil(t, table.Err()) }
func TestGearmanSource(t *testing.T) { c := &mockClient{Mock: &mock.Mock{}, chans: []chan *packet.Packet{}} c.On("Submit", "function", []byte("workload"), mock.Anything, mock.Anything).Return(nil, nil).Once() numCalls := 0 table := New(c, "function", []byte("workload"), func(in []byte) (optimus.Row, error) { numCalls++ assert.Equal(t, in, []byte(fmt.Sprintf("%d", numCalls))) return optimus.Row{"field1": fmt.Sprintf("value%d", numCalls)}, nil }) expected := []optimus.Row{ {"field1": "value1"}, {"field1": "value2"}, } go func() { // Wait until a packet has been submitted for len(getChans(c)) == 0 { time.Sleep(time.Millisecond) } packets := getChans(c)[0] packets <- handlePacket("", packet.WorkData, [][]byte{[]byte("1")}) packets <- handlePacket("", packet.WorkData, [][]byte{[]byte("2")}) packets <- handlePacket("", packet.WorkComplete, nil) }() assert.Equal(t, expected, tests.GetRows(table)) assert.Nil(t, table.Err()) }
func TestJSONSource(t *testing.T) { table := New(bytes.NewBufferString(jsonData)) expected := []optimus.Row{ {"header1": "field1", "header2": "field2", "header3": "field3"}, {"header1": "field4", "header2": "field5", "header3": "field6"}, {"header1": "field7", "header2": "field8", "header3": "field9"}, } assert.Equal(t, expected, tests.GetRows(table)) assert.Nil(t, table.Err()) }
func TestGearmanSourceFail(t *testing.T) { c := &mockClient{Mock: &mock.Mock{}, chans: []chan *packet.Packet{}} c.On("Submit", "function", []byte("workload"), mock.Anything, mock.Anything).Return(nil, nil).Once() table := New(c, "function", []byte("workload"), func(in []byte) (optimus.Row, error) { t.Fatal("never expected converter to be called") return nil, nil }) go func() { packets := c.chans[0] packets <- handlePacket("", packet.WorkWarning, [][]byte{[]byte("1")}) packets <- handlePacket("", packet.WorkFail, nil) }() expected := []optimus.Row{} assert.Equal(t, expected, tests.GetRows(table)) assert.EqualError(t, table.Err(), "gearman job 'function' failed with warnings: 1") }
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()) } } } }
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) }
func TestCSVSource(t *testing.T) { table := New(bytes.NewBufferString(csvData)) assert.Equal(t, expected, tests.GetRows(table)) assert.Nil(t, table.Err()) }