-
Notifications
You must be signed in to change notification settings - Fork 89
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
Add "Download GeoJSON" menu option #2138
Merged
rfontanarosa
merged 8 commits into
master
from
rfontanarosa/2013/export-job-data-allow-data-to-be-exported-as-geojson
Feb 10, 2025
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
167dffa
added export geojson endpoint
rfontanarosa bb43428
Merge branch 'master' into rfontanarosa/2013/export-job-data-allow-da…
rfontanarosa d6a79f6
Merge branch 'master' into rfontanarosa/2013/export-job-data-allow-da…
rfontanarosa ae96571
addressed two minor descriptions
rfontanarosa f7b088f
merge
rfontanarosa f3fc154
removed console.log
rfontanarosa 959bebb
changed "is accessible" condition
rfontanarosa ca6a44c
added a comment to explain how the geojson is formatted
rfontanarosa File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,201 @@ | ||
/** | ||
* Copyright 2024 The Ground Authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { | ||
createMockFirestore, | ||
stubAdminApi, | ||
} from '@ground/lib/dist/testing/firestore'; | ||
import { | ||
createGetRequestSpy, | ||
createResponseSpy, | ||
} from './testing/http-test-helpers'; | ||
import {DecodedIdToken} from 'firebase-admin/auth'; | ||
import HttpStatus from 'http-status-codes'; | ||
import {SURVEY_ORGANIZER_ROLE} from './common/auth'; | ||
import {resetDatastore} from './common/context'; | ||
import {Firestore} from 'firebase-admin/firestore'; | ||
import {exportGeojsonHandler} from './export-geojson'; | ||
import {registry} from '@ground/lib'; | ||
import {GroundProtos} from '@ground/proto'; | ||
|
||
import Pb = GroundProtos.ground.v1beta1; | ||
const sv = registry.getFieldIds(Pb.Survey); | ||
const j = registry.getFieldIds(Pb.Job); | ||
const t = registry.getFieldIds(Pb.Task); | ||
const l = registry.getFieldIds(Pb.LocationOfInterest); | ||
const pr = registry.getFieldIds(Pb.LocationOfInterest.Property); | ||
const p = registry.getFieldIds(Pb.Point); | ||
const c = registry.getFieldIds(Pb.Coordinates); | ||
const g = registry.getFieldIds(Pb.Geometry); | ||
const mq = registry.getFieldIds(Pb.Task.MultipleChoiceQuestion); | ||
const op = registry.getFieldIds(Pb.Task.MultipleChoiceQuestion.Option); | ||
|
||
describe('export()', () => { | ||
let mockFirestore: Firestore; | ||
const email = 'somebody@test.it'; | ||
const survey = { | ||
[sv.name]: 'Test survey', | ||
[sv.acl]: { | ||
[email]: SURVEY_ORGANIZER_ROLE, | ||
}, | ||
}; | ||
const job1 = { | ||
id: 'job123', | ||
[j.name]: 'Test job', | ||
[j.tasks]: [ | ||
{ | ||
[t.id]: 'task001', | ||
[t.prompt]: 'What is the meaning of life?', | ||
[t.textQuestion]: { | ||
['1' /* type */]: Pb.Task.TextQuestion.Type.SHORT_TEXT, | ||
}, | ||
}, | ||
{ | ||
[t.id]: 'task002', | ||
[t.prompt]: 'How much?', | ||
[t.numberQuestion]: { | ||
['1' /* type */]: Pb.Task.NumberQuestion.Type.FLOAT, | ||
}, | ||
}, | ||
{ | ||
[t.id]: 'task003', | ||
[t.prompt]: 'When?', | ||
[t.dateTimeQuestion]: { | ||
['1' /* type */]: Pb.Task.DateTimeQuestion.Type.BOTH_DATE_AND_TIME, | ||
}, | ||
}, | ||
{ | ||
[t.id]: 'task004', | ||
[t.prompt]: 'Which ones?', | ||
[t.multipleChoiceQuestion]: { | ||
[mq.type]: Pb.Task.MultipleChoiceQuestion.Type.SELECT_MULTIPLE, | ||
[mq.options]: [ | ||
{ | ||
[op.id]: 'aaa', | ||
[op.index]: 1, | ||
[op.label]: 'AAA', | ||
}, | ||
{ | ||
[op.id]: 'bbb', | ||
[op.index]: 2, | ||
[op.label]: 'BBB', | ||
}, | ||
], | ||
[mq.hasOtherOption]: true, | ||
}, | ||
}, | ||
{ | ||
[t.id]: 'task005', | ||
[t.prompt]: 'Where are you now?', | ||
[t.captureLocation]: { | ||
['1' /* min_accuracy_meters */]: 999999, | ||
}, | ||
}, | ||
{ | ||
[t.id]: 'task006', | ||
[t.prompt]: 'Take a photo', | ||
[t.takePhoto]: { | ||
['1' /* min_heading_degrees */]: 0, | ||
['2' /* max_heading_degrees */]: 360, | ||
}, | ||
}, | ||
], | ||
}; | ||
const pointLoi1 = { | ||
id: 'loi100', | ||
[l.id]: 'loi100', | ||
[l.jobId]: job1.id, | ||
[l.customTag]: 'POINT_001', | ||
[l.geometry]: { | ||
[g.point]: {[p.coordinates]: {[c.latitude]: 10.1, [c.longitude]: 125.6}}, | ||
}, | ||
[l.submissionCount]: 0, | ||
[l.source]: Pb.LocationOfInterest.Source.IMPORTED, | ||
[l.properties]: { | ||
name: {[pr.stringValue]: 'Dinagat Islands'}, | ||
area: {[pr.numericValue]: 3.08}, | ||
}, | ||
}; | ||
const testCases = [ | ||
{ | ||
desc: 'export point', | ||
jobId: job1.id, | ||
survey: survey, | ||
jobs: [job1], | ||
lois: [pointLoi1], | ||
expectedFilename: 'test-job.geojson', | ||
expectedGeojson: { | ||
type: 'FeatureCollection', | ||
features: [ | ||
{ | ||
type: 'Feature', | ||
properties: {name: 'Dinagat Islands', area: 3.08}, | ||
geometry: {type: 'Point', coordinates: [125.6, 10.1]}, | ||
}, | ||
{ | ||
type: 'Feature', | ||
properties: null, | ||
geometry: {type: 'Point', coordinates: [8.3, 47.05]}, | ||
}, | ||
], | ||
}, | ||
}, | ||
]; | ||
|
||
beforeEach(() => { | ||
mockFirestore = createMockFirestore(); | ||
stubAdminApi(mockFirestore); | ||
}); | ||
|
||
afterEach(() => { | ||
resetDatastore(); | ||
}); | ||
|
||
testCases.forEach(({desc, jobId, survey, jobs, lois, expectedFilename}) => | ||
it(desc, async () => { | ||
// Populate database. | ||
mockFirestore.doc(`surveys/${survey.id}`).set(survey); | ||
jobs?.forEach(({id, ...job}) => | ||
mockFirestore.doc(`surveys/${survey.id}/jobs/${id}`).set(job) | ||
); | ||
lois?.forEach(({id, ...loi}) => | ||
mockFirestore.doc(`surveys/${survey.id}/lois/${id}`).set(loi) | ||
); | ||
|
||
// Build mock request and response. | ||
const req = await createGetRequestSpy({ | ||
url: '/exportGeojson', | ||
query: { | ||
survey: survey.id, | ||
job: jobId, | ||
}, | ||
}); | ||
const chunks: string[] = []; | ||
const res = createResponseSpy(chunks); | ||
|
||
// Run export handler. | ||
await exportGeojsonHandler(req, res, {email} as DecodedIdToken); | ||
|
||
// Check post-conditions. | ||
expect(res.status).toHaveBeenCalledOnceWith(HttpStatus.OK); | ||
expect(res.type).toHaveBeenCalledOnceWith('application/json'); | ||
expect(res.setHeader).toHaveBeenCalledOnceWith( | ||
'Content-Disposition', | ||
`attachment; filename=${expectedFilename}` | ||
); | ||
}) | ||
); | ||
}); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are a fair few things from the function that are not tested here, especially error cases. I understand not testing everything, but anything that's especially important that you think we should test?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After investigating the export-csv test logic, I found that the jsonstream library uses a queue for writing to the stream and I didn't find a way to capture this.
Maybe we can change the mocking strategy, so instead of mocking the entire response, I'll likely get better results by focusing my tests on the transformation of he LOI data into GeoJSON features.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wdyt @nwkotto ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, that makes sense, it would be nice to check that the transformation from LOI to GeoJSON is working correctly, instead of the focus on the overall request. But I don't want to hold this up, either, if you've tested this manually -- could split out that functionality and test it in a follow-up PR.