Bump test coverage for Exif node.

Remove dependancy on actual jpg files.
This commit is contained in:
dceejay 2015-05-10 19:27:35 +01:00
parent 87c93dc925
commit 36ac3817a7
5 changed files with 196 additions and 16 deletions

View File

@ -16,7 +16,7 @@
var should = require("should");
var sinon = require('sinon');
var fs = require("fs");
//var fs = require("fs");
var helper = require('../../../test/helper.js');
var exifNode = require('../../../utility/exif/94-exif.js');
@ -28,8 +28,9 @@ describe('exif node', function() {
});
afterEach(function(done) {
helper.unload();
helper.stopServer(done);
helper.unload().then(function() {
helper.stopServer(done);
});
});
it('extracts location data from Exif data of JPEG', function(done) {
@ -37,11 +38,25 @@ describe('exif node', function() {
var ExifImage = exif.ExifImage;
// the jpg file is a single black dot but it was originally a photo taken at IBM Hursley
//console.log(process.cwd());
var data = fs.readFileSync("test/utility/exif/exif_test_image.jpg", null); // extracting genuine exif data to be fed back as the result of the stubbed ExifImage constructor
//var data = fs.readFileSync("test/utility/exif/exif_test_image.jpg", null); // extracting genuine exif data to be fed back as the result of the stubbed ExifImage constructor
//var data = fs.readFileSync("exif_test_image.jpg", null); // extracting genuine exif data to be fed back as the result of the stubbed ExifImage constructor
var flow = [{id:"exifNode1", type:"exif", wires:[["helperNode1"]]},
{id:"helperNode1", type:"helper"}];
var gpsmsg = { gps: { GPSLatitudeRef: 'N',
GPSLatitude: [ 50, 57, 22.4697 ],
GPSLongitudeRef: 'W',
GPSLongitude: [ 1, 22, 1.2467 ],
GPSAltitudeRef: 0,
GPSAltitude: 50,
GPSTimeStamp: [ 7, 32, 2 ],
GPSImgDirectionRef: 'M',
GPSImgDirection: 267,
GPSProcessingMethod: 'ASCII\u0000\u0000\u0000FUSED',
GPSDateStamp: '2014:06:10' }
};
var spy = sinon.stub(exif, 'ExifImage', function(arg1,arg2) { arg2(null,gpsmsg); });
helper.load(exifNode, flow, function() {
var exifNode1 = helper.getNode("exifNode1");
var helperNode1 = helper.getNode("helperNode1");
@ -49,10 +64,48 @@ describe('exif node', function() {
helperNode1.on("input", function(msg) {
msg.location.lat.should.equal(50.95624); // this data is stored in the jpg file
msg.location.lon.should.equal(-1.36701);
exif.ExifImage.restore();
done();
});
exifNode1.receive({payload:data});
exifNode1.receive({payload:new Buffer("hello")});
});
});
it('extracts location data in Southern and Eastern hemispheres', function(done) {
var exif = require('exif');
var ExifImage = exif.ExifImage;
// the jpg file is a single black dot but it was originally a photo taken at IBM Hursley
//console.log(process.cwd());
//var data = fs.readFileSync("test/utility/exif/exif_test_image.jpg", null); // extracting genuine exif data to be fed back as the result of the stubbed ExifImage constructor
//var data = fs.readFileSync("exif_test_image.jpg", null); // extracting genuine exif data to be fed back as the result of the stubbed ExifImage constructor
var flow = [{id:"exifNode1", type:"exif", wires:[["helperNode1"]]},
{id:"helperNode1", type:"helper"}];
var gpsmsg = { gps: { GPSLatitudeRef: 'S',
GPSLatitude: [ 50, 57, 22.4697 ],
GPSLongitudeRef: 'E',
GPSLongitude: [ 1, 22, 1.2467 ],
GPSTimeStamp: [ 7, 32, 2 ],
GPSImgDirectionRef: 'M',
GPSImgDirection: 267,
GPSProcessingMethod: 'ASCII\u0000\u0000\u0000FUSED',
GPSDateStamp: '2014:06:10' }
};
var spy = sinon.stub(exif, 'ExifImage', function(arg1,arg2) { arg2(null,gpsmsg); });
helper.load(exifNode, flow, function() {
var exifNode1 = helper.getNode("exifNode1");
var helperNode1 = helper.getNode("helperNode1");
helperNode1.on("input", function(msg) {
msg.location.lat.should.equal(-50.95624); // this data is stored in the jpg file
msg.location.lon.should.equal(1.36701);
exif.ExifImage.restore();
done();
});
exifNode1.receive({payload:new Buffer("hello")});
});
});
@ -61,11 +114,17 @@ describe('exif node', function() {
var ExifImage = exif.ExifImage;
// the jpg file is a single black dot but it was originally a photo taken at IBM Hursley
//console.log(process.cwd());
var data = fs.readFileSync("test/utility/exif/exif_test_image2.jpg", null); // extracting genuine exif data to be fed back as the result of the stubbed ExifImage constructor
//var data = fs.readFileSync("test/utility/exif/exif_test_image2.jpg", null); // extracting genuine exif data to be fed back as the result of the stubbed ExifImage constructor
//var data = fs.readFileSync("exif_test_image2.jpg", null); // extracting genuine exif data to be fed back as the result of the stubbed ExifImage constructor
var flow = [{id:"exifNode1", type:"exif", wires:[["helperNode1"]]},
{id:"helperNode1", type:"helper"}];
var gpsmsg = {};
var spy = sinon.stub(exif, 'ExifImage',
function(arg1,arg2){
arg2(null,gpsmsg);
});
helper.load(exifNode, flow, function() {
var exifNode1 = helper.getNode("exifNode1");
var helperNode1 = helper.getNode("helperNode1");
@ -77,17 +136,18 @@ describe('exif node', function() {
logEvents.should.have.length(1);
logEvents[0][0].should.have.a.property('msg');
logEvents[0][0].msg.toString().should.startWith("The incoming image did not contain Exif GPS");
exif.ExifImage.restore();
done();
},150);
exifNode1.receive({payload:data});
exifNode1.receive({payload:new Buffer("hello")});
});
});
it('should report if not a jpeg', function(done) {
var exif = require('exif');
var ExifImage = exif.ExifImage;
// the jpg file is a single black dot but it was originally a photo taken at IBM Hursley
// this time just use a buffer that isn't an jpeg image
var data = new Buffer("hello");
var eD;
var flow = [{id:"exifNode1", type:"exif", wires:[["helperNode1"]]},
@ -162,4 +222,125 @@ describe('exif node', function() {
exifNode1.receive({topic:data});
});
});
it('should report if bad latitude', function(done) {
var exif = require('exif');
var ExifImage = exif.ExifImage;
var data = new Buffer("hello");
var eD;
var flow = [{id:"exifNode1", type:"exif", wires:[["helperNode1"]]},
{id:"helperNode1", type:"helper"}];
var gpsmsg = { gps: { GPSLatitudeRef: 'N',
GPSLatitude: [ 50, 57 ],
GPSLongitudeRef: 'W',
GPSLongitude: [ 1, 22, 1.2467 ],
GPSAltitudeRef: 0,
GPSAltitude: 50,
GPSTimeStamp: [ 7, 32, 2 ] }
};
var spy = sinon.stub(exif, 'ExifImage',
function(arg1,arg2){
arg2(null,gpsmsg);
});
helper.load(exifNode, flow, function() {
var exifNode1 = helper.getNode("exifNode1");
var helperNode1 = helper.getNode("helperNode1");
setTimeout(function() {
var logEvents = helper.log().args.filter(function(evt) {
return evt[0].type == "exif";
});
logEvents.should.have.length(1);
logEvents[0][0].should.have.a.property('msg');
logEvents[0][0].msg.toString().should.startWith("Invalid latitude data,");
exif.ExifImage.restore();
done();
},150);
exifNode1.receive({payload:data});
});
});
it('should report if bad longitude', function(done) {
var exif = require('exif');
var ExifImage = exif.ExifImage;
var data = new Buffer("hello");
var eD;
var flow = [{id:"exifNode1", type:"exif", wires:[["helperNode1"]]},
{id:"helperNode1", type:"helper"}];
var gpsmsg = { gps: { GPSLatitudeRef: 'N',
GPSLatitude: [ 50, 57, 1.3 ],
GPSLongitudeRef: 'W',
GPSLongitude: [ 1, 22 ],
GPSAltitudeRef: 0,
GPSAltitude: 50,
GPSTimeStamp: [ 7, 32, 2 ] }
};
var spy = sinon.stub(exif, 'ExifImage',
function(arg1,arg2){
arg2(null,gpsmsg);
});
helper.load(exifNode, flow, function() {
var exifNode1 = helper.getNode("exifNode1");
var helperNode1 = helper.getNode("helperNode1");
setTimeout(function() {
var logEvents = helper.log().args.filter(function(evt) {
return evt[0].type == "exif";
});
logEvents.should.have.length(1);
logEvents[0][0].should.have.a.property('msg');
logEvents[0][0].msg.toString().should.startWith("Invalid longitude data,");
exif.ExifImage.restore();
done();
},150);
exifNode1.receive({payload:data});
});
});
it('should report if unsure about location', function(done) {
var exif = require('exif');
var ExifImage = exif.ExifImage;
var data = new Buffer("hello");
var eD;
var flow = [{id:"exifNode1", type:"exif", wires:[["helperNode1"]]},
{id:"helperNode1", type:"helper"}];
var gpsmsg = { gps: { GPSLatitudeRef: 'N',
GPSLatitude: [ 50, 57, 1.3 ],
GPSAltitudeRef: 0,
GPSAltitude: 50,
GPSTimeStamp: [ 7, 32, 2 ] }
};
var spy = sinon.stub(exif, 'ExifImage',
function(arg1,arg2){
arg2(null,gpsmsg);
});
helper.load(exifNode, flow, function() {
var exifNode1 = helper.getNode("exifNode1");
var helperNode1 = helper.getNode("helperNode1");
setTimeout(function() {
var logEvents = helper.log().args.filter(function(evt) {
return evt[0].type == "exif";
});
logEvents.should.have.length(1);
logEvents[0][0].should.have.a.property('msg');
logEvents[0][0].msg.toString().should.startWith("The location of this image cannot be determined safely");
exif.ExifImage.restore();
done();
},150);
exifNode1.receive({payload:data});
});
});
});

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.7 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 285 B

View File

@ -37,16 +37,15 @@ module.exports = function(RED) {
*/
function addMsgLocationDataFromExifGPSData(msg) {
var gpsData = msg.exif.gps; // declaring variable purely to make checks more readable
if(gpsData.GPSAltitude) {
if(!msg.location) {
msg.location = {};
}
if (gpsData.GPSAltitude) {
/* istanbul ignore else */
if (!msg.location) { msg.location = {}; }
msg.location.alt = gpsData.GPSAltitude;
}
if(gpsData.GPSLatitudeRef && gpsData.GPSLatitude && gpsData.GPSLongitudeRef && gpsData.GPSLongitude) { // location can be determined, OK
if (gpsData.GPSLatitudeRef && gpsData.GPSLatitude && gpsData.GPSLongitudeRef && gpsData.GPSLongitude) { // location can be determined, OK
// The data provided in Exif is in degrees, minutes, seconds, this is to be converted into a single floating point degree
if(gpsData.GPSLatitude.length === 3) { // OK to convert latitude
if(gpsData.GPSLongitude.length === 3) { // OK to convert longitude
if (gpsData.GPSLatitude.length === 3) { // OK to convert latitude
if (gpsData.GPSLongitude.length === 3) { // OK to convert longitude
var latitude = convertDegreesMinutesSecondsToDecimals(gpsData.GPSLatitude[0], gpsData.GPSLatitude[1], gpsData.GPSLatitude[2]);
latitude = Math.round(latitude * 100000)/100000; // 5dp is approx 1m resolution...

View File

@ -1,6 +1,6 @@
{
"name" : "node-red-node-exif",
"version" : "0.0.2",
"version" : "0.0.3",
"description" : "A Node-RED node that extracts Exif information from JPEG image buffers.",
"dependencies" : {
"exif": "0.4.0"