@@ -625,7 +625,7 @@ describe('ServerSidePurge', () => {
625625 } ) ;
626626 } ) ;
627627
628- it ( 'should keep requesting docs_by_replication_key and select only relevant records to purge ' , ( ) => {
628+ it ( 'should request docs_by_replication_key once per chunk of subject ids when pages are not full ' , ( ) => {
629629 sinon . stub ( db , 'queryMedic' ) ;
630630 const contacts = Array . from ( { length : 1500 } ) . map ( ( _ , idx ) => ( {
631631 id : idx ,
@@ -636,26 +636,20 @@ describe('ServerSidePurge', () => {
636636 return Promise . resolve ( { rows : contacts . slice ( startkey_docid , limit + startkey_docid ) } ) ;
637637 } ) ;
638638
639- // 1 in 5000 records is relevant
640- const fiveK = 5000 ;
641- const reports = Array . from ( { length : 430000 } ) . map ( ( _ , idx ) => ( {
639+ const reports = Array . from ( { length : 10600 } ) . map ( ( _ , idx ) => ( {
642640 id : `id${ idx } ` ,
643641 fields : {
644642 key : `key${ idx } ` ,
645- subject : 'irrelevant' ,
643+ subject : `key ${ idx } ` ,
646644 type : DOC_TYPES . DATA_RECORD ,
647- needs_signoff : idx % fiveK !== 0
648645 } ,
649646 } ) ) ;
650647
651648 sinon . stub ( request , 'post' ) ;
652- request . post . onCall ( 0 ) . resolves ( { hits : reports . slice ( 0 , 200000 ) , bookmark : '1' } ) ;
653- request . post . onCall ( 1 ) . resolves ( { hits : reports . slice ( 200000 , 220000 ) } ) ;
654-
655- request . post . onCall ( 2 ) . resolves ( { hits : reports . slice ( 220000 , 420000 ) , bookmark : '2' } ) ;
656- request . post . onCall ( 3 ) . resolves ( { hits : reports . slice ( 420000 , 430000 ) } ) ;
649+ request . post . onCall ( 0 ) . resolves ( { hits : reports . slice ( 0 , 10000 ) , bookmark : '1' } ) ;
650+ request . post . onCall ( 1 ) . resolves ( { hits : reports . slice ( 10000 , 10600 ) , bookmark : '2' } ) ;
657651
658- request . post . onCall ( 4 ) . resolves ( { hits : reports . slice ( 0 , 20000 ) } ) ;
652+ request . post . onCall ( 2 ) . resolves ( { hits : reports . slice ( 0 , 200 ) , bookmark : '3' } ) ;
659653
660654 sinon . stub ( db . medic , 'allDocs' )
661655 . callsFake ( ( { keys } ) => Promise . resolve ( { rows : keys . map ( ( key ) => ( { doc : { _id : key } } ) ) } ) ) ;
@@ -665,45 +659,21 @@ describe('ServerSidePurge', () => {
665659
666660 return service . __get__ ( 'purgeContacts' ) ( roles , purgeFn ) . then ( ( ) => {
667661 chai . expect ( db . queryMedic . callCount ) . to . equal ( 3 ) ;
668- chai . expect ( request . post . callCount ) . to . equal ( 5 ) ;
669-
670- chai . expect ( request . post . args [ 0 ] ) . excludingEvery ( 'q' ) . to . deep . equal ( [ {
671- body : { limit : 200000 } ,
672- uri : `${ environment . couchUrl } /_design/medic/_nouveau/docs_by_replication_key`
673- } ] ) ;
674- chai . expect ( request . post . args [ 1 ] ) . excludingEvery ( 'q' ) . to . deep . equal ( [ {
675- body : {
676- limit : 200000 ,
677- bookmark : '1' ,
678- } ,
679- uri : `${ environment . couchUrl } /_design/medic/_nouveau/docs_by_replication_key`
680- } ] ) ;
681-
682- chai . expect ( request . post . args [ 2 ] ) . excludingEvery ( 'q' ) . to . deep . equal ( [ {
683- body : { limit : 200000 , } ,
684- uri : `${ environment . couchUrl } /_design/medic/_nouveau/docs_by_replication_key`
685- } ] ) ;
686- chai . expect ( request . post . args [ 3 ] ) . excludingEvery ( 'q' ) . to . deep . equal ( [ {
687- body : { limit : 200000 , bookmark : '2' } ,
688- uri : `${ environment . couchUrl } /_design/medic/_nouveau/docs_by_replication_key`
689- } ] ) ;
690-
691- chai . expect ( request . post . args [ 4 ] ) . excludingEvery ( 'q' ) . to . deep . equal ( [ {
692- body : { limit : 200000 , } ,
693- uri : `${ environment . couchUrl } /_design/medic/_nouveau/docs_by_replication_key`
694- } ] ) ;
662+ chai . expect ( request . post . callCount ) . to . equal ( 3 ) ;
663+ request . post . args . forEach ( args => {
664+ chai . expect ( args ) . excludingEvery ( 'q' ) . to . deep . equal ( [ {
665+ body : { limit : 20000 } ,
666+ uri : `${ environment . couchUrl } /_design/medic/_nouveau/docs_by_replication_key`
667+ } ] ) ;
668+ } ) ;
695669
696670 chai . expect ( db . medic . allDocs . callCount ) . to . equal ( 2 ) ;
697671
698- const getRelevantRecordsIds = ( length ) => Array
699- . from ( { length : length / fiveK } )
700- . map ( ( _ , idx ) => `id${ idx * fiveK } ` ) ;
701-
702672 chai . expect ( db . medic . allDocs . args [ 0 ] ) . to . deep . equal ( [ {
703- keys : getRelevantRecordsIds ( reports . length ) , include_docs : true ,
673+ keys : reports . map ( report => report . id ) , include_docs : true ,
704674 } ] ) ;
705675 chai . expect ( db . medic . allDocs . args [ 1 ] ) . to . deep . equal ( [ {
706- keys : getRelevantRecordsIds ( 20000 ) , include_docs : true ,
676+ keys : reports . slice ( 0 , 200 ) . map ( report => report . id ) , include_docs : true ,
707677 } ] ) ;
708678 chai . expect ( service . __get__ ( 'contactsBatchSize' ) ) . to . equal ( 1000 ) ;
709679 } ) ;
@@ -876,8 +846,8 @@ describe('ServerSidePurge', () => {
876846 chai . expect ( request . post . args [ 0 ] ) . to . deep . equal ( [ {
877847 uri : `${ environment . couchUrl } /_design/medic/_nouveau/docs_by_replication_key` ,
878848 body : {
879- q : 'key :(first OR f1 OR s1 OR f2 OR f4 OR s4) AND type:data_record' ,
880- limit : 200000
849+ q : 'subject :(first OR f1 OR s1 OR f2 OR f4 OR s4) AND type:data_record' ,
850+ limit : 20000
881851 } ,
882852 } ] ) ;
883853 chai . expect ( db . medic . allDocs . callCount ) . to . equal ( 1 ) ;
@@ -1032,8 +1002,8 @@ describe('ServerSidePurge', () => {
10321002 chai . expect ( request . post . args [ 0 ] ) . to . deep . equal ( [ {
10331003 uri : `${ environment . couchUrl } /_design/medic/_nouveau/docs_by_replication_key` ,
10341004 body : {
1035- q : 'key :(first OR f1 OR s1 OR f2 OR s2) AND type:data_record' ,
1036- limit : 200000
1005+ q : 'subject :(first OR f1 OR s1 OR f2 OR s2) AND type:data_record' ,
1006+ limit : 20000
10371007 } ,
10381008 } ] ) ;
10391009 chai . expect ( db . medic . allDocs . callCount ) . to . equal ( 1 ) ;
@@ -1088,7 +1058,7 @@ describe('ServerSidePurge', () => {
10881058 } ) ;
10891059 } ) ;
10901060
1091- it ( 'should correctly ignore reports with needs_signoff when they emit submitter lineage' , ( ) => {
1061+ it ( 'should query reports with needs_signoff by subject rather than submitter lineage' , ( ) => {
10921062 sinon . stub ( db , 'queryMedic' ) ;
10931063 db . queryMedic . onCall ( 0 ) . resolves ( { rows : [
10941064 { id : 'first' , key : 'district_hospital' , doc : { _id : 'first' , type : CONTACT_TYPES . DISTRICT_HOSPITAL } } ,
@@ -1112,22 +1082,10 @@ describe('ServerSidePurge', () => {
11121082 fields : { key : 's1' , type : DOC_TYPES . DATA_RECORD , subject : 's1' , needs_signoff : true , submitter : 'f1' }
11131083 } ,
11141084 { id : 'f1-m1' , fields : { key : 'f1' , type : DOC_TYPES . DATA_RECORD , subject : 'f1' , submitter : 'f1' } } ,
1115- {
1116- id : 'f2-r1' ,
1117- fields : { key : 'f2' , type : DOC_TYPES . DATA_RECORD , subject : 'random' , needs_signoff : true , submitter : 'f2' }
1118- } ,
1119- {
1120- id : 'f2-r2' ,
1121- fields : { key : 'f2' , type : DOC_TYPES . DATA_RECORD , subject : 'random' , needs_signoff : true , submitter : 'other' }
1122- } ,
11231085 {
11241086 id : 'f2-r3' ,
11251087 fields : { key : 'f2' , type : DOC_TYPES . DATA_RECORD , needs_signoff : true , submitter : 'f2' , subject : 'f2' }
11261088 } ,
1127- {
1128- id : 'f2-r4' ,
1129- fields : { key : 'f2' , type : DOC_TYPES . DATA_RECORD , needs_signoff : true , submitter : 'f2' , subject : 'f6' }
1130- } ,
11311089 ] } ) ;
11321090 sinon . stub ( db . medic , 'allDocs' ) . onCall ( 0 ) . resolves ( { rows : [
11331091 { id : 'f1-r1' , doc : { _id : 'f1-r1' ,
@@ -1143,8 +1101,8 @@ describe('ServerSidePurge', () => {
11431101 chai . expect ( request . post . args [ 0 ] ) . to . deep . equal ( [ {
11441102 uri : `${ environment . couchUrl } /_design/medic/_nouveau/docs_by_replication_key` ,
11451103 body : {
1146- q : 'key :(first OR f1 OR s1 OR f2) AND type:data_record' ,
1147- limit : 200000
1104+ q : 'subject :(first OR f1 OR s1 OR f2) AND type:data_record' ,
1105+ limit : 20000
11481106 } ,
11491107 } ] ) ;
11501108 chai . expect ( db . medic . allDocs . callCount ) . to . equal ( 1 ) ;
0 commit comments