Skip to content

Commit ca0942a

Browse files
property on list shortcut
1 parent cb74166 commit ca0942a

File tree

4 files changed

+63
-15
lines changed

4 files changed

+63
-15
lines changed

webtau-docs/webtau/REST/data-node.md

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
:include-groovy: com/twosigma/webtau/http/HttpTest.groovy {entry: "use groovy closure as validation", bodyOnly: true}
44

55
Values that you access inside validation block are special values of `DataNode` type. When you assert them using `should` statement
6-
they act as proxies that record every assertion you do.
6+
they act as proxies that record every assertion that you do.
77

88

99
# Extracting Values
@@ -17,10 +17,16 @@ When you return a value from a validation block, it automatically gets converted
1717
Note: asserting that value after returning will not track and associated assertions with the call anymore. Use it only
1818
to get values required for consequent test calls.
1919

20+
# Properties On Lists
21+
22+
:include-json: objectTestResponse.json
23+
24+
If you have a list of objects like `complexList` above, you can access all its children property value with `complexList.k2`.
25+
26+
:include-groovy: com/twosigma/webtau/http/HttpTest.groovy {entry: "groovy children key shortcut", bodyOnly: true}
2027

2128
# Find
2229

23-
:include-json: objectTestResponse.json
2430

2531
Special values inside assertion block have convenient methods
2632

@@ -42,3 +48,9 @@ Note: While values inside a predicate are normal values, the result of `find` an
4248
Use `collect` to transform a collection of items
4349

4450
:include-groovy: com/twosigma/webtau/http/HttpTest.groovy {entry: "groovy transform list", bodyOnly: true}
51+
52+
# Combine
53+
54+
Methods `find` and `collect` can be chained
55+
56+
:include-groovy: com/twosigma/webtau/http/HttpTest.groovy {entry: "groovy findAll, collect, and sum", bodyOnly: true}

webtau-rest-groovy/src/test/groovy/com/twosigma/webtau/http/HttpTest.groovy

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -381,18 +381,6 @@ class HttpTest {
381381
assert found[0].getClass() == Integer
382382
}
383383

384-
@Test
385-
void "groovy findAlll, collect, and sum"() {
386-
def sum = http.get("/end-point") {
387-
return complexList
388-
.findAll { it.k1.startsWith('v1') }
389-
.collect { it.k2 }
390-
.sum()
391-
}
392-
393-
assert sum == 70
394-
}
395-
396384
@Test
397385
void "groovy findAll on body that is not a list"() {
398386
def found = http.get("/end-point") {
@@ -445,6 +433,25 @@ class HttpTest {
445433
assert transformed == []
446434
}
447435

436+
@Test
437+
void "groovy findAll, collect, and sum"() {
438+
def sum = http.get("/end-point") {
439+
return complexList
440+
.findAll { k1.startsWith('v1') }
441+
.collect { k2 }
442+
.sum()
443+
}
444+
445+
assert sum == 70
446+
}
447+
448+
@Test
449+
void "groovy children key shortcut"() {
450+
http.get("/end-point") {
451+
complexList.k2.should == [30, 40]
452+
}
453+
}
454+
448455
@Test
449456
void "send form data"() {
450457
byte[] content = [0, 1, 2, 101, 102, 103, 0] as byte[]

webtau-rest/src/main/java/com/twosigma/webtau/http/datanode/StructuredDataNode.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.Collections;
2222
import java.util.List;
2323
import java.util.Map;
24+
import java.util.stream.Collectors;
2425

2526
import static java.util.stream.Collectors.joining;
2627

@@ -56,7 +57,13 @@ public DataNodeId id() {
5657

5758
@Override
5859
public DataNode get(String name) {
59-
return (children != null && children.containsKey(name)) ? children.get(name) : new NullDataNode(id.child(name));
60+
if (isList()) {
61+
return getAsCollectFromList(name);
62+
}
63+
64+
return (children != null && children.containsKey(name)) ?
65+
children.get(name):
66+
new NullDataNode(id.child(name));
6067
}
6168

6269
@Override
@@ -117,4 +124,11 @@ public String toString() {
117124

118125
return "{" + children.entrySet().stream().map(e -> e.getKey() + ": " + e.getValue()).collect(joining(", ")) + "}";
119126
}
127+
128+
private DataNode getAsCollectFromList(String name) {
129+
return new StructuredDataNode(id.child(name),
130+
values.stream()
131+
.map(n -> n.get(name))
132+
.collect(Collectors.toList()));
133+
}
120134
}

webtau-rest/src/test/groovy/com/twosigma/webtau/http/datanode/StructuredDataNodeTest.groovy

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,19 @@ class StructuredDataNodeTest {
6262

6363
node.get().checkLevel.should == CheckLevel.ExplicitFailed
6464
}
65+
66+
@Test
67+
void "shortcut for children props"() {
68+
def node = DataNodeBuilder.fromList(new DataNodeId("body"), [
69+
[name: 'name1', score: 10],
70+
[name: 'name2', score: 20],
71+
])
72+
73+
node.name.should(equal(['name1', 'name2']))
74+
75+
node.get(0).get('name').get().checkLevel.should == CheckLevel.ExplicitPassed
76+
node.get(1).get('name').get().checkLevel.should == CheckLevel.ExplicitPassed
77+
node.get(0).get('score').get().checkLevel.should == CheckLevel.None
78+
node.get(1).get('score').get().checkLevel.should == CheckLevel.None
79+
}
6580
}

0 commit comments

Comments
 (0)