>From 3e3cbbc866d53d97cc7e04a84c4388f9547d70fa Mon Sep 17 00:00:00 2001 From: Sebastian Spaeth Date: Thu, 25 Jun 2015 14:15:01 +0200 Subject: [PATCH] Fix string-related tests for Python3 filebased storages are ultimately derived from io.FileIO and thus take bytes/bytesarrays, not strings/unicode. Fix tests by encoding strings to bytes when necessary and compare read in values to bytes. Also, when we manually open() a file in a test, add "rb" to the mode to open the file in binary mode. --- mediagoblin/tests/test_storage.py | 27 ++++++++++++++------------- mediagoblin/tests/test_workbench.py | 4 ++-- 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/mediagoblin/tests/test_storage.py b/mediagoblin/tests/test_storage.py index 5cb1672..fa91e65 100644 --- a/mediagoblin/tests/test_storage.py +++ b/mediagoblin/tests/test_storage.py @@ -170,33 +170,34 @@ def test_basic_storage_get_file(): filepath = ['dir1', 'dir2', 'ourfile.txt'] with this_storage.get_file(filepath, 'w') as our_file: - our_file.write('First file') + our_file.write('First file'.encode('utf-8')) with this_storage.get_file(filepath, 'r') as our_file: - assert our_file.read() == 'First file' + assert our_file.read() == b'First file' assert os.path.exists(os.path.join(tmpdir, 'dir1/dir2/ourfile.txt')) - with open(os.path.join(tmpdir, 'dir1/dir2/ourfile.txt'), 'r') as our_file: - assert our_file.read() == 'First file' + with open(os.path.join(tmpdir, 'dir1/dir2/ourfile.txt'), 'rb') as our_file: + assert our_file.read() == b'First file' # Write to the same path but try to get a unique file. new_filepath = this_storage.get_unique_filepath(filepath) assert not os.path.exists(os.path.join(tmpdir, *new_filepath)) + # filebased storages derive from io.FileIO and write/read bytes not unicode with this_storage.get_file(new_filepath, 'w') as our_file: - our_file.write('Second file') + our_file.write('Second file'.encode('utf-8')) with this_storage.get_file(new_filepath, 'r') as our_file: - assert our_file.read() == 'Second file' + assert our_file.read() == b'Second file' assert os.path.exists(os.path.join(tmpdir, *new_filepath)) - with open(os.path.join(tmpdir, *new_filepath), 'r') as our_file: - assert our_file.read() == 'Second file' + with open(os.path.join(tmpdir, *new_filepath), 'rb') as our_file: + assert our_file.read() == b'Second file' # Read from an existing file manually_written_file = os.makedirs( os.path.join(tmpdir, 'testydir')) - with open(os.path.join(tmpdir, 'testydir/testyfile.txt'), 'w') as testyfile: - testyfile.write('testy file! so testy.') + with open(os.path.join(tmpdir, 'testydir/testyfile.txt'), 'wb') as testyfile: + testyfile.write(b'testy file! so testy.') with this_storage.get_file(['testydir', 'testyfile.txt']) as testyfile: - assert testyfile.read() == 'testy file! so testy.' + assert testyfile.read() == b'testy file! so testy.' this_storage.delete_file(filepath) this_storage.delete_file(new_filepath) @@ -212,7 +213,7 @@ def test_basic_storage_delete_file(): filepath = ['dir1', 'dir2', 'ourfile.txt'] with this_storage.get_file(filepath, 'w') as our_file: - our_file.write('Testing this file') + our_file.write(b'Testing this file') assert os.path.exists( os.path.join(tmpdir, 'dir1/dir2/ourfile.txt')) @@ -281,7 +282,7 @@ def test_basic_storage_copy_locally(): filepath = ['dir1', 'dir2', 'ourfile.txt'] with this_storage.get_file(filepath, 'w') as our_file: - our_file.write('Testing this file') + our_file.write('Testing this file'.encode('utf-8')) new_file_dest = os.path.join(dest_tmpdir, 'file2.txt') diff --git a/mediagoblin/tests/test_workbench.py b/mediagoblin/tests/test_workbench.py index f3ff57e..34e3dd8 100644 --- a/mediagoblin/tests/test_workbench.py +++ b/mediagoblin/tests/test_workbench.py @@ -52,7 +52,7 @@ class TestWorkbench(object): tmpfile_name = this_workbench.joinpath('temp.txt') tmpfile = open(tmpfile_name, 'w') with tmpfile: - tmpfile.write('lollerskates') + tmpfile.write('lollerskates'.encode('utf-8')) assert os.path.exists(tmpfile_name) @@ -69,7 +69,7 @@ class TestWorkbench(object): filepath = ['dir1', 'dir2', 'ourfile.txt'] with this_storage.get_file(filepath, 'w') as our_file: - our_file.write('Our file') + our_file.write('Our file'.encode('utf-8'))) # with a local file storage filename = this_workbench.localized_file(this_storage, filepath) -- 2.1.4