1
+ from django .core .exceptions import ImproperlyConfigured
2
+ from django .test import TestCase
3
+ from pymongo .write_concern import WriteConcern
4
+
5
+ from django_mongodb_backend .base import DatabaseWrapper
6
+
7
+
8
+ class WriteConcerrTests (TestCase ):
9
+ def test_parse_write_concern_dict (self ):
10
+ """Test parsing write concern from dictionary configuration."""
11
+ settings_dict = {
12
+ 'NAME' : 'test_db' ,
13
+ 'OPTIONS' : {
14
+ 'WRITE_CONCERN' : {'w' : 'majority' , 'j' : True , 'wtimeout' : 5000 }
15
+ }
16
+ }
17
+ wrapper = DatabaseWrapper (settings_dict )
18
+
19
+ self .assertIsInstance (wrapper ._write_concern , WriteConcern )
20
+ self .assertIsNotNone (wrapper ._write_concern )
21
+ self .assertEqual (wrapper ._write_concern .document ['w' ], 'majority' )
22
+ self .assertEqual (wrapper ._write_concern .document ['j' ], True )
23
+ self .assertEqual (wrapper ._write_concern .document ['wtimeout' ], 5000 )
24
+
25
+ def test_parse_write_concern_string (self ):
26
+ """Test parsing write concern from string configuration."""
27
+ settings_dict = {
28
+ 'NAME' : 'test_db' ,
29
+ 'OPTIONS' : {
30
+ 'WRITE_CONCERN' : 'majority'
31
+ }
32
+ }
33
+ wrapper = DatabaseWrapper (settings_dict )
34
+
35
+ self .assertIsInstance (wrapper ._write_concern , WriteConcern )
36
+ self .assertIsNotNone (wrapper ._write_concern )
37
+ self .assertEqual (wrapper ._write_concern .document ['w' ], 'majority' )
38
+
39
+ def test_parse_write_concern_int (self ):
40
+ """Test parsing write concern from integer configuration."""
41
+ settings_dict = {
42
+ 'NAME' : 'test_db' ,
43
+ 'OPTIONS' : {
44
+ 'WRITE_CONCERN' : 2
45
+ }
46
+ }
47
+ wrapper = DatabaseWrapper (settings_dict )
48
+
49
+ self .assertIsInstance (wrapper ._write_concern , WriteConcern )
50
+ self .assertIsNotNone (wrapper ._write_concern )
51
+ self .assertEqual (wrapper ._write_concern .document ['w' ], 2 )
52
+
53
+ def test_parse_write_concern_none (self ):
54
+ """Test that None write concern config results in None."""
55
+ settings_dict = {
56
+ 'NAME' : 'test_db' ,
57
+ 'OPTIONS' : {}
58
+ }
59
+ wrapper = DatabaseWrapper (settings_dict )
60
+
61
+ self .assertIsNone (wrapper ._write_concern )
62
+
63
+ def test_parse_write_concern_invalid_type (self ):
64
+ """Test that invalid write concern type raises ImproperlyConfigured."""
65
+ settings_dict = {
66
+ 'NAME' : 'test_db' ,
67
+ 'OPTIONS' : {
68
+ 'WRITE_CONCERN' : ['invalid' , 'type' ]
69
+ }
70
+ }
71
+
72
+ with self .assertRaises (ImproperlyConfigured ):
73
+ DatabaseWrapper (settings_dict )
0 commit comments