Esempio n. 1
0
func TestMultipleFieldmapError(t *testing.T) {
	table := optimus.Transform(defaultSource(),
		SafeFieldmap(map[string][]string{"header1": {"header4"}, "header2": {"header4"}}))

	tests.HasRows(t, table, 0)
	assert.EqualError(t, table.Err(), "Detected multiple mappings to the same value for key header4")
}
Esempio n. 2
0
// 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)
}
Esempio n. 3
0
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)
}
Esempio n. 4
0
// 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)
}
Esempio n. 5
0
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))
}