22
33const assert = require ( 'assert' ) ;
44const watson = require ( '../../index' ) ;
5+ const sinon = require ( 'sinon' ) ;
56const nock = require ( 'nock' ) ;
67const fs = require ( 'fs' ) ;
78const omit = require ( 'object.omit' ) ;
@@ -31,6 +32,7 @@ describe('visual_recognition', function() {
3132 } ;
3233
3334 const classify_path = '/v3/classify?' + api_key_qs + '&' + version_qs ;
35+ const detect_faces_path = '/v3/detect_faces?' + api_key_qs + '&' + version_qs ;
3436 const classifiers_path = '/v3/classifiers?' + api_key_qs + '&' + version_qs ;
3537 const foo_classifiers_path = '/v3/classifiers/foo?' + api_key_qs + '&' + version_qs ;
3638 const mock_classify = {
@@ -228,7 +230,7 @@ describe('visual_recognition', function() {
228230 } ) ;
229231
230232 it ( 'should generate a valid payload with streams' , function ( ) {
231- const params = { images_file : fake_file } ;
233+ const params = { image_file : fake_file } ;
232234 const req = visual_recognition . classify ( params , noop ) ;
233235 assert . equal ( req . uri . href , service . url + classify_path ) ;
234236 assert . equal ( req . method , 'POST' ) ;
@@ -243,7 +245,7 @@ describe('visual_recognition', function() {
243245 } ) ;
244246
245247 it ( 'should generate a valid paylod with buffers' , function ( ) {
246- const params = { images_file : fake_buffer } ;
248+ const params = { images_file : fake_buffer , parameters : { pwmers : [ 'me' , 'IBM' ] } } ;
247249 const req = visual_recognition . classify ( params , noop ) ;
248250 assert . equal ( req . uri . href , service . url + classify_path ) ;
249251 assert . equal ( req . method , 'POST' ) ;
@@ -281,7 +283,69 @@ describe('visual_recognition', function() {
281283 const req = visual_recognition . classify ( params , noop ) ;
282284 assert . equal ( req . method , 'POST' ) ;
283285 assert . equal ( req . uri . pathname , URL . parse ( service . url + classify_path ) . pathname ) ;
284- // classifier_ids, owners, url and threshold are now encapsulated
286+ // classifier_ids, owners, url and threshold are now encapsulated
287+ // in params.parameters
288+ // and are uploaded as a formData object
289+ assert ( req . formData ) ;
290+ assert ( req . formData . parameters ) ;
291+ const parameters = JSON . parse ( req . formData . parameters ) ;
292+ assert . deepEqual ( parameters . classifier_ids , params . classifier_ids ) ;
293+ } ) ;
294+ } ) ;
295+
296+ describe ( 'detectFaces()' , function ( ) {
297+ it ( 'should check no parameters provided' , function ( ) {
298+ visual_recognition . detectFaces ( { } , missingParameter ) ;
299+ visual_recognition . detectFaces ( null , missingParameter ) ;
300+ visual_recognition . detectFaces ( undefined , missingParameter ) ;
301+ visual_recognition . detectFaces ( { images_file : '' } , missingParameter ) ;
302+ } ) ;
303+
304+ it ( 'should generate a valid payload with streams' , function ( ) {
305+ const params = { image_file : fake_file } ;
306+ const req = visual_recognition . detectFaces ( params , noop ) ;
307+ assert . equal ( req . uri . href , service . url + detect_faces_path ) ;
308+ assert . equal ( req . method , 'POST' ) ;
309+ // we always convert files to request-style objects
310+ assert . equal ( req . formData . images_file . value . path , fake_file . path ) ;
311+ assert . equal ( req . formData . images_file . value , params . images_file ) ;
312+ } ) ;
313+
314+ it ( 'should generate a valid paylod with buffers' , function ( ) {
315+ const params = { images_file : fake_buffer , parameters : { pwmers : [ 'me' , 'IBM' ] } } ;
316+ const req = visual_recognition . detectFaces ( params , noop ) ;
317+ assert . equal ( req . uri . href , service . url + detect_faces_path ) ;
318+ assert . equal ( req . method , 'POST' ) ;
319+ // we always convert files to request-style objects
320+ assert . equal ( req . formData . images_file . options . filename , null ) ;
321+ assert . equal ( req . formData . images_file . value , params . images_file ) ;
322+ } ) ;
323+
324+ it ( 'should generate a valid payload with an image file' , function ( ) {
325+ const params = {
326+ images_file : fake_file ,
327+ classifier_ids : [ 'foo' , 'bar' ]
328+ } ;
329+
330+ const req = visual_recognition . detectFaces ( params , noop ) ;
331+ assert . equal ( req . uri . href , service . url + detect_faces_path ) ;
332+ assert . equal ( req . method , 'POST' ) ;
333+ // we always convert files to request-style objects
334+ assert . equal ( req . formData . images_file . value . path , fake_file . path ) ;
335+ const uploadedParameters = JSON . parse ( req . formData . parameters ) ;
336+ assert . deepEqual ( uploadedParameters . classifier_ids , params . classifier_ids ) ;
337+ } ) ;
338+
339+ it ( 'should generate a valid payload with a url' , function ( ) {
340+ const params = {
341+ url : 'https://watson-test-resources.mybluemix.net/resources/obama.jpg' ,
342+ classifier_ids : [ 'foo' , 'bar' ]
343+ } ;
344+
345+ const req = visual_recognition . detectFaces ( params , noop ) ;
346+ assert . equal ( req . method , 'POST' ) ;
347+ assert . equal ( req . uri . pathname , URL . parse ( service . url + detect_faces_path ) . pathname ) ;
348+ // classifier_ids, owners, url and threshold are now encapsulated
285349 // in params.parameters
286350 // and are uploaded as a formData object
287351 assert ( req . formData ) ;
@@ -331,6 +395,43 @@ describe('visual_recognition', function() {
331395 done ( ) ;
332396 } ) ;
333397 } ) ;
398+ describe ( 'retrainClassifier()' , function ( ) {
399+ it ( 'should call updateClassifier()' , function ( ) {
400+ visual_recognition . retrainClassifier ( { } , missingParameter ) ;
401+ } )
402+ } ) ;
403+
404+ describe ( 'updateClassifier()' , function ( ) {
405+ it ( 'should check no/insufficient parameters provided' , function ( ) {
406+ visual_recognition . updateClassifier ( { } , missingParameter ) ;
407+ visual_recognition . updateClassifier ( null , missingParameter ) ;
408+ visual_recognition . updateClassifier ( undefined , missingParameter ) ;
409+ visual_recognition . updateClassifier ( { positive_examples : '' , name : 'foo' } , missingParameter ) ;
410+ visual_recognition . updateClassifier ( { foo_positive_examples : '' , name : 'foo' } , missingParameter ) ;
411+ visual_recognition . updateClassifier ( { positive_examples : '' , negative_examples : '' , name : 'foo' } , missingParameter ) ; // positive examples must include a tag
412+ visual_recognition . updateClassifier ( { foo_positive_examples : '' , negative_examples : '' } , missingParameter ) ; // missing name
413+ } ) ;
414+
415+ it ( 'should generate a valid payload with streams' , function ( done ) {
416+ const params = {
417+ foo_positive_examples : fake_file ,
418+ negative_examples : fake_file ,
419+ classifier_id : 'foo'
420+ } ;
421+
422+ // todo: make this fully async
423+ const req = visual_recognition . updateClassifier ( params , function ( err ) {
424+ if ( err ) {
425+ done ( err ) ;
426+ }
427+ } ) ;
428+ assert . equal ( req . uri . href , service . url + foo_classifiers_path ) ;
429+ assert . equal ( req . method , 'POST' ) ;
430+ assert . ok ( req . formData . foo_positive_examples ) ;
431+ assert . ok ( req . formData . negative_examples ) ;
432+ done ( ) ;
433+ } ) ;
434+ } ) ;
334435
335436 describe ( 'deleteClassifier()' , function ( ) {
336437 it ( 'should check no parameters provided' , function ( ) {
@@ -412,4 +513,38 @@ describe('visual_recognition', function() {
412513 ) ;
413514 } ) ;
414515 } ) ;
516+
517+ describe ( 'Print a warning for deprecated methods()' , function ( ) {
518+ let spy ;
519+ beforeEach ( function ( ) {
520+ spy = sinon . stub ( console , 'warn' ) ;
521+ } ) ;
522+ afterEach ( function ( ) {
523+ spy . restore ( ) ;
524+ } ) ;
525+ after ( function ( ) {
526+ spy . restore ( ) ;
527+ } ) ;
528+
529+ const deprecatedMethods = [
530+ 'recognizeText' ,
531+ 'createCollection' ,
532+ 'getCollection' ,
533+ 'listCollections' ,
534+ 'deleteCollection' ,
535+ 'addImage' ,
536+ 'listImages' ,
537+ 'getImage' ,
538+ 'deleteImage' ,
539+ 'setImageData' ,
540+ 'getImageData' ,
541+ 'deleteImageData' ,
542+ 'findSimilar'
543+ ] . forEach ( function ( method ) {
544+ it ( `${ method } should print a warning message` , function ( ) {
545+ visual_recognition [ method ] ( { } , noop ) ;
546+ assert ( spy . calledOnce ) ;
547+ } ) ;
548+ } ) ;
549+ } ) ;
415550} ) ;
0 commit comments