def test_file_gzip_ascii_readwrite(self): """Test gzip compression and read from compressed ascii text file in Python 2""" if state.py2: FileWriter(self.ascii_path).gzip(self.ascii_string) gzip_contents = FileReader(self.ascii_path + ".gz").read_gzip() self.assertEqual(gzip_contents, self.ascii_string) elif state.py3: FileWriter(self.ascii_path).gzip(bytes(self.ascii_string, 'utf-8')) gzip_contents = FileReader(self.ascii_path + ".gz").read_gzip() self.assertEqual(gzip_contents.decode('ascii'), self.ascii_string)
def test_file_gzip_utf8_readwrite_explicit_decode(self): """Test gzip compression and read from compressed unicode text file with explicit utf-8 decode""" if state.py2: FileWriter(self.unicode_path).gzip(self.unicode_string) gzip_contents = FileReader(self.unicode_path + ".gz").read_gzip("utf-8") # when read with explicit utf-8 decoding, strings should match self.assertEqual(gzip_contents, self.unicode_string) elif state.py3: FileWriter(self.unicode_path).gzip(bytes(self.unicode_string, 'utf-8')) gzip_contents = FileReader(self.unicode_path + ".gz").read_gzip("utf-8") # when read with explicit utf-8 decoding, strings should match self.assertEqual(gzip_contents, self.unicode_string)
def test_file_ascii_safewrite(self): """Test safe_write() to confirm does not overwrite existing file""" os.remove(self.ascii_path) #remove the existing text file for tests if os.path.exists(self.ascii_path): raise IOError("The ascii test file was not deleted. (test_IO.py.test_file_ascii_safewrite)") else: safe_response = FileWriter(self.ascii_path).safe_write(self.ascii_string) # attempt safe_write when no preexisting file present ascii_text = FileReader(self.ascii_path).read() self.assertEqual(ascii_text, self.ascii_string) # assert that the correct text was written self.assertEqual(safe_response, True) # assert that returns True when file not present and writes if os.path.exists(self.ascii_path): self.assertEqual(FileWriter(self.ascii_path).safe_write(self.ascii_string), False) #confirm that returns False to calling function when there is a pre-existing file else: raise IOError("The ascii test file is not present (test_IO.py.test_file_ascii_safewrite)")
def test_file_readutf8_writeutf8_string_type(self): FileWriter(self.unicode_path).write_utf8(self.unicode_string) unicode_text = FileReader(self.unicode_path).read_utf8() if state.py2: self.assertEqual(type(unicode("test string")), type(unicode_text)) # confirm that python2 treats as unicode elif state.py3: self.assertEqual(type(str("test string")), type(unicode_text)) # confirm that python3 treats as str
def test_file_ascii_readwrite_string_type(self): FileWriter(self.ascii_path).write(self.ascii_string) # file write ascii_text = FileReader(self.ascii_path).read() # file read if state.py2: self.assertEqual(type(unicode("test string")), type(ascii_text)) #python 2 treats all input as unicode type elif state.py3: self.assertEqual(type(str("test string")), type(ascii_text)) #python 3 treats all input as str
def test_http_post_text_file_present_request_overwrite(self): """Test HTTP request text file write does occur when file present and request overwrite""" filepath = os.path.join('testfiles', 'testdir', 'post.txt') if not file_exists(filepath): fw = FileWriter(filepath) fw.write('test') http = HTTP('http://httpbin.org/gzip') response = http.post_bin_write_file(filepath, overwrite_existing=True) self.assertEqual(True, response)
def test_http_post_binary_file_present_request_overwrite(self): """Test HTTP POST request binary file write when file does exist and request for overwrite""" filepath = os.path.join('testfiles', 'testdir', 'post.gz') if not file_exists(filepath): fw = FileWriter(filepath) fw.write('test') http = HTTP('http://httpbin.org/gzip') response = http.post_bin_write_file(filepath, overwrite_existing=True) self.assertEqual(True, response) self.assertEqual(True, file_exists(filepath))
def test_http_get_binary_file_exists_request_overwrite(self): """Test HTTP GET request and write binary file executes the write when the file exists and overwrite requested""" filepath = os.path.join('testfiles', 'testdir', 'test.tar.gz') fw = FileWriter(filepath) fw.write("test") if not file_exists(filepath): raise RuntimeError("Missing test file for the unit test") http = HTTP("https://github.com/chrissimpkins/six-four/tarball/master") http.get_bin_write_file(filepath, overwrite_existing=True) self.assertTrue(file_exists(filepath))
def test_http_get_text_exists_request_overwrite(self): """Test HTTP GET request with text file write does overwrite existing file when requested to do so""" filepath = os.path.join('testfiles', 'testdir', 'test.txt') http = HTTP( 'https://raw.github.com/chrissimpkins/six-four/master/LICENSE') fw = FileWriter(filepath) fw.write("test") http.get_txt_write_file(filepath, overwrite_existing=True) self.assertEqual( FileReader(filepath).read().strip(), self.http_string.strip())
def setUp(self): self.sysfilepath = make_path("testfiles", "testdir", "systest.txt") self.sysdirpath = make_path("testfiles", "testdir") self.bogusfilepath = make_path("testfiles", "testdir", "bogusfile.text") self.metafilepath = make_path("testfiles", "keep", "metadata.txt") self.dir_file_path = make_path("testfiles", "keep") self.dir_file_list = [ "file1.txt", "file2.txt", "file3.py", "metadata.txt", "test.tar.gz" ] FileWriter(self.sysfilepath).write("test")
def test_file_utf8_readwrite(self): """Test write and read of a utf-8 encoded file""" FileWriter(self.unicode_path).write_utf8(self.unicode_string) unicode_text = FileReader(self.unicode_path).read_utf8() self.assertEqual(unicode_text, self.unicode_string)
def test_file_readlines_unicode(self): """Test line reads with unicode text""" FileWriter(self.unicode_path).write_utf8(self.multiline_unicode_string) line_list = FileReader(self.unicode_path).readlines_utf8() self.assertEqual(line_list, self.uni_multi_list)
def test_file_utf8_append_works_with_utf8(self): FileWriter(self.unicode_path).write_utf8(self.unicode_string) FileWriter(self.unicode_path).append(self.unicode_string) unicode_text = FileReader(self.unicode_path).read_utf8() self.assertTrue(len(unicode_text)>0) self.assertEqual(unicode_text, (self.unicode_string*2))
def test_file_utf8_readas_writeas(self): """Test read_as & write_as with utf-8 encoding""" FileWriter(self.unicode2_path).write_as(self.unicode_string, "utf-8") unicode_text = FileReader(self.unicode2_path).read_as("utf-8") self.assertEqual(unicode_text, self.unicode_string)
def test_file_ascii_readwrite(self): """Test write and read of ascii encoded file""" FileWriter(self.ascii_path).write(self.ascii_string) # file write ascii_text = FileReader(self.ascii_path).read() # file read self.assertEqual(ascii_text, self.ascii_string)
def test_file_ascii_readwrite_append(self): """Test append of ascii text to existing file""" FileWriter(self.ascii_path).append(self.ascii_string) #append a second string of the ascii text ascii_text = FileReader(self.ascii_path).read() self.assertEqual(ascii_text, (self.ascii_string)*2) #confirm that it equals two of the ascii strings
def test_file_append_missing_file(self): """Test append of ascii text raises IOError when file missing""" with (self.assertRaises(IOError)): FileWriter(self.bogus_path).append(self.ascii_string)
def test_file_append_utf8_missing_file(self): """Test append of unicode text raises IOError when file missing""" with (self.assertRaises(IOError)): FileWriter(self.bogus_path).append_utf8(self.unicode_string)
def test_file_utf8_readwrite_noraise_unicodeerror(self): """Test read and write does not raise unicode errors in Python 2 or 3""" FileWriter(self.unicode_path).write(self.unicode_string) unicode_text = FileReader(self.unicode_path).read() self.assertEqual(self.unicode_string, unicode_text)
def test_file_utf8_write_noraise_unicodeerror(self): """Test write of a utf-8 file with write method does not raise UnicodeEncodeError in Python 2 or 3""" FileWriter(self.unicode_path).write(self.unicode_string) unicode_text = FileReader(self.unicode_path).read_utf8() self.assertEqual(self.unicode_string, unicode_text)
def test_file_readlines_as_utf8(self): """Test line reads with readlines_as from utf8 file""" FileWriter(self.unicode_path).write_utf8(self.multiline_unicode_string) line_list = FileReader(self.unicode_path).readlines_as("utf-8") self.assertEqual(line_list, self.uni_multi_list)
def test_file_readlines_as_ascii(self): """Test line reads with readlines_as from ascii file""" FileWriter(self.ascii_path).write(self.multiline_string) line_list = FileReader(self.ascii_path).readlines_as("ascii") self.assertEqual(line_list, self.multiline_list)
def test_file_bin_readwrite(self): """Test read and write of binary data""" FileWriter(self.binary_path).write_bin(self.binary_string) bin_data = FileReader(self.binary_path).read_bin() self.assertEqual(bin_data, self.binary_string)
def test_file_readlines(self): """Test line reads with ascii text""" FileWriter(self.multiline_path).write(self.multiline_string) line_list = FileReader(self.multiline_path).readlines() self.assertEqual(line_list, self.multiline_list)
def test_file_bin_read_unicode_as_bin(self): """Test read of unicode as binary with decode""" FileWriter(self.unicode_path).write_utf8(self.unicode_string) bin_data = FileReader(self.unicode_path).read_bin() #read unicode file as binary uni_text = bin_data.decode("utf-8") #decode to utf-8 self.assertEqual(uni_text, self.unicode_string)