diff --git a/src/utilities/jsonutility.py b/src/utilities/jsonutility.py index e6e9372..49bb581 100644 --- a/src/utilities/jsonutility.py +++ b/src/utilities/jsonutility.py @@ -1,17 +1,30 @@ __author__ = 'stubbfel' import json -import utilities.textfileutility as tfu - -def write_json(filename, json_object): - - valid_types = (dict, str) - if not isinstance(json_object, valid_types): - raise ValueError - - if isinstance(json_object, str): - json_object = json.loads(json_object) - - json_string = json.dumps(json_object, indent=4) - tfu.write_text_file(filename, json_string) +from utilities.textfileutility import TextFile + + +class JsonFile(TextFile): + + def __init__(self, filename): + """ + init class + :param filename: path to the file + """ + super(JsonFile, self).__init__(filename) + + def write_json(self, json_object): + """ + write a json object to a text file + :param json_object: json object, which has to be saved. If the object is a string, + the method will be check if its a valid json object + :raise ValueError: will be raised if json_object is not a dictionary or string or the json object is not valid + """ + valid_types = (dict, str) + assert isinstance(json_object, valid_types) + if isinstance(json_object, str): + json_object = json.loads(json_object) + + json_string = json.dumps(json_object, indent=4) + self.write_text_file(json_string) diff --git a/src/utilities/textfileutility.py b/src/utilities/textfileutility.py index 9d19795..567cc36 100644 --- a/src/utilities/textfileutility.py +++ b/src/utilities/textfileutility.py @@ -1,11 +1,48 @@ __author__ = 'stubbfel' +import os.path -def write_text_file(filename, file_content): +class TextFile: - if not isinstance(filename, str): - raise ValueError + def __init__(self, filename): + """ + init the class and set the filename + :param filename: + """ + assert isinstance(filename, str) + self._filename = filename - with open(filename, 'w', newline='\n') as file: - file.write(file_content) - file.close() \ No newline at end of file + def write_text_file(self, file_content): + """ + write strings to a text file + :param file_content: strings which has to been write i a text file + :raise ValueError: will be raised, when the content is not a string + """ + assert isinstance(self._filename, str) + with open(self._filename, 'w', newline='\n') as file: + file.write(file_content) + file.close() + + def check_and_remove_file(self): + """ + Method remove this file, if the file don't exists the method don't call remove + """ + if self.exists_file: + os.remove(self._filename) + + @property + def exists_file(self): + """ + check if this file exists or not + :return: true if the file exists, otherwise false + """ + return os.path.isfile(self._filename) + + @property + def get_filename(self): + """ + get the file Name + :return: str + """ + assert isinstance(self._filename, str) + return self._filename \ No newline at end of file diff --git a/testenv/test.json b/testenv/test.json deleted file mode 100644 index 9e26dfe..0000000 --- a/testenv/test.json +++ /dev/null @@ -1 +0,0 @@ -{} \ No newline at end of file diff --git a/testenv/tests/utilities/testjsonutilities.py b/testenv/tests/utilities/testjsonutilities.py index 694d646..5f80a35 100644 --- a/testenv/tests/utilities/testjsonutilities.py +++ b/testenv/tests/utilities/testjsonutilities.py @@ -1,16 +1,27 @@ __author__ = 'stubbfel' import unittest -import utilities.jsonutility as ju +from utilities.jsonutility import JsonFile +import os.path class JsonUtilitiesTest(unittest.TestCase): + def setUp(self): + self.__jf = JsonFile("test.json") + self.__jf.check_and_remove_file() + + def test_write_json(self): + self.__jf.write_json({1: "4711", "123": "0815"}) + self.assertTrue(os.path.isfile("test.json")) + self.__jf.check_and_remove_file() + def test_write_json_returns_error_if_json_object_is_emptystring(self): - self.assertRaises(ValueError, ju.write_json, "test.json", "") + self.assertRaises(ValueError, self.__jf.write_json, "") + self.__jf.check_and_remove_file() def test_write_json_returns_error_if_json_object_is_wrong_json_string(self): - self.assertRaises(ValueError, ju.write_json, "test.json", "{1}") - + self.assertRaises(ValueError, self.__jf.write_json, "{1}") + self.__jf.check_and_remove_file() if __name__ == '__main__': unittest.main() diff --git a/testenv/tests/utilities/testtextfileutility.py b/testenv/tests/utilities/testtextfileutility.py new file mode 100644 index 0000000..0d3eec3 --- /dev/null +++ b/testenv/tests/utilities/testtextfileutility.py @@ -0,0 +1,39 @@ +__author__ = 'stubbfel' + +import unittest +from utilities.textfileutility import TextFile +import os.path + + +class TextFileUtilitiesTest(unittest.TestCase): + def setUp(self): + self.__tf = TextFile("test.txt") + self.__tf.check_and_remove_file() + + def test_write_file(self): + self.__tf.write_text_file("this is a test") + self.__tf.check_and_remove_file() + + def test_exists_file_true_case(self): + self.assertEqual(os.path.isfile("test.txt"), self.__tf.exists_file) + self.__tf.check_and_remove_file() + + def test_exists_file_false_case(self): + self.__tf.check_and_remove_file() + self.assertEqual(os.path.isfile("test.txt"), self.__tf.exists_file) + + def test_check_and_remove_file(self): + self.__tf.write_text_file("this is a test") + self.__tf.check_and_remove_file() + self.assertFalse(self.__tf.exists_file) + + def test_get_filename_true(self): + self.assertEqual(self.__tf.get_filename, "test.txt") + + def test_get_filename_false(self): + self.assertNotEqual(self.__tf.get_filename, "test1.txt") + + + +if __name__ == '__main__': + unittest.main()