def test_cache_head_obeys_explict_utf8_encoding_avoids_chardet_exception(self): emoji_header = 'asciiHeader1,🍏Header1,asciiHeader2'.encode('utf-8') with mock.patch('builtins.open', mock.mock_open(read_data=emoji_header)): try: function_return = cache._FileMemo('anyFile').head(encoding='utf-8') except UnicodeDecodeError: self.fail("Failed to decode emoji")
def test_importer_methods(self): # Kind of a dumb test, but for consistency we just test everything. memo = cache._FileMemo('/tmp/test') imp = importer.ImporterProtocol() self.assertIsInstance(imp.FLAG, str) self.assertFalse(imp.identify(memo)) self.assertFalse(imp.extract(memo)) self.assertFalse(imp.file_account(memo)) self.assertFalse(imp.file_date(memo)) self.assertFalse(imp.file_name(memo))
def test_cache_head_encoding(self): data = b'asciiHeader1,\xf0\x9f\x8d\x8fHeader1,asciiHeader2' # The 15th bytes is in the middle of the unicode character. num_bytes = 15 if sys.version_info < (3, 7): # Reading the documentation, a partial read from longer # mocked file data should work just fine, however, this # does not seem the case in practice. data = data[:num_bytes] with mock.patch('builtins.open', mock.mock_open(read_data=data)): string = cache._FileMemo('filepath').head(num_bytes, encoding='utf-8') self.assertEqual(string, 'asciiHeader1,')
def test_cache(self): with tempfile.NamedTemporaryFile() as tmpfile: shutil.copy(__file__, tmpfile.name) wrap = cache._FileMemo(tmpfile.name) # Check attributes. self.assertEqual(tmpfile.name, wrap.name) # Check that caching works. converter = mock.MagicMock(return_value='abc') self.assertEqual('abc', wrap.convert(converter)) self.assertEqual('abc', wrap.convert(converter)) self.assertEqual('abc', wrap.convert(converter)) self.assertEqual(1, converter.call_count)
def test_match(self, filename): """\ DATE,TYPE,REF #,DESCRIPTION,FEES,AMOUNT,BALANCE 2014-04-14,BUY,14167001,BOUGHT +CSKO 50 @98.35,7.95,-4925.45,25674.63 2014-05-08,BUY,12040838,BOUGHT +HOOL 121 @79.11,7.95,-9580.26,16094.37 """ importer = fileonly.Importer(matchers=[ ('filename', 'te?mp'), ('content', 'DATE,TYPE,REF #,DESCRIPTION,FEES,AMOUNT') ], filing='Assets:BofA:Checking', prefix='bofa') file = cache._FileMemo(filename) self.assertTrue(importer.identify(file)) assert importer.file_name(file).startswith('bofa.')
def test_cache_head_and_contents(self): with tempfile.NamedTemporaryFile() as tmpfile: shutil.copy(__file__, tmpfile.name) wrap = cache._FileMemo(tmpfile.name) contents = wrap.convert(cache.contents) self.assertIsInstance(contents, str) self.assertGreater(len(contents), 128) contents2 = wrap.contents() self.assertEqual(contents, contents2) head = wrap.convert(cache.head(128)) self.assertIsInstance(head, str) self.assertEqual(128, len(head)) mimetype = wrap.convert(cache.mimetype) self.assertRegex(mimetype, r'text/(x-(python|c\+\+)|plain)')
def test_cache_head_and_contents(self): with tempfile.NamedTemporaryFile() as tmpfile: shutil.copy(__file__, tmpfile.name) wrap = cache._FileMemo(tmpfile.name) contents = wrap.convert(cache.contents) self.assertIsInstance(contents, str) self.assertGreater(len(contents), 128) contents2 = wrap.contents() self.assertEqual(contents, contents2) head = wrap.convert(cache.head(128)) self.assertIsInstance(head, str) self.assertEqual(128, len(head)) mimetype = wrap.convert(cache.mimetype) self.assertIn(mimetype, { 'text/plain', 'text/x-python', 'text/x-script.python', 'text/c++' })
def test_cache_head_obeys_explict_utf8_encoding_avoids_chardet_exception( self): data = b'asciiHeader1,\xf0\x9f\x8d\x8fHeader1,asciiHeader2' with mock.patch('builtins.open', mock.mock_open(read_data=data)): string = cache._FileMemo('filepath').head(encoding='utf-8') self.assertEqual(string, data.decode('utf8'))