Skip to content

Commit 8857dcf

Browse files
committed
DEVEXP-551: Assert JSON arrays are the same with test:assert-same-values
1 parent 2880f27 commit 8857dcf

File tree

5 files changed

+222
-9
lines changed

5 files changed

+222
-9
lines changed

marklogic-unit-test-client/src/test/ml-modules/root/test/suites/Assertions/assert-equal-json.sjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ function assertThrowsErrorWithMessage(f, errorName, errorMessage)
6363
test.fail('Function did not fail');
6464
}
6565
catch (e) {
66-
test.success();
6766
xdmp.log(e, 'info');
6867
let actual = e.stack.substr(0, e.stack.indexOf(" at "))
6968
if (!e.stack.includes(errorName)) {
@@ -72,6 +71,7 @@ function assertThrowsErrorWithMessage(f, errorName, errorMessage)
7271
if (!e.stack.includes(errorMessage)) {
7372
test.fail(`Function failed, but did not contain expected message [${errorMessage}] actual [${actual}]`);
7473
}
74+
return test.success();
7575
}
7676
}
7777

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
xquery version "1.0-ml";
2+
import module namespace test="http://marklogic.com/test" at "/test/test-helper.xqy";
3+
4+
((
5+
test:assert-same-values(
6+
array-node { },
7+
array-node { },
8+
"Two empty arrays, should be equal."
9+
)
10+
,
11+
test:assert-same-values(
12+
array-node { 1 },
13+
array-node { 1 },
14+
"Two arrays with the same single elements, should be equal."
15+
)
16+
,
17+
test:assert-same-values(
18+
array-node { 1, 2, 4, 3 },
19+
array-node { 3, 4, 1, 2 },
20+
"Two arrays with the same elements, in a different order, should be equal."
21+
)
22+
,
23+
test:assert-same-values(
24+
array-node { 1, 2, 4, 3, 4 },
25+
array-node { 3, 4, 1, 4, 2 },
26+
"Two arrays with the same elements (including duplicates), in a different order, should be equal."
27+
)
28+
,
29+
test:assert-same-values(
30+
array-node { "A", "F", "BG", "E", "BC" },
31+
array-node { "BC", "F", "A", "E", "BG" },
32+
"Two arrays with the same string elements, in a different order, should be equal."
33+
)
34+
,
35+
test:assert-throws-error-with-message(
36+
function() {
37+
test:assert-same-values(
38+
array-node { 1, 2, 4, 5 },
39+
array-node { 1, 2, 4, 5, 5 },
40+
"An array with a duplicate should not equal an array without that duplicate."
41+
)
42+
},
43+
"ASSERT-EQUAL-FAILED",
44+
"An array with a duplicate should not equal an array without that duplicate.; expected: (1, 2, 4, ...) actual: (1, 2, 4, ...)"
45+
)
46+
,
47+
test:assert-throws-error-with-message(
48+
function() {
49+
test:assert-same-values(
50+
array-node { 1, 2, 4 },
51+
array-node { 1, 2, 4, 5 },
52+
"A longer Actual array should not equal a shorter Expected array."
53+
)
54+
},
55+
"ASSERT-EQUAL-FAILED",
56+
"A longer Actual array should not equal a shorter Expected array.; expected: (1, 2, 4) actual: (1, 2, 4, ...)"
57+
)
58+
,
59+
test:assert-throws-error-with-message(
60+
function() {
61+
test:assert-same-values(
62+
array-node { 1, 2, 4 },
63+
array-node { 1, 2 },
64+
"A shorter Actual array should not equal a longer Expected array."
65+
)
66+
},
67+
"ASSERT-EQUAL-FAILED",
68+
"A shorter Actual array should not equal a longer Expected array.; expected: (1, 2, 4) actual: (1, 2)"
69+
)
70+
,
71+
test:assert-throws-error-with-message(
72+
function() {
73+
test:assert-same-values(
74+
array-node { 1, 2, 4 },
75+
array-node { 1, 2, object-node {"A": "4"} },
76+
"This actually causes an exception since the items are not comparable."
77+
)
78+
},
79+
"err:XPTY0004",
80+
"Items not comparable"
81+
)
82+
))
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
xquery version "1.0-ml";
2+
import module namespace test="http://marklogic.com/test" at "/test/test-helper.xqy";
3+
4+
((
5+
test:assert-same-values(
6+
(),
7+
(),
8+
"Two empty sequences, should be equal."
9+
),
10+
test:assert-same-values(
11+
(1),
12+
(1),
13+
"Two sequences with a single matching atomic element, should be equal."
14+
),
15+
test:assert-same-values(
16+
(1, 2, 4, 3),
17+
(3, 4, 1, 2),
18+
"Two sequences with a multiple matching atomic elements, should be equal."
19+
),
20+
test:assert-same-values(
21+
(1, 2, 4, 3, 4),
22+
(3, 4, 1, 4, 2),
23+
"Two sequences with a multiple matching atomic elements, should be equal."
24+
),
25+
test:assert-same-values(
26+
(<A/>),
27+
(<A/>),
28+
"Two sequences with the same single XML element, should be equal."
29+
),
30+
test:assert-same-values(
31+
(<A>a</A>, <B>b</B>, <D>d</D>, <C>c</C>),
32+
(<C>c</C>, <D>d</D>, <A>a</A>, <B>b</B>),
33+
"Two sequences with the same multiple XML elements, should be equal."
34+
)
35+
))
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
const test = require('/test/test-helper.xqy');
2+
3+
function assertThrowsErrorWithMessage(f, errorName, errorMessage)
4+
{
5+
try {
6+
f();
7+
test.fail('Function did not fail');
8+
}
9+
catch (e) {
10+
xdmp.log(e, 'info');
11+
let actual = e.stack.substr(0, e.stack.indexOf(" at "))
12+
if (!e.stack.includes(errorName)) {
13+
test.fail(`Function failed, but did not contain expected error [${errorName}] actual: [${actual}]`);
14+
}
15+
if (!e.stack.includes(errorMessage)) {
16+
test.fail(`Function failed, but did not contain expected message [${errorMessage}] actual [${actual}]`);
17+
}
18+
return test.success();
19+
}
20+
}
21+
22+
[
23+
test.assertSameValues([], []),
24+
test.assertSameValues([1], [1]),
25+
test.assertSameValues([1, 2, 4, 3], [3, 4, 1, 2]),
26+
test.assertSameValues([1, 2, 4, 3, 4], [3, 4, 1, 4, 2]),
27+
test.assertSameValues(["A", "F", "BG", "E", "BC"], ["BC", "F", "A", "E", "BG"]),
28+
assertThrowsErrorWithMessage(
29+
function() {
30+
test.assertSameValues([1, 2, 4, 5], [1, 2, 4, 5, 5], "An array with a duplicate should not equal an array without that duplicate.")
31+
},
32+
"ASSERT-EQUAL-FAILED",
33+
"An array with a duplicate should not equal an array without that duplicate.; expected: (1, 2, 4, ...) actual: (1, 2, 4, ...)"
34+
),
35+
assertThrowsErrorWithMessage(
36+
function() {
37+
test.assertSameValues([1, 2, 4], [1, 2, 4, 5], "A longer Actual array should not equal a shorter Expected array.")
38+
},
39+
"ASSERT-EQUAL-FAILED",
40+
"A longer Actual array should not equal a shorter Expected array.; expected: (1, 2, 4) actual: (1, 2, 4, ...)"
41+
),
42+
assertThrowsErrorWithMessage(
43+
function() {
44+
test.assertSameValues([1, 2, 4], [1, 2], "A shorter Actual array should not equal a longer Expected array.")
45+
},
46+
"ASSERT-EQUAL-FAILED",
47+
"A shorter Actual array should not equal a longer Expected array.; expected: (1, 2, 4) actual: (1, 2)"
48+
),
49+
assertThrowsErrorWithMessage(
50+
function() {
51+
test.assertSameValues([1, 2, 4], [1, 2, "S"], "This actually causes an exception since the items are not comparable.")
52+
},
53+
"err:XPTY0004",
54+
"Items not comparable"
55+
),
56+
assertThrowsErrorWithMessage(
57+
function() {
58+
test.assertSameValues([{"A": "a"}, {"B": "b"}], [{"B": "b"}, {"A": "a"}], "This actually causes an exception since we do not handle JSON objects and the items are not comparable.")
59+
},
60+
"err:XPTY0004",
61+
"Items not comparable"
62+
)
63+
]

marklogic-unit-test-modules/src/main/ml-modules/root/test/test-helper.xqy

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -531,14 +531,8 @@ declare function test:assert-same-values($expected as item()*, $actual as item()
531531

532532
declare function test:assert-same-values($expected as item()*, $actual as item()*, $message as xs:string*)
533533
{
534-
let $expected-ordered :=
535-
for $e in $expected
536-
order by $e
537-
return $e
538-
let $actual-ordered :=
539-
for $a in $actual
540-
order by $a
541-
return $a
534+
let $expected-ordered := test:sort-array-or-sequence($expected)
535+
let $actual-ordered := test:sort-array-or-sequence($actual)
542536
return test:assert-equal($expected-ordered, $actual-ordered, $message)
543537
};
544538

@@ -990,3 +984,42 @@ declare private function test:assert-throws-error_($function as xdmp:function, $
990984
test:success()
991985
}
992986
};
987+
988+
(:
989+
: If the description of incoming is 'array-node',
990+
: then the developer is invoking this in XQuery and is passing in an XQuery array-node.
991+
: If the description is 'json:array',
992+
: then the developer is invoking this in JavaScript and is passing in a regular array,
993+
: or the developer is invoking this in XQuery and is using json:array() to build the object
994+
: Otherwise, the developer is invoking this in XQuery and is passing in a regular sequence.
995+
:)
996+
declare private function test:sort-array-or-sequence($incoming as item()*)
997+
{
998+
if (fn:contains(xdmp:describe($incoming), "array-node")) then
999+
test:sort-array-nodes($incoming)
1000+
else if (fn:contains(xdmp:describe($incoming), "json:array")) then
1001+
test:sort-json-array($incoming)
1002+
else
1003+
test:sort-sequence($incoming)
1004+
};
1005+
1006+
declare private function test:sort-sequence($incoming as item()*)
1007+
{
1008+
for $element in $incoming
1009+
order by $element
1010+
return $element
1011+
};
1012+
1013+
declare private function test:sort-array-nodes($incoming as item()*)
1014+
{
1015+
for $element in $incoming/*
1016+
order by $element/data()
1017+
return $element/data()
1018+
};
1019+
1020+
declare private function test:sort-json-array($incoming as item()*)
1021+
{
1022+
for $i in 1 to fn:count(json:array-values($incoming))
1023+
order by $incoming[$i]
1024+
return $incoming[$i]
1025+
};

0 commit comments

Comments
 (0)