pythonunit-testingamazon-s3boto3moto

how to mock s3 last_modified for unit tests to a file?


I would like to test the last last_modified timestamp for the s3 file in the unit test. When i try to get the timestamp for an s3 file it always gives me the latest timestamp when i run the unit test, but i have multiple s3 files and iam planning on using the latest timestamp for these these multiple files to assert. So the latest timestamp will not help in this case as all of them are the same, even doing a freezetime will get me a single timestamp for all the files where as I am looking specifically. The doc does not talk a lot about testing this but i found this https://github.com/getmoto/moto/issues/933 for reference which also does not have enough information, can someone help me on how i can set the value for the last_modified and test the timestamp for the s3 file?


Solution

  • The last_modified timestamp is set to when the file was uploaded. So if you want to see different values for attribute, you could use different freeze_time contexts:

    with freeze_time(time_1):
        s3.upload_file(..)
    with freeze_time(time_2):
        s3.upload_file(..)
    with freeze_time(time_3):
        s3.upload_file(..)
    

    A call to list_objects will then return different last_modified-values for different objects.