-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Utils fixes + add missing test coverage
Make a few changes to utils usage to make them more testable and have better APIs, and add missing test coverage for this module. Fix a couple of edge cases with the pretty file size util discovered by unit tests Add nosetests and coverage config to simplify build script, and omit cli.py for now pending issue #13.
- Loading branch information
Showing
5 changed files
with
83 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[report] | ||
omit = s3_browser/cli.py |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import datetime | ||
import unittest | ||
|
||
from s3_browser import utils | ||
|
||
|
||
class UtilsTest(unittest.TestCase): | ||
def test_pretty_size(self): | ||
"""Test that the pretty-size util approximates filesizes correctly""" | ||
cases = [ | ||
(0, '0 B'), | ||
(233, '233 B'), | ||
(1023, '1023 B'), | ||
(1024, '1 KB'), | ||
(1024 ** 2 - 1, '1 MB'), | ||
(12345678, '12 MB'), | ||
(1024 ** 3 + 100, '1 GB'), | ||
(1024 ** 4 + 1, '1 TB'), | ||
(1024 ** 5 * 2, '2048 TB'), | ||
] | ||
|
||
for v, expected in cases: | ||
actual = utils.pretty_size(v) | ||
self.assertEqual(actual, expected) | ||
|
||
def test_strip_s3_metadata(self): | ||
"""Test that full s3 metadata is correctly stripped to essentials""" | ||
|
||
# Anonymised sample response from a head_object call with boto3 | ||
data = { | ||
'ResponseMetadata': { | ||
'RequestId': 'XXXXXXXXXXXXXXXX', | ||
'HostId': 'hhhhhhhhhhhhhhhhhhhhhhhhhh', | ||
'HTTPStatusCode': 200, | ||
'HTTPHeaders': { | ||
'x-amz-id-2': 'ababababababababaabababababab', | ||
'x-amz-request-id': 'XXXXXXXXXXXXXXXX', | ||
'date': 'Wed, 20 Oct 2021 00:00:00 GMT', | ||
'last-modified': 'Fri, 22 May 2021 00:00:00 GMT', | ||
'etag': '"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"', | ||
'accept-ranges': 'bytes', | ||
'content-type': 'application/json', | ||
'server': 'AmazonS3', | ||
'content-length': '13337' | ||
}, | ||
'RetryAttempts': 0 | ||
}, | ||
'AcceptRanges': 'bytes', | ||
'LastModified': datetime.datetime(2021, 5, 22, 0, 0, 0), | ||
'ContentLength': 13409, | ||
'ETag': '"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"', | ||
'ContentType': 'application/x-tar', | ||
'Metadata': {} | ||
} | ||
|
||
expected = { | ||
'Content-Length': '13 KB (13337 bytes)', | ||
'Content-Type': 'application/json', | ||
'Last-Modified': 'Fri, 22 May 2021 00:00:00 GMT', | ||
'Metadata': {} | ||
} | ||
|
||
actual = utils.strip_s3_metadata(data) | ||
|
||
self.assertEqual(actual, expected) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
[nosetests] | ||
cover-package=s3_browser | ||
cover-inclusive=1 | ||
cover-html=1 | ||
cover-min-percentage=75 | ||
cover-html-dir=build/coverage |