Ticket #5332: 0001-Fix-string-related-tests-for-Python3.patch

File 0001-Fix-string-related-tests-for-Python3.patch, 4.7 KB (added by spaetz, 9 years ago)
  • mediagoblin/tests/test_storage.py

    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():  
    170170    filepath = ['dir1', 'dir2', 'ourfile.txt']
    171171
    172172    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'))
    174174    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'
    176176    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'
    179179
    180180    # Write to the same path but try to get a unique file.
    181181    new_filepath = this_storage.get_unique_filepath(filepath)
    182182    assert not os.path.exists(os.path.join(tmpdir, *new_filepath))
    183183
     184    # filebased storages derive from io.FileIO and write/read bytes not unicode
    184185    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'))
    186187    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'
    188189    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'
    191192
    192193    # Read from an existing file
    193194    manually_written_file = os.makedirs(
    194195        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.')
    197198
    198199    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.'
    200201
    201202    this_storage.delete_file(filepath)
    202203    this_storage.delete_file(new_filepath)
    def test_basic_storage_delete_file():  
    212213
    213214    filepath = ['dir1', 'dir2', 'ourfile.txt']
    214215    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')
    216217
    217218    assert os.path.exists(
    218219        os.path.join(tmpdir, 'dir1/dir2/ourfile.txt'))
    def test_basic_storage_copy_locally():  
    281282
    282283    filepath = ['dir1', 'dir2', 'ourfile.txt']
    283284    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'))
    285286
    286287    new_file_dest = os.path.join(dest_tmpdir, 'file2.txt')
    287288
  • mediagoblin/tests/test_workbench.py

    diff --git a/mediagoblin/tests/test_workbench.py b/mediagoblin/tests/test_workbench.py
    index f3ff57e..34e3dd8 100644
    a b class TestWorkbench(object):  
    5252        tmpfile_name = this_workbench.joinpath('temp.txt')
    5353        tmpfile = open(tmpfile_name, 'w')
    5454        with tmpfile:
    55             tmpfile.write('lollerskates')
     55            tmpfile.write('lollerskates'.encode('utf-8'))
    5656
    5757        assert os.path.exists(tmpfile_name)
    5858
    class TestWorkbench(object):  
    6969        filepath = ['dir1', 'dir2', 'ourfile.txt']
    7070
    7171        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')))
    7373
    7474        # with a local file storage
    7575        filename = this_workbench.localized_file(this_storage, filepath)