Add stations cloud functions and tests
This commit is contained in:
@ -18,7 +18,7 @@ exports.getLiveTrainData = functions.https.onRequest((request, response) => {
|
||||
// fetch the "liveTrainData" collection
|
||||
admin.firestore().collection('liveTrainData').get().then((snapshot) => {
|
||||
if (snapshot.empty) {
|
||||
response.send({data: "Error fetching data from the database"});
|
||||
response.send({data: "Error fetching live train data from the database"});
|
||||
return;
|
||||
}
|
||||
// iterate through each of the collection's documents
|
||||
@ -30,6 +30,70 @@ exports.getLiveTrainData = functions.https.onRequest((request, response) => {
|
||||
});
|
||||
})
|
||||
|
||||
// function to fetch station data from the Firestore database
|
||||
exports.getStationData = functions.https.onRequest((request, response) => {
|
||||
response.set('Access-Control-Allow-Origin', '*');
|
||||
response.set('Access-Control-Allow-Credentials', 'true');
|
||||
let jsonData = [];
|
||||
|
||||
cors(request, response, () => {
|
||||
// fetch the "stations" collection
|
||||
admin.firestore().collection('stations').get().then((snapshot) => {
|
||||
if (snapshot.empty) {
|
||||
response.send({data: "Error fetching station data from the database"})
|
||||
return;
|
||||
}
|
||||
// iterate through each of the collection's documents
|
||||
snapshot.forEach(doc => {
|
||||
jsonData.push(doc.data());
|
||||
});
|
||||
response.json({data: jsonData});
|
||||
})
|
||||
});
|
||||
})
|
||||
|
||||
// function to populate the Firestore database with station data from Irish Rail
|
||||
exports.postStationData = functions.https.onRequest((request, response) => {
|
||||
response.set('Access-Control-Allow-Origin', '*');
|
||||
response.set('Access-Control-Allow-Credentials', 'true');
|
||||
|
||||
cors(request, response, () => {
|
||||
axios.get('http://api.irishrail.ie/realtime/realtime.asmx/getAllStationsXML')
|
||||
.then((res) => {
|
||||
// XML to JSON
|
||||
parseString(res.data, function(err, result) {
|
||||
let jsonStr = JSON.stringify(result);
|
||||
let jsonObj = JSON.parse(jsonStr);
|
||||
let jsonData = jsonObj.ArrayOfObjStation.objStation;
|
||||
|
||||
// batch delete all of the "stations" collection's documents
|
||||
var db = admin.firestore();
|
||||
admin.firestore().collection('stations').get().then((snapshot) => {
|
||||
var batchDelete = db.batch();
|
||||
snapshot.forEach(doc => {
|
||||
batchDelete.delete(doc.ref);
|
||||
});
|
||||
|
||||
batchDelete.commit().then(function() {
|
||||
// batch write all station JSON objects to the "stations" collection
|
||||
var batchWrite = db.batch();
|
||||
|
||||
jsonData.forEach((doc) => {
|
||||
// set the station's ID as the document ID
|
||||
var docID = db.collection('stations').doc(doc["StationCode"][0]);
|
||||
batchWrite.set(docID, doc);
|
||||
});
|
||||
|
||||
batchWrite.commit().then(function () {
|
||||
response.send({data: "Successfully fetched and uploaded station data from Irish Rail"});
|
||||
});
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
// function to populate the Firestore database with live train data from Irish Rail
|
||||
exports.postLiveTrainData = functions.https.onRequest((request, response) => {
|
||||
response.set('Access-Control-Allow-Origin', '*');
|
||||
@ -66,7 +130,7 @@ exports.postLiveTrainData = functions.https.onRequest((request, response) => {
|
||||
});
|
||||
|
||||
batchWrite.commit().then(function () {
|
||||
response.send({data: "Successfully fetched and uploaded data from Irish Rail"});
|
||||
response.send({data: "Successfully fetched and uploaded live train data from Irish Rail"});
|
||||
});
|
||||
})
|
||||
})
|
||||
|
@ -25,5 +25,25 @@ describe('Firebase cloud function tests', function() {
|
||||
const result = await chai.request('https://us-central1-irishrailtracker.cloudfunctions.net')
|
||||
.get('/postLiveTrainData')
|
||||
expect(result.statusCode).to.equal(200);
|
||||
}),
|
||||
|
||||
this.timeout(100000);
|
||||
it('test getting station data', async() => {
|
||||
const result = await chai.request('https://us-central1-irishrailtracker.cloudfunctions.net')
|
||||
.get('/getStationData')
|
||||
expect(result.statusCode).to.equal(200);
|
||||
expect(result.body.data).to.be.an('Array');
|
||||
expect(result.body.data[0]).haveOwnProperty('StationDesc');
|
||||
expect(result.body.data[0]).haveOwnProperty('StationLatitude');
|
||||
expect(result.body.data[0]).haveOwnProperty('StationLongitude');
|
||||
expect(result.body.data[0]).haveOwnProperty('StationCode');
|
||||
expect(result.body.data[0]).haveOwnProperty('StationId');
|
||||
})
|
||||
|
||||
this.timeout(100000);
|
||||
it('Test updating the database with live station data', async() => {
|
||||
const result = await chai.request('https://us-central1-irishrailtracker.cloudfunctions.net')
|
||||
.get('/postStationData')
|
||||
expect(result.statusCode).to.equal(200);
|
||||
})
|
||||
})
|
Reference in New Issue
Block a user