@@ -8,7 +8,9 @@ var Promise = require('sporks/scripts/promise'),
88describe ( 'node and browser' , function ( ) {
99
1010 var slouch = null ,
11- id = 0 ;
11+ id = 0 ,
12+ cluster = null ,
13+ replicatedDBs = null ;
1214
1315 var data = {
1416 test_db1 : {
@@ -45,6 +47,11 @@ describe('node and browser', function () {
4547 }
4648 } ;
4749
50+ var uniqueName = function ( dbName ) {
51+ // Use a unique id as back to back creation/deletion of the same DBs can lead to problems
52+ return dbName + '_' + id ;
53+ } ;
54+
4855 var createDocs = function ( db , docs ) {
4956 var promises = [ ] ;
5057 docs . forEach ( function ( doc ) {
@@ -66,22 +73,24 @@ describe('node and browser', function () {
6673 var createData = function ( ) {
6774 var promises = [ ] ;
6875 sporks . each ( data , function ( data , db ) {
69- promises . push ( createDatabase ( db + '_' + id , data ) ) ;
76+ promises . push ( createDatabase ( uniqueName ( db ) , data ) ) ;
7077 } ) ;
7178 return Promise . all ( promises ) ;
7279 } ;
7380
7481 var destroyData = function ( ) {
7582 var promises = [ ] ;
7683 sporks . each ( data , function ( data , db ) {
77- promises . push ( slouch . db . destroy ( db + '_' + id ) ) ;
84+ promises . push ( slouch . db . destroy ( uniqueName ( db ) ) ) ;
7885 } ) ;
7986 return Promise . all ( promises ) ;
8087 } ;
8188
8289 var docsShouldEql = function ( db , docs ) {
8390 var savedDocs = { } ;
84- return slouch . doc . all ( db , { include_docs : true } ) . each ( function ( item ) {
91+ return slouch . doc . all ( db , {
92+ include_docs : true
93+ } ) . each ( function ( item ) {
8594 // Delete _rev as not important for comparison
8695 delete item . doc . _rev ;
8796 savedDocs [ item . doc . _id ] = item . doc ;
@@ -109,17 +118,35 @@ describe('node and browser', function () {
109118 var dataShouldEql = function ( ) {
110119 var promises = [ ] ;
111120 sporks . each ( data , function ( data , db ) {
112- promises . push ( dbDataShouldEql ( db + '_' + id , data ) ) ;
121+ promises . push ( dbDataShouldEql ( uniqueName ( db ) , data ) ) ;
113122 } ) ;
114123 return Promise . all ( promises ) ;
115124 } ;
116125
126+ var replicate = function ( params ) {
127+ cluster = new Cluster ( params ) ;
128+
129+ // Spy
130+ var _replicateDB = cluster . _replicateDB ;
131+ cluster . _replicateDB = function ( db ) {
132+ if ( db . indexOf ( 'test_db' ) !== - 1 ) {
133+ replicatedDBs . push ( db ) ;
134+ }
135+ return _replicateDB . apply ( this , arguments ) ;
136+ } ;
137+
138+ return cluster . replicate ( ) . then ( function ( ) {
139+ return dataShouldEql ( ) ;
140+ } ) ;
141+ } ;
142+
117143 beforeEach ( function ( ) {
118144 slouch = new Slouch ( 'http://admin:admin@localhost:5984' ) ;
119145
120- // Use a unique id as back to back creation/deletion of the same DBs can lead to problems
121146 id ++ ;
122147
148+ replicatedDBs = [ ] ;
149+
123150 return createData ( ) ;
124151 } ) ;
125152
@@ -128,26 +155,29 @@ describe('node and browser', function () {
128155 } ) ;
129156
130157 it ( 'should replicate' , function ( ) {
131- var cluster = new Cluster ( {
158+ return replicate ( {
132159 source : 'http://admin:admin@localhost:5984' ,
133160 target : 'http://admin:admin@localhost:5984'
134- } ) ;
135- return cluster . replicate ( ) . then ( function ( ) {
136- return dataShouldEql ( ) ;
161+ } ) . then ( function ( ) {
162+ replicatedDBs . should . eql ( [ uniqueName ( 'test_db1' ) , uniqueName ( 'test_db2' ) ] ) ;
137163 } ) ;
138164 } ) ;
139165
140- // it('should skip when replicating', function () {
141- // var cluster = new Cluster({
142- // source: 'http://admin:admin@localhost:5984',
143- // target: 'http://admin:admin@localhost:5984',
144- // skip: ['_global_changes', '_replicator', '_users']
145- // });
146- // return cluster.replicate();
147- // });
166+ it ( 'should skip when replicating' , function ( ) {
167+ return replicate ( {
168+ source : 'http://admin:admin@localhost:5984' ,
169+ target : 'http://admin:admin@localhost:5984' ,
170+ skip : [ uniqueName ( 'test_db2' ) ]
171+ } ) . then ( function ( ) {
172+ replicatedDBs . should . eql ( [ uniqueName ( 'test_db1' ) ] ) ;
173+ } ) ;
174+ } ) ;
148175
149176 // it('should support custom concurrency when replicating', function () {
150177 //
151178 // });
152179
180+ // it('should replicate to different database', function () {
181+ // });
182+
153183} ) ;
0 commit comments