103 lines
4.5 KiB
Python
Executable File
103 lines
4.5 KiB
Python
Executable File
__author__ = 'dev'
|
|
|
|
import feedparser
|
|
import os.path
|
|
import unittest
|
|
import utilities.file_methods as fm
|
|
|
|
|
|
class TestFileMethod(unittest.TestCase):
|
|
|
|
def setUp(self):
|
|
self.__text_file_path = "test.txt"
|
|
self.__json_file_path = "test.json"
|
|
self.__file_wrong_file_path = "noFile.txt"
|
|
self.__text_file_content = "this is a test"
|
|
self.__json_object_file_content = {"1": "4711", "123": "0815"}
|
|
self.__json_string_file_content = "{\"1\": \"4711\", \"123\": \"0815\"}"
|
|
self.__feed_file_path = "testfeeds/rss/example/feed.xml"
|
|
self.__feed_json_file_path = "testfeed.json"
|
|
self.__empty_string = ""
|
|
self.__wrong_dict = "{1}"
|
|
|
|
def tearDown(self):
|
|
fm.check_and_remove_file(self.__text_file_path)
|
|
fm.check_and_remove_file(self.__json_file_path)
|
|
|
|
def test_write_text_file(self):
|
|
fm.write_text_file(self.__text_file_path, self.__text_file_content)
|
|
self.assertTrue(fm.exists_file(self.__text_file_path))
|
|
self.assertEqual(fm.read_text_file(self.__text_file_path), self.__text_file_content)
|
|
|
|
def test_read_text_file(self):
|
|
fm.write_text_file(self.__text_file_path, self.__text_file_content)
|
|
self.assertEqual(fm.read_text_file(self.__text_file_path), self.__text_file_content)
|
|
|
|
def test_read_text_file_non_existing_file(self):
|
|
self.assertRaises(FileNotFoundError, fm.read_text_file, self.__file_wrong_file_path)
|
|
|
|
def test_exists_file_true_case(self):
|
|
fm.write_text_file(self.__text_file_path, self.__text_file_content)
|
|
self.assertEqual(os.path.isfile(self.__text_file_path), fm.exists_file(self.__text_file_path))
|
|
|
|
def test_exists_file_false_case(self):
|
|
self.assertEqual(os.path.isfile(self.__text_file_path), fm.exists_file(self.__text_file_path))
|
|
|
|
def test_check_and_remove_file(self):
|
|
fm.write_text_file(self.__text_file_path, self.__text_file_content)
|
|
fm.check_and_remove_file(self.__text_file_path)
|
|
self.assertFalse(fm.exists_file(self.__text_file_path))
|
|
|
|
def test_read_json_file(self):
|
|
fm.write_json_file(self.__json_file_path, self.__json_object_file_content)
|
|
jo = fm.read_json_file(self.__json_file_path)
|
|
self.assertDictEqual(jo, self.__json_object_file_content)
|
|
|
|
def test_read_json_file_from_string(self):
|
|
fm.write_json_file(self.__json_file_path, self.__json_string_file_content)
|
|
jo = fm.read_json_file(self.__json_file_path)
|
|
self.assertDictEqual(jo, self.__json_object_file_content)
|
|
|
|
def test_read_json_file_returns_error_if_json_object_is_emptystring(self):
|
|
fm.write_text_file(self.__json_file_path, self.__empty_string)
|
|
self.assertRaises(ValueError, fm.read_json_file, self.__json_file_path)
|
|
|
|
def test_read_json_file_returns_error_if_json_object_is_wrong_json_string(self):
|
|
fm.write_text_file(self.__json_file_path, self.__wrong_dict)
|
|
self.assertRaises(ValueError, fm.read_json_file, self.__json_file_path)
|
|
|
|
def test_write_json_file(self):
|
|
fm.write_json_file(self.__json_file_path, self.__json_object_file_content)
|
|
self.assertTrue(os.path.isfile(self.__json_file_path))
|
|
|
|
def test_write_json_string_file(self):
|
|
fm.write_json_file(self.__json_file_path, self.__json_string_file_content )
|
|
self.assertTrue(os.path.isfile(self.__json_file_path))
|
|
|
|
def test_write_json_file_returns_error_if_json_object_is_emptystring(self):
|
|
self.assertRaises(ValueError, fm.write_json_file, self.__json_file_path, self.__empty_string)
|
|
|
|
def test_write_json_file_returns_error_if_json_object_is_wrong_json_string(self):
|
|
self.assertRaises(ValueError, fm.write_json_file, self.__json_file_path, self.__wrong_dict)
|
|
|
|
def test_read_feed_file(self):
|
|
feed = fm.read_feed_file(self.__feed_file_path)
|
|
self.assertTrue(isinstance(feed, feedparser.FeedParserDict))
|
|
|
|
def test_write_feed_json_file(self):
|
|
feed = fm.read_feed_file(self.__feed_file_path)
|
|
self.assertTrue(isinstance(feed, feedparser.FeedParserDict))
|
|
fm.write_feed_json_file(self.__feed_json_file_path, feed, None)
|
|
feed2 = fm.read_json_file(self.__feed_json_file_path)
|
|
|
|
self.assertTrue(isinstance(feed2, dict))
|
|
fm.check_and_remove_file(self.__feed_json_file_path)
|
|
|
|
def test_write_feed_file_none_feed_parser_object(self):
|
|
self.assertRaises(AssertionError, fm.write_feed_json_file, self.__feed_json_file_path, None, None)
|
|
fm.check_and_remove_file(self.__feed_json_file_path)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main()
|