From 5a847958d3dc0923486d3ead34632fc1241f2f8a Mon Sep 17 00:00:00 2001 From: Hussain Kanafani <hussainkanafani@gmail.com> Date: Wed, 1 Jul 2020 01:43:24 +0200 Subject: [PATCH] util methods with unit tests added --- unit-test/__init__.py | 0 unit-test/test_utils.py | 50 +++++++++++++++++++++++++++++++++++++++++ utils.py | 35 +++++++++++++++++++++++++++++ 3 files changed, 85 insertions(+) create mode 100644 unit-test/__init__.py create mode 100644 unit-test/test_utils.py create mode 100644 utils.py diff --git a/unit-test/__init__.py b/unit-test/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/unit-test/test_utils.py b/unit-test/test_utils.py new file mode 100644 index 0000000..61b12b4 --- /dev/null +++ b/unit-test/test_utils.py @@ -0,0 +1,50 @@ +from unittest import TestCase +import unittest.mock as mock +from utils import digits_in_string, make_directory, read_json +import random +import os.path as osp +import os +import json + + +class TestUtils(TestCase): + def setUp(self): + self.current_dir = os.getcwd() + + def tearDown(self): + for dir in os.listdir(self.current_dir): + if (osp.isdir(dir)): + os.rmdir(dir) + + def test_digits_in_string(self): + txt = 'video10' + digit = 10 + self.assertEqual(digits_in_string(txt), int(digit)) + self.assertEqual(type(digits_in_string(txt)), int) + + def test_make_directory(self): + dir = os.path.join(self.current_dir, 'test_dir' + str(random.randint(1, 10000))) + print(dir) + dir = make_directory(dir) + self.assertTrue(osp.exists(dir), False) + + def test_open_json_file(self): + # test valid JSON + read_data = json.dumps({'a': 1, 'b': 2, 'c': 3}) + mock_open = mock.mock_open(read_data=read_data) + with mock.patch('builtins.open', mock_open): + result = read_json('file') + self.assertEqual({'a': 1, 'b': 2, 'c': 3}, result) + # test invalid JSON + read_data = '' + mock_open = mock.mock_open(read_data=read_data) + with mock.patch("builtins.open", mock_open): + with self.assertRaises(ValueError) as context: + read_json('file') + self.assertEqual( + 'file is not valid', str(context.exception)) + # test file does not exist + with self.assertRaises(IOError) as context: + read_json('null') + self.assertEqual( + 'null does not exist.', str(context.exception)) diff --git a/utils.py b/utils.py new file mode 100644 index 0000000..9368437 --- /dev/null +++ b/utils.py @@ -0,0 +1,35 @@ +import re +import json +import os + + +def read_json(file_path): + try: + with open(file_path, 'r') as f: + try: + return json.load(f) + except ValueError: + raise ValueError('{} is not valid'.format(file_path)) + except IOError: + raise IOError('{} does not exist.'.format(file_path)) + + +def write_json(obj, file_path): + make_directory(os.path.dirname(file_path)) + with open(file_path, 'w') as f: + json.dump(obj, f) + + +def digits_in_string(text): + non_digit = re.compile("\D") + return int(non_digit.sub("", text)) + + +def make_directory(directory): + if not os.path.exists(directory): + try: + os.makedirs(directory) + except OSError as err: + raise + + return directory -- GitLab