4
4
from unittest import mock
5
5
6
6
from django .core .exceptions import MultipleObjectsReturned , ObjectDoesNotExist
7
- from django .db import DEFAULT_DB_ALIAS , DatabaseError , connections , models
7
+ from django .db import DEFAULT_DB_ALIAS , DatabaseError , connection , connections , models
8
8
from django .db .models .manager import BaseManager
9
9
from django .db .models .query import MAX_GET_RESULTS , EmptyQuerySet
10
10
from django .test import (
11
11
SimpleTestCase ,
12
12
TestCase ,
13
13
TransactionTestCase ,
14
+ skipIfDBFeature ,
14
15
skipUnlessDBFeature ,
15
16
)
16
17
from django .utils .translation import gettext_lazy
@@ -224,6 +225,7 @@ def test_not_equal_and_equal_operators_behave_as_expected_on_instances(self):
224
225
Article .objects .get (id__exact = a1 .id ), Article .objects .get (id__exact = a2 .id )
225
226
)
226
227
228
+ @skipUnlessDBFeature ("supports_microsecond_precision" )
227
229
def test_microsecond_precision (self ):
228
230
a9 = Article (
229
231
headline = "Article 9" ,
@@ -235,6 +237,33 @@ def test_microsecond_precision(self):
235
237
datetime (2005 , 7 , 31 , 12 , 30 , 45 , 180 ),
236
238
)
237
239
240
+ @skipIfDBFeature ("supports_microsecond_precision" )
241
+ def test_microsecond_precision_not_supported (self ):
242
+ # In MySQL, microsecond-level precision isn't always available. You'll
243
+ # lose microsecond-level precision once the data is saved.
244
+ a9 = Article (
245
+ headline = "Article 9" ,
246
+ pub_date = datetime (2005 , 7 , 31 , 12 , 30 , 45 , 180 ),
247
+ )
248
+ a9 .save ()
249
+ self .assertEqual (
250
+ Article .objects .get (id__exact = a9 .id ).pub_date ,
251
+ datetime (2005 , 7 , 31 , 12 , 30 , 45 ),
252
+ )
253
+
254
+ @skipIfDBFeature ("supports_microsecond_precision" )
255
+ def test_microsecond_precision_not_supported_edge_case (self ):
256
+ # If microsecond-level precision isn't available, you'll lose
257
+ # microsecond-level precision once the data is saved.
258
+ a = Article .objects .create (
259
+ headline = "Article" ,
260
+ pub_date = datetime (2008 , 12 , 31 , 23 , 59 , 59 , 999999 ),
261
+ )
262
+ self .assertEqual (
263
+ Article .objects .get (pk = a .pk ).pub_date ,
264
+ datetime (2008 , 12 , 31 , 23 , 59 , 59 , 999000 ),
265
+ )
266
+
238
267
def test_manually_specify_primary_key (self ):
239
268
# You can manually specify the primary key when creating a new object.
240
269
a101 = Article (
@@ -818,6 +847,13 @@ def _update(self, *args, **kwargs):
818
847
819
848
820
849
class ModelRefreshTests (TestCase ):
850
+ def _truncate_ms (self , val ):
851
+ # Some databases don't support microseconds in datetimes which causes
852
+ # problems when comparing the original value to that loaded from the DB.
853
+ if connection .features .supports_microsecond_precision :
854
+ return val
855
+ return val - timedelta (microseconds = val .microsecond )
856
+
821
857
def test_refresh (self ):
822
858
a = Article .objects .create (pub_date = datetime .now ())
823
859
Article .objects .create (pub_date = datetime .now ())
@@ -877,7 +913,7 @@ def test_refresh_null_fk(self):
877
913
self .assertEqual (s2 .selfref , s1 )
878
914
879
915
def test_refresh_unsaved (self ):
880
- pub_date = datetime .now ()
916
+ pub_date = self . _truncate_ms ( datetime .now () )
881
917
a = Article .objects .create (pub_date = pub_date )
882
918
a2 = Article (id = a .pk )
883
919
with self .assertNumQueries (1 ):
0 commit comments