1- from datetime import datetime , time
1+ from datetime import date , datetime , time
22
33import pymongo
44from bson .binary import Binary
88from .models import (
99 Appointment ,
1010 Billing ,
11- EncryptedNumbers ,
11+ CreditCard ,
1212 Patient ,
1313 PatientPortalUser ,
1414 PatientRecord ,
1515)
16- from .test_base import QueryableEncryptionTests
16+ from .test_base import QueryableEncryptionTestCase
1717
1818
19- class FieldTests (QueryableEncryptionTests ):
20- def test_appointment (self ):
21- self .assertEqual (Appointment .objects .get (time = "8:00" ).time , time (8 , 0 ))
19+ class FieldTests (QueryableEncryptionTestCase ):
20+ def setUp (self ):
21+ Patient .objects .create (
22+ patient_id = 1 ,
23+ patient_name = "John Doe" ,
24+ patient_notes = "patient notes " * 25 ,
25+ registration_date = datetime (2023 , 10 , 1 , 12 , 0 , 0 ),
26+ is_active = True ,
27+ email = "john.doe@example.com" ,
28+ )
29+ PatientRecord .objects .create (
30+ ssn = "123-45-6789" ,
31+ birth_date = "1969-01-01" ,
32+ profile_picture = b"image data" ,
33+ patient_age = 50 ,
34+ weight = 180.0 ,
35+ )
36+
37+ def test_binaryfield (self ):
38+ self .assertEqual (
39+ PatientRecord .objects .get (profile_picture = b"image data" ).profile_picture , b"image data"
40+ )
41+
42+ def test_booleanfield (self ):
43+ self .assertTrue (Patient .objects .get (patient_id = 1 ).is_active )
44+
45+ def test_charfield (self ):
46+ CreditCard .objects .create (cc_type = "Visa" , cc_number = "1234567890123456" )
47+ self .assertEqual (CreditCard .objects .get (cc_type = "Visa" ).cc_type , "Visa" )
48+ self .assertEqual (PatientRecord .objects .get (ssn = "123-45-6789" ).ssn , "123-45-6789" )
2249
23- def test_billing (self ):
50+ def test_datefield (self ):
2451 self .assertEqual (
25- Billing .objects .get (cc_number = 1234567890123456 ). cc_number , 1234567890123456
52+ PatientRecord .objects .get (birth_date = "1969-1-1" ). birth_date , date ( 1969 , 1 , 1 )
2653 )
27- self .assertEqual (Billing .objects .get (cc_type = "Visa" ).cc_type , "Visa" )
54+
55+ def test_datetimefield (self ):
56+ self .assertEqual (
57+ Patient .objects .get (
58+ registration_date = datetime (2023 , 10 , 1 , 12 , 0 , 0 )
59+ ).registration_date ,
60+ datetime (2023 , 10 , 1 , 12 , 0 , 0 ),
61+ )
62+
63+ def test_decimalfield (self ):
64+ Billing .objects .create (account_balance = 100.50 )
2865 self .assertTrue (Billing .objects .filter (account_balance__gte = 100.0 ).exists ())
2966
30- def test_patientportaluser (self ):
67+ def test_emailfield (self ):
3168 self .assertEqual (
32- PatientPortalUser .objects .get (ip_address = "127.0.0.1 " ).ip_address , "127.0.0.1 "
69+ Patient .objects .get (email = "john.doe@example.com " ).email , "john.doe@example.com "
3370 )
3471
35- def test_patientrecord (self ):
36- self .assertEqual (PatientRecord .objects .get (ssn = "123-45-6789" ).ssn , "123-45-6789" )
37- with self .assertRaises (PatientRecord .DoesNotExist ):
38- PatientRecord .objects .get (ssn = "000-00-0000" )
39- self .assertTrue (PatientRecord .objects .filter (birth_date__gte = "1969-01-01" ).exists ())
72+ def test_floatfield (self ):
73+ self .assertTrue (PatientRecord .objects .filter (weight__gte = 175.0 ).exists ())
74+
75+ def test_integerfield (self ):
76+ CreditCard .objects .create (cc_type = "Visa" , cc_number = "1234567890123456" )
77+ self .assertEqual (
78+ CreditCard .objects .get (cc_number = 1234567890123456 ).cc_number , 1234567890123456
79+ )
80+
81+ def test_ipaddressfield (self ):
82+ PatientPortalUser .objects .create (ip_address = "127.0.0.1" , url = "https://example.com" )
4083 self .assertEqual (
41- PatientRecord .objects .get (ssn = "123-45-6789 " ).profile_picture , b"image data "
84+ PatientPortalUser .objects .get (ip_address = "127.0.0.1 " ).ip_address , "127.0.0.1 "
4285 )
86+
87+ def test_smallintegerfield (self ):
4388 self .assertTrue (PatientRecord .objects .filter (patient_age__gte = 40 ).exists ())
4489 self .assertFalse (PatientRecord .objects .filter (patient_age__gte = 80 ).exists ())
45- self .assertTrue (PatientRecord .objects .filter (weight__gte = 175.0 ).exists ())
4690
47- # Test encrypted patient record in unencrypted database.
91+ def test_timefield (self ):
92+ Appointment .objects .create (time = "8:00" )
93+ self .assertEqual (Appointment .objects .get (time = "8:00" ).time , time (8 , 0 ))
94+
95+ def test_encrypted_patient_record_in_encrypted_database (self ):
96+ patients = connections ["encrypted" ].database .encryption__patient .find ()
97+ self .assertEqual (len (list (patients )), 1 )
98+ records = connections ["encrypted" ].database .encryption__patientrecord .find ()
99+ self .assertTrue ("__safeContent__" in records [0 ])
100+
101+ def test_encrypted_patient_record_in_unencrypted_database (self ):
48102 conn_params = connections ["encrypted" ].get_connection_params ()
49103 db_name = settings .DATABASES ["encrypted" ]["NAME" ]
50104 if conn_params .pop ("auto_encryption_opts" , False ):
@@ -56,32 +110,8 @@ def test_patientrecord(self):
56110 ssn = patientrecords [0 ]["ssn" ]
57111 self .assertTrue (isinstance (ssn , Binary ))
58112
59- def test_patient (self ):
113+ def test_textfield (self ):
60114 self .assertEqual (
61115 Patient .objects .get (patient_notes = "patient notes " * 25 ).patient_notes ,
62116 "patient notes " * 25 ,
63117 )
64- self .assertEqual (
65- Patient .objects .get (
66- registration_date = datetime (2023 , 10 , 1 , 12 , 0 , 0 )
67- ).registration_date ,
68- datetime (2023 , 10 , 1 , 12 , 0 , 0 ),
69- )
70- self .assertTrue (Patient .objects .get (patient_id = 1 ).is_active )
71- self .assertEqual (
72- Patient .objects .get (email = "john.doe@example.com" ).email , "john.doe@example.com"
73- )
74-
75- # Test decrypted patient record in encrypted database.
76- patients = connections ["encrypted" ].database .encryption__patient .find ()
77- self .assertEqual (len (list (patients )), 1 )
78- records = connections ["encrypted" ].database .encryption__patientrecord .find ()
79- self .assertTrue ("__safeContent__" in records [0 ])
80-
81- def test_pos_small_int (self ):
82- obj = EncryptedNumbers .objects .get (pos_smallint = 12345 )
83- self .assertEqual (obj .pos_smallint , 12345 )
84-
85- def test_small_int (self ):
86- obj = EncryptedNumbers .objects .get (smallint = - 12345 )
87- self .assertEqual (obj .smallint , - 12345 )
0 commit comments