From 87192e35dcc54a24f92941e59aa244e7c245f562 Mon Sep 17 00:00:00 2001 From: stubbfel Date: Mon, 19 May 2014 22:03:31 +0200 Subject: [PATCH] add read methods to text and json files --- src/utilities/{jsonutility.py => jsonfile.py} | 10 +++- .../{textfileutility.py => textfile.py} | 9 ++++ testenv/tests/utilities/testjsonutilities.py | 47 +++++++++++++++---- .../tests/utilities/testtextfileutility.py | 19 ++++++-- 4 files changed, 68 insertions(+), 17 deletions(-) rename src/utilities/{jsonutility.py => jsonfile.py} (74%) rename src/utilities/{textfileutility.py => textfile.py} (84%) diff --git a/src/utilities/jsonutility.py b/src/utilities/jsonfile.py similarity index 74% rename from src/utilities/jsonutility.py rename to src/utilities/jsonfile.py index 49bb581..85f242b 100644 --- a/src/utilities/jsonutility.py +++ b/src/utilities/jsonfile.py @@ -1,6 +1,6 @@ __author__ = 'stubbfel' import json -from utilities.textfileutility import TextFile +from utilities.textfile import TextFile class JsonFile(TextFile): @@ -12,7 +12,7 @@ class JsonFile(TextFile): """ super(JsonFile, self).__init__(filename) - def write_json(self, json_object): + def write_json_file(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, @@ -27,4 +27,10 @@ class JsonFile(TextFile): json_string = json.dumps(json_object, indent=4) self.write_text_file(json_string) + def read_json_file(self): + """ + method read and convert the content of the file to a json object + :return: dict - json object + """ + return json.loads(self.read_text_file()) diff --git a/src/utilities/textfileutility.py b/src/utilities/textfile.py similarity index 84% rename from src/utilities/textfileutility.py rename to src/utilities/textfile.py index 567cc36..215ac32 100644 --- a/src/utilities/textfileutility.py +++ b/src/utilities/textfile.py @@ -30,6 +30,15 @@ class TextFile: if self.exists_file: os.remove(self._filename) + def read_text_file(self): + """ + method return and read the content of a text file + + :return: str - file content + """ + with open(self._filename, newline='\n') as file: + return file.read() + @property def exists_file(self): """ diff --git a/testenv/tests/utilities/testjsonutilities.py b/testenv/tests/utilities/testjsonutilities.py index 5f80a35..f6d465f 100644 --- a/testenv/tests/utilities/testjsonutilities.py +++ b/testenv/tests/utilities/testjsonutilities.py @@ -1,7 +1,8 @@ __author__ = 'stubbfel' import unittest -from utilities.jsonutility import JsonFile +from utilities.jsonfile import JsonFile +from utilities.textfile import TextFile import os.path @@ -10,18 +11,44 @@ class JsonUtilitiesTest(unittest.TestCase): self.__jf = JsonFile("test.json") self.__jf.check_and_remove_file() - def test_write_json(self): - self.__jf.write_json({1: "4711", "123": "0815"}) + def tearDown(self): + self.__jf.check_and_remove_file() + + def test_read_json_file(self): + self.__jf.write_json_file({1: "4711", "123": "0815"}) + jo = self.__jf.read_json_file() + self.assertEqual(jo["1"], "4711") + self.assertEqual(jo["123"], "0815") + + def test_read_json_file_from_string(self): + self.__jf.write_json_file("{\"1\": \"4711\", \"123\": \"0815\"}") + jo = self.__jf.read_json_file() + self.assertEqual(jo["1"], "4711") + self.assertEqual(jo["123"], "0815") + + def test_read_json_file_returns_error_if_json_object_is_emptystring(self): + tf = TextFile("test.json") + tf.write_text_file("") + self.assertRaises(ValueError, self.__jf.read_json_file) + + def test_read_json_file_returns_error_if_json_object_is_wrong_json_string(self): + tf = TextFile("test.json") + tf.write_text_file("{1}") + self.assertRaises(ValueError, self.__jf.read_json_file) + + def test_write_json_file(self): + self.__jf.write_json_file({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, self.__jf.write_json, "") - self.__jf.check_and_remove_file() + def test_write_json_file(self): + self.__jf.write_json_file("{\"1\": \"4711\", \"123\": \"0815\"}") + self.assertTrue(os.path.isfile("test.json")) - def test_write_json_returns_error_if_json_object_is_wrong_json_string(self): - self.assertRaises(ValueError, self.__jf.write_json, "{1}") - self.__jf.check_and_remove_file() + def test_write_json_file_returns_error_if_json_object_is_emptystring(self): + self.assertRaises(ValueError, self.__jf.write_json_file, "") + + def test_write_json_file_returns_error_if_json_object_is_wrong_json_string(self): + self.assertRaises(ValueError, self.__jf.write_json_file, "{1}") if __name__ == '__main__': unittest.main() diff --git a/testenv/tests/utilities/testtextfileutility.py b/testenv/tests/utilities/testtextfileutility.py index 0d3eec3..83976bf 100644 --- a/testenv/tests/utilities/testtextfileutility.py +++ b/testenv/tests/utilities/testtextfileutility.py @@ -1,7 +1,7 @@ __author__ = 'stubbfel' import unittest -from utilities.textfileutility import TextFile +from utilities.textfile import TextFile import os.path @@ -10,16 +10,25 @@ class TextFileUtilitiesTest(unittest.TestCase): 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") + def tearDown(self): self.__tf.check_and_remove_file() + def test_write_text_file(self): + self.__tf.write_text_file("this is a test") + self.assertTrue(self.__tf.exists_file) + self.assertEqual(self.__tf.read_text_file(), "this is a test") + + def test_read_text_file(self): + self.__tf.write_text_file("this is a test") + self.assertEqual(self.__tf.read_text_file(), "this is a test") + + def test_read_text_file_non_existing_file(self): + self.assertRaises(FileNotFoundError, self.__tf.read_text_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):