-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathtest_s3fs.py
More file actions
82 lines (63 loc) · 2.61 KB
/
test_s3fs.py
File metadata and controls
82 lines (63 loc) · 2.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
from __future__ import unicode_literals
import os
import unittest
from nose.plugins.attrib import attr
from fs.test import FSTestCases
from fs_s3fs import S3FS
import boto3
BUCKET_NAME = os.environ.get("S3FS_TEST_BUCKET", "fsexample")
class TestS3FS(FSTestCases, unittest.TestCase):
"""Test S3FS implementation from dir_path."""
bucket_name = BUCKET_NAME
s3 = boto3.resource("s3")
client = boto3.client("s3")
def make_fs(self):
self._delete_bucket_contents()
return S3FS(self.bucket_name)
def _delete_bucket_contents(self):
response = self.client.list_objects(Bucket=self.bucket_name)
contents = response.get("Contents", ())
for obj in contents:
self.client.delete_object(Bucket=self.bucket_name, Key=obj["Key"])
@attr("slow")
class TestS3FSSubDir(FSTestCases, unittest.TestCase):
"""Test S3FS implementation from dir_path."""
bucket_name = BUCKET_NAME
s3 = boto3.resource("s3")
client = boto3.client("s3")
def make_fs(self):
self._delete_bucket_contents()
self.s3.Object(self.bucket_name, "subdirectory").put()
return S3FS(self.bucket_name, dir_path="subdirectory")
def _delete_bucket_contents(self):
response = self.client.list_objects(Bucket=self.bucket_name)
contents = response.get("Contents", ())
for obj in contents:
self.client.delete_object(Bucket=self.bucket_name, Key=obj["Key"])
class TestS3FSHelpers(unittest.TestCase):
def test_path_to_key(self):
s3 = S3FS("foo")
self.assertEqual(s3._path_to_key("foo.bar"), "foo.bar")
self.assertEqual(s3._path_to_key("foo/bar"), "foo/bar")
def test_path_to_key_subdir(self):
s3 = S3FS("foo", "/dir")
self.assertEqual(s3._path_to_key("foo.bar"), "dir/foo.bar")
self.assertEqual(s3._path_to_key("foo/bar"), "dir/foo/bar")
def test_upload_args(self):
s3 = S3FS("foo", acl="acl", cache_control="cc")
self.assertDictEqual(
s3._get_upload_args("test.jpg"),
{"ACL": "acl", "CacheControl": "cc", "ContentType": "image/jpeg"},
)
self.assertDictEqual(
s3._get_upload_args("test.mp3"),
{"ACL": "acl", "CacheControl": "cc", "ContentType": "audio/mpeg"},
)
self.assertDictEqual(
s3._get_upload_args("test.json"),
{"ACL": "acl", "CacheControl": "cc", "ContentType": "application/json"},
)
self.assertDictEqual(
s3._get_upload_args("unknown.unknown"),
{"ACL": "acl", "CacheControl": "cc", "ContentType": "binary/octet-stream"},
)