comparison test/db_test_base.py @ 5725:6923225fd781

Handle UnicodeDecodeError in file class when file contents are not text (e.g. jpg). This shows up in python3 due to conversion from bytes to string. This allows binary file retrieval via the rest interface to complete.
author John Rouillard <rouilj@ieee.org>
date Wed, 22 May 2019 20:56:59 -0400
parents cd33bbf03cf3
children 486824f4455a
comparison
equal deleted inserted replaced
5724:059625fbc40d 5725:6923225fd781
301 301
302 # test set & retrieve (this time for file contents) 302 # test set & retrieve (this time for file contents)
303 nid = self.db.file.create(content=ustr) 303 nid = self.db.file.create(content=ustr)
304 self.assertEqual(self.db.file.get(nid, 'content'), ustr) 304 self.assertEqual(self.db.file.get(nid, 'content'), ustr)
305 self.assertEqual(self.db.file.get(nid, 'binary_content'), s2b(ustr)) 305 self.assertEqual(self.db.file.get(nid, 'binary_content'), s2b(ustr))
306
307 def testStringBinary(self):
308 ''' Create file with binary content that is not able
309 to be interpreted as unicode. Try to cause file module
310 trigger and handle UnicodeDecodeError
311 and get valid output
312 '''
313 # test set & retrieve
314 bstr = b'\x00\xF0\x34\x33' # random binary data
315
316 # test set & retrieve (this time for file contents)
317 nid = self.db.file.create(content=bstr)
318 print(nid)
319 print(repr(self.db.file.get(nid, 'content')))
320 print(repr(self.db.file.get(nid, 'binary_content')))
321 p3val='file1 is not text, retrieve using binary_content property. mdsum: 0e1d1b47e4bd1beab3afc9b79f596c1d'
322
323 if sys.version_info[0] > 2:
324 # python 3
325 self.assertEqual(self.db.file.get(nid, 'content'), p3val)
326 self.assertEqual(self.db.file.get(nid, 'binary_content'),
327 bstr)
328 else:
329 # python 2
330 self.assertEqual(self.db.file.get(nid, 'content'), bstr)
331 self.assertEqual(self.db.file.get(nid, 'binary_content'), bstr)
306 332
307 # Link 333 # Link
308 def testLinkChange(self): 334 def testLinkChange(self):
309 self.assertRaises(IndexError, self.db.issue.create, title="spam", 335 self.assertRaises(IndexError, self.db.issue.create, title="spam",
310 status='100') 336 status='100')

Roundup Issue Tracker: http://roundup-tracker.org/