Skip to content

Commit ba6ee47

Browse files
authored
Fix entity bug where the wrong properties of an entity are populated (#914)
* Fix entity bug: join on ds_property_field formDefId instead of schema * Updated test to test removing an entity bind
1 parent 891edc2 commit ba6ee47

File tree

3 files changed

+94
-1
lines changed

3 files changed

+94
-1
lines changed

lib/model/query/datasets.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ JOIN form_defs ON fs.id = form_defs."schemaId"
347347
JOIN dataset_form_defs ON
348348
dataset_form_defs."formDefId" = form_defs."id"
349349
LEFT OUTER JOIN ds_property_fields ON
350-
ds_property_fields."schemaId" = form_fields."schemaId"
350+
ds_property_fields."formDefId" = form_defs."id"
351351
AND ds_property_fields."path" = form_fields."path"
352352
LEFT OUTER JOIN ds_properties ON
353353
ds_properties."id" = ds_property_fields."dsPropertyId"

test/data/xml.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,7 @@ module.exports = {
584584
</meta>
585585
<name>Alice</name>
586586
<age>88</age>
587+
<hometown>Chicago</hometown>
587588
</data>`,
588589
two: `<data xmlns:jr="http://openrosa.org/javarosa" xmlns:entities="http://www.opendatakit.org/xforms" id="simpleEntity" version="1.0">
589590
<meta>
@@ -595,6 +596,7 @@ module.exports = {
595596
</meta>
596597
<name>Jane</name>
597598
<age>30</age>
599+
<hometown>Boston</hometown>
598600
</data>`,
599601
three: `<data xmlns:jr="http://openrosa.org/javarosa" xmlns:entities="http://www.opendatakit.org/xforms" id="simpleEntity" version="1.0">
600602
<meta>
@@ -606,6 +608,7 @@ module.exports = {
606608
</meta>
607609
<name>John</name>
608610
<age>40</age>
611+
<hometown>Toronto</hometown>
609612
</data>`,
610613
four: `<data xmlns:jr="http://openrosa.org/javarosa" xmlns:entities="http://www.opendatakit.org/xforms" id="simpleEntity" version="1.0">
611614
<meta>
@@ -617,6 +620,7 @@ module.exports = {
617620
</meta>
618621
<name>Robert</name>
619622
<age>18</age>
623+
<hometown>Seattle</hometown>
620624
</data>`
621625
},
622626
multiPropertyEntity: {

test/integration/api/datasets.js

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2154,6 +2154,95 @@ describe('datasets and entities', () => {
21542154
});
21552155
});
21562156

2157+
describe('form schemas and dataset properties', () => {
2158+
it('should populate entity properties based on correct form schema', testService(async (service, container) => {
2159+
const asAlice = await service.login('alice');
2160+
2161+
await asAlice.post('/v1/projects/1/forms?publish=true')
2162+
.set('Content-Type', 'application/xml')
2163+
.send(testData.forms.simpleEntity)
2164+
.expect(200);
2165+
2166+
// Submission to old (and only) version of form should have only age filled in
2167+
await asAlice.post('/v1/projects/1/forms/simpleEntity/submissions')
2168+
.send(testData.instances.simpleEntity.one)
2169+
.set('Content-Type', 'application/xml')
2170+
.expect(200);
2171+
2172+
await exhaust(container);
2173+
2174+
// Upload a new version of the form with saveto added to hometown
2175+
await asAlice.post('/v1/projects/1/forms/simpleEntity/draft')
2176+
.set('Content-Type', 'application/xml')
2177+
.send(testData.forms.simpleEntity
2178+
.replace('<bind nodeset="/data/hometown" type="string"/>', '<bind nodeset="/data/hometown" type="string" entities:saveto="hometown"/>'))
2179+
.expect(200);
2180+
2181+
await asAlice.post('/v1/projects/1/forms/simpleEntity/draft/publish?version=2.0')
2182+
.expect(200);
2183+
2184+
// Submission to old version of form should make entity with age filled in
2185+
await asAlice.post('/v1/projects/1/forms/simpleEntity/submissions')
2186+
.send(testData.instances.simpleEntity.two)
2187+
.set('Content-Type', 'application/xml')
2188+
.expect(200);
2189+
2190+
// Submission to new version of form should make entity with hometown filled in
2191+
await asAlice.post('/v1/projects/1/forms/simpleEntity/submissions')
2192+
.send(testData.instances.simpleEntity.three.replace('version="1.0"', 'version="2.0"'))
2193+
.set('Content-Type', 'application/xml')
2194+
.expect(200);
2195+
2196+
await exhaust(container);
2197+
2198+
// Upload a new version of the form with saveto removed from age
2199+
await asAlice.post('/v1/projects/1/forms/simpleEntity/draft')
2200+
.set('Content-Type', 'application/xml')
2201+
.send(testData.forms.simpleEntity
2202+
.replace('<bind nodeset="/data/age" type="int" entities:saveto="age"/>', '<bind nodeset="/data/age" type="int"/>')
2203+
.replace('<bind nodeset="/data/hometown" type="string"/>', '<bind nodeset="/data/hometown" type="string" entities:saveto="hometown"/>'))
2204+
.expect(200);
2205+
2206+
await asAlice.post('/v1/projects/1/forms/simpleEntity/draft/publish?version=3.0')
2207+
.expect(200);
2208+
2209+
await asAlice.post('/v1/projects/1/forms/simpleEntity/submissions')
2210+
.send(testData.instances.simpleEntity.four.replace('version="1.0"', 'version="3.0"'))
2211+
.set('Content-Type', 'application/xml')
2212+
.expect(200);
2213+
2214+
await exhaust(container);
2215+
2216+
// Submission 1 - should just have name and age
2217+
await asAlice.get('/v1/projects/1/datasets/people/entities/12345678-1234-4123-8234-123456789abc')
2218+
.expect(200)
2219+
.then(({ body: person }) => {
2220+
person.currentVersion.should.have.property('data').which.is.eql({ age: '88', first_name: 'Alice' });
2221+
});
2222+
2223+
// Submission 2 - should also just have name and age
2224+
await asAlice.get('/v1/projects/1/datasets/people/entities/12345678-1234-4123-8234-123456789aaa')
2225+
.expect(200)
2226+
.then(({ body: person }) => {
2227+
person.currentVersion.should.have.property('data').which.is.eql({ age: '30', first_name: 'Jane' });
2228+
});
2229+
2230+
// Submission 3 - should have name, age and hometown filled in
2231+
await asAlice.get('/v1/projects/1/datasets/people/entities/12345678-1234-4123-8234-123456789bbb')
2232+
.expect(200)
2233+
.then(({ body: person }) => {
2234+
person.currentVersion.should.have.property('data').which.is.eql({ age: '40', hometown: 'Toronto', first_name: 'John' });
2235+
});
2236+
2237+
// Submission 4 - should have name and hometown filled in, NO age
2238+
await asAlice.get('/v1/projects/1/datasets/people/entities/12345678-1234-4123-8234-123456789ccc')
2239+
.expect(200)
2240+
.then(({ body: person }) => {
2241+
person.currentVersion.should.have.property('data').which.is.eql({ first_name: 'Robert', hometown: 'Seattle' });
2242+
});
2243+
}));
2244+
});
2245+
21572246
describe('dataset and entities should have isolated lifecycle', () => {
21582247
it('should allow a form that has created an entity to be purged', testService(async (service, container) => {
21592248
const asAlice = await service.login('alice');

0 commit comments

Comments
 (0)