2021-05-13 23:43:52 +00:00
|
|
|
package yqlib
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
var uniqueOperatorScenarios = []expressionScenario{
|
|
|
|
{
|
2022-04-29 02:16:57 +00:00
|
|
|
description: "Unique array of scalars (string/numbers)",
|
|
|
|
subdescription: "Note that unique maintains the original order of the array.",
|
|
|
|
document: `[2,1,3,2]`,
|
|
|
|
expression: `unique`,
|
2021-05-13 23:43:52 +00:00
|
|
|
expected: []string{
|
2023-10-18 01:11:53 +00:00
|
|
|
"D0, P[], (!!seq)::[2, 1, 3]\n",
|
2021-05-13 23:43:52 +00:00
|
|
|
},
|
|
|
|
},
|
2024-06-16 00:48:01 +00:00
|
|
|
{
|
|
|
|
description: "Unique splat",
|
|
|
|
skipDoc: true,
|
|
|
|
document: `[2,1,2]`,
|
|
|
|
expression: `unique[]`,
|
|
|
|
expected: []string{
|
|
|
|
"D0, P[0], (!!int)::2\n",
|
|
|
|
"D0, P[1], (!!int)::1\n",
|
|
|
|
},
|
|
|
|
},
|
2021-05-13 23:43:52 +00:00
|
|
|
{
|
|
|
|
description: "Unique nulls",
|
|
|
|
subdescription: "Unique works on the node value, so it considers different representations of nulls to be different",
|
2021-05-14 05:01:44 +00:00
|
|
|
document: `[~,null, ~, null]`,
|
|
|
|
expression: `unique`,
|
2021-05-13 23:43:52 +00:00
|
|
|
expected: []string{
|
2023-10-18 01:11:53 +00:00
|
|
|
"D0, P[], (!!seq)::[~, null]\n",
|
2021-05-13 23:43:52 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "Unique all nulls",
|
|
|
|
subdescription: "Run against the node tag to unique all the nulls",
|
2021-05-14 05:01:44 +00:00
|
|
|
document: `[~,null, ~, null]`,
|
|
|
|
expression: `unique_by(tag)`,
|
2021-05-13 23:43:52 +00:00
|
|
|
expected: []string{
|
2023-10-18 01:11:53 +00:00
|
|
|
"D0, P[], (!!seq)::[~]\n",
|
2021-05-13 23:43:52 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2024-06-15 07:04:48 +00:00
|
|
|
description: "Unique array objects",
|
|
|
|
document: `[{name: harry, pet: cat}, {name: billy, pet: dog}, {name: harry, pet: cat}]`,
|
|
|
|
expression: `unique`,
|
|
|
|
expected: []string{
|
|
|
|
"D0, P[], (!!seq)::[{name: harry, pet: cat}, {name: billy, pet: dog}]\n",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "Unique array of objects by a field",
|
2021-05-14 05:01:44 +00:00
|
|
|
document: `[{name: harry, pet: cat}, {name: billy, pet: dog}, {name: harry, pet: dog}]`,
|
|
|
|
expression: `unique_by(.name)`,
|
2021-05-13 23:43:52 +00:00
|
|
|
expected: []string{
|
2023-10-18 01:11:53 +00:00
|
|
|
"D0, P[], (!!seq)::[{name: harry, pet: cat}, {name: billy, pet: dog}]\n",
|
2021-05-13 23:43:52 +00:00
|
|
|
},
|
|
|
|
},
|
2024-06-15 07:04:48 +00:00
|
|
|
{
|
|
|
|
description: "Unique array of arrays",
|
|
|
|
document: `[[cat,dog], [cat, sheep], [cat,dog]]`,
|
|
|
|
expression: `unique`,
|
|
|
|
expected: []string{
|
|
|
|
"D0, P[], (!!seq)::[[cat, dog], [cat, sheep]]\n",
|
|
|
|
},
|
|
|
|
},
|
2021-05-16 04:00:30 +00:00
|
|
|
{
|
|
|
|
skipDoc: true,
|
|
|
|
document: `[{name: harry, pet: cat}, {pet: fish}, {name: harry, pet: dog}]`,
|
|
|
|
expression: `unique_by(.name)`,
|
|
|
|
expected: []string{
|
2023-10-18 01:11:53 +00:00
|
|
|
"D0, P[], (!!seq)::[{name: harry, pet: cat}, {pet: fish}]\n",
|
2021-05-16 04:00:30 +00:00
|
|
|
},
|
|
|
|
},
|
2024-06-16 00:48:01 +00:00
|
|
|
{
|
|
|
|
description: "unique by splat",
|
|
|
|
skipDoc: true,
|
|
|
|
document: `[{name: harry, pet: cat}, {pet: fish}, {name: harry, pet: dog}]`,
|
|
|
|
expression: `unique_by(.name)[]`,
|
|
|
|
expected: []string{
|
|
|
|
"D0, P[0], (!!map)::{name: harry, pet: cat}\n",
|
|
|
|
"D0, P[1], (!!map)::{pet: fish}\n",
|
|
|
|
},
|
|
|
|
},
|
2021-05-16 04:00:30 +00:00
|
|
|
{
|
|
|
|
skipDoc: true,
|
|
|
|
document: `[{name: harry, pet: cat}, {pet: fish}, {name: harry, pet: dog}]`,
|
|
|
|
expression: `unique_by(.cat.dog)`,
|
|
|
|
expected: []string{
|
2023-10-18 01:11:53 +00:00
|
|
|
"D0, P[], (!!seq)::[{name: harry, pet: cat}]\n",
|
2021-05-16 04:00:30 +00:00
|
|
|
},
|
|
|
|
},
|
2022-05-31 06:28:53 +00:00
|
|
|
{
|
|
|
|
skipDoc: true,
|
|
|
|
document: "# abc\n[{name: harry, pet: cat}, {pet: fish}, {name: harry, pet: dog}]\n# xyz",
|
|
|
|
expression: `unique_by(.name)`,
|
|
|
|
expected: []string{
|
2023-10-18 01:11:53 +00:00
|
|
|
"D0, P[], (!!seq)::# abc\n[{name: harry, pet: cat}, {pet: fish}]\n# xyz\n",
|
2022-05-31 06:28:53 +00:00
|
|
|
},
|
|
|
|
},
|
2021-05-13 23:43:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestUniqueOperatorScenarios(t *testing.T) {
|
|
|
|
for _, tt := range uniqueOperatorScenarios {
|
|
|
|
testScenario(t, &tt)
|
|
|
|
}
|
2021-12-21 04:02:07 +00:00
|
|
|
documentOperatorScenarios(t, "unique", uniqueOperatorScenarios)
|
2021-05-13 23:43:52 +00:00
|
|
|
}
|