Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update entities features for new spec #697

Merged
merged 2 commits into from
Nov 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.javarosa.core.model.instance.FormInstance;
import org.javarosa.core.model.instance.TreeElement;
import org.javarosa.xpath.expr.XPathFuncExpr;
import org.jetbrains.annotations.Nullable;

public class EntityFormParser {
Expand All @@ -17,10 +18,10 @@ public static String parseFirstDatasetToCreate(FormInstance mainInstance) {
TreeElement entity = getEntityElement(mainInstance);

if (entity != null) {
TreeElement create = entity.getFirstChild(ENTITIES_NAMESPACE, "create");
String create = entity.getAttributeValue(null, "create");

if (create != null) {
if (create.isRelevant()) {
if (XPathFuncExpr.boolStr(create)) {
return entity.getAttributeValue(null, "dataset");
}
}
Expand All @@ -35,7 +36,7 @@ public static TreeElement getEntityElement(FormInstance mainInstance) {
TreeElement meta = root.getFirstChild("meta");

if (meta != null) {
return meta.getFirstChild(ENTITIES_NAMESPACE, "entity");
return meta.getFirstChild("entity");
} else {
return null;
}
Expand Down
95 changes: 29 additions & 66 deletions src/test/java/org/javarosa/entities/EntitiesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ public void fillingFormWithoutCreate_doesNotCreateAnyEntities() throws IOExcepti
mainInstance(
t("data id=\"entity-form\"",
t("name"),
t("orx:meta",
t("entities:entity dataset=\"people\"")
t("meta",
t("entity dataset=\"people\"")
)
)
),
Expand Down Expand Up @@ -93,10 +93,8 @@ public void fillingFormWithCreate_makesEntityAvailable() throws IOException, XFo
mainInstance(
t("data id=\"create-entity-form\"",
t("name"),
t("orx:meta",
t("entities:entity dataset=\"people\"",
t("entities:create")
)
t("meta",
t("entity dataset=\"people\" create=\"1\"")
)
)
),
Expand All @@ -121,7 +119,7 @@ public void fillingFormWithCreate_makesEntityAvailable() throws IOException, XFo
}

@Test
public void fillingFormWithNonRelevantCreate_doesNotCreateAnyEntities() throws IOException, XFormParser.ParseException {
public void fillingFormWithDynamicCreateExpression_conditionallyCreatesEntities() throws IOException, XFormParser.ParseException {
Scenario scenario = Scenario.init("Create entity form", XFormsElement.html(
asList(
new Pair<>("entities", "http://www.opendatakit.org/xforms/entities")
Expand All @@ -133,14 +131,12 @@ public void fillingFormWithNonRelevantCreate_doesNotCreateAnyEntities() throws I
t("data id=\"create-entity-form\"",
t("name"),
t("join"),
t("orx:meta",
t("entities:entity dataset=\"members\"",
t("entities:create")
)
t("meta",
t("entity dataset=\"members\" create=\"\"")
)
)
),
bind("/data/orx:meta/entities:entity/entities:create").relevant("/data/join = 'yes'"),
bind("/data/meta/entity/@create").calculate("/data/join = 'yes'"),
bind("/data/name").type("string").withAttribute("entities", "saveto", "name")
)
),
Expand All @@ -154,10 +150,22 @@ public void fillingFormWithNonRelevantCreate_doesNotCreateAnyEntities() throws I

scenario.next();
scenario.answer("Roman Roy");
scenario.answer(scenario.choicesOf("/data/join").get(1));
scenario.next();
scenario.answer(scenario.choicesOf("/data/join").get(0));

scenario.finalizeInstance();
List<Entity> entities = scenario.getFormEntryController().getModel().getExtras().get(Entities.class).getEntities();
assertThat(entities.size(), equalTo(1));

scenario.newInstance();
scenario.getFormEntryController().addPostProcessor(new EntityFormFinalizationProcessor());
scenario.next();
scenario.answer("Roman Roy");
scenario.next();
scenario.answer(scenario.choicesOf("/data/join").get(1));

scenario.finalizeInstance();
entities = scenario.getFormEntryController().getModel().getExtras().get(Entities.class).getEntities();
assertThat(entities.size(), equalTo(0));
}

Expand All @@ -173,10 +181,8 @@ public void entityFormCanBeSerialized() throws IOException, DeserializationExcep
mainInstance(
t("data id=\"create-entity-form\"",
t("name"),
t("orx:meta",
t("entities:entity dataset=\"people\"",
t("entities:create")
)
t("meta",
t("entities:entity dataset=\"people\" create=\"1\"")
)
)
),
Expand Down Expand Up @@ -215,10 +221,8 @@ public void entitiesNamespaceWorksRegardlessOfName() throws IOException, Deseria
mainInstance(
t("data id=\"create-entity-form\"",
t("name"),
t("orx:meta",
t("blah:entity dataset=\"people\"",
t("blah:create")
)
t("meta",
t("entity dataset=\"people\" create=\"1\"")
)
)
),
Expand All @@ -241,43 +245,6 @@ public void entitiesNamespaceWorksRegardlessOfName() throws IOException, Deseria
assertThat(entities.get(0).properties, equalTo(asList(new Pair<>("name", "Tom Wambsgans"))));
}

@Test
public void mustUseCorrectNamespace() throws IOException, XFormParser.ParseException {
Scenario scenario = Scenario.init("Create entity form", XFormsElement.html(
asList(
new Pair<>("entities", "http://www.example.com/xforms/entities")
),
head(
title("Create entity form"),
model(asList(new Pair<>("entities:entities-version", EntityFormParseProcessor.SUPPORTED_VERSION + ".1")),
mainInstance(
t("data id=\"create-entity-form\"",
t("name"),
t("orx:meta",
t("entities:entity dataset=\"people\"",
t("entities:create")
)
)
)
),
bind("/data/name").type("string").withAttribute("entities", "saveto", "name")
)
),
body(
input("/data/name")
)
));

scenario.getFormEntryController().addPostProcessor(new EntityFormFinalizationProcessor());

scenario.next();
scenario.answer("Tom Wambsgans");

scenario.finalizeInstance();
List<Entity> entities = scenario.getFormEntryController().getModel().getExtras().get(Entities.class).getEntities();
assertThat(entities.size(), equalTo(0));
}

@Test
public void fillingFormWithSelectSaveTo_andWithCreate_savesValuesCorrectlyToEntity() throws IOException, XFormParser.ParseException {
Scenario scenario = Scenario.init("Create entity form", XFormsElement.html(
Expand All @@ -290,10 +257,8 @@ public void fillingFormWithSelectSaveTo_andWithCreate_savesValuesCorrectlyToEnti
mainInstance(
t("data id=\"create-entity-form\"",
t("team"),
t("orx:meta",
t("entities:entity dataset=\"people\"",
t("entities:create")
)
t("meta",
t("entity dataset=\"people\" create=\"1\"")
)
)
),
Expand Down Expand Up @@ -328,10 +293,8 @@ public void savetoIsRemovedFromBindAttributesForClients() throws IOException, XF
mainInstance(
t("data id=\"create-entity-form\"",
t("name"),
t("orx:meta",
t("entities:entity dataset=\"people\"",
t("entities:create")
)
t("meta",
t("entity dataset=\"people\" create=\"1\"")
)
)
),
Expand Down
16 changes: 6 additions & 10 deletions src/test/java/org/javarosa/entities/EntityFormParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import org.junit.Test;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

import static java.util.Arrays.asList;
Expand All @@ -26,26 +25,23 @@
public class EntityFormParserTest {

@Test
public void parseFirstDatasetToCreate_ignoresDatasetWithCreateActionWithIncorrectNamespace() throws IOException, XFormParser.ParseException {
public void parseFirstDatasetToCreate_findsCreateWithTrueString() throws XFormParser.ParseException {
XFormsElement form = XFormsElement.html(
asList(
new Pair<>("correct", "http://www.opendatakit.org/xforms/entities"),
new Pair<>("incorrect", "blah")
new Pair<>("entities", "http://www.opendatakit.org/xforms/entities")
),
head(
title("Create entity form"),
model(
mainInstance(
t("data id=\"create-entity-form\"",
t("name"),
t("orx:meta",
t("correct:entity dataset=\"people\"",
t("incorrect:create")
)
t("meta",
t("entity dataset=\"people\" create=\"true\"")
)
)
),
bind("/data/name").type("string").withAttribute("correct", "saveto", "name")
bind("/data/name").type("string").withAttribute("entities", "saveto", "name")
)
),
body(
Expand All @@ -57,6 +53,6 @@ public void parseFirstDatasetToCreate_ignoresDatasetWithCreateActionWithIncorrec
FormDef formDef = parser.parse(null);

String dataset = EntityFormParser.parseFirstDatasetToCreate(formDef.getMainInstance());
assertThat(dataset, equalTo(null));
assertThat(dataset, equalTo("people"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public void whenVersionIsMissing_parsesWithoutError() throws XFormParser.ParseEx
mainInstance(
t("data id=\"create-entity-form\"",
t("name"),
t("orx:meta")
t("meta")
)
),
bind("/data/name").type("string")
Expand Down Expand Up @@ -68,10 +68,8 @@ public void whenVersionIsMissing_andThereIsAnEntityElement_throwsException() {
mainInstance(
t("data id=\"create-entity-form\"",
t("name"),
t("orx:meta",
t("entities:entity dataset=\"people\"",
t("entities:create")
)
t("meta",
t("entity dataset=\"people\"")
)
)
),
Expand Down Expand Up @@ -111,10 +109,8 @@ public void whenVersionIsNotRecognized_throwsException() throws XFormParser.Pars
mainInstance(
t("data id=\"create-entity-form\"",
t("name"),
t("orx:meta",
t("entities:entity dataset=\"people\"",
t("entities:create")
)
t("meta",
t("entity dataset=\"people\"")
)
)
),
Expand Down Expand Up @@ -146,10 +142,8 @@ public void whenVersionIsNewPatch_parsesCorrectly() throws XFormParser.ParseExce
mainInstance(
t("data id=\"create-entity-form\"",
t("name"),
t("orx:meta",
t("entities:entity dataset=\"people\"",
t("entities:create")
)
t("meta",
t("entity dataset=\"people\"")
)
)
),
Expand Down Expand Up @@ -182,10 +176,8 @@ public void saveTosWithIncorrectNamespaceAreIgnored() throws XFormParser.ParseEx
mainInstance(
t("data id=\"create-entity-form\"",
t("name"),
t("orx:meta",
t("correct:entity dataset=\"people\"",
t("correct:create")
)
t("meta",
t("entity dataset=\"people\"")
)
)
),
Expand Down