Repository navigation
Expand file tree
/
Copy pathdocument_tests.py
More file actions
784 lines (729 loc) · 27.6 KB
/
Copy pathdocument_tests.py
File metadata and controls
784 lines (729 loc) · 27.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
#!/usr/bin/env python
# Copyright (c) 2015 IBM. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
_document_tests_
document module - Unit tests for the Document class
See configuration options for environment variables in unit_t_db_base
module docstring.
"""
import unittest
import mock
import json
import requests
import os
import uuid
import inspect
from cloudant.document import Document
from cloudant.error import CloudantDocumentException
from .. import StringIO, unicode_
from .unit_t_db_base import UnitTestDbBase
def find_fixture(name):
import tests.unit.fixtures as fixtures
dirname = os.path.dirname(inspect.getsourcefile(fixtures))
filename = os.path.join(dirname, name)
return filename
class CloudantDocumentExceptionTests(unittest.TestCase):
"""
Ensure CloudantDocumentException functions as expected.
"""
def test_raise_without_code(self):
"""
Ensure that a default exception/code is used if none is provided.
"""
with self.assertRaises(CloudantDocumentException) as cm:
raise CloudantDocumentException()
self.assertEqual(cm.exception.status_code, 100)
def test_raise_using_invalid_code(self):
"""
Ensure that a default exception/code is used if invalid code is provided.
"""
with self.assertRaises(CloudantDocumentException) as cm:
raise CloudantDocumentException('foo')
self.assertEqual(cm.exception.status_code, 100)
def test_raise_without_args(self):
"""
Ensure that a default exception/code is used if the message requested
by the code provided requires an argument list and none is provided.
"""
with self.assertRaises(CloudantDocumentException) as cm:
raise CloudantDocumentException(102)
self.assertEqual(cm.exception.status_code, 100)
def test_raise_with_proper_code_and_args(self):
"""
Ensure that the requested exception is raised.
"""
with self.assertRaises(CloudantDocumentException) as cm:
raise CloudantDocumentException(102, 'foo')
self.assertEqual(cm.exception.status_code, 102)
class DocumentTests(UnitTestDbBase):
"""
Document unit tests
"""
def setUp(self):
"""
Set up test attributes
"""
super(DocumentTests, self).setUp()
self.db_set_up()
def tearDown(self):
"""
Reset test attributes
"""
self.db_tear_down()
super(DocumentTests, self).tearDown()
def test_constructor_with_docid(self):
"""
Test instantiating a Document providing an id
"""
doc = Document(self.db, 'julia006')
self.assertIsInstance(doc, Document)
self.assertEqual(doc.r_session, self.db.r_session)
self.assertEqual(doc.get('_id'), 'julia006')
def test_document_url(self):
"""
Test that the document url is populated correctly
"""
doc = Document(self.db, 'julia006')
self.assertEqual(
doc.document_url, '/'.join((self.db.database_url, 'julia006'))
)
def test_document_url_encodes_correctly(self):
"""
Test that the document url is populated and encoded correctly
"""
doc = Document(self.db, 'http://example.com')
self.assertEqual(
doc.document_url,
'/'.join((self.db.database_url, 'http%3A%2F%2Fexample.com'))
)
def test_design_document_url(self):
"""
Test that the document url is populated correctly when a design document
id is provided.
"""
doc = Document(self.db, '_design/ddoc001')
self.assertEqual(
doc.document_url,
'/'.join((self.db.database_url, '_design/ddoc001'))
)
def test_design_document_url_encodes_correctly(self):
"""
Test that the document url is populated and encoded correctly
"""
doc = Document(self.db, '_design/http://example.com')
self.assertEqual(
doc.document_url,
'/'.join((self.db.database_url, '_design/http%3A%2F%2Fexample.com'))
)
def test_constructor_without_docid(self):
"""
Test instantiating a Document without providing an id
"""
doc = Document(self.db)
self.assertIsInstance(doc, Document)
self.assertEqual(doc.r_session, self.db.r_session)
self.assertIsNone(doc.get('_id'))
self.assertIsNone(doc.document_url)
def test_document_exists(self):
"""
Tests that the result of True is expected when the document exists,
and False is expected when the document is nonexistent remotely.
"""
doc = Document(self.db)
self.assertFalse(doc.exists())
doc['_id'] = 'julia006'
self.assertFalse(doc.exists())
doc.create()
self.assertTrue(doc.exists())
def test_document_exists_raises_httperror(self):
"""
Test document exists raises an HTTPError.
"""
# Mock HTTPError when running against CouchDB and Cloudant
resp = requests.Response()
resp.status_code = 400
self.client.r_session.head = mock.Mock(return_value=resp)
doc = Document(self.db)
doc['_id'] = 'julia006'
with self.assertRaises(requests.HTTPError) as cm:
doc.exists()
err = cm.exception
self.assertEqual(err.response.status_code, 400)
self.client.r_session.head.assert_called_with(doc.document_url)
def test_retrieve_document_json(self):
"""
Test the document dictionary renders as json appropriately
"""
doc = Document(self.db)
doc['_id'] = 'julia006'
doc['name'] = 'julia'
doc['age'] = 6
doc_as_json = doc.json()
self.assertIsInstance(doc_as_json, str)
self.assertEqual(json.loads(doc_as_json), doc)
def test_create_document_with_docid(self):
"""
Test creating a document providing an id
"""
doc = Document(self.db, 'julia006')
doc['name'] = 'julia'
doc['age'] = 6
self.assertFalse(doc.exists())
self.assertIsNone(doc.get('_rev'))
doc.create()
self.assertTrue(doc.exists())
self.assertTrue(doc.get('_rev').startswith('1-'))
def test_create_document_with_docid_encoded_url(self):
"""
Test creating a document providing an id that has an encoded url
"""
doc = Document(self.db, 'http://example.com')
doc['name'] = 'julia'
doc['age'] = 6
self.assertFalse(doc.exists())
self.assertIsNone(doc.get('_rev'))
doc.create()
self.assertTrue(doc.exists())
self.assertTrue(doc.get('_rev').startswith('1-'))
def test_create_document_without_docid(self):
"""
Test creating a document remotely without providing an id
"""
doc = Document(self.db)
doc['name'] = 'julia'
doc['age'] = 6
self.assertFalse(doc.exists())
self.assertIsNone(doc.get('_id'))
self.assertIsNone(doc.get('_rev'))
doc.create()
self.assertTrue(doc.exists())
self.assertIsNotNone(doc.get('_id'))
self.assertTrue(doc.get('_rev').startswith('1-'))
def test_create_existing_document(self):
"""
Test creating an already existing document
"""
doc = Document(self.db, 'julia006')
doc.create()
with self.assertRaises(requests.HTTPError) as cm:
doc.create()
err = cm.exception
self.assertEqual(
err.response.status_code,
409
)
def test_fetch_document_without_docid(self):
"""
Test fetching document content with no id provided
"""
doc = Document(self.db)
try:
doc.fetch()
self.fail('Above statement should raise an Exception')
except CloudantDocumentException as err:
self.assertEqual(
str(err),
'A document id is required to fetch document contents. '
'Add an _id key and value to the document and re-try.'
)
def test_fetch_non_existing_document(self):
"""
Test fetching document content from a non-existing document
"""
doc = Document(self.db, 'julia006')
try:
doc.fetch()
self.fail('Above statement should raise an Exception')
except requests.HTTPError as err:
self.assertEqual(err.response.status_code, 404)
def test_fetch_existing_document_with_docid(self):
"""
Test fetching document content from an existing document
"""
doc = Document(self.db, 'julia006')
doc['name'] = 'julia'
doc['age'] = 6
doc.create()
new_doc = Document(self.db, 'julia006')
new_doc.fetch()
self.assertEqual(new_doc, doc)
def test_appended_error_message_using_save_with_invalid_key(self):
"""
Test that saving a document with an invalid remote key will
throw an HTTPError with additional error details from util
method append_response_error_content.
"""
# First create the document
doc = Document(self.db, 'julia006')
# Add an invalid key and try to save document
doc['_invalid_key'] = 'jules'
with self.assertRaises(requests.HTTPError) as cm:
doc.save()
err = cm.exception
# Should be a 400 error code, but CouchDB 1.6 issues a 500
if err.response.status_code == 500:
#Check this is CouchDB 1.6
self.assertTrue(self.client.r_session.head(self.url).headers['Server'].find('CouchDB/1.6.') >= 0,
'500 returned but was not CouchDB 1.6.x')
self.assertEqual(
str(err.response.reason),
'Internal Server Error doc_validation Bad special document member: _invalid_key'
)
else:
self.assertEqual(
str(err.response.reason),
'Bad Request doc_validation Bad special document member: _invalid_key'
)
self.assertEqual(
err.response.status_code,
400
)
def test_fetch_existing_document_with_docid_encoded_url(self):
"""
Test fetching document content from an existing document where the
document id requires an encoded url
"""
doc = Document(self.db, 'http://example.com')
doc['name'] = 'julia'
doc['age'] = 6
doc.create()
new_doc = Document(self.db, 'http://example.com')
new_doc.fetch()
self.assertEqual(new_doc, doc)
def test_create_document_using_save(self):
"""
Test that save functionality works. If a document does
not exist remotely then create it.
"""
doc = Document(self.db, 'julia006')
doc['name'] = 'julia'
doc['age'] = 6
self.assertIsNone(doc.get('_rev'))
doc.save()
self.assertTrue(doc.exists())
self.assertTrue(doc['_rev'].startswith('1-'))
remote_doc = Document(self.db, 'julia006')
remote_doc.fetch()
self.assertEqual(remote_doc, doc)
def test_update_document_using_save(self):
"""
Test that save functionality works. If a document exists
remotely then update it.
"""
# First create the document
doc = Document(self.db, 'julia006')
doc['name'] = 'julia'
doc['age'] = 6
doc.save()
# Now test that the document gets updated
doc['name'] = 'jules'
doc.save()
self.assertTrue(doc['_rev'].startswith('2-'))
remote_doc = Document(self.db, 'julia006')
remote_doc.fetch()
self.assertEqual(remote_doc, doc)
self.assertEqual(remote_doc['name'], 'jules')
def test_update_document_with_encoded_url(self):
"""
Test that updating a document where the document id requires that the
document url be encoded is successful.
"""
# First create the document
doc = Document(self.db, 'http://example.com')
doc['name'] = 'julia'
doc['age'] = 6
doc.save()
# Now test that the document gets updated
doc['name'] = 'jules'
doc.save()
self.assertTrue(doc['_rev'].startswith('2-'))
remote_doc = Document(self.db, 'http://example.com')
remote_doc.fetch()
self.assertEqual(remote_doc, doc)
self.assertEqual(remote_doc['name'], 'jules')
def test_list_field_append_successfully(self):
"""
Test the static helper method to successfully append to a list field.
"""
doc = Document(self.db)
self.assertEqual(doc, {})
doc.list_field_append(doc, 'pets', 'cat')
self.assertEqual(doc, {'pets': ['cat']})
doc.list_field_append(doc, 'pets', 'dog')
self.assertEqual(doc, {'pets': ['cat', 'dog']})
doc.list_field_append(doc, 'pets', None)
self.assertEqual(doc, {'pets': ['cat', 'dog']})
def test_list_field_append_failure(self):
"""
Test the static helper method to append to a list
field errors as expected.
"""
doc = Document(self.db)
doc.field_set(doc, 'name', 'julia')
try:
doc.list_field_append(doc, 'name', 'isabel')
self.fail('Above statement should raise an Exception')
except CloudantDocumentException as err:
self.assertEqual(str(err), 'The field name is not a list.')
self.assertEqual(doc, {'name': 'julia'})
def test_list_field_remove_successfully(self):
"""
Test the static helper method to successfully remove from a list field.
"""
doc = Document(self.db)
self.assertEqual(doc, {})
doc.list_field_append(doc, 'pets', 'cat')
doc.list_field_append(doc, 'pets', 'dog')
self.assertEqual(doc, {'pets': ['cat', 'dog']})
doc.list_field_remove(doc, 'pets', 'dog')
self.assertEqual(doc, {'pets': ['cat']})
def test_list_field_remove_failure(self):
"""
Test the static helper method to remove from a list
field errors as expected.
"""
doc = Document(self.db)
doc.field_set(doc, 'name', 'julia')
try:
doc.list_field_remove(doc, 'name', 'julia')
self.fail('Above statement should raise an Exception')
except CloudantDocumentException as err:
self.assertEqual(str(err), 'The field name is not a list.')
self.assertEqual(doc, {'name': 'julia'})
def test_field_set_and_replace(self):
"""
Test the static helper method to set or replace a field value.
"""
doc = Document(self.db)
self.assertEqual(doc, {})
doc.field_set(doc, 'name', 'julia')
self.assertEqual(doc, {'name': 'julia'})
doc.field_set(doc, 'name', 'jules')
self.assertEqual(doc, {'name': 'jules'})
doc.field_set(doc, 'pets', ['cat', 'dog'])
self.assertEqual(doc, {'name': 'jules', 'pets': ['cat', 'dog']})
doc.field_set(doc, 'pets', None)
self.assertEqual(doc, {'name': 'jules'})
def test_update_field(self):
"""
Test that we can update a single field remotely using the
update_field method.
"""
doc = Document(self.db, 'julia006')
doc['name'] = 'julia'
doc['age'] = 6
doc['pets'] = ['cat', 'dog']
doc.create()
self.assertTrue(doc['_rev'].startswith('1-'))
self.assertEqual(doc['pets'], ['cat', 'dog'])
doc.update_field(doc.list_field_append, 'pets', 'fish')
self.assertTrue(doc['_rev'].startswith('2-'))
self.assertEqual(doc['pets'], ['cat', 'dog', 'fish'])
def test_delete_document_failure(self):
"""
Test failure condition when attempting to remove a document
from the remote database.
"""
doc = Document(self.db, 'julia006')
doc['name'] = 'julia'
doc['age'] = 6
doc['pets'] = ['cat', 'dog']
try:
doc.delete()
self.fail('Above statement should raise an Exception')
except CloudantDocumentException as err:
self.assertEqual(
str(err),
'Attempting to delete a doc with no _rev. '
'Try running .fetch and re-try.'
)
def test_delete_document_success(self):
"""
Test that we can remove a document from the remote
database successfully.
"""
doc = Document(self.db, 'julia006')
doc['name'] = 'julia'
doc['age'] = 6
doc['pets'] = ['cat', 'dog']
doc.create()
self.assertTrue(doc.exists())
doc.delete()
self.assertFalse(doc.exists())
self.assertEqual(doc, {'_id': 'julia006'})
def test_delete_document_success_with_encoded_url(self):
"""
Test that we can remove a document from the remote
database successfully when the document id requires an encoded url.
"""
doc = Document(self.db, 'http://example.com')
doc['name'] = 'julia'
doc['age'] = 6
doc['pets'] = ['cat', 'dog']
doc.create()
self.assertTrue(doc.exists())
doc.delete()
self.assertFalse(doc.exists())
self.assertEqual(doc, {'_id': 'http://example.com'})
def test_document_context_manager(self):
"""
Test that the __enter__ and __exit__ methods perform as expected
when initiated through a document context manager.
"""
new_doc = Document(self.db, 'julia006')
new_doc.create()
self.assertTrue(new_doc.exists())
del new_doc
with Document(self.db, 'julia006') as doc:
self.assertTrue(all(x in list(doc.keys()) for x in ['_id', '_rev']))
self.assertTrue(doc['_rev'].startswith('1-'))
doc['name'] = 'julia'
doc['age'] = 6
self.assertTrue(doc['_rev'].startswith('2-'))
self.assertEqual(self.db['julia006'], doc)
def test_setting_id(self):
"""
Ensure that proper processing occurs when setting the _id
"""
doc = Document(self.db)
self.assertIsNone(doc.get('_id'))
self.assertEqual(doc._document_id, None)
doc['_id'] = 'julia006'
self.assertEqual(doc['_id'], 'julia006')
self.assertEqual(doc._document_id, 'julia006')
def test_removing_id(self):
"""
Ensure that proper processing occurs when removing the _id
"""
doc = Document(self.db)
doc['_id'] = 'julia006'
del doc['_id']
self.assertIsNone(doc.get('_id'))
self.assertEqual(doc._document_id, None)
def test_get_text_attachment(self):
"""
Test the retrieval of a text attachment
"""
doc = self.db.create_document(
{'_id': 'julia006', 'name': 'julia', 'age': 6}
)
attachment = StringIO()
try:
filename = 'attachment-{0}{1}'.format(unicode_(uuid.uuid4()), '.txt')
attachment.write('This is line one of the attachment.\n')
attachment.write('This is line two of the attachment.\n')
resp = doc.put_attachment(
filename,
'text/plain',
attachment.getvalue()
)
with open(find_fixture(filename), 'wt') as f:
text_attachment = doc.get_attachment(filename, write_to=f)
self.assertEqual(text_attachment, attachment.getvalue())
with open(find_fixture(filename), 'rt') as f:
self.assertEqual(f.read(), attachment.getvalue())
finally:
attachment.close()
os.remove(find_fixture(filename))
def test_get_json_attachment(self):
"""
Test the retrieval of a json attachment
"""
doc = self.db.create_document(
{'_id': 'julia006', 'name': 'julia', 'age': 6}
)
try:
filename = 'attachment-{0}{1}'.format(unicode_(uuid.uuid4()), '.json')
data = {'foo': 'bar', 'baz': 99}
resp = doc.put_attachment(
filename,
'application/json',
json.dumps(data)
)
with open(find_fixture(filename), 'wt') as f:
json_attachment = doc.get_attachment(filename, write_to=f)
self.assertIsInstance(json_attachment, dict)
self.assertEqual(json_attachment, data)
with open(find_fixture(filename), 'rt') as f:
self.assertEqual(f.read(), json.dumps(data))
finally:
os.remove(find_fixture(filename))
def test_get_binary_attachment(self):
"""
Test the retrieval of a binary attachment
"""
doc = self.db.create_document(
{'_id': 'julia006', 'name': 'julia', 'age': 6}
)
try:
filename = 'attachment-{0}{1}'.format(unicode_(uuid.uuid4()), '.jpg')
data = None
with open(find_fixture('smile.jpg'), 'rb') as f:
data = f.read()
resp = doc.put_attachment(filename,'image/jpeg', data)
with open(find_fixture(filename), 'wb') as f:
binary_attachment = doc.get_attachment(filename, write_to=f)
self.assertEqual(binary_attachment, data)
with open(find_fixture(filename), 'rb') as f:
self.assertEqual(f.read(), data)
finally:
os.remove(find_fixture(filename))
def test_attachment_management(self):
"""
Test the adding, retrieving, updating, and deleting of attachments
"""
doc = self.db.create_document(
{'_id': 'julia006', 'name': 'julia', 'age': 6}
)
attachment = StringIO()
try:
attachment.write('This is line one of the attachment.\n')
attachment.write('This is line two of the attachment.\n')
self.assertTrue(doc['_rev'].startswith('1-'))
# Test adding an attachment
resp = doc.put_attachment(
'attachment.txt',
'text/plain',
attachment.getvalue()
)
self.assertTrue(resp['ok'])
self.assertTrue(resp['rev'].startswith('2-'))
self.assertEqual(doc['_rev'], resp['rev'])
self.assertTrue(
all(x in list(doc.keys()) for x in [
'_id',
'_rev',
'name',
'age',
'_attachments'
])
)
self.assertTrue(
all(x in list(doc['_attachments'].keys()) for x in [
'attachment.txt'
])
)
orig_size = doc['_attachments']['attachment.txt']['length']
self.assertEqual(orig_size, len(attachment.getvalue()))
# Confirm that the local document dictionary matches
# the document on the database.
expected = Document(self.db, 'julia006')
expected.fetch()
# Test retrieving an attachment
self.assertEqual(
doc.get_attachment('attachment.txt', attachment_type='text'),
attachment.getvalue()
)
# Test update an attachment
attachment.write('This is line three of the attachment.\n')
resp = doc.put_attachment(
'attachment.txt',
'text/plain',
attachment.getvalue()
)
self.assertTrue(resp['ok'])
self.assertTrue(resp['rev'].startswith('3-'))
self.assertEqual(doc['_rev'], resp['rev'])
self.assertTrue(
all(x in list(doc.keys()) for x in [
'_id',
'_rev',
'name',
'age',
'_attachments'
])
)
self.assertTrue(
all(x in list(doc['_attachments'].keys()) for x in [
'attachment.txt'
])
)
updated_size = doc['_attachments']['attachment.txt']['length']
self.assertTrue(updated_size > orig_size)
self.assertEqual(updated_size, len(attachment.getvalue()))
self.assertEqual(
doc.get_attachment('attachment.txt', attachment_type='text'),
attachment.getvalue()
)
# Confirm that the local document dictionary matches
# the document on the database.
expected = Document(self.db, 'julia006')
expected.fetch()
# Test delete attachments
# Add a second attachment so we can fully test
# delete functionality.
resp = doc.put_attachment(
'attachment2.txt',
'text/plain',
attachment.getvalue()
)
# Test deleting an attachment from a document
# with multiple atatchments.
resp = doc.delete_attachment('attachment.txt')
self.assertTrue(resp['ok'])
self.assertTrue(resp['rev'].startswith('5-'))
self.assertEqual(doc['_rev'], resp['rev'])
self.assertTrue(
all(x in list(doc.keys()) for x in [
'_id',
'_rev',
'name',
'age',
'_attachments'
])
)
# Confirm that the local document dictionary matches
# the document on the database.
expected = Document(self.db, 'julia006')
expected.fetch()
self.assertEqual(doc, expected)
# Test deleting an attachment from a document
# with a single attachment.
resp = doc.delete_attachment('attachment2.txt')
self.assertTrue(resp['ok'])
self.assertTrue(resp['rev'].startswith('6-'))
self.assertEqual(doc['_rev'], resp['rev'])
self.assertTrue(
all(x in list(doc.keys()) for x in [
'_id',
'_rev',
'name',
'age'
])
)
# Confirm that the local document dictionary matches
# the document on the database.
expected = Document(self.db, 'julia006')
expected.fetch()
self.assertEqual(doc, expected)
finally:
attachment.close()
def test_document_request_fails_after_client_disconnects(self):
"""
Test that after disconnecting from a client any objects created based
on that client are not able to make requests.
"""
self.client.connect()
doc = Document(self.db, 'julia001')
doc.save()
self.client.disconnect()
try:
with self.assertRaises(AttributeError):
doc.fetch()
self.assertIsNone(doc.r_session)
finally:
self.client.connect()
if __name__ == '__main__':
unittest.main()