Skip to content

Commit

Permalink
Unit tests to check for linear structure with non counted repeats
Browse files Browse the repository at this point in the history
  • Loading branch information
shubham1g5 committed Apr 16, 2024
1 parent 6f168eb commit af0cc1d
Show file tree
Hide file tree
Showing 4 changed files with 184 additions and 60 deletions.
11 changes: 0 additions & 11 deletions src/main/java/org/commcare/formplayer/beans/RepeatRequestBean.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
@JsonIgnoreProperties(ignoreUnknown = true)
public class RepeatRequestBean extends SessionRequestBean {
private String repeatIndex;
private String formIndex;

// our JSON-Object mapping lib (Jackson) requires a default constructor
public RepeatRequestBean() {
Expand All @@ -31,14 +30,4 @@ public void setRepeatIndex(String repeatIndex) {
public String toString() {
return "RepeatRequestBean [repeatIndex: " + repeatIndex + ", sessionId: " + sessionId + "]";
}

@JsonGetter(value = "form_ix")
public String getFormIndex() {
return formIndex;
}

@JsonSetter(value = "form_ix")
public void setFormIndex(String formIndex) {
this.formIndex = formIndex;
}
}
21 changes: 9 additions & 12 deletions src/test/java/org/commcare/formplayer/tests/BaseTestClass.java
Original file line number Diff line number Diff line change
Expand Up @@ -605,11 +605,10 @@ NotificationMessage deleteApplicationDbs() throws Exception {
);
}

FormEntryResponseBean newRepeatRequest(String sessionId) throws Exception {
String newRepeatRequestPayload = FileUtils.getFile(this.getClass(),
"requests/new_repeat/new_repeat.json");
RepeatRequestBean newRepeatRequestBean = mapper.readValue(newRepeatRequestPayload,
RepeatRequestBean.class);
FormEntryResponseBean newRepeatRequest(String sessionId, String repeatIndex) throws Exception {
RepeatRequestBean newRepeatRequestBean = new RepeatRequestBean();
newRepeatRequestBean.setRepeatIndex(repeatIndex);
newRepeatRequestBean.setSessionId(sessionId);
populateFromSession(newRepeatRequestBean, sessionId);

return generateMockQuery(
Expand All @@ -621,13 +620,11 @@ FormEntryResponseBean newRepeatRequest(String sessionId) throws Exception {
);
}

FormEntryResponseBean deleteRepeatRequest(String sessionId) throws Exception {

String newRepeatRequestPayload = FileUtils.getFile(this.getClass(),
"requests/delete_repeat/delete_repeat.json");

RepeatRequestBean deleteRepeatRequest = mapper.readValue(newRepeatRequestPayload,
RepeatRequestBean.class);
FormEntryResponseBean deleteRepeatRequest(String sessionId, String repeatIndex)
throws Exception {
RepeatRequestBean deleteRepeatRequest = new RepeatRequestBean();
deleteRepeatRequest.setRepeatIndex(repeatIndex);
deleteRepeatRequest.setSessionId(sessionId);
populateFromSession(deleteRepeatRequest, sessionId);
return generateMockQuery(
ControllerType.FORM,
Expand Down
161 changes: 124 additions & 37 deletions src/test/java/org/commcare/formplayer/tests/RepeatTests.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.commcare.formplayer.tests;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.commcare.formplayer.beans.FormEntryResponseBean;
import org.commcare.formplayer.beans.NewFormResponse;
Expand Down Expand Up @@ -35,60 +37,145 @@ public void setUp() throws Exception {
}

@Test
public void testRepeat() throws Exception {

public void testRepeatNonCountedSimple() throws Exception {
NewFormResponse newSessionResponse = startNewForm("requests/new_form/new_form.json",
"xforms/repeat.xml");
QuestionBean[] tree = newSessionResponse.getTree();
assert (tree.length == 2);
QuestionBean dummyNode = tree[1];
assertEquals("false", dummyNode.getExists());

String sessionId = newSessionResponse.getSessionId();
FormEntryResponseBean newRepeatResponseBean = newRepeatRequest(sessionId, "1_0");

FormEntryResponseBean newRepeatResponseBean = newRepeatRequest(sessionId);

QuestionBean[] tree = newRepeatResponseBean.getTree();

assert (tree.length == 2);
QuestionBean questionBean = tree[1];
assert (questionBean.getChildren() != null);
QuestionBean[] children = questionBean.getChildren();
// Verify the repeat has been added to form tree correctly
tree = newRepeatResponseBean.getTree();
assert (tree.length == 3);
QuestionBean firstRepeat = tree[1];
assertEquals("true", firstRepeat.getExists());
assert (firstRepeat.getChildren().length == 1);
QuestionBean[] children = firstRepeat.getChildren();
assert (children.length == 1);
QuestionBean child = children[0];
assert (child.getIx().contains("1_0"));
children = child.getChildren();
assert (children.length == 1);
child = children[0];
assert (child.getIx().contains("1_0,0"));

// verify that a second dummy repeat node is added with "exists=false"
QuestionBean secondRepeat = tree[2];
assertEquals("false", secondRepeat.getExists());
assert (secondRepeat.getChildren().length == 0);

newRepeatResponseBean = newRepeatRequest(sessionId);

// Add another repeat and verify the form tree accordingly
newRepeatResponseBean = newRepeatRequest(sessionId, "1_1");
tree = newRepeatResponseBean.getTree();
assert (tree.length == 2);
questionBean = tree[1];
assert (questionBean.getChildren() != null);
children = questionBean.getChildren();
assert (children.length == 2);
assert (tree.length == 4);
secondRepeat = tree[2];
assertEquals("true", secondRepeat.getExists());
assert (secondRepeat.getChildren().length == 1);
children = secondRepeat.getChildren();
assert (children[0].getIx().contains("1_1,0"));

QuestionBean thirdRepeat = tree[3];
assertEquals("false", thirdRepeat.getExists());
assert (thirdRepeat.getChildren().length == 0);

newRepeatRequest(sessionId, "1_2");
answerQuestionGetResult("1_0,0", "repeat 1", sessionId);
answerQuestionGetResult("1_1,0", "repeat 2", sessionId);
answerQuestionGetResult("1_2,0", "repeat 3", sessionId);

// delete second repeat
FormEntryResponseBean deleteRepeatResponseBean = deleteRepeatRequest(sessionId,"1_1,0");

// Verify that we deleted the repeat at right index
tree = deleteRepeatResponseBean.getTree();
assert (tree.length == 4);
firstRepeat = tree[1];
secondRepeat = tree[2];
thirdRepeat = tree[3];
assertEquals(firstRepeat.getChildren()[0].getAnswer(),"repeat 1");
assertEquals(secondRepeat.getChildren()[0].getAnswer(),"repeat 3");
assertEquals("false", thirdRepeat.getExists());

child = children[0];
assert (child.getIx().contains("1_0"));
QuestionBean[] children2 = child.getChildren();
assert (children2.length == 1);
child = children2[0];
assert (child.getIx().contains("1_0,0"));

child = children[1];
children2 = child.getChildren();
assert (children2.length == 1);
child = children2[0];
assert (child.getIx().contains("1_1,0"));
// delete second repeat again from the new tree
deleteRepeatResponseBean = deleteRepeatRequest(sessionId, "1_1,0");
tree = deleteRepeatResponseBean.getTree();
assert (tree.length == 3);
firstRepeat = tree[1];
secondRepeat = tree[2];
assertEquals(firstRepeat.getChildren()[0].getAnswer(),"repeat 1");
assertEquals("false", secondRepeat.getExists());
}

FormEntryResponseBean deleteRepeatResponseBean = deleteRepeatRequest(sessionId);
@Test
public void testRepeatNonCountedNested() throws Exception {
NewFormResponse newSessionResponse = startNewForm("requests/new_form/new_form.json",
"xforms/nested_repeat.xml");
QuestionBean[] tree = newSessionResponse.getTree();
assert (tree.length == 1);
QuestionBean dummyNode = tree[0];
assertEquals("false", dummyNode.getExists());

tree = deleteRepeatResponseBean.getTree();
String sessionId = newSessionResponse.getSessionId();
FormEntryResponseBean newRepeatResponseBean = newRepeatRequest(sessionId, "0_0");
tree = newRepeatResponseBean.getTree();

// Verify the repeat has been added to form tree correctly
assert (tree.length == 2);
questionBean = tree[1];
assert (questionBean.getChildren() != null);
children = questionBean.getChildren();
QuestionBean firstRepeat = tree[0];
assertEquals("true", firstRepeat.getExists());
assert (firstRepeat.getChildren().length == 1);
QuestionBean[] children = firstRepeat.getChildren();
assert (children.length == 1);
QuestionBean child = children[0];
assertEquals(child.getIx(), "0_0,0_0");
assertEquals("false", child.getExists());

// Child repeat request
newRepeatResponseBean = newRepeatRequest(sessionId, "0_0, 0_0");
tree = newRepeatResponseBean.getTree();
assert (tree.length == 2);
children = tree[0].getChildren();
assert (children.length == 2);
QuestionBean firstChild = children[0];
assertEquals(firstChild.getIx(), "0_0,0_0");
assertEquals("true", firstChild.getExists());
QuestionBean secondChild = children[1];
assertEquals(secondChild.getIx(), "0_0,0_1");
assertEquals("false", secondChild.getExists());

newRepeatRequest(sessionId, "0_0, 0_1");

// create another parent repeat with three children
newRepeatRequest(sessionId, "0_1");
newRepeatRequest(sessionId, "0_1, 0_0");
newRepeatRequest(sessionId, "0_1, 0_1");
tree = newRepeatRequest(sessionId, "0_1, 0_2").getTree();

// verify the form tree has 2 parent node with 2 and 3 children respectively
// count below is count + 1 to account for dummy node
assertEquals(3, tree.length);
assertEquals (3, tree[0].getChildren().length);
assertEquals (4, tree[1].getChildren().length);

// Answer repeats
answerQuestionGetResult("0_0,0_0,0", "repeat 1_1", sessionId);
answerQuestionGetResult("0_0,0_1,0", "repeat 1_2", sessionId);
answerQuestionGetResult("0_1,0_0,0", "repeat 2_1", sessionId);
answerQuestionGetResult("0_1,0_1,0", "repeat 2_2", sessionId);
answerQuestionGetResult("0_1,0_2,0", "repeat 2_3", sessionId);

// delete 1_1 and 2_2 and verify the state
deleteRepeatRequest(sessionId, "0_0,0_0");
tree = deleteRepeatRequest(sessionId, "0_1,0_1").getTree();
assertEquals(3, tree.length);
assertEquals (2, tree[0].getChildren().length);
assertEquals (3, tree[1].getChildren().length);
assertEquals(tree[0].getChildren()[0].getChildren()[0].getAnswer(),"repeat 1_2");
assertEquals(tree[0].getChildren()[1].getExists(),"false");
assertEquals(tree[1].getChildren()[0].getChildren()[0].getAnswer(),"repeat 2_1");
assertEquals(tree[1].getChildren()[1].getChildren()[0].getAnswer(),"repeat 2_3");
assertEquals(tree[1].getChildren()[2].getExists(),"false");
}

@Test
Expand Down
51 changes: 51 additions & 0 deletions src/test/resources/xforms/nested_repeat.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<h:html xmlns:h="http://www.w3.org/1999/xhtml" xmlns:orx="http://openrosa.org/jr/xforms" xmlns="http://www.w3.org/2002/xforms" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:jr="http://openrosa.org/javarosa" xmlns:vellum="http://commcarehq.org/xforms/vellum">
<h:head>
<h:title>Nested Non Counted</h:title>
<model>
<instance>
<data xmlns:jrm="http://dev.commcarehq.org/jr/xforms" xmlns="http://openrosa.org/formdesigner/118CD69C-D541-49DB-BAA9-5F9BE5852782" uiVersion="1" version="49" name="Nested Non Counted">
<rg_non_counted jr:template="">
<nested_repeat jr:template="">
<t/>
</nested_repeat>
</rg_non_counted>
</data>
</instance>
<instance id="commcaresession" src="jr://instance/session"/>
<bind nodeset="/data/rg_non_counted"/>
<bind nodeset="/data/rg_non_counted/nested_repeat"/>
<bind nodeset="/data/rg_non_counted/nested_repeat/t" type="xsd:string"/>
<itext>
<translation lang="en" default="">
<text id="t1-label">
<value>T1</value>
</text>
<text id="rg_non_counted-label">
<value>RG Non Counted</value>
</text>
<text id="rg_non_counted/nested_repeat-label">
<value>Nested Repeat</value>
</text>
<text id="rg_non_counted/nested_repeat/t-label">
<value>T</value>
</text>
</translation>
</itext>
</model>
</h:head>
<h:body>
<group>
<label ref="jr:itext('rg_non_counted-label')"/>
<repeat nodeset="/data/rg_non_counted">
<group>
<label ref="jr:itext('rg_non_counted/nested_repeat-label')"/>
<repeat nodeset="/data/rg_non_counted/nested_repeat">
<input ref="/data/rg_non_counted/nested_repeat/t">
<label ref="jr:itext('rg_non_counted/nested_repeat/t-label')"/>
</input>
</repeat>
</group>
</repeat>
</group>
</h:body>
</h:html>

0 comments on commit af0cc1d

Please sign in to comment.