From 3e3cbbc866d53d97cc7e04a84c4388f9547d70fa Mon Sep 17 00:00:00 2001
From: Sebastian Spaeth <Sebastian@SSpaeth.de>
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
|
b
|
def test_basic_storage_get_file():
|
| 170 | 170 | filepath = ['dir1', 'dir2', 'ourfile.txt'] |
| 171 | 171 | |
| 172 | 172 | with this_storage.get_file(filepath, 'w') as our_file: |
| 173 | | our_file.write('First file') |
| | 173 | our_file.write('First file'.encode('utf-8')) |
| 174 | 174 | with this_storage.get_file(filepath, 'r') as our_file: |
| 175 | | assert our_file.read() == 'First file' |
| | 175 | assert our_file.read() == b'First file' |
| 176 | 176 | assert os.path.exists(os.path.join(tmpdir, 'dir1/dir2/ourfile.txt')) |
| 177 | | with open(os.path.join(tmpdir, 'dir1/dir2/ourfile.txt'), 'r') as our_file: |
| 178 | | assert our_file.read() == 'First file' |
| | 177 | with open(os.path.join(tmpdir, 'dir1/dir2/ourfile.txt'), 'rb') as our_file: |
| | 178 | assert our_file.read() == b'First file' |
| 179 | 179 | |
| 180 | 180 | # Write to the same path but try to get a unique file. |
| 181 | 181 | new_filepath = this_storage.get_unique_filepath(filepath) |
| 182 | 182 | assert not os.path.exists(os.path.join(tmpdir, *new_filepath)) |
| 183 | 183 | |
| | 184 | # filebased storages derive from io.FileIO and write/read bytes not unicode |
| 184 | 185 | with this_storage.get_file(new_filepath, 'w') as our_file: |
| 185 | | our_file.write('Second file') |
| | 186 | our_file.write('Second file'.encode('utf-8')) |
| 186 | 187 | with this_storage.get_file(new_filepath, 'r') as our_file: |
| 187 | | assert our_file.read() == 'Second file' |
| | 188 | assert our_file.read() == b'Second file' |
| 188 | 189 | assert os.path.exists(os.path.join(tmpdir, *new_filepath)) |
| 189 | | with open(os.path.join(tmpdir, *new_filepath), 'r') as our_file: |
| 190 | | assert our_file.read() == 'Second file' |
| | 190 | with open(os.path.join(tmpdir, *new_filepath), 'rb') as our_file: |
| | 191 | assert our_file.read() == b'Second file' |
| 191 | 192 | |
| 192 | 193 | # Read from an existing file |
| 193 | 194 | manually_written_file = os.makedirs( |
| 194 | 195 | os.path.join(tmpdir, 'testydir')) |
| 195 | | with open(os.path.join(tmpdir, 'testydir/testyfile.txt'), 'w') as testyfile: |
| 196 | | testyfile.write('testy file! so testy.') |
| | 196 | with open(os.path.join(tmpdir, 'testydir/testyfile.txt'), 'wb') as testyfile: |
| | 197 | testyfile.write(b'testy file! so testy.') |
| 197 | 198 | |
| 198 | 199 | with this_storage.get_file(['testydir', 'testyfile.txt']) as testyfile: |
| 199 | | assert testyfile.read() == 'testy file! so testy.' |
| | 200 | assert testyfile.read() == b'testy file! so testy.' |
| 200 | 201 | |
| 201 | 202 | this_storage.delete_file(filepath) |
| 202 | 203 | this_storage.delete_file(new_filepath) |
| … |
… |
def test_basic_storage_delete_file():
|
| 212 | 213 | |
| 213 | 214 | filepath = ['dir1', 'dir2', 'ourfile.txt'] |
| 214 | 215 | with this_storage.get_file(filepath, 'w') as our_file: |
| 215 | | our_file.write('Testing this file') |
| | 216 | our_file.write(b'Testing this file') |
| 216 | 217 | |
| 217 | 218 | assert os.path.exists( |
| 218 | 219 | os.path.join(tmpdir, 'dir1/dir2/ourfile.txt')) |
| … |
… |
def test_basic_storage_copy_locally():
|
| 281 | 282 | |
| 282 | 283 | filepath = ['dir1', 'dir2', 'ourfile.txt'] |
| 283 | 284 | with this_storage.get_file(filepath, 'w') as our_file: |
| 284 | | our_file.write('Testing this file') |
| | 285 | our_file.write('Testing this file'.encode('utf-8')) |
| 285 | 286 | |
| 286 | 287 | new_file_dest = os.path.join(dest_tmpdir, 'file2.txt') |
| 287 | 288 | |
diff --git a/mediagoblin/tests/test_workbench.py b/mediagoblin/tests/test_workbench.py
index f3ff57e..34e3dd8 100644
|
a
|
b
|
class TestWorkbench(object):
|
| 52 | 52 | tmpfile_name = this_workbench.joinpath('temp.txt') |
| 53 | 53 | tmpfile = open(tmpfile_name, 'w') |
| 54 | 54 | with tmpfile: |
| 55 | | tmpfile.write('lollerskates') |
| | 55 | tmpfile.write('lollerskates'.encode('utf-8')) |
| 56 | 56 | |
| 57 | 57 | assert os.path.exists(tmpfile_name) |
| 58 | 58 | |
| … |
… |
class TestWorkbench(object):
|
| 69 | 69 | filepath = ['dir1', 'dir2', 'ourfile.txt'] |
| 70 | 70 | |
| 71 | 71 | with this_storage.get_file(filepath, 'w') as our_file: |
| 72 | | our_file.write('Our file') |
| | 72 | our_file.write('Our file'.encode('utf-8'))) |
| 73 | 73 | |
| 74 | 74 | # with a local file storage |
| 75 | 75 | filename = this_workbench.localized_file(this_storage, filepath) |