33# Picard, the next-generation MusicBrainz tagger
44#
55# Copyright (C) 2021 Bob Swift
6- # Copyright (C) 2021 Philipp Wolfer
6+ # Copyright (C) 2021, 2024 Philipp Wolfer
77#
88# This program is free software; you can redistribute it and/or
99# modify it under the terms of the GNU General Public License
2121
2222
2323import datetime
24+ from unittest .mock import patch
2425
2526import yaml
2627
3334)
3435
3536
36- class _DateTime (datetime .datetime ):
37+ class MockDateTime (datetime .datetime ):
3738 @classmethod
38- def utcnow (cls ):
39- return cls (year = 2020 , month = 1 , day = 2 , hour = 12 , minute = 34 , second = 56 , microsecond = 789 , tzinfo = None )
39+ def now (cls , tz = None ):
40+ if tz == datetime .timezone .utc :
41+ return cls (year = 2020 , month = 1 , day = 2 , hour = 12 , minute = 34 , second = 56 , microsecond = 789 , tzinfo = None )
42+ else :
43+ raise Exception ("Unexpected parameter tz=%r" % tz )
4044
4145
4246class PicardScriptTest (PicardTestCase ):
4347
44- original_datetime = datetime .datetime
45-
46- def setUp (self ):
47- super ().setUp ()
48- # Save original datetime object and substitute one returning
49- # a fixed utcnow() value for testing.
50- datetime .datetime = _DateTime
51-
52- def tearDown (self ):
53- # Restore original datetime object
54- datetime .datetime = self .original_datetime
55-
5648 def assertYamlEquals (self , yaml_str , obj , msg = None ):
5749 self .assertEqual (obj , yaml .safe_load (yaml_str ), msg )
5850
@@ -88,6 +80,7 @@ def test_script_object_3(self):
8880 self .assertEqual (test_script .last_updated , '2021-04-26' )
8981 self .assertEqual (test_script ['last_updated' ], '2021-04-26' )
9082
83+ @patch ('datetime.datetime' , MockDateTime )
9184 def test_script_object_4 (self ):
9285 # Check updating values that modify `last_updated`.
9386 test_script = PicardScript (title = 'Script 1' , script = 'Script text' , id = '12345' , last_updated = '2021-04-26' )
@@ -97,6 +90,7 @@ def test_script_object_4(self):
9790 self .assertEqual (test_script .last_updated , '2020-01-02 12:34:56 UTC' )
9891 self .assertEqual (test_script ['last_updated' ], '2020-01-02 12:34:56 UTC' )
9992
93+ @patch ('datetime.datetime' , MockDateTime )
10094 def test_script_object_5 (self ):
10195 # Check updating values from dict that modify `last_updated`.
10296 test_script = PicardScript (title = 'Script 1' , script = 'Script text' , id = '12345' , last_updated = '2021-04-26' )
0 commit comments