-
-
Notifications
You must be signed in to change notification settings - Fork 382
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix issues #152, #153 and #154 #155
Changes from 3 commits
448192e
fa7c183
f7a8c56
6f8f7b8
d453af2
084f43e
2c79505
8017985
a3f9fe1
2188b2b
6c22ed8
3e93fb1
14588a5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,7 +9,7 @@ | |
else: | ||
import unittest | ||
|
||
import boto | ||
import boto3 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seems to be a big change; does it belong here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @piskvorky It's not really that big a change. This is test code, and it writes a mock object to a mock S3 bucket. The code for doing this with boto and boto3 is slightly different, but the end result is the same (the tests still pass without changing the code). If you look at the remainder of the changes in test_s3.py, you'll see the tests aren't strongly coupled to either boto or boto3. The real benefit to using boto3 in tests is that it matches the implementation: our S3 implementation uses boto3 under the covers. Also, using boto3 is future-proof: newer versions of moto mock boto and boto3 separately. That is, objects mocked via boto are not visible to boto3 and vice versa. This means that if we upgrade to the most recent moto version (unlike the year-old version currently used in the test environment), our boto-based tests will break. This change solves that problem before it happens. |
||
import moto | ||
|
||
import smart_open | ||
|
@@ -21,15 +21,11 @@ | |
def create_bucket_and_key(bucket_name='mybucket', key_name='mykey', contents=None): | ||
# fake connection, bucket and key | ||
_LOGGER.debug('%r', locals()) | ||
conn = boto.connect_s3() | ||
conn.create_bucket(bucket_name) | ||
mybucket = conn.get_bucket(bucket_name) | ||
mykey = boto.s3.key.Key() | ||
mykey.name = key_name | ||
mykey.bucket = mybucket | ||
s3 = boto3.resource('s3') | ||
mybucket = s3.create_bucket(Bucket=bucket_name) | ||
mykey = s3.Object(bucket_name, key_name) | ||
if contents is not None: | ||
_LOGGER.debug('len(contents): %r', len(contents)) | ||
mykey.set_contents_from_string(contents) | ||
mykey.put(Body=contents) | ||
return mybucket, mykey | ||
|
||
|
||
|
@@ -47,7 +43,7 @@ def test_iter(self): | |
"""Are S3 files iterated over correctly?""" | ||
# a list of strings to test with | ||
expected = u"hello wořld\nhow are you?".encode('utf8') | ||
bucket, key = create_bucket_and_key(contents=expected) | ||
create_bucket_and_key(contents=expected) | ||
|
||
# connect to fake s3 and read from the fake key we filled above | ||
fin = smart_open.s3.BufferedInputBase('mybucket', 'mykey') | ||
|
@@ -57,15 +53,15 @@ def test_iter(self): | |
def test_iter_context_manager(self): | ||
# same thing but using a context manager | ||
expected = u"hello wořld\nhow are you?".encode('utf8') | ||
bucket, key = create_bucket_and_key(contents=expected) | ||
create_bucket_and_key(contents=expected) | ||
with smart_open.s3.BufferedInputBase('mybucket', 'mykey') as fin: | ||
output = [line.rstrip(b'\n') for line in fin] | ||
self.assertEqual(output, expected.split(b'\n')) | ||
|
||
def test_read(self): | ||
"""Are S3 files read correctly?""" | ||
content = u"hello wořld\nhow are you?".encode('utf8') | ||
bucket, key = create_bucket_and_key(contents=content) | ||
create_bucket_and_key(contents=content) | ||
_LOGGER.debug('content: %r len: %r', content, len(content)) | ||
|
||
fin = smart_open.s3.BufferedInputBase('mybucket', 'mykey') | ||
|
@@ -76,7 +72,7 @@ def test_read(self): | |
def test_seek_beginning(self): | ||
"""Does seeking to the beginning of S3 files work correctly?""" | ||
content = u"hello wořld\nhow are you?".encode('utf8') | ||
bucket, key = create_bucket_and_key(contents=content) | ||
create_bucket_and_key(contents=content) | ||
|
||
fin = smart_open.s3.BufferedInputBase('mybucket', 'mykey') | ||
self.assertEqual(content[:6], fin.read(6)) | ||
|
@@ -91,7 +87,7 @@ def test_seek_beginning(self): | |
def test_seek_start(self): | ||
"""Does seeking from the start of S3 files work correctly?""" | ||
content = u"hello wořld\nhow are you?".encode('utf8') | ||
bucket, key = create_bucket_and_key(contents=content) | ||
create_bucket_and_key(contents=content) | ||
|
||
fin = smart_open.s3.BufferedInputBase('mybucket', 'mykey') | ||
seek = fin.seek(6) | ||
|
@@ -102,7 +98,7 @@ def test_seek_start(self): | |
def test_seek_current(self): | ||
"""Does seeking from the middle of S3 files work correctly?""" | ||
content = u"hello wořld\nhow are you?".encode('utf8') | ||
bucket, key = create_bucket_and_key(contents=content) | ||
create_bucket_and_key(contents=content) | ||
|
||
fin = smart_open.s3.BufferedInputBase('mybucket', 'mykey') | ||
self.assertEqual(fin.read(5), b'hello') | ||
|
@@ -113,7 +109,7 @@ def test_seek_current(self): | |
def test_seek_end(self): | ||
"""Does seeking from the end of S3 files work correctly?""" | ||
content = u"hello wořld\nhow are you?".encode('utf8') | ||
bucket, key = create_bucket_and_key(contents=content) | ||
create_bucket_and_key(contents=content) | ||
|
||
fin = smart_open.s3.BufferedInputBase('mybucket', 'mykey') | ||
seek = fin.seek(-4, whence=smart_open.s3.END) | ||
|
@@ -122,7 +118,7 @@ def test_seek_end(self): | |
|
||
def test_detect_eof(self): | ||
content = u"hello wořld\nhow are you?".encode('utf8') | ||
bucket, key = create_bucket_and_key(contents=content) | ||
create_bucket_and_key(contents=content) | ||
|
||
fin = smart_open.s3.BufferedInputBase('mybucket', 'mykey') | ||
fin.read() | ||
|
@@ -137,7 +133,7 @@ def test_read_gzip(self): | |
buf.close = lambda: None # keep buffer open so that we can .getvalue() | ||
with contextlib.closing(gzip.GzipFile(fileobj=buf, mode='w')) as zipfile: | ||
zipfile.write(expected) | ||
bucket, key = create_bucket_and_key(contents=buf.getvalue()) | ||
create_bucket_and_key(contents=buf.getvalue()) | ||
|
||
# | ||
# Make sure we're reading things correctly. | ||
|
@@ -159,6 +155,26 @@ def test_read_gzip(self): | |
|
||
self.assertEqual(expected, actual) | ||
|
||
def test_readline(self): | ||
content = b'englishman\nin\nnew\nyork\n' | ||
create_bucket_and_key(contents=content) | ||
|
||
with smart_open.s3.BufferedInputBase('mybucket', 'mykey') as fin: | ||
actual = list(fin) | ||
|
||
expected = [b'englishman\n', b'in\n', b'new\n', b'york\n'] | ||
self.assertEqual(expected, actual) | ||
|
||
def test_readline_tiny_buffer(self): | ||
content = b'englishman\nin\nnew\nyork\n' | ||
create_bucket_and_key(contents=content) | ||
|
||
with smart_open.s3.BufferedInputBase('mybucket', 'mykey', buffer_size=8) as fin: | ||
actual = list(fin) | ||
|
||
expected = [b'englishman\n', b'in\n', b'new\n', b'york\n'] | ||
self.assertEqual(expected, actual) | ||
|
||
|
||
@moto.mock_s3 | ||
class BufferedOutputBaseTest(unittest.TestCase): | ||
|
@@ -168,7 +184,7 @@ class BufferedOutputBaseTest(unittest.TestCase): | |
""" | ||
def test_write_01(self): | ||
"""Does writing into s3 work correctly?""" | ||
mybucket, mykey = create_bucket_and_key() | ||
create_bucket_and_key() | ||
test_string = u"žluťoučký koníček".encode('utf8') | ||
|
||
# write into key | ||
|
@@ -182,7 +198,7 @@ def test_write_01(self): | |
|
||
def test_write_01a(self): | ||
"""Does s3 write fail on incorrect input?""" | ||
mybucket, mykey = create_bucket_and_key() | ||
create_bucket_and_key() | ||
|
||
try: | ||
with smart_open.s3.BufferedOutputBase('mybucket', 'writekey') as fin: | ||
|
@@ -194,7 +210,7 @@ def test_write_01a(self): | |
|
||
def test_write_02(self): | ||
"""Does s3 write unicode-utf8 conversion work?""" | ||
mybucket, mykey = create_bucket_and_key() | ||
create_bucket_and_key() | ||
|
||
smart_open_write = smart_open.s3.BufferedOutputBase('mybucket', 'writekey') | ||
smart_open_write.tell() | ||
|
@@ -205,7 +221,7 @@ def test_write_02(self): | |
|
||
def test_write_03(self): | ||
"""Does s3 multipart chunking work correctly?""" | ||
mybucket, mykey = create_bucket_and_key() | ||
create_bucket_and_key() | ||
|
||
# write | ||
smart_open_write = smart_open.s3.BufferedOutputBase( | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Both are binary?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're right. We're not using TEXT_NEWLINE right now, so I removed it.