2828from .unit_t_db_base import UnitTestDbBase
2929from .. import BYTETYPE
3030
31+
3132class DbUpdatesTestsBase (UnitTestDbBase ):
3233 """
3334 Common _db_updates tests methods
@@ -41,8 +42,9 @@ def setUp(self):
4142 self .client .connect ()
4243 self .db_names = list ()
4344 self .new_dbs = list ()
44- self .create_db_updates ()
45- self .create_dbs ()
45+ if not self .is_couchdb_1x_version ():
46+ self .create_db_updates ()
47+ self .create_dbs ()
4648
4749 def tearDown (self ):
4850 """
@@ -52,43 +54,55 @@ def tearDown(self):
5254 changes = list ()
5355 [db .delete () for db in self .new_dbs ]
5456 # Check the changes in the _db_updates feed to assert that the test databases are deleted
55- while not test_dbs_deleted :
56- feed = Feed (self .client , timeout = 1000 )
57- for change in feed :
58- if change ['db_name' ] in self .db_names and change ['type' ] == 'deleted' :
59- changes .append (change )
60- if len (changes ) == 2 :
61- test_dbs_deleted = True
62- feed .stop ()
63- self .delete_db_updates ()
57+ if not self .is_couchdb_1x_version ():
58+ while not test_dbs_deleted and not self .is_couchdb_1x_version ():
59+ feed = Feed (self .client , timeout = 1000 )
60+ for change in feed :
61+ if change ['db_name' ] in self .db_names and change ['type' ] == 'deleted' :
62+ changes .append (change )
63+ if len (changes ) == 2 :
64+ test_dbs_deleted = True
65+ feed .stop ()
66+ self .delete_db_updates ()
6467 self .client .disconnect ()
6568 super (DbUpdatesTestsBase , self ).tearDown ()
6669
6770 def create_dbs (self ):
68- self .db_names = [self .dbname () for x in range (2 )]
69- self .new_dbs += [self .client .create_database (dbname ) for dbname in self .db_names ]
70- # Verify that all created databases are listed in _db_updates
71- all_dbs_exist = False
72- while not all_dbs_exist :
73- changes = list ()
74- feed = Feed (self .client , timeout = 1000 )
75- for change in feed :
76- changes .append (change )
77- if len (changes ) == 3 :
78- all_dbs_exist = True
79- feed .stop ()
71+ if not self .is_couchdb_1x_version ():
72+ self .db_names = [self .dbname () for x in range (2 )]
73+ self .new_dbs += [self .client .create_database (dbname ) for dbname in self .db_names ]
74+ # Verify that all created databases are listed in _db_updates
75+ all_dbs_exist = False
76+ while not all_dbs_exist :
77+ changes = list ()
78+ feed = Feed (self .client , timeout = 1000 )
79+ for change in feed :
80+ changes .append (change )
81+ if len (changes ) == 3 :
82+ all_dbs_exist = True
83+ feed .stop ()
84+ else :
85+ self .new_dbs += [(self .client .create_database (self .dbname ())) for x in range (3 )]
8086
8187 def assert_changes_in_db_updates_feed (self , changes ):
8288 """
8389 Assert that databases created in setup for db_updates_tests exist when looping through _db_updates feed
8490 Note: During the creation of _global_changes database, a doc called '_dbs' is created and seen in _db_updates
8591 """
86- self .dbs = ['_dbs' , self .new_dbs [0 ].database_name , self .new_dbs [1 ].database_name ]
87- types = ['created' , 'updated' ]
88- for doc in changes :
89- self .assertIsNotNone (doc ['seq' ])
90- self .assertTrue (doc ['db_name' ] in self .dbs )
91- self .assertTrue (doc ['type' ] in types )
92+ if not self .is_couchdb_1x_version ():
93+ self .dbs = ['_dbs' , self .new_dbs [0 ].database_name , self .new_dbs [1 ].database_name ]
94+ types = ['created' , 'updated' ]
95+ for doc in changes :
96+ self .assertIsNotNone (doc ['seq' ])
97+ self .assertTrue (doc ['db_name' ] in self .dbs )
98+ self .assertTrue (doc ['type' ] in types )
99+ else :
100+ self .assertDictEqual (
101+ changes [0 ], {'db_name' : self .new_dbs [0 ].database_name , 'type' : 'created' })
102+ self .assertDictEqual (
103+ changes [1 ], {'db_name' : self .new_dbs [1 ].database_name , 'type' : 'created' })
104+ self .assertDictEqual (
105+ changes [2 ], {'db_name' : self .new_dbs [2 ].database_name , 'type' : 'created' })
92106
93107@unittest .skipIf (os .environ .get ('RUN_CLOUDANT_TESTS' ),
94108 'Skipping CouchDB _db_updates feed tests' )
@@ -116,9 +130,12 @@ def test_stop_iteration_of_continuous_feed_with_heartbeat(self):
116130 feed = Feed (self .client , feed = 'continuous' , timeout = 100 )
117131 changes = list ()
118132 for change in feed :
119- changes .append (change )
120- if len (changes ) == 3 :
121- feed .stop ()
133+ if not change and self .is_couchdb_1x_version ():
134+ self .create_dbs ()
135+ else :
136+ changes .append (change )
137+ if len (changes ) == 3 :
138+ feed .stop ()
122139 self .assert_changes_in_db_updates_feed (changes )
123140 self .assertEqual (len (changes ), 3 )
124141
@@ -130,9 +147,12 @@ def test_get_raw_content(self):
130147 raw_content = list ()
131148 for raw_line in feed :
132149 self .assertIsInstance (raw_line , BYTETYPE )
133- raw_content .append (raw_line )
134- if len (raw_content ) == 3 :
135- feed .stop ()
150+ if not raw_line and self .is_couchdb_1x_version ():
151+ self .create_dbs ()
152+ else :
153+ raw_content .append (raw_line )
154+ if len (raw_content ) == 3 :
155+ feed .stop ()
136156 changes = [json .loads (unicode_ (x )) for x in raw_content ]
137157 self .assert_changes_in_db_updates_feed (changes )
138158
@@ -142,11 +162,20 @@ def test_get_longpoll_feed_as_default(self):
142162 """
143163 feed = Feed (self .client , timeout = 1000 )
144164 changes = list ()
145- for change in feed :
146- self .assertIsNotNone (change )
147- changes .append (change )
148- self .assert_changes_in_db_updates_feed (changes )
149- self .assertEqual (len (changes ), 3 )
165+ if self .is_couchdb_1x_version ():
166+ for change in feed :
167+ self .assertIsNone (change )
168+ changes .append (change )
169+ self .assertEqual (len (changes ), 1 )
170+ self .assertIsNone (changes [0 ])
171+ else :
172+ for change in feed :
173+ self .assertIsNotNone (change )
174+ changes .append (change )
175+ if len (changes ) == 3 :
176+ feed .stop ()
177+ self .assert_changes_in_db_updates_feed (changes )
178+ self .assertEqual (len (changes ), 3 )
150179
151180 def test_get_longpoll_feed_explicit (self ):
152181 """
@@ -155,11 +184,20 @@ def test_get_longpoll_feed_explicit(self):
155184 """
156185 feed = Feed (self .client , timeout = 1000 , feed = 'longpoll' )
157186 changes = list ()
158- for change in feed :
159- self .assertIsNotNone (change )
160- changes .append (change )
161- self .assert_changes_in_db_updates_feed (changes )
162- self .assertEqual (len (changes ), 3 )
187+ if self .is_couchdb_1x_version ():
188+ for change in feed :
189+ self .assertIsNone (change )
190+ changes .append (change )
191+ self .assertEqual (len (changes ), 1 )
192+ self .assertIsNone (changes [0 ])
193+ else :
194+ for change in feed :
195+ self .assertIsNotNone (change )
196+ changes .append (change )
197+ if len (changes ) == 3 :
198+ feed .stop ()
199+ self .assert_changes_in_db_updates_feed (changes )
200+ self .assertEqual (len (changes ), 3 )
163201
164202 def test_get_continuous_with_timeout (self ):
165203 """
@@ -168,13 +206,16 @@ def test_get_continuous_with_timeout(self):
168206 """
169207 feed = Feed (self .client , feed = 'continuous' , heartbeat = False , timeout = 1000 )
170208 changes = list ()
171- for change in feed :
172- self .assertIsNotNone (change )
173- changes .append (change )
174- if len (changes ) == 3 :
175- feed .stop ()
176- self .assert_changes_in_db_updates_feed (changes )
177- self .assertEqual (len (changes ), 3 )
209+ if self .is_couchdb_1x_version ():
210+ self .assertListEqual ([x for x in feed ], [])
211+ else :
212+ for change in feed :
213+ self .assertIsNotNone (change )
214+ changes .append (change )
215+ if len (changes ) == 3 :
216+ feed .stop ()
217+ self .assert_changes_in_db_updates_feed (changes )
218+ self .assertEqual (len (changes ), 3 )
178219
179220 def test_invalid_argument (self ):
180221 """
0 commit comments