Home > OS >  My unit test for searching string in a text file fails a bytes-like object is required, not 'st
My unit test for searching string in a text file fails a bytes-like object is required, not 'st

Time:09-19

My test:

def test_search(self):
    """
    Test of searching content in a file.
    """
    tmp_path = '/tmp/test2.txt'
    content = 'HelloWorld! Too Much!'

    f = open(tmp_path, "w")
    f.write(content)
    f.close()

    file_utils = bob.FileUtils()
    search_result = file_utils.search(tmp_path, 'Much')

The code I am testing:

def search(self, file: str, search: str) -> bool:
    """
    Search for a string in a file.
    """
    print('-------')
    print(file)
    print(search)
    try:
        with open(file, 'rb', 0) as f, mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ) as s:
            r = s.find(search) != -1
    except FileNotFoundError:
        self.Error().file_not_found_error(file)
    except OSError:
        self.Error().os_error(file)
    except Exception as err:
        self.Error().exception(file, err)
    finally:
        f.close()
    
    return r

From what I read I needed to use the mode rb but it still throws this error:

$ python3 -m unittest                                                                                                                                                                          1 ⨯
............................................-------
/tmp/test2.txt
Much
Unexpected error opening /tmp/test2.txt is TypeError("a bytes-like object is required, not 'str'")
ERROR:root:Unexpected error opening /tmp/test2.txt is TypeError("a bytes-like object is required, not 'str'")
Traceback (most recent call last):
  File "/home/user/Projects/SiteNetSoft/Bob/BobToolServerImageBuilder/bob.py", line 498, in search
    r = s.find(search) != -1
TypeError: a bytes-like object is required, not 'str'
E....
======================================================================
ERROR: test_search (test_bob.TestFileUtils)
Test of searching content in a file.
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/user/Projects/SiteNetSoft/Bob/BobToolServerImageBuilder/bob.py", line 498, in search
    r = s.find(search) != -1
TypeError: a bytes-like object is required, not 'str'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/user/Projects/SiteNetSoft/Bob/BobToolServerImageBuilder/test_bob.py", line 521, in test_search
    search_result = file_utils.search(tmp_path, 'Much')
  File "/home/user/Projects/SiteNetSoft/Bob/BobToolServerImageBuilder/bob.py", line 504, in search
    self.Error().exception(file, err)
  File "/home/user/Projects/SiteNetSoft/Bob/BobToolServerImageBuilder/bob.py", line 543, in exception
    sys.exit(1)
SystemExit: 1

----------------------------------------------------------------------
Ran 49 tests in 0.020s

FAILED (errors=1)

CodePudding user response:

When you call search_result = file_utils.search(tmp_path, 'Much') you is sendinga str to this function, but in r = s.find(search) != -1, you are trying to use it as a byte string.

Try to add a b'<STR>' when calling the function, this will convert the str. -> byte-str and solve the problem.

NIT: Always when you see the TypeError: try to compare the types of the inputs, a simple line print(type(<input>)) can help you to understand what is going on.

  • Related