Changeset 1873:551f3d4ddf29


Ignore:
Timestamp:
Jan 1, 2020, 3:38:18 PM (3 months ago)
Author:
Stefan Schwarzer <sschwarzer@…>
Branch:
default
amend_source:
56994b3972563dd057fc6133c97777ee0c82411d
histedit_source:
0f8474d0017f8441598ed88ebd57115c7ee618b9
Message:
Simplify parser test logic

When creating a "dir line" in a `dir_line` call it's (at least for me)
much more intuitive if the time in the resulting string is the local
time (or a naive datetime) I pass to `dir_line`. However, the previous
implementation implicitly applied `time.localtime`, which confused me
a lot during debugging ticket 131.

The changed implementation of `dir_line` takes a naive `datetime`
object and just applies the format, without any timezone changes.

To make everything fit together, change `_test_time_shift` to
internally create the server time as a `datetime` object, not as a
float in seconds.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • test/test_stat.py

    r1872 r1873  
    487487    # line parser for determining the year works.
    488488    #
    489     def datetime_string(self, time_float):
    490         """
    491         Return a datetime string generated from the value in
    492         `time_float`. The parameter value is a floating point value
    493         as returned by `time.time()`. The returned string is built as
    494         if it were from a Unix FTP server (format: MMM dd hh:mm")
    495         """
    496         time_tuple = time.localtime(time_float)
    497         return time.strftime("%b %d %H:%M", time_tuple)
    498 
    499     def dir_line(self, time_float):
     489    def dir_line(self, datetime_):
    500490        """
    501491        Return a directory line as from a Unix FTP server. Most of
     
    504494        """
    505495        line_template = "-rw-r--r--   1   45854   200   4604   {}   index.html"
    506         return line_template.format(self.datetime_string(time_float))
     496        datetime_string = datetime_.strftime("%b %d %H:%M")
     497        return line_template.format(datetime_string)
    507498
    508499    def assert_equal_times(self, time1, time2):
     
    528519            host._stat._parser = ftputil.stat.UnixParser()
    529520            host.set_time_shift(supposed_time_shift)
    530             utc_time = time.time()
    531             server_time = utc_time + supposed_time_shift + deviation
     521            local_server_time = datetime.datetime.now() + datetime.timedelta(
     522                seconds=supposed_time_shift + deviation
     523            )
    532524            stat_result = host._stat._parser.parse_line(
    533                 self.dir_line(server_time), host.time_shift()
     525                self.dir_line(local_server_time), host.time_shift()
    534526            )
    535527            # We expect `st_mtime` in UTC.
    536             self.assert_equal_times(stat_result.st_mtime, utc_time + deviation)
     528            self.assert_equal_times(
     529                stat_result.st_mtime,
     530                (
     531                    local_server_time
     532                    # Convert back to local client time.
     533                    - datetime.timedelta(seconds=supposed_time_shift)
     534                    # `timestamp()` implicitly converts from local time to UTC.
     535                ).timestamp(),
     536            )
    537537
    538538    def test_time_shifts(self):
Note: See TracChangeset for help on using the changeset viewer.