@@ -51,90 +51,97 @@ def test_patient(self):
5151class EncryptedFieldTests (TestCase ):
5252 databases = {"default" , "encrypted" }
5353
54- def _assert_equality (self , model_cls , val ):
54+ def assertEquality (self , model_cls , val ):
5555 model_cls .objects .create (value = val )
5656 fetched = model_cls .objects .get (value = val )
5757 self .assertEqual (fetched .value , val )
5858
59- def _assert_range (self , model_cls , low , high , threshold ):
59+ def assertRange (self , model_cls , * , low , high , threshold ):
6060 model_cls .objects .create (value = low )
6161 model_cls .objects .create (value = high )
6262 # equality check for both
6363 self .assertEqual (model_cls .objects .get (value = low ).value , low )
6464 self .assertEqual (model_cls .objects .get (value = high ).value , high )
65- # range check using Python-side length (avoid unsupported count aggregation)
65+ # range check using `len()` because MongoDB does not support
66+ # SQL-style COUNT aggregation and we can't rely on the queryset's
67+ # count() method
6668 objs = list (model_cls .objects .filter (value__gt = threshold ))
6769 self .assertEqual (len (objs ), 1 )
6870 self .assertEqual (objs [0 ].value , high )
6971
7072 # Equality-only fields
7173 def test_binary (self ):
72- self ._assert_equality (EncryptedBinaryTest , b"\x00 \x01 \x02 " )
74+ self .assertEquality (EncryptedBinaryTest , b"\x00 \x01 \x02 " )
7375
7476 def test_boolean (self ):
75- self ._assert_equality (EncryptedBooleanTest , True )
77+ self .assertEquality (EncryptedBooleanTest , True )
7678
7779 def test_char (self ):
78- self ._assert_equality (EncryptedCharTest , "hello" )
80+ self .assertEquality (EncryptedCharTest , "hello" )
7981
8082 def test_email (self ):
81- self ._assert_equality (EncryptedEmailTest , "test@example.com" )
83+ self .assertEquality (EncryptedEmailTest , "test@example.com" )
8284
8385 def test_ip (self ):
84- self ._assert_equality (EncryptedGenericIPAddressTest , "192.168.0.1" )
86+ self .assertEquality (EncryptedGenericIPAddressTest , "192.168.0.1" )
8587
8688 def test_text (self ):
87- self ._assert_equality (EncryptedTextTest , "some text" )
89+ self .assertEquality (EncryptedTextTest , "some text" )
8890
8991 def test_url (self ):
90- self ._assert_equality (EncryptedURLTest , "https://example.com" )
92+ self .assertEquality (EncryptedURLTest , "https://example.com" )
9193
9294 # Range fields
9395 def test_big_integer (self ):
94- self ._assert_range (EncryptedBigIntegerTest , 100 , 200 , 150 )
96+ self .assertRange (EncryptedBigIntegerTest , low = 100 , high = 200 , threshold = 150 )
9597
9698 def test_date (self ):
9799 d1 = datetime .date (2024 , 6 , 1 )
98100 d2 = datetime .date (2024 , 6 , 10 )
99- self ._assert_range (EncryptedDateTest , d1 , d2 , datetime .date (2024 , 6 , 5 ))
101+ self .assertRange (EncryptedDateTest , low = d1 , high = d2 , threshold = datetime .date (2024 , 6 , 5 ))
100102
101103 def test_datetime (self ):
102104 dt1 = datetime .datetime (2024 , 6 , 1 , 12 , 0 )
103105 dt2 = datetime .datetime (2024 , 6 , 2 , 12 , 0 )
104- self ._assert_range (EncryptedDateTimeTest , dt1 , dt2 , datetime .datetime (2024 , 6 , 2 , 0 , 0 ))
106+ self .assertRange (
107+ EncryptedDateTimeTest , low = dt1 , high = dt2 , threshold = datetime .datetime (2024 , 6 , 2 , 0 , 0 )
108+ )
105109
106110 def test_decimal (self ):
107- self ._assert_range (
108- EncryptedDecimalTest , Decimal ("123.45" ), Decimal ("200.50" ), Decimal ("150" )
111+ self .assertRange (
112+ EncryptedDecimalTest ,
113+ low = Decimal ("123.45" ),
114+ high = Decimal ("200.50" ),
115+ threshold = Decimal ("150" ),
109116 )
110117
111118 def test_duration (self ):
112- self ._assert_range (
119+ self .assertRange (
113120 EncryptedDurationTest ,
114- datetime .timedelta (days = 3 ),
115- datetime .timedelta (days = 10 ),
116- datetime .timedelta (days = 5 ),
121+ low = datetime .timedelta (days = 3 ),
122+ high = datetime .timedelta (days = 10 ),
123+ threshold = datetime .timedelta (days = 5 ),
117124 )
118125
119126 def test_float (self ):
120- self ._assert_range (EncryptedFloatTest , 1.23 , 4.56 , 3.0 )
127+ self .assertRange (EncryptedFloatTest , low = 1.23 , high = 4.56 , threshold = 3.0 )
121128
122129 def test_integer (self ):
123- self ._assert_range (EncryptedIntegerTest , 5 , 10 , 7 )
130+ self .assertRange (EncryptedIntegerTest , low = 5 , high = 10 , threshold = 7 )
124131
125132 def test_positive_big_integer (self ):
126- self ._assert_range (EncryptedPositiveBigIntegerTest , 100 , 500 , 200 )
133+ self .assertRange (EncryptedPositiveBigIntegerTest , low = 100 , high = 500 , threshold = 200 )
127134
128135 def test_positive_integer (self ):
129- self ._assert_range (EncryptedPositiveIntegerTest , 10 , 20 , 15 )
136+ self .assertRange (EncryptedPositiveIntegerTest , low = 10 , high = 20 , threshold = 15 )
130137
131138 def test_positive_small_integer (self ):
132- self ._assert_range (EncryptedPositiveSmallIntegerTest , 5 , 8 , 6 )
139+ self .assertRange (EncryptedPositiveSmallIntegerTest , low = 5 , high = 8 , threshold = 6 )
133140
134141 def test_small_integer (self ):
135- self ._assert_range (EncryptedSmallIntegerTest , - 5 , 2 , 0 )
142+ self .assertRange (EncryptedSmallIntegerTest , low = - 5 , high = 2 , threshold = 0 )
136143
137144 def test_time (self ):
138145 t1 = datetime .time (10 , 0 )
139146 t2 = datetime .time (15 , 0 )
140- self ._assert_range (EncryptedTimeTest , t1 , t2 , datetime .time (12 , 0 ))
147+ self .assertRange (EncryptedTimeTest , low = t1 , high = t2 , threshold = datetime .time (12 , 0 ))
0 commit comments